Expected behavior
qml.is_commuting(qml.CZ(wires=[0,1]), qml.SWAP(wires=[0,1])) should return True, because CZ and SWAP on the same two wires commute — their matrices satisfy AB = BA.
Actual behavior
Direct matrix verification confirms they do commute:
qml.is_commuting(CZ, SWAP): False
np.allclose(A @ B, B @ A): True
The root cause is in pennylane/ops/functions/is_commuting.py (line ~393). The internal logic uses a control-wire / target-wire heuristic to determine commutation via lookup tables. It decomposes CZ into an asymmetric "control wire + target PauliZ" representation, then checks target-wire overlap with SWAP and concludes they don't commute. But CZ is symmetric under wire exchange — swapping its two wires gives the same gate — so this asymmetric decomposition is inappropriate and leads to a wrong answer.
Additional information
Reproduces 100% of the time on PennyLane 0.45.0.
Source code
import pennylane as qml
import numpy as np
print("qml.is_commuting(CZ, SWAP):", qml.is_commuting(qml.CZ(wires=[0,1]), qml.SWAP(wires=[0,1])))
A = qml.matrix(qml.CZ(wires=[0,1]))
B = qml.matrix(qml.SWAP(wires=[0,1]))
print("AB == BA:", np.allclose(A @ B, B @ A))
Tracebacks
qml.is_commuting(CZ, SWAP): False
AB == BA: True
System information
Version: 0.45.0
Platform info: macOS
Python version: 3.13.13
Existing GitHub issues
Expected behavior
qml.is_commuting(qml.CZ(wires=[0,1]), qml.SWAP(wires=[0,1]))should returnTrue, because CZ and SWAP on the same two wires commute — their matrices satisfyAB = BA.Actual behavior
Direct matrix verification confirms they do commute:
The root cause is in
pennylane/ops/functions/is_commuting.py(line ~393). The internal logic uses a control-wire / target-wire heuristic to determine commutation via lookup tables. It decomposes CZ into an asymmetric "control wire + target PauliZ" representation, then checks target-wire overlap with SWAP and concludes they don't commute. But CZ is symmetric under wire exchange — swapping its two wires gives the same gate — so this asymmetric decomposition is inappropriate and leads to a wrong answer.Additional information
Reproduces 100% of the time on PennyLane 0.45.0.
Source code
Tracebacks
System information
Existing GitHub issues