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_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_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_add_remove_vertices(self): g = Graph() v = g.add_vertex() self.assertEqual(g.num_vertices(), 1) g.add_vertices(3) self.assertEqual(g.num_vertices(), 4) g.remove_vertex(v) self.assertEqual(g.num_vertices(), 3)
def test_scalar(self): g = Graph() x = g.add_vertex(VertexType.Z, row = 0, phase = 1 / 2) y = g.add_vertex(VertexType.Z, row = 1, phase = 1 / 4) g.add_edge(g.edge(x, y), edgetype = EdgeType.SIMPLE) full_reduce(g) val = to_quimb_tensor(g).contract(output_inds = ()) expected_val = 1 + np.exp(1j * np.pi * 3 / 4) self.assertTrue(abs(val - expected_val) < 1e-9)
def test_set_attributes(self): g = Graph() v = g.add_vertex() g.set_phase(v, 1) self.assertEqual(g.phase(v), 1) g.set_type(v, 2) self.assertEqual(g.type(v), 2) g.set_row(v, 3) self.assertEqual(g.row(v), 3) g.set_qubit(v, 2) self.assertEqual(g.qubit(v), 2)
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_xor_tensor(self): g = Graph() x = g.add_vertex(VertexType.BOUNDARY) y = g.add_vertex(VertexType.BOUNDARY) v = g.add_vertex(VertexType.Z) z = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, v), edgetype = EdgeType.HADAMARD) g.add_edge(g.edge(y, v), edgetype = EdgeType.HADAMARD) g.add_edge(g.edge(v, z), edgetype = EdgeType.HADAMARD) tn = to_quimb_tensor(g) for x in range(2): for y in range(2): for z in range(2): self.assertTrue(abs((tn & qtn.Tensor(data = [1 - x, x], inds = ("0",)) & qtn.Tensor(data = [1 - y, y], inds = ("1",)) & qtn.Tensor(data = [1 - z, z], inds = ("3",))).contract(output_inds = ()) - ((x ^ y) == z) / np.sqrt(2)) < 1e-9)
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_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_phases_tensor(self): # This diagram represents a 1-input 1-output Z-spider of phase pi/2, # but written using two Z-spiders of phases pi/6 and pi/3 that are # connected by a simple edge. g = Graph() x = g.add_vertex(VertexType.BOUNDARY) v = g.add_vertex(VertexType.Z, phase = 1. / 6.) w = g.add_vertex(VertexType.Z, phase = 1. / 3.) y = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, v), edgetype = EdgeType.SIMPLE) g.add_edge(g.edge(v, w), edgetype = EdgeType.SIMPLE) g.add_edge(g.edge(w, y), edgetype = EdgeType.SIMPLE) tn = to_quimb_tensor(g) self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1, 0], inds = ("3",))) .contract(output_inds = ()) - 1) < 1e-9) self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [0, 1j], inds = ("3",))) .contract(output_inds = ()) + 1) < 1e-9)
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_hadamard_tensor(self): g = Graph() x = g.add_vertex(VertexType.BOUNDARY) y = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, y), edgetype = EdgeType.HADAMARD) tn = to_quimb_tensor(g) self.assertTrue(abs((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1 / np.sqrt(2), 1 / np.sqrt(2)], inds = ("1",))) .contract(output_inds = ()) - 1) < 1e-9) self.assertTrue(abs((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [1 / np.sqrt(2), -1 / np.sqrt(2)], inds = ("1",))) .contract(output_inds = ()) - 1) < 1e-9)
def test_id_tensor(self): g = Graph() x = g.add_vertex(VertexType.BOUNDARY) y = g.add_vertex(VertexType.BOUNDARY) g.add_edge(g.edge(x, y), edgetype = EdgeType.SIMPLE) tn = to_quimb_tensor(g) self.assertTrue((tn & qtn.Tensor(data = [0, 1], inds = ("0",)) & qtn.Tensor(data = [0, 1], inds = ("1",))) .contract(output_inds = ()) == 1) self.assertTrue((tn & qtn.Tensor(data = [1, 0], inds = ("0",)) & qtn.Tensor(data = [1, 0], inds = ("1",))) .contract(output_inds = ()) == 1)
def test_set_attributes(self): g = Graph() v = g.add_vertex() self.assertEqual(g.phase(v),0) g.set_phase(v,1) self.assertEqual(g.phase(v),1) self.assertEqual(g.type(v),VertexType.BOUNDARY) g.set_type(v,VertexType.X) self.assertEqual(g.type(v),VertexType.X) self.assertFalse(g.is_ground(v)) g.set_ground(v) self.assertTrue(g.is_ground(v)) g.set_row(v,3) self.assertEqual(g.row(v),3) g.set_qubit(v,2) self.assertEqual(g.qubit(v),2)