pyzx icon indicating copy to clipboard operation
pyzx copied to clipboard

Should the circuits before and after `extract_circuit` be equivalent?

Open anitalu724 opened this issue 2 years ago • 3 comments

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.

anitalu724 avatar Mar 21 '22 13:03 anitalu724

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

jvdwetering avatar Mar 22 '22 11:03 jvdwetering

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.

jvdwetering avatar Mar 22 '22 11:03 jvdwetering

OK! I get it. Thanks for your responses.

anitalu724 avatar Apr 12 '22 02:04 anitalu724

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?

dlyongemallo avatar Nov 03 '23 10:11 dlyongemallo

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.

jvdwetering avatar Nov 03 '23 10:11 jvdwetering