How to use the strawberryfields.ops.Gaussian function in StrawberryFields

To help you get started, we’ve selected a few StrawberryFields examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github XanaduAI / strawberryfields / tests / frontend / test_ops_decompositions.py View on Github external
def test_incorrect_means_length(self, hbar):
        """Test that an exception is raised len(means)!=len(cov)"""
        cov = random_covariance(3, hbar=hbar)

        with pytest.raises(ValueError, match="must have the same length"):
            ops.Gaussian(cov, r=np.array([0]))
github XanaduAI / strawberryfields / tests / frontend / test_ops_decompositions.py View on Github external
def test_thermal_decomposition(self, hbar, tol):
        """Test that a thermal covariance matrix decomposes into Thermal preparations."""
        n = 3
        prog = sf.Program(n)
        nbar = np.array([0.453, 0.23, 0.543])
        cov = np.diag(np.tile(2 * nbar + 1, 2)) * hbar / 2

        G = ops.Gaussian(cov)
        cmds = G.decompose(prog.register)

        assert len(cmds) == n

        # calculating the resulting decomposed symplectic
        for i, cmd in enumerate(cmds):
            assert isinstance(cmd.op, ops.Thermal)
            assert np.allclose(cmd.op.p[0].x, nbar[i], atol=tol, rtol=0)
github XanaduAI / strawberryfields / tests / frontend / test_program.py View on Github external
def test_user_defined_decomposition_false(self):
        """Test that an operation that is both a primitive AND
        a decomposition (for instance, ops.Gaussian in the gaussian
        backend) can have it's decomposition behaviour user defined.

        In this case, the Gaussian operation should remain after compilation.
        """
        prog = sf.Program(2)
        cov = np.ones((4, 4)) + np.eye(4)
        r = np.array([0, 1, 1, 2])
        with prog.context as q:
            ops.Gaussian(cov, r, decomp=False) | q

        prog = prog.compile(target='gaussian')
        assert len(prog) == 1
        circuit = prog.circuit
        assert circuit[0].op.__class__.__name__ == "Gaussian"

        # test compilation against multiple targets in sequence
        with pytest.raises(program.CircuitError, match="The operation Gaussian is not a primitive for the target 'fock'"):
            prog = prog.compile(target='fock')
github XanaduAI / strawberryfields / tests / frontend / test_ops_decompositions.py View on Github external
def test_rotated_squeezed_decomposition(self, hbar, tol):
        """Test that a rotated squeezed covariance matrix decomposes into Squeezed preparations"""
        n = 3
        prog = sf.Program(n)

        sq_r = np.array([0.453, 0.23, 0.543])
        sq_phi = np.array([-0.123, 0.2143, 0.021])

        S = np.diag(np.exp(np.concatenate([-sq_r, sq_r])))

        for i, phi in enumerate(sq_phi):
            S = _rotation(phi / 2, i, n) @ S

        cov = S @ S.T * (hbar / 2)
        G = ops.Gaussian(cov)
        cmds = G.decompose(prog.register)

        assert len(cmds) == n

        # calculating the resulting decomposed symplectic
        for i, cmd in enumerate(cmds):
            assert isinstance(cmd.op, ops.Squeezed)
            assert np.allclose(cmd.op.p[0].x, sq_r[i], atol=tol, rtol=0)
            assert np.allclose(cmd.op.p[1].x, sq_phi[i], atol=tol, rtol=0)
github XanaduAI / strawberryfields / tests / frontend / test_program.py View on Github external
def test_user_defined_decomposition_true(self):
        """Test that an operation that is both a primitive AND
        a decomposition (for instance, ops.Gaussian in the gaussian
        backend) can have it's decomposition behaviour user defined.

        In this case, the Gaussian operation should compile
        to a Squeezed preparation.
        """
        prog = sf.Program(3)
        r = 0.453
        cov = np.array([[np.exp(-2*r), 0], [0, np.exp(2*r)]])*sf.hbar/2
        with prog.context:
            ops.Gaussian(cov, decomp=True) | 0

        new_prog = prog.compile(target='gaussian')

        assert len(new_prog) == 1

        circuit = new_prog.circuit
        assert circuit[0].op.__class__.__name__ == "Squeezed"
        assert circuit[0].op.p[0] == r
github XanaduAI / strawberryfields / tests / integration / test_decompositions_integration.py View on Github external
def test_displaced_squeezed(self, setup_eng, hbar, tol):
        """Testing decomposed displaced squeezed state"""
        eng, prog = setup_eng(3)
        cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3)
        means = np.array([0, 0.1, 0.2, -0.1, 0.3, 0])

        with prog.context as q:
            ops.Gaussian(cov, means) | q

        state = eng.run(prog).state
        assert np.allclose(state.cov(), cov, atol=tol)
        assert np.allclose(state.means(), means, atol=tol)
        assert len(eng.run_progs[-1]) == 7
github XanaduAI / strawberryfields / tests / integration / test_decompositions_integration.py View on Github external
def test_random_state_pure(self, setup_eng, V_pure, r_means, tol):
        """Test applying a pure covariance state"""
        eng, prog = setup_eng(3)
        with prog.context as q:
            ops.Gaussian(V_pure, r_means) | q

        state = eng.run(prog).state
        assert np.allclose(state.cov(), V_pure, atol=tol, rtol=0)
        assert np.allclose(state.means(), r_means, atol=tol, rtol=0)
        assert len(eng.run_progs[-1]) == 21
github XanaduAI / strawberryfields / tests / integration / test_decompositions_integration.py View on Github external
def test_thermal(self, setup_eng, hbar, tol):
        """Testing a thermal state"""
        eng, prog = setup_eng(3)
        cov = np.diag(hbar * (np.array([0.3, 0.4, 0.2] * 2) + 0.5))

        with prog.context as q:
            ops.Gaussian(cov, decomp=False) | q

        state = eng.run(prog).state
        assert np.allclose(state.cov(), cov, atol=tol)
github XanaduAI / pennylane-sf / pennylane_sf / fock.py View on Github external
hbar (float): the convention chosen in the canonical commutation
            relation :math:`[x, p] = i \hbar`
    """
    name = 'Strawberry Fields Fock PennyLane plugin'
    short_name = 'strawberryfields.fock'

    _operation_map = {
        'CatState': Catstate,
        'CoherentState': Coherent,
        'FockDensityMatrix': DensityMatrix,
        'DisplacedSqueezedState': DisplacedSqueezed,
        'FockState': Fock,
        'FockStateVector': Ket,
        'SqueezedState': Squeezed,
        'ThermalState': Thermal,
        'GaussianState': Gaussian,
        'Beamsplitter': BSgate,
        'CrossKerr': CKgate,
        'ControlledAddition': CXgate,
        'ControlledPhase': CZgate,
        'Displacement': Dgate,
        'Kerr': Kgate,
        'QuadraticPhase': Pgate,
        'Rotation': Rgate,
        'TwoModeSqueezing': S2gate,
        'Squeezing': Sgate,
        'CubicPhase': Vgate,
        'Interferometer': Interferometer
    }

    _observable_map = {
        'NumberOperator': mean_photon,