Skip to content

[BUG] qml.is_commuting returns False for commuting CZ and SWAP gates #9623

Description

@YujinSong-hep

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

  • I have searched existing GitHub issues to make sure the issue does not already exist.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛Something isn't workingcommunity-botIssue suspected to be found by a bot

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions