How to use the strawberryfields.Program 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_circuitdrawer.py View on Github external
def test_not_drawable(self, tmpdir):
        prog = sf.Program(3)

        with prog.context as q:
            ops.BSgate(0, 2) | (q[0], q[2])

        with pytest.raises(NotDrawableException):
            prog.draw_circuit(tex_dir=tmpdir)
github XanaduAI / strawberryfields / tests / frontend / test_ops_decompositions.py View on Github external
def test_apply_decomp(self, hbar):
        """Test that the apply method, when decomp = False, calls the Backend directly."""
        prog = sf.Program(3)
        cov = random_covariance(3, hbar=hbar)

        class DummyBackend:
            """Dummy backend class"""
            def prepare_gaussian_state(*args):
                """Raises a syntax error when called"""
                raise SyntaxError

        G = ops.Gaussian(cov, decomp=False)
        with pytest.raises(SyntaxError):
            G._apply(prog.register, DummyBackend())
github XanaduAI / strawberryfields / tests / frontend / test_engine.py View on Github external
def prog(backend):
    """Program fixture."""
    prog = sf.Program(2)
    with prog.context as q:
        ops.Dgate(0.5) | q[0]
    return prog
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 / pennylane-sf / tests / test_gaussian.py View on Github external
def SF_gate_reference(sf_op, wires, *args):
    """SF reference circuit for gate tests"""
    eng = sf.Engine("gaussian")
    prog = sf.Program(2)
    with prog.context as q:
        sf.ops.S2gate(0.1) | q
        sf_op(*args) | [q[i] for i in wires]

    state = eng.run(prog).state
    return state.mean_photon(0)[0], state.mean_photon(1)[0]
github XanaduAI / strawberryfields / tests / frontend / test_program.py View on Github external
def test_merge_pairwise_cancelling(self, permute_gates):
        """Optimizer merging chains that cancel out pairwise"""
        prog = sf.Program(3)

        with prog.context:
            for G in permute_gates:
                G | 0
                G.H | 0

        prog = prog.optimize()
        assert len(prog) == 0
github XanaduAI / strawberryfields / tests / frontend / test_circuitdrawer.py View on Github external
def test_x_1(self, tmpdir):
        prog = sf.Program(3)

        with prog.context as q:
            ops.Xgate(1) | (q[1])

        x_test_1_output = dedent(
            r"""            \documentclass{article}
            \pagestyle{empty}
            \usepackage{qcircuit}
            \begin{document}
            \Qcircuit {
             & \qw  & \qw \\
             & \gate{X}  & \qw \\
             & \qw  & \qw \\
            }
            \end{document}"""
        )
github XanaduAI / strawberryfields / tests / frontend / test_circuitspecs_chip0.py View on Github external
def test_50_50_BSgate(self, tol):
        """Test 50-50 BSgates correctly compile"""
        prog = sf.Program(4)

        with prog.context as q:
            ops.S2gate(0.5) | (q[0], q[2])
            ops.S2gate(0.5) | (q[1], q[3])
            ops.BSgate() | (q[0], q[1])
            ops.BSgate() | (q[2], q[3])
            ops.MeasureFock() | q

        res = prog.compile("chip0")

        expected = sf.Program(4)

        with expected.context as q:
            ops.S2gate(0.5, 0) | (q[0], q[2])
            ops.S2gate(0.5, 0) | (q[1], q[3])

            # corresponds to BSgate() on modes [0, 1]
            ops.Rgate(0) | (q[0])
            ops.BSgate(np.pi / 4, np.pi / 2) | (q[0], q[1])
            ops.Rgate(3 * np.pi / 2) | (q[0])
            ops.BSgate(np.pi / 4, np.pi / 2) | (q[0], q[1])
            ops.Rgate(3 * np.pi / 4) | (q[0])
            ops.Rgate(-np.pi / 4) | (q[1])

            # corresponds to BSgate() on modes [2, 3]
            ops.Rgate(0) | (q[2])
            ops.BSgate(np.pi / 4, np.pi / 2) | (q[2], q[3])
github XanaduAI / strawberryfields / examples / teleportation.py View on Github external
#!/usr/bin/env python3
import strawberryfields as sf
from strawberryfields.ops import *
from strawberryfields.utils import scale
from numpy import pi, sqrt

# initialize engine and program objects
eng = sf.Engine(backend="gaussian")
teleportation = sf.Program(3)

with teleportation.context as q:
    psi, alice, bob = q[0], q[1], q[2]

    # state to be teleported:
    Coherent(1+0.5j) | psi

    # 50-50 beamsplitter
    BS = BSgate(pi/4, 0)

    # maximally entangled states
    Squeezed(-2) | alice
    Squeezed(2) | bob
    BS | (alice, bob)

    # Alice performs the joint measurement
github XanaduAI / strawberryfields / examples / hamiltonian_simulation.py View on Github external
#!/usr/bin/env python3
import strawberryfields as sf
from strawberryfields.ops import *
from numpy import pi

# initialize engine and program objects
eng = sf.Engine(backend="fock", backend_options={"cutoff_dim": 5})
ham_simulation = sf.Program(2)

# set the Hamiltonian parameters
J = 1           # hopping transition
U = 1.5         # on-site interaction
k = 20          # Lie product decomposition terms
t = 1.086       # timestep
theta = -J*t/k  
r = -U*t/(2*k)

with ham_simulation.context as q:
    # prepare the initial state
    Fock(2) | q[0]

    # Two node tight-binding
    # Hamiltonian simulation