From 625fc0fd85e55058cf0383e3148e8f2e7c06469e Mon Sep 17 00:00:00 2001 From: Paul Tirlisan Date: Wed, 9 Feb 2022 18:22:23 +0000 Subject: [PATCH] Fixed scalar bug in to_quimb_tensor method. --- pyzx/quimb.py | 2 ++ tests/test_quimb.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pyzx/quimb.py b/pyzx/quimb.py index 94f632a1..b0eceab5 100644 --- a/pyzx/quimb.py +++ b/pyzx/quimb.py @@ -66,6 +66,8 @@ def to_quimb_tensor(g: BaseGraph) -> 'qtn.TensorNetwork': # Grab the float factor and exponent from the scalar scalar_float = np.exp(1j * np.pi * g.scalar.phase) * g.scalar.floatfactor + for node in g.scalar.phasenodes: # Each node is a Fraction + scalar_float *= 1 + np.exp(1j * np.pi * node) scalar_exp = math.log10(math.sqrt(2)) * g.scalar.power2 # If the TN is empty, create a single 0-tensor with scalar factor, otherwise diff --git a/tests/test_quimb.py b/tests/test_quimb.py index 3ab5b4ff..d52c5f13 100644 --- a/tests/test_quimb.py +++ b/tests/test_quimb.py @@ -37,6 +37,7 @@ from pyzx.graph import Graph from pyzx.utils import EdgeType, VertexType from pyzx.quimb import to_quimb_tensor +from pyzx.simplify import full_reduce @unittest.skipUnless(np, "numpy needs to be installed for this to run") @unittest.skipUnless(qu, "quimb needs to be installed for this to run") @@ -111,6 +112,18 @@ def test_phases_tensor(self): 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_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) + if __name__ == '__main__': unittest.main()