zxcalc / pyzx

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

Clifford simp adds a phase of n*pi/2 #229

Closed rafaelha closed 5 months ago

rafaelha commented 6 months ago

In the following code snippet, the two tensors differ by a phase of 1j. Running this for different seeds, I find that the phase of z is either [1, 1j, -1, -1j].

import numpy as np
import random
import pyzx as zx

random.seed(1)
g1 = zx.generate.cliffordT(3, 20, p_t=0)
g2 = g1.copy()

zx.clifford_simp(g2)  # Removing this line makes the assert pass
assert np.allclose(g1.to_tensor(), g2.to_tensor())

z = g1 + g2.adjoint()
zx.clifford_simp(z)
# print(z.to_tensor())
assert z.is_id()
assert z.scalar.to_number() == 1, f"scalar is {z.scalar.to_number()}" 
# >> AssertionError: scalar is (-1.8369701987210297e-16-1j)

version: pyzx 0.8.0

rafaelha commented 6 months ago

Looking into this further, the problem was simply that BaseGraph.adjoint() did not conjugate the phase. Adding the line g.scalar.add_phase(- 2 * g.scalar.phase) in BaseGraph.copy fixed everything.

jvdwetering commented 5 months ago

I think this can now be closed?