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_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_v_1(self, tmpdir): prog = sf.Program(3) with prog.context as q: ops.Vgate(1) | (q[1]) v_test_1_output = dedent(r""" \documentclass{article} \usepackage{qcircuit} \begin{document} \Qcircuit { & \qw & \qw \\ & \gate{V} & \qw \\ & \qw & \qw \\ } \end{document}""") result = prog.draw_circuit(tex_dir=tmpdir)[1] assert result == v_test_1_output, failure_message( result, v_test_1_output)
def test_v_1(self, tmpdir): eng, q = sf.Engine(3) with eng: ops.Vgate(1) | (q[1]) v_test_1_output = dedent(r""" \documentclass{article} \usepackage{qcircuit} \begin{document} \Qcircuit { & \qw & \qw \\ & \gate{V} & \qw \\ & \qw & \qw \\ } \end{document}""") result = eng.draw_circuit(print_queued_ops=True, tex_dir=tmpdir)[1] assert result == v_test_1_output, failure_message( result, v_test_1_output)
def test_complex(init): modes = 4 cutoff_dim = 6 initial_state = np.zeros([cutoff_dim] * modes, dtype=complex) # The ket below corresponds to a single photon going into each of the modes initial_state[init] = 1 prog = sf.Program(modes) s_d_params = 0.01 with prog.context as q: ops.Ket(initial_state) | q # Initial state preparation # Gaussian Layer ops.S2gate(s_d_params, s_d_params) | (q[0], q[1]) ops.BSgate(1.9, 1.7) | (q[1], q[2]) ops.BSgate(0.9, 0.2) | (q[0], q[1]) # Non-Gaussian Layer ops.Kgate(0.5) | q[3] ops.CKgate(0.7) | (q[2], q[3]) # Gaussian Layer ops.BSgate(1.0, 0.4) | (q[0], q[1]) ops.BSgate(2.0, 1.5) | (q[1], q[2]) ops.Dgate(s_d_params) | q[0] ops.Dgate(s_d_params) | q[0] ops.Sgate(s_d_params, s_d_params) | q[1] # Non-Gaussian Layer ops.Vgate(0.5) | q[2] # We run the simulation eng = sf.Engine("fock", backend_options={"cutoff_dim": cutoff_dim}) results_norm = eng.run(prog) prog_merged = prog.compile(compiler="gaussian_merge") results_merged = eng.run(prog_merged) ket = results_norm.state.ket() ket_merged = results_merged.state.ket() assert np.allclose(np.abs(np.sum(np.conj(ket) * ket_merged)), 1)