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_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_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 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_circuit_extract(self): random.seed(SEED) for i in range(5): circ = cliffordT(4,50,0.1) clifford_simp(circ,quiet=True) with self.subTest(i=i): t = tensorfy(circ) circuit_extract(circ) t2 = tensorfy(circ) 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 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_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 test_streaming_extract(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 = streaming_extract(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 to_matrix(self): """Returns a representation of the graph as a matrix using :func:`~pyzx.tensor.tensorfy`""" return tensor_to_matrix(tensorfy(self), len(self.inputs), len(self.outputs))
def to_tensor(self): """Returns a representation of the graph as a tensor using :func:`~pyzx.tensor.tensorfy`""" return tensorfy(self)
def to_tensor(self, preserve_scalar=True): """Returns a representation of the graph as a tensor using :func:`~pyzx.tensor.tensorfy`""" return tensorfy(self, preserve_scalar)