zxcalc / pyzx

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

Hadamard cancellation rule #200

Open wzpxq opened 8 months ago

wzpxq commented 8 months ago

Unless I am missing it there is no "Hadamard cancellation" implemented.

Consider the following graph:

g = zx.Graph() 

v0 = g.add_vertex(zx.VertexType.BOUNDARY, 0, 0)
v1 = g.add_vertex(zx.VertexType.H_BOX, 0, 1)
g.add_edge((v0, v1))
v2 = g.add_vertex(zx.VertexType.H_BOX, 0, 2)
g.add_edge((v1, v2))
v3 = g.add_vertex(zx.VertexType.BOUNDARY, 0, 3)
g.add_edge((v2, v3))

full_reduce, clifford_simp have no effect on it (nor did I find any other API that cancels the two Hadamards.

wzpxq commented 8 months ago

The same goes also for H_BOX next to an H-edge, btw:

g = zx.Graph() 

v0 = g.add_vertex(zx.VertexType.BOUNDARY, 0, 0)
v1 = g.add_vertex(zx.VertexType.H_BOX, 0, 1)
g.add_edge((v0, v1), zx.EdgeType.HADAMARD) 
v2 = g.add_vertex(zx.VertexType.BOUNDARY, 0, 2)
g.add_edge((v1, v2))
dlyongemallo commented 7 months ago

full_reduce assumes that the input is a ZX-diagram, meaning it shouldn't have H_BOXes. I agree that this is confusing, and at some stage probably a conversion from H_BOX to Hadamard edges should happen. At the minimum, full_reduce should warn that the input is invalid.

See also #161 for some background.

dnadlinger commented 6 months ago

I also bumped into this as a first-time user of zxlive; lowering 2-ary H boxes sounds like a sensible pass to implement (and I agree, a warning for "invalid" inputs would have helped a lot in figuring out what is going on here!).

wzpxq commented 6 months ago

Thank you for the two replies. So, as I have understood it, I simply have to call zx.hsimplify.from_hypergraph_form(g) to turn all H_BOXes into HADAMARD edges, before full_reduce can do its thing. I tried it and it works. But I suggest that if that H_BOX conversion will not be included into full_reduce that at least a new prominent API should be created that incorporates all simplifications to any graph.