def test_invalid_loss(self, adj): """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"): sample.sample(A=adj, n_mean=1.0, n_samples=1, loss=2)
def test_loss(self, monkeypatch, adj): """Test if function correctly creates the SF program for lossy GBS.""" mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) sample.sample(A=adj, n_mean=1, threshold=False, loss=0.5) p_func = mock_eng_run.call_args[0][0] assert isinstance(p_func.circuit[-2].op, sf.ops.LossChannel)
def test_pnr(self, monkeypatch, adj): """Test if function correctly creates the SF program for photon-number resolving GBS.""" mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) sample.sample(A=adj, n_mean=1, threshold=False) p_func = mock_eng_run.call_args[0][0] assert isinstance(p_func.circuit[-1].op, sf.ops.MeasureFock)
def test_no_loss(self, monkeypatch, adj): """Test if function correctly creates the SF program for GBS without loss.""" mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) sample.sample(A=adj, n_mean=1, threshold=False) 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_seed(dim, adj): """Test for the function ``strawberryfields.apps.sample.seed``. Checks that samples are identical after repeated initialization of ``seed``.""" sample.seed(1968) q_s_1 = sample.sample(A=adj, n_mean=2, n_samples=10, threshold=False) sample.seed(1968) q_s_2 = sample.sample(A=adj, n_mean=2, n_samples=10, threshold=False) assert np.array_equal(q_s_1, q_s_2)
def test_all_loss(self, monkeypatch, adj, dim): """Test if function samples from the vacuum when maximum loss is applied.""" mock_eng_run = mock.MagicMock() with monkeypatch.context() as m: m.setattr(sf.LocalEngine, "run", mock_eng_run) sample.sample(A=adj, n_mean=1, threshold=False, 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 make_sample(adj, max_Sample, queue): t0 = time.time() sampling = sample.sample(adj, 8, max_Sample) t1 = time.time() total_n = t1 - t0 ret = (total_n, sampling) queue.put(ret) # time.sleep(0.01) # kills the thread above return ret
def make_sample(f_adj, max_samp, queue): start_sampling.wait() ts = time.time() sampling = sample.sample(f_adj, 8, max_samp) start_sampling.set() tsf = time.time() total_n = ts - tsf ret = (total_n, sampling) queue.put(ret) # time.sleep(0.01) # kills the thread above return ret
def test_pnr_integration(self, adj, integration_sample_number): """Integration test to check if function returns samples of correct form, i.e., correct number of samples, correct number of modes, all non-negative integers """ samples = np.array( sample.sample(A=adj, n_mean=1.0, n_samples=integration_sample_number, threshold=False) ) dims = samples.shape assert len(dims) == 2 assert dims == (integration_sample_number, len(adj)) assert samples.dtype == "int" assert (samples >= 0).all()
def test_invalid_n_mean(self, adj): """Test if function raises a ``ValueError`` when the mean photon number is specified to be negative.""" with pytest.raises(ValueError, match="Mean photon number must be non-negative"): sample.sample(A=adj, n_mean=-1.0, n_samples=1)
def test_invalid_n_samples(self, adj): """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"): sample.sample(A=adj, n_mean=1.0, n_samples=0)
def test_invalid_adjacency(self, dim): """Test if function raises a ``ValueError`` for a matrix that is not symmetric""" with pytest.raises(ValueError, match="Input must be a NumPy array"): adj_asym = np.triu(np.ones((dim, dim))) sample.sample(A=adj_asym, n_mean=1.0)
Let's take a look at both types of sampling methods. We can generate samples from a random 5-dimensional symmetric matrix: """ from strawberryfields.apps import sample import numpy as np modes = 5 n_mean = 6 samples = 5 A = np.random.normal(0, 1, (modes, modes)) A = A + A.T s_thresh = sample.sample(A, n_mean, samples, threshold=True) s_pnr = sample.sample(A, n_mean, samples, threshold=False) print(s_thresh) print(s_pnr) ############################################################################## # In each case, a sample is a sequence of integers of length five, i.e., ``len(modes) = 5``. # Threshold samples are ``0``'s and ``1``'s, corresponding to whether or not photons were # detected in a mode. A ``1`` here is conventionally called a "click". PNR samples are # non-negative integers counting the number of photons detected in each mode. For example, # suppose a PNR sample is ``[2, 1, 1, 0, 0]``, meaning that 2 photons were detected in mode 0, # 1 photons were detected in modes 1 and 2, and 0 photons were detected in modes 3 and 4. If # threshold detectors were used instead, the sample would be: ``[1, 1, 1, 0, 0]``. # # A more general :func:`~.apps.sample.gaussian` function allows for sampling from arbitrary pure