TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
64 stars 21 forks source link

[Bug]: `to_pyzx_graph`/`from_pyzx_graph` don't preserve the semantics when calling `pyzx.simplify.full_reduce`. #235

Open thierry-martinez opened 1 week ago

thierry-martinez commented 1 week ago

The following code should output fidelity 1, but outputs fidelity 0.5. The code takes a circuit with a Toffoli gate, transpiles it, converts the pattern to a PyZX graph, performs full_reduce and then converts the resulting graph back to a pattern: simulating the two patterns don't give equivalent state vectors.

from graphix import Circuit
from graphix.opengraph import OpenGraph
import pyzx
from graphix.pyzx import to_pyzx_graph, from_pyzx_graph
import numpy as np
from copy import deepcopy

c = Circuit(3)
c.ccx(0, 1, 2)
p = c.transpile(opt=False).pattern
og = OpenGraph.from_pattern(p)
pyg = to_pyzx_graph(og)
pyg.normalize()
pyg_copy = deepcopy(pyg)
pyzx.simplify.full_reduce(pyg)
pyg.normalize()
t = pyzx.tensorfy(pyg)
t2 = pyzx.tensorfy(pyg_copy)
assert pyzx.compare_tensors(t, t2)
og2 = from_pyzx_graph(pyg)
p2 = og2.to_pattern()
s = p.simulate_pattern()
s2 = p2.simulate_pattern()
print(np.abs(np.dot(s.flatten().conj(), s2.flatten())))