Skip to content

[Bug]: Decompositions drop global phases, making controlled gates wrong #637

Closed

Description

@thierry-martinez

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 $|{+}\cdots{+}\rangle$, so the control qubit is in superposition. Statevectors are compared with 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))  # False

H is a reference case that insert_control handles correctly. The failures come from three different decompositions:

  • S, SDG, T, TDG, SX, SXDG: insert_control routes these gates through instructions_to_jcz, which decomposes them as RZ or RX rotations without the global phase. For instance, S = e^{iπ/4} RZ(π/2).
  • CCX, CSWAP: decompose_ccx uses RZ(±π/4) and RZ(π/2) where the Nielsen & Chuang circuit uses T, TDG and S, which loses a global phase of e^{iπ/8}. CSWAP goes through CCX.
  • X, Y, Z: insert_control handles these gates directly (as CNOT, CY and CZ), but their J-∧z decompositions returned by instructions_to_jcz are RX(π) and RZ(π) (and Y = i XZ built from them), whereas X = i RX(π) and Z = i RZ(π).

Activity

  1. added 2 commits that reference this issue on Oct 29, 2026
    41b42ae
    54d0766
  2. added a commit that references this issue on Nov 5, 2026
  3. added a commit that references this issue on Nov 11, 2026
    01ccb7e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions