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_Pgate(self, setup_eng, pure, hbar, tol): """Test the action of the P gate in phase space""" if not pure: pytest.skip("Test only runs on pure states") N = 1 eng, prog = setup_eng(N) r = 3 x1 = 2 p1 = 1.3 s = 0.5 with prog.context as q: ops.Sgate(r) | q ops.Xgate(x1) | q ops.Zgate(p1) | q ops.Pgate(s) | q state = eng.run(prog).state Pmat = np.array([[1, 0], [s, 1]]) Vexpected = 0.5 * hbar * Pmat @ np.diag(np.exp([-2 * r, 2 * r])) @ Pmat.T rexpected = Pmat @ np.array([x1, p1]) # 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": 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_decomposition_operation_compiled(self): """Test decomposition operation gets decomposed if compiled""" # create a test program prog = Program(1) with prog.context as q: ops.Pgate(0.43) | q[0] bb = io.to_blackbird(prog) expected = {"op": "Pgate", "modes": [0], "args": [0.43], "kwargs": {}} assert bb.operations[0] == expected bb = io.to_blackbird(prog.compile(compiler="gaussian")) assert bb.operations[0]["op"] == "Sgate" assert bb.operations[1]["op"] == "Rgate"
def test_Pgate_decomp_equal(self, setup_eng, s, tol): """Tests that the Pgate gives the same transformation as its decomposition.""" eng, prog = setup_eng(1) r = np.arccosh(np.sqrt(1 + (s / 2) ** 2)) theta = np.arctan(s / 2) phi = -np.sign(s) * np.pi / 2 - theta with prog.context as q: ops.Pgate(s) | q # run decomposition with reversed arguments ops.Rgate(-theta) | q ops.Sgate(r, phi + np.pi) | q eng.run(prog) assert np.all(eng.backend.is_vacuum(tol))
def test_one_mode_gates_from_operators(self, drawer): prog = sf.Program(3) with prog.context as q: ops.Xgate(1) | (q[0]) ops.Zgate(1) | (q[0]) 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: method, mode = drawer._gate_from_operator(op) assert callable(method) and hasattr(drawer, method.__name__) assert mode == 1
def test_one_mode_gates_from_operators(self, drawer): eng, q = sf.Engine(3) with eng: ops.Xgate(1) | (q[0]) ops.Zgate(1) | (q[0]) 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: method, mode = drawer._gate_from_operator(op) assert callable(method) and hasattr(drawer, method.__name__) assert mode == 1
def test_decomposition_operation_compiled(self): """Test decomposition operation gets decomposed if compiled""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.Pgate(0.43) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("Pgate", [0.43], (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected xir_prog = io.to_xir(sf_prog.compile(compiler="gaussian")) assert xir_prog.statements[0].name == "Sgate" assert xir_prog.statements[1].name == "Rgate"
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_p_1(self, tmpdir): prog = sf.Program(3) with prog.context as q: ops.Pgate(1) | (q[1]) p_test_1_output = dedent(r""" \documentclass{article} \usepackage{qcircuit} \begin{document} \Qcircuit { & \qw & \qw \\ & \gate{P} & \qw \\ & \qw & \qw \\ } \end{document}""") result = prog.draw_circuit(tex_dir=tmpdir)[1] assert result == p_test_1_output, failure_message( result, p_test_1_output)
def test_p_1(self, tmpdir): eng, q = sf.Engine(3) with eng: ops.Pgate(1) | (q[1]) p_test_1_output = dedent(r""" \documentclass{article} \usepackage{qcircuit} \begin{document} \Qcircuit { & \qw & \qw \\ & \gate{P} & \qw \\ & \qw & \qw \\ } \end{document}""") result = eng.draw_circuit(print_queued_ops=True, tex_dir=tmpdir)[1] assert result == p_test_1_output, failure_message( result, p_test_1_output)
def test_Pgate(self, setup_eng, pure, hbar, tol): """Test the action of the P gate in phase space""" if not pure: pytest.skip("Test only runs on pure states") N = 1 eng, prog = setup_eng(N) r = 3 x1 = 2 p1 = 1.3 s = 0.5 with prog.context as q: ops.Sgate(r) | q ops.Xgate(x1) | q ops.Zgate(p1) | q ops.Pgate(s) | q state = eng.run(prog).state Pmat = np.array([[1, 0], [s, 1]]) Vexpected = 0.5 * hbar * Pmat @ np.diag(np.exp([-2 * r, 2 * r ])) @ Pmat.T assert np.allclose(Vexpected, state.cov(), atol=tol, rtol=0) rexpected = Pmat @ np.array([x1, p1]) assert np.allclose(rexpected, state.means(), atol=tol, rtol=0)