def test_invalid_n_mean(self): """Test if function raises a ``ValueError`` when the mean photon number is specified to be negative.""" g = nx.complete_graph(5) with pytest.raises(ValueError, match="Mean photon number must be non-negative"): similarity.prob_orbit_mc(g, [1, 1, 1, 1], n_mean=-1)
def test_invalid_samples(self): """Test if function raises a ``ValueError`` when a number of samples less than one is requested.""" g = nx.complete_graph(5) with pytest.raises(ValueError, match="Number of samples must be at least one"): similarity.prob_orbit_mc(g, [1, 1, 1, 1], samples=0)
def test_known_result(self): """Tests if the probability for known cases is correctly reproduced.""" g = nx.complete_graph(3) assert np.allclose(similarity.prob_orbit_mc(g, [1, 1], 2), 0.2422152682538481) assert np.allclose(similarity.prob_orbit_mc(g, [4], 1), 0)
def test_invalid_loss(self): """Test if function raises a ``ValueError`` when the loss parameter is specified outside of range.""" g = nx.complete_graph(5) with pytest.raises( ValueError, match="Loss parameter must take a value between zero and one"): similarity.prob_orbit_mc(g, [1, 1, 1, 1], loss=2)
def test_no_loss(self, monkeypatch): """Test if function correctly creates the SF program for GBS without loss.""" graph = nx.complete_graph(5) mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) similarity.prob_orbit_mc(graph, [1, 1, 1, 1], samples=1) p_func = mock_eng_run.call_args[0][0] assert not all([isinstance(op, sf.ops.LossChannel) for op in p_func.circuit])
def test_all_loss(self, monkeypatch): """Test if function samples from the vacuum when maximum loss is applied.""" dim = 5 graph = nx.complete_graph(dim) mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) similarity.prob_orbit_mc(graph, [1, 1, 1, 1], samples=1, loss=1) p_func = mock_eng_run.call_args[0][0] eng = sf.LocalEngine(backend="gaussian") state = eng.run(p_func).state cov = state.cov() disp = state.displacement() assert np.allclose(cov, 0.5 * state.hbar * np.eye(2 * dim)) assert np.allclose(disp, np.zeros(dim))
def test_mean_computation_orbit(self, monkeypatch): """Tests if the calculation of the sample mean is performed correctly. The test monkeypatches the fock_prob function so that the probability is the same for each sample and is equal to 1/5, i.e., one over the number of samples in the orbit [1,1,1,1] for 5 modes.""" graph = nx.complete_graph(5) with monkeypatch.context() as m: m.setattr( "strawberryfields.backends.gaussianbackend.GaussianState.fock_prob", lambda *args, **kwargs: 0.2, ) assert np.allclose(similarity.prob_orbit_mc(graph, [1, 1, 1, 1]), 1.0)
def test_prob_vacuum_orbit(self): """Tests if the function gives the right probability for the empty orbit when the GBS device has been configured to have zero mean photon number.""" graph = nx.complete_graph(10) assert similarity.prob_orbit_mc(graph, [], 0) == 1.0
def test_max_loss(self): """Test if function samples from the vacuum when maximum loss is applied.""" graph = nx.complete_graph(5) assert similarity.prob_orbit_mc(graph, [1, 1, 1, 1], samples=1, loss=1) == 0.0
def test_odd_photon_numbers(self, k): """Test if function returns zero probability for odd number of total photons.""" graph = nx.complete_graph(10) assert similarity.prob_orbit_mc(graph, [k]) == 0.0