def test_invalid_n_samples(self, d): """Test if function raises a ``ValueError`` when a number of samples less than one is requested.""" with pytest.raises(ValueError, match="Number of samples must be at least one"): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state, t, U, w, 0, cf)
def test_negative_input_state(self, d): """Test if function raises a ``ValueError`` when input state contains negative values.""" with pytest.raises( ValueError, match="Input state must not contain negative values"): in_state, t, U, w, ns, cf = d dynamics.sample_fock([-i for i in in_state], t, U, w, ns, cf)
def test_zero_frequency(self, d): """Test if function raises a ``ValueError`` when zero frequencies are given.""" with pytest.raises( ValueError, match="Vibrational frequencies must be larger than zero"): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state, t, U, 0.0 * w, ns, cf)
def test_invalid_loss(self, d): """Test if function raises a ``ValueError`` when the loss parameter is specified outside of range.""" with pytest.raises( ValueError, match="Loss parameter must take a value between zero and one"): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state, t, U, w, ns, cf, -1)
def test_complex_unitary(self, d): """Test if function raises a ``ValueError`` when a complex unitary is given.""" with pytest.raises( ValueError, match= "The normal mode to local mode transformation matrix must be real" ): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state, t, 1.0j * U, w, ns, cf)
def test_invalid_input_photon(self, d): """Test if function raises a ``ValueError`` when the number of photons in each input state mode is not smaller than cutoff.""" with pytest.raises( ValueError, match= "Number of photons in each input mode must be smaller than cutoff", ): in_state, t, U, w, ns, cf = d dynamics.sample_fock([i * 2 for i in in_state], t, U, w, ns, 3)
def test_invalid_mode(self, d): """Test if function raises a ``ValueError`` when the number of modes in the input state and the normal-to-local transformation matrix are different.""" with pytest.raises( ValueError, match= "Number of modes in the input state and the normal-to-local transformation", ): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state + [0], t, U, w, ns, cf)
def test_rgate(self, monkeypatch, d): """Test if function correctly uses the rotation parameter in the rgates.""" if len(d[0]) == 2: mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) dynamics.sample_fock(*d) p_func = mock_eng_run.call_args[0][0] assert np.allclose(p_func.circuit[3].op.p, -7.374345193888777) assert np.allclose(p_func.circuit[4].op.p, -7.13449983982334)
def test_loss(self, monkeypatch, d): """Test if function correctly creates the SF program for lossy circuits.""" def save_hist(*args): call_history.append(args[1]) return sf.engine.Result call_history = [] with monkeypatch.context() as m: m.setattr(sf.engine.Result, "samples", np.array([[0]])) m.setattr(sf.LocalEngine, "run", save_hist) dynamics.sample_fock(*d, loss=0.5) assert isinstance(call_history[0].circuit[-2].op, sf.ops.LossChannel)
def test_interferometer(self, monkeypatch, d): """Test if function correctly uses the interferometer unitaries.""" if len(d[0]) == 2: mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) dynamics.sample_fock(*d) p_func = mock_eng_run.call_args[0][0] _, _, U, _, _, _ = d assert np.allclose(p_func.circuit[2].op.p, U.T) assert np.allclose(p_func.circuit[5].op.p, U)
def test_op_order(self, monkeypatch, d): """Test if function correctly applies the operations.""" if len(d[0]) == 2: mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) dynamics.sample_fock(*d) p_func = mock_eng_run.call_args[0][0] assert isinstance(p_func.circuit[0].op, sf.ops.Fock) assert isinstance(p_func.circuit[1].op, sf.ops.Fock) assert isinstance(p_func.circuit[2].op, sf.ops.Interferometer) assert isinstance(p_func.circuit[3].op, sf.ops.Rgate) assert isinstance(p_func.circuit[4].op, sf.ops.Rgate) assert isinstance(p_func.circuit[5].op, sf.ops.Interferometer) assert isinstance(p_func.circuit[6].op, sf.ops.MeasureFock)
def test_fock_integration(d): """Integration test for the function ``strawberryfields.apps.qchem.dynamics.sample_fock`` to check if it returns samples of correct form, i.e., correct number of samples, correct number of modes, all non-negative integers.""" samples = np.array(dynamics.sample_fock(*d)) dims = samples.shape assert len(dims) == 2 assert dims == (d[4], len(d[2])) assert samples.dtype == "int" assert (samples >= 0).all()
def test_invalid_time(self, d): """Test if function raises a ``ValueError`` when a negative time is given.""" with pytest.raises(ValueError, match="Time must be zero or positive"): in_state, t, U, w, ns, cf = d dynamics.sample_fock(in_state, -t, U, w, ns, cf)