def test_CXgate(self, setup_eng, pure, hbar, tol):
     """Test the action of the CX gate in phase space"""
     if not pure:
         pytest.skip("Test only runs on pure states")
     N = 2
     eng, prog = setup_eng(N)
     r = 3
     x1 = 2
     x2 = 1
     p1 = 1.37
     p2 = 2.71
     s = 0.5
     with prog.context as q:
         ops.Sgate(r) | q[0]
         ops.Xgate(x1) | q[0]
         ops.Zgate(p1) | q[0]
         ops.Sgate(r) | q[1]
         ops.Xgate(x2) | q[1]
         ops.Zgate(p2) | q[1]
         ops.CXgate(s) | q
     state = eng.run(prog).state
     CXmat = np.array([[1, 0, 0, 0], [s, 1, 0, 0], [0, 0, 1, -s],
                       [0, 0, 0, 1]])
     Vexpected = 0.5 * hbar * CXmat @ np.diag(
         np.exp([-2 * r, -2 * r, 2 * r, 2 * r])) @ CXmat.T
     # Checks the covariance matrix is transformed correctly
     assert np.allclose(state.cov(), Vexpected, atol=tol, rtol=0)
     rexpected = CXmat @ np.array([x1, x2, p1, p2])
     # Checks the means are transformed correctly
     assert np.allclose(state.means(), rexpected, atol=tol, rtol=0)
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])
        ops.Fourier | q[0]
        ops.Xgate(0.4) | q[1]
        ops.Zgate(0.5) | q[3]
    compiled_circuit = circuit.compile(compiler="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)
    def test_CZgate_decomp_equal(self, setup_eng, s, tol):
        """Tests that the CZgate gives the same transformation as its decomposition."""
        eng, prog = setup_eng(2)

        with prog.context as q:
            ops.CZgate(s) | q
            # run decomposition with reversed arguments
            ops.Rgate(-np.pi / 2) | q[1]
            ops.CXgate(-s) | q
            ops.Rgate(np.pi / 2) | q[1]

        eng.run(prog)
        assert np.all(eng.backend.is_vacuum(tol))
    def test_two_mode_gates_from_operators(self, drawer):
        prog = sf.Program(3)

        with prog.context as q:
            ops.CXgate(1) | (q[0], q[1])
            ops.CZgate(1) | (q[0], q[1])
            ops.BSgate(1) | (q[0], q[1])
            ops.S2gate(1) | (q[0], q[1])
            ops.CKgate(1) | (q[0], q[1])

        for op in prog.circuit:
            method, mode = drawer._gate_from_operator(op)
            assert callable(method) and hasattr(drawer, method.__name__)
            assert mode == 2
    def test_two_mode_gates_from_operators(self, drawer):
        eng, q = sf.Engine(3)

        with eng:
            ops.CXgate(1) | (q[0], q[1])
            ops.CZgate(1) | (q[0], q[1])
            ops.BSgate(1) | (q[0], q[1])
            ops.S2gate(1) | (q[0], q[1])
            ops.CKgate(1) | (q[0], q[1])

        for op in eng.cmd_queue:
            method, mode = drawer._gate_from_operator(op)
            assert callable(method) and hasattr(drawer, method.__name__)
            assert mode == 2
Пример #6
0
    def test_CXgate(self, setup_eng, pure, hbar, tol):
        """Test the action of the CX gate in phase space"""
        if not pure:
            pytest.skip("Test only runs on pure states")
        N = 2
        eng, prog = setup_eng(N)
        r = 3
        x1 = 2
        x2 = 1
        p1 = 1.37
        p2 = 2.71
        s = 0.5
        with prog.context as q:
            ops.Sgate(r) | q[0]
            ops.Xgate(x1) | q[0]
            ops.Zgate(p1) | q[0]
            ops.Sgate(r) | q[1]
            ops.Xgate(x2) | q[1]
            ops.Zgate(p2) | q[1]
            ops.CXgate(s) | q
        state = eng.run(prog).state

        CXmat = np.array([[1, 0, 0, 0], [s, 1, 0, 0], [0, 0, 1, -s],
                          [0, 0, 0, 1]])

        Vexpected = 0.5 * hbar * CXmat @ np.diag(
            np.exp([-2 * r, -2 * r, 2 * r, 2 * r])) @ CXmat.T
        rexpected = CXmat @ np.array([x1, x2, p1, p2])

        # Check the covariance and mean transformed correctly
        if eng.backend_name == "gaussian":
            assert np.allclose(state.cov(), Vexpected, atol=tol, rtol=0)
            assert np.allclose(state.means(), rexpected, atol=tol, rtol=0)

        elif eng.backend_name == "bosonic":
            indices = from_xp(2)
            Vexpected = Vexpected[:, indices][indices, :]
            rexpected = rexpected[indices]
            assert np.allclose(state.covs(),
                               np.expand_dims(Vexpected, axis=0),
                               atol=tol,
                               rtol=0)
            assert np.allclose(state.means(),
                               np.expand_dims(rexpected, axis=0),
                               atol=tol,
                               rtol=0)
    def test_parse_op(self, drawer):
        prog = sf.Program(3)

        with prog.context as q:
            ops.Xgate(1) | (q[0])
            ops.Zgate(1) | (q[0])
            ops.CXgate(1) | (q[0], q[1])
            ops.CZgate(1) | (q[0], q[1])
            ops.BSgate(0, 1) | (q[0], q[1])
            ops.S2gate(0, 1) | (q[0], q[1])
            ops.CKgate(1) | (q[0], q[1])
            ops.Kgate(1) | (q[0])
            ops.Vgate(1) | (q[0])
            ops.Pgate(1) | (q[0])
            ops.Rgate(1) | (q[0])
            ops.Sgate(1) | (q[0])
            ops.Dgate(1) | (q[0])

        for op in prog.circuit:
            drawer.parse_op(op)

        expected_circuit_matrix = [
            [
                "\\gate{X}",
                "\\gate{Z}",
                "\\ctrl{1}",
                "\\ctrl{1}",
                "\\multigate{1}{BS}",
                "\\multigate{1}{S}",
                "\\ctrl{1}",
                "\\gate{K}",
                "\\gate{V}",
                "\\gate{P}",
                "\\gate{R}",
                "\\gate{S}",
                "\\gate{D}",
            ],
            ["\\qw"] * 2 +
            ["\\targ", "\\gate{Z}", "\\ghost{BS}", "\\ghost{S}", "\\gate{K}"] +
            ["\\qw"] * 6,
            ["\\qw"] * 13,
        ]

        assert drawer._circuit_matrix == expected_circuit_matrix
    def test_parse_op(self, drawer):
        eng, q = sf.Engine(3)

        with eng:
            ops.Xgate(1) | (q[0])
            ops.Zgate(1) | (q[0])
            ops.CXgate(1) | (q[0], q[1])
            ops.CZgate(1) | (q[0], q[1])
            ops.BSgate(0, 1) | (q[0], q[1])
            ops.S2gate(0, 1) | (q[0], q[1])
            ops.CKgate(1) | (q[0], q[1])
            ops.Kgate(1) | (q[0])
            ops.Vgate(1) | (q[0])
            ops.Pgate(1) | (q[0])
            ops.Rgate(1) | (q[0])
            ops.Sgate(1) | (q[0])
            ops.Dgate(1) | (q[0])

        for op in eng.cmd_queue:
            drawer.parse_op(op)

        expected_circuit_matrix = [
            [
                "\\gate{X}",
                "\\gate{Z}",
                "\\ctrl{1}",
                "\\ctrl{1}",
                "\\multigate{1}{BS}",
                "\\multigate{1}{S}",
                "\\ctrl{1}",
                "\\gate{K}",
                "\\gate{V}",
                "\\gate{P}",
                "\\gate{R}",
                "\\gate{S}",
                "\\gate{D}",
            ],
            ["\\qw"] * 2 +
            ["\\targ", "\\gate{Z}", "\\ghost{BS}", "\\ghost{S}", "\\gate{K}"] +
            ["\\qw"] * 6,
            ["\\qw"] * 13,
        ]

        assert drawer._circuit_matrix == expected_circuit_matrix
    def test_cx(self, tmpdir):
        prog = sf.Program(3)

        with prog.context as q:
            ops.CXgate(1) | (q[0], q[1])

        cx_test_output = dedent(r"""            \documentclass{article}
            \usepackage{qcircuit}
            \begin{document}
            \Qcircuit {
             & \ctrl{1}  & \qw \\
             & \targ  & \qw \\
             & \qw  & \qw \\
            }
            \end{document}""")

        result = prog.draw_circuit(tex_dir=tmpdir)[1]
        assert result == cx_test_output, failure_message(
            result, cx_test_output)
    def test_cx(self, tmpdir):
        eng, q = sf.Engine(3)

        with eng:
            ops.CXgate(1) | (q[0], q[1])

        cx_test_output = dedent(r"""            \documentclass{article}
            \usepackage{qcircuit}
            \begin{document}
            \Qcircuit {
             & \ctrl{1}  & \qw \\
             & \targ  & \qw \\
             & \qw  & \qw \\
            }
            \end{document}""")

        result = eng.draw_circuit(print_queued_ops=True, tex_dir=tmpdir)[1]
        assert result == cx_test_output, failure_message(
            result, cx_test_output)
    def test_CXgate_decomp_equal(self, setup_eng, s, tol):
        """Tests that the CXgate gives the same transformation as its decomposition."""
        eng, prog = setup_eng(2)

        r = np.arcsinh(-s / 2)
        y = -1 / np.cosh(r)
        x = -np.tanh(r)
        theta = np.arctan2(y, x) / 2

        with prog.context as q:
            ops.CXgate(s) | q
            # run decomposition with reversed arguments
            ops.BSgate(-(np.pi / 2 + theta), 0) | q
            ops.Sgate(r, np.pi) | q[0]
            ops.Sgate(r, 0) | q[1]
            ops.BSgate(-theta, 0) | q

        eng.run(prog)
        assert np.all(eng.backend.is_vacuum(tol))