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_remove_isolated_vertex_preserves_semantics(self): g = Graph() v = g.add_vertex(1, 0, 0) g2 = g.copy() g2.remove_isolated_vertices() self.assertTrue(compare_tensors(g, g2)) self.assertEqual(g2.scalar.to_number(), 2) g.set_phase(v, Fraction(1)) g2 = g.copy() g2.remove_isolated_vertices() self.assertTrue(compare_tensors(g, g2)) self.assertAlmostEqual(g2.scalar.to_number(), 0)
def test_adjoint(self): random.seed(SEED) circ = cliffords(3, 16) t = tensorfy(circ) t_adj = adjoint(t) circ_adj = tensorfy(circ.adjoint()) self.assertTrue(compare_tensors(t_adj, circ_adj))
def test_cliffordT_preserves_graph_semantics(self): random.seed(SEED) g = cliffordT(4, 20, 0.2) c = Circuit.from_graph(g) g2 = c.to_graph() t = tensorfy(g, False) t2 = tensorfy(g2, False) self.assertTrue(compare_tensors(t, t2, False))
def test_cliffords_preserves_graph_semantics(self): random.seed(SEED) g = cliffords(5,30) c = Circuit.from_graph(g) g2 = c.to_graph() t = tensorfy(g) t2 = tensorfy(g2) self.assertTrue(compare_tensors(t,t2))
def test_circuit_extract_preserves_semantics(self): random.seed(SEED) g = cliffordT(5, 70, 0.15) t = g.to_tensor(False) clifford_simp(g, quiet=True) c = extract_circuit(g) t2 = c.to_tensor(False) self.assertTrue(compare_tensors(t, t2, False))
def test_circuit_extract_preserves_semantics(self): random.seed(SEED) g = cliffordT(5, 70, 0.15) t = g.to_tensor() clifford_simp(g, quiet=True) circuit_extract(g) t2 = Circuit.from_graph(g).to_tensor() self.assertTrue(compare_tensors(t,t2))
def test_streaming_extract(self): random.seed(SEED) for i in range(5): circ = cliffordT(4,50,0.1) t = tensorfy(circ) clifford_simp(circ,quiet=True) with self.subTest(i=i): c = streaming_extract(circ) t2 = c.to_tensor() self.assertTrue(compare_tensors(t,t2))
def test_compose(self): random.seed(SEED) circ1 = cliffords(3, 15) circ2 = cliffords(3, 20) t1 = tensorfy(circ1) t2 = tensorfy(circ2) comp1 = compose_tensors(t1, t2) circ1.compose(circ2) comp2 = tensorfy(circ1) self.assertTrue(compare_tensors(comp1, comp2))
def func_test(self, func, prepare=None): for i,c in enumerate(self.circuits): with self.subTest(i=i, func=func.__name__): if prepare: for f in prepare: f(c,quiet=True) t = tensorfy(c) func(c, quiet=True) t2 = tensorfy(c) self.assertTrue(compare_tensors(t,t2)) del t, t2
def test_extract_circuit(self): random.seed(SEED) for i in range(5): circ = cliffordT(4, 50, 0.1) t = tensorfy(circ, False) clifford_simp(circ, quiet=True) with self.subTest(i=i): c = extract_circuit(circ) t2 = c.to_tensor(False) self.assertTrue(compare_tensors(t, t2, False))
def test_id_graph(self): g = Graph() i = g.add_vertex(0, 0, 0) o = g.add_vertex(0, 0, 1) g.inputs.append(i) g.outputs.append(o) g.add_edge((i, o)) t = tensorfy(g) id_array = np.array([[1, 0], [0, 1]]) self.assertTrue(np.allclose(t, id_array)) self.assertTrue(compare_tensors(t, id_array))
def test_greedy_cut_extract(self): random.seed(SEED) for i in range(5): circ = cliffordT(4,50,0.1) clifford_simp(circ,quiet=True) circ.normalise() with self.subTest(i=i): t = tensorfy(circ) greedy_cut_extract(circ) t2 = tensorfy(circ) self.assertTrue(compare_tensors(t,t2))
def test_remove_isolated_pair_preserves_semantics(self): for i, j, k in itertools.product([1, 2], repeat=3): for phase1, phase2 in itertools.product([0, 1, 2], [0, 4, 5]): with self.subTest(i=i, j=j, k=k, phase1=phase1, phase2=phase2): g = Graph() v = g.add_vertex(i, 0, 0, phase=phase1) w = g.add_vertex(j, 1, 0, phase=phase2) g.add_edge((v, w), k) g2 = g.copy() g2.remove_isolated_vertices() self.assertEqual(g2.num_vertices(), 0) self.assertTrue(compare_tensors(g, g2))
def test_equality_of_id_zx_graph_to_id(self): g = Graph() i = g.add_vertex(0, 0, 0) o = g.add_vertex(0, 0, 2) g.inputs.append(i) g.outputs.append(o) g2 = g.copy() g.add_edge((i, o)) v = g2.add_vertex(1, 0, 1) g2.add_edges([(i, v), (v, o)]) tensor1 = tensorfy(g) tensor2 = tensorfy(g2) self.assertTrue(compare_tensors(tensor1, tensor2))
def test_supplementarity_simp(self): g = Graph() v = g.add_vertex(1,0,0,phase=Fraction(1,4)) w = g.add_vertex(1,1,0,phase=Fraction(7,4)) g.add_edge((v,w),2) vs = [] for i in range(3): h = g.add_vertex(1,i,2,Fraction(1)) vs.append(h) g.add_edges([(v,h),(w,h)],2) t = g.to_tensor() i = supplementarity_simp(g,quiet=True) self.assertEqual(i,1) self.assertTrue(compare_tensors(t,g.to_tensor()))
def test_inequality_id_and_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] g2 = g.copy() g.add_edges([(i1, o2), (i2, o1)]) g2.add_edges([(i1, o1), (i2, o2)]) id_id = tensorfy(g2) swap = tensorfy(g) self.assertFalse(compare_tensors(id_id, swap))
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_clifford_extract(self): random.seed(SEED) tests = 0 tries = 0 while True: tries += 1 circ = cliffords(5,70) clifford_simp(circ,quiet=True) circ.normalise() if circ.depth()>3: continue # It is not in normal form, so skip this one tests += 1 with self.subTest(test=tests,tries=tries): t = tensorfy(circ) clifford_extract(circ,1,2) t2 = tensorfy(circ) self.assertTrue(compare_tensors(t,t2)) if tests>5: break
def compare(a, b): try: if not compare_tensors(a, b): raise AssertionError("Not equal") except (ValueError, MemoryError): pass
def compare(a, b, scalar_accurate=True): if not compare_tensors(a, b, scalar_accurate): raise AssertionError("Not equal")
def compare(a,b): if not compare_tensors(a, b.to_tensor()): raise AssertionError("Not equal")
def test_scalar_difference(self): array = np.array([[0, 1], [1, 0]]) scalar = 0.75 + 3j self.assertTrue(compare_tensors(scalar * array, array))
def test_trivial_inequality(self): array = np.array([[1, 0], [0, 1]]) array2 = np.array([[0, 1], [1, 0]]) self.assertFalse(compare_tensors(array, array2))
def test_compose_unitary(self): g = self.graph g.set_edge_type(g.edge(self.v, self.o1), 2) g2 = g.adjoint() g2.compose(g) self.assertTrue(compare_tensors(g2, identity(2), False))