zxcalc / pyzx

Python library for quantum circuit rewriting and optimisation using the ZX-calculus
Apache License 2.0
387 stars 116 forks source link

`full_reduce` scalar edge case #272

Open y-richie-y opened 2 months ago

y-richie-y commented 2 months ago

These examples are ran on the version of pyzx on pip:

In this example the right thing happens and we get (1 + 1j)

from pyzx import Graph, full_reduce
g = Graph()
g.add_vertex(ty=1, phase=0.5)
g.add_vertex(ty=1, phase=0)
g.add_edge((0, 1))
full_reduce(g)
print(g.scalar.to_number())

but in this example full_reduce does not reduce the diagram fully and the scalar is still 1:

from pyzx import Graph, full_reduce
g = Graph()
g.add_vertex(ty=1, phase=1)
full_reduce(g)
print(g.scalar.to_number())

This could be intended behaviour, but what is the "correct" way to reduce a pyzx diagram to a scalar?

jvdwetering commented 2 months ago

Hhmm, that is an interesting edge case. What might be happening is that in the second case none of the rewrite rules in full_reduce match, and hence the method in the Graph that gets rid of scalar nodes also does not get called. This could be fixed by having a final round of removing scalar spiders at the end of full_reduce