Skip to content

Commit

Permalink
Fixed scalar bug in to_quimb_tensor method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Tirlisan committed Feb 9, 2022
1 parent ff14067 commit 625fc0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyzx/quimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_quimb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 625fc0f

Please sign in to comment.