How to use the strawberryfields.LocalEngine 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_circuitspecs_gaussianunitary.py View on Github external
def test_non_primitive_gates():
    """Tests that the compiler is able to compile a number of non-primitive Gaussian gates"""

    width = 6
    eng = sf.LocalEngine(backend="gaussian")
    eng1 = sf.LocalEngine(backend="gaussian")
    circuit = sf.Program(width)
    A = np.random.rand(width, width) + 1j * np.random.rand(width, width)
    A = A + A.T
    valsA = np.linalg.svd(A, compute_uv=False)
    A = A / 2 * np.max(valsA)
    B = np.random.rand(width // 2, width // 2) + 1j * np.random.rand(width // 2, width // 2)
    valsB = np.linalg.svd(B, compute_uv=False)
    B = B / 2 * valsB
    B = np.block([[0 * B, B], [B.T, 0 * B]])
    with circuit.context as q:
        ops.GraphEmbed(A) | q
        ops.BipartiteGraphEmbed(B) | q
        ops.Pgate(0.1) | q[1]
        ops.CXgate(0.2) | (q[0], q[1])
        ops.MZgate(0.4, 0.5) | (q[2], q[3])
github XanaduAI / strawberryfields / tests / backend / test_tf_import.py View on Github external
def test_incorrect_python_version(self, monkeypatch):
        """Test that an exception is raised if the version
        of Python installed is > 3.6"""
        with monkeypatch.context() as m:
            m.setattr("sys.version_info", (3, 8, 1))
            m.setattr(tensorflow, "__version__", "1.12.2")

            with pytest.raises(ImportError, match="you will need to install Python 3.6"):
                reload(sf.backends.tfbackend)
                sf.LocalEngine('tf')
github XanaduAI / strawberryfields / tests / frontend / test_engine.py View on Github external
def eng(backend):
    """Engine fixture."""
    return sf.LocalEngine(backend)
github XanaduAI / strawberryfields / tests / frontend / test_circuitspecs_gaussianunitary.py View on Github external
def test_gaussian_program(depth, width):
    """Tests that a circuit and its compiled version produce the same Gaussian state"""
    eng = sf.LocalEngine(backend="gaussian")
    eng1 = sf.LocalEngine(backend="gaussian")
    circuit = sf.Program(width)
    with circuit.context as q:
        for _ in range(depth):
            U, s, V, alphas = random_params(width, 2.0 / depth, 1.0)
            ops.Interferometer(U) | q
            for i in range(width):
                ops.Sgate(s[i]) | q[i]
            ops.Interferometer(V) | q
            for i in range(width):
                ops.Dgate(alphas[i]) | q[i]
    compiled_circuit = circuit.compile("gaussian_unitary")
    cv = eng.run(circuit).state.cov()
    mean = eng.run(circuit).state.means()

    cv1 = eng1.run(compiled_circuit).state.cov()
github XanaduAI / strawberryfields / tests / frontend / test_circuitspecs_gaussianunitary.py View on Github external
def test_displacements_only(depth, width):
    """Tests that a circuit and its compiled version produce
    the same Gaussian state when there are only displacements"""
    eng = sf.LocalEngine(backend="gaussian")
    eng1 = sf.LocalEngine(backend="gaussian")
    circuit = sf.Program(width)
    with circuit.context as q:
        for _ in range(depth):
            alphas = np.random.rand(width)+1j*np.random.rand(width)
            for i in range(width):
                ops.Dgate(alphas[i]) | q[i]
    compiled_circuit = circuit.compile("gaussian_unitary")
    cv = eng.run(circuit).state.cov()
    mean = eng.run(circuit).state.means()

    cv1 = eng1.run(compiled_circuit).state.cov()
    mean1 = eng1.run(compiled_circuit).state.means()
    assert np.allclose(cv, cv1)
    assert np.allclose(mean, mean1)
github XanaduAI / strawberryfields / tests / frontend / test_io.py View on Github external
def eng(backend):
    """Engine fixture."""
    return sf.LocalEngine(backend)
github XanaduAI / strawberryfields / tests / frontend / test_engine.py View on Github external
def test_bad_backend(self):
        """Backend must be a string or a BaseBackend instance."""
        with pytest.raises(TypeError, match='backend must be a string or a BaseBackend instance'):
            eng = sf.LocalEngine(0)
github XanaduAI / strawberryfields / tests / apps / test_similarity.py View on Github external
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))
github XanaduAI / strawberryfields / tests / frontend / test_circuitspecs_gaussianunitary.py View on Github external
def test_gaussian_program(depth, width):
    """Tests that a circuit and its compiled version produce the same Gaussian state"""
    eng = sf.LocalEngine(backend="gaussian")
    eng1 = sf.LocalEngine(backend="gaussian")
    circuit = sf.Program(width)
    with circuit.context as q:
        for _ in range(depth):
            U, s, V, alphas = random_params(width, 2.0 / depth, 1.0)
            ops.Interferometer(U) | q
            for i in range(width):
                ops.Sgate(s[i]) | q[i]
            ops.Interferometer(V) | q
            for i in range(width):
                ops.Dgate(alphas[i]) | q[i]
    compiled_circuit = circuit.compile("gaussian_unitary")
    cv = eng.run(circuit).state.cov()
    mean = eng.run(circuit).state.means()

    cv1 = eng1.run(compiled_circuit).state.cov()
    mean1 = eng1.run(compiled_circuit).state.means()