Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix moment commutation detection for group-commuting operations (#6659) #7082

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chamodtharakaperera
Copy link

Fixes #6659

This PR fixes an issue where cirq.commutes() incorrectly returns False for moments that commute as a whole even when their individual operations don't commute. For example, given two Z gates and an RXX gate, [Z⊗Z, RXX] = 0 even though neither Z commutes with RXX individually.

The solution:

  • First tries fast path of checking individual operation commutation
  • If individual operations don't commute or result is indeterminate, falls back to checking if the full moments commute via unitary calculation
  • Properly handles edge cases:
    • Disjoint qubit sets (trivially commute)
    • Non-unitary operations (returns NotImplemented)
    • Indeterminate cases (returns NotImplemented)

Added comprehensive test cases covering:

  • Original Z⊗Z with RXX commutation case
  • Disjoint qubit operations
  • Mixed cases where some operations commute individually and others as a group
  • Non-commuting cases to verify negative results
  • Complex cases requiring unitary calculation

Test outputs demonstrate correct behavior for all cases including the original issue where Z⊗Z should commute with RXX.

Add pairwise commuting checks and, when inconclusive, fall back to full
                  unitary commutator. Includes new tests showing Z⊗Z commutes with RXX.
                  Fixes quantumlib#6659.
@chamodtharakaperera chamodtharakaperera requested review from vtomole and a team as code owners February 23, 2025 17:02
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 23, 2025
Copy link

google-cla bot commented Feb 23, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issue when commuting moments
2 participants