예제 #1
0
    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_CZgate(self, setup_eng, pure, hbar, tol):
        """Test the action of the CZ 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.CZgate(s) | q
        state = eng.run(prog).state
        CZmat = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, s, 1, 0],
                          [s, 0, 0, 1]])
        Vexpected = 0.5 * hbar * CZmat @ np.diag(
            np.exp([-2 * r, -2 * r, 2 * r, 2 * r])) @ CZmat.T
        rexpected = CZmat @ 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 = xxpp_to_xpxp(np.arange(2 * 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_cz(self, tmpdir):
        prog = sf.Program(3)

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

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

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

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

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

        result = eng.draw_circuit(print_queued_ops=True, tex_dir=tmpdir)[1]
        assert result == cz_test_output, failure_message(
            result, cz_test_output)