Beispiel #1
0
 def test_invalid_loss(self):
     """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.gaussian(*p, 1, loss=2)
Beispiel #2
0
    def test_loss(self, monkeypatch):
        """Test if function correctly creates the SF program for lossy GBS."""
        mock_eng_run = mock.MagicMock()

        with monkeypatch.context() as m:
            m.setattr(sf.Engine, "run", mock_eng_run)
            sample.gaussian(*p, 1, loss=0.5)
            p_func = mock_eng_run.call_args[0][0]

        assert isinstance(p_func.circuit[-2].op, sf.ops.LossChannel)
Beispiel #3
0
    def test_no_loss(self, monkeypatch):
        """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.Engine, "run", mock_eng_run)
            sample.gaussian(*p, 1)
            p_func = mock_eng_run.call_args[0][0]

        assert not all(
            [isinstance(op, sf.ops.LossChannel) for op in p_func.circuit])
Beispiel #4
0
    def test_all_loss(self, monkeypatch):
        """Test if function samples from the vacuum when maximum loss is applied."""
        dim = len(alpha)
        mock_eng_run = mock.MagicMock()

        with monkeypatch.context() as m:
            m.setattr(sf.Engine, "run", mock_eng_run)
            sample.gaussian(*p, 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))
Beispiel #5
0
def test_gaussian_integration(integration_sample_number):
    """Integration test for the function ``strawberryfields.gbs.sample.gaussian`` 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(sample.gaussian(*p,
                                       n_samples=integration_sample_number))

    dims = samples.shape

    assert len(dims) == 2
    assert dims == (integration_sample_number, len(alpha))
    assert samples.dtype == "int"
    assert (samples >= 0).all()
Beispiel #6
0
 def test_invalid_n_samples(self):
     """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.gaussian(*p, -1)