def test_magic_state_decomposition_is_correct(self): c = Circuit(6) for i in range(6): c.add_gate("T", i) g = c.to_graph() gsum = replace_magic_states(g) self.assertTrue(np.allclose(g.to_tensor(), gsum.to_tensor()))
def test_two_qubit_gate_semantics(self): c = Circuit(2) c.add_gate("CNOT", 0, 1) cnot_matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) self.assertTrue(compare_tensors(c.to_matrix(), cnot_matrix)) c = Circuit(2) c.add_gate("CZ", 0, 1) cz_matrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]]) self.assertTrue(compare_tensors(c.to_matrix(), cz_matrix))
def test_three_cnots_is_swap(self): g = Graph() i1 = g.add_vertex(0, 0, 0) i2 = g.add_vertex(0, 1, 0) o1 = g.add_vertex(0, 0, 1) o2 = g.add_vertex(0, 1, 1) g.inputs = [i1, i2] g.outputs = [o1, o2] g.add_edges([(i1, o2), (i2, o1)]) swap = tensorfy(g) c = Circuit(2) c.add_gate("CNOT", 0, 1) c.add_gate("CNOT", 1, 0) c.add_gate("CNOT", 0, 1) three_cnots = tensorfy(c.to_graph()) self.assertTrue(compare_tensors(swap, three_cnots))
def test_cz_optimize_extract(self): qb_no = 8 c = Circuit(qb_no) for i in range(qb_no): for j in range(i + 1, qb_no): c.add_gate("CZ", i, j) g = c.to_graph() clifford_simp(g, quiet=True) c2 = extract_circuit(g) cnot_count = 0 for gate in c2.gates: if isinstance(gate, CNOT): cnot_count += 1 self.assertTrue(cnot_count == 4) self.assertTrue(c.verify_equality(c2))
def to_qasm(self): if self.compiled_program is None: return super().to_qasm() circuit = Circuit(self.n_qubits) comments = [] for g in self.compiled_program: if isinstance(g, Pragma): wiring = " ".join(["//", g.command, "["+g.freeform_string[2:-1]+"]"]) comments.append(wiring) elif isinstance(g, Gate): if g.name == "CZ": circuit.add_gate("CZ", g.qubits[0].index, g.qubits[1].index) elif g.name == "RX": circuit.add_gate("XPhase", g.qubits[0].index, g.params[0]) elif g.name == "RZ": circuit.add_gate("ZPhase", g.qubits[0].index, g.params[0]) else: print("Unsupported gate found!", g) qasm = circuit.to_qasm() return '\n'.join(comments+[qasm])
def setUp(self): c = Circuit(3) c.add_gate("CNOT",0,1) c.add_gate("S",2) c.add_gate("CNOT",2,1) self.c = c
def test_verify_equality_permutation_option(self): c1 = Circuit(2) c2 = Circuit(2) c2.add_gate("SWAP", 0, 1) self.assertTrue(c1.verify_equality(c2, up_to_swaps=True)) self.assertFalse(c1.verify_equality(c2, up_to_swaps=False))