pyzx
pyzx copied to clipboard
Should the circuits before and after `extract_circuit` be equivalent?
My code:
c1 = zx.Circuit.load('Circuits/Fast/tof_3_before')
g1 = c1.to_graph()
g2 = g1.copy()
c2 = zx.extract_circuit(g2)
print(zx.compare_tensors(c1,c2))
print(c1.verify_equality(c2))
The results are both False.
I'm wondering if the extract_circuit
function should result in an equivalent circuit after implementation?
Thanks a lot.
The problem is that extract_circuit
expects the diagram to be in graph-like form (only Z-spiders, all spiders fused). If it isn't then its output is just wrong. I thought extract_circuit
was doing this normalization step itself, but apparently it doesn't. I can imagine this is quite confusing, so I should probably just add this preprocessing to extract_circuit
If you want the quick fix: first call zx.to_gh(g2)
and then zx.spider_simp(g2)
. When you extract from g2
then, it gives you the correct outcome.
OK! I get it. Thanks for your responses.
The docstring for extract_circuit
currently says Given a graph put into semi-normal form by :func:`~pyzx.simplify.full_reduce`, ...
. Is this already equivalent to "expects the diagram to be in graph-like form (only Z-spiders, all spiders fused)"? Or does the docstring need to be clarified?
So full_reduce
actually expects the graph to have a gflow, but this is not a condition that is easily expressed. This is why in the docstring I say you should have called full_reduce()
first, since if you do that on a graph that comes from a circuit, then it is guaranteed to have a gflow. Making sure all the spiders are Z and maximally fused is a necessary condition, but it might not be sufficient.