Repository navigation
Description
Issue detected and written with the help of Claude Opus 5.5.
Several decompositions in graphix.transpiler are only correct up to a global phase. That's harmless at top level, but insert_control controls these decompositions, which turns the missing global phase into a relative phase. The resulting controlled gate is then wrong.
The script below compares the output of insert_control with the exact controlled matrix, built with graphix.ops.controlled from the matrices in graphix.ops.Ops. The input is np.allclose, not fidelity, so global phase counts.
import numpy as np
from graphix import Circuit, Instruction
from graphix.ops import Ops, controlled
from graphix.states import BasicStates
from graphix.transpiler import insert_control, instructions_to_jcz
def check(instrs, u):
"""Compare `insert_control(0, instrs)` with `controlled(u)` on |+...+>."""
n = int(np.log2(len(u))) + 1
state = Circuit(n, instr=insert_control(0, instrs)).simulate(input_state=BasicStates.PLUS).state
expected = controlled(u) @ np.full(2**n, 2 ** (-n / 2))
return np.allclose(state.flatten(), expected)
# Reference case: handled correctly.
print(check([Instruction.H(1)], Ops.H)) # True
# Wrong when controlled with `insert_control`.
print(check([Instruction.S(1)], Ops.S)) # False
print(check([Instruction.SDG(1)], Ops.SDG)) # False
print(check([Instruction.T(1)], Ops.T)) # False
print(check([Instruction.TDG(1)], Ops.TDG)) # False
print(check([Instruction.SX(1)], Ops.SX)) # False
print(check([Instruction.SXDG(1)], Ops.SXDG)) # False
print(check([Instruction.CCX((1, 2), 3)], Ops.CCX)) # False
print(check([Instruction.CSWAP(1, (2, 3))], Ops.CSWAP)) # False
# Wrong when the J-∧z decomposition is controlled.
print(check(instructions_to_jcz([Instruction.X(1)]), Ops.X)) # False
print(check(instructions_to_jcz([Instruction.Y(1)]), Ops.Y)) # False
print(check(instructions_to_jcz([Instruction.Z(1)]), Ops.Z)) # FalseH is a reference case that insert_control handles correctly. The failures come from three different decompositions:
- S, SDG, T, TDG, SX, SXDG:
insert_controlroutes these gates throughinstructions_to_jcz, which decomposes them asRZorRXrotations without the global phase. For instance,S = e^{iπ/4} RZ(π/2). - CCX, CSWAP:
decompose_ccxusesRZ(±π/4)andRZ(π/2)where the Nielsen & Chuang circuit usesT,TDGandS, which loses a global phase ofe^{iπ/8}.CSWAPgoes throughCCX. - X, Y, Z:
insert_controlhandles these gates directly (asCNOT,CYandCZ), but their J-∧z decompositions returned byinstructions_to_jczareRX(π)andRZ(π)(andY = i XZbuilt from them), whereasX = i RX(π)andZ = i RZ(π).
Activity
Fix TeamGraphix#637: Fix global phase of J-∧z decompositions
Fix TeamGraphix#637: Fix global phase of J-∧z decompositions
TeamGraphix#637: Fix pyright error in controlled-decomposition test
Fix TeamGraphix#637: Fix global phase of J-∧z decompositions (TeamGra…