Skip to content

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Feb 11, 2025

Fixes #7052. Also fixes #6764.

EigenGates of the same class generally differ only by the phases that they apply to each eigenspace. Therefore their _value_equality_values_ can be determined uniquely by calculating these phases. This is both faster (no need to calculate the period or canonical exponent) and more robust (exponentiation of phases is accounted for) than the existing implementation. The simplification also allows it to handle large global shifts that cause division-by-zero errors in period calculations (#6764). Additionally, many different edge cases with symbolic exponents are easier to identify and account for.

This also allows us to get rid of some superfluous _value_equality_values_ implementations in subclasses, except where the eigenspaces themselves are changeable.

@daxfohl daxfohl requested review from vtomole and a team as code owners February 11, 2025 19:57
@daxfohl daxfohl requested a review from maffoo February 11, 2025 19:57
@CirqBot CirqBot added the size: M 50< lines changed <250 label Feb 11, 2025
Copy link

codecov bot commented Feb 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.65%. Comparing base (4436b85) to head (9e1ab93).
Report is 39 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7057      +/-   ##
==========================================
- Coverage   98.66%   98.65%   -0.01%     
==========================================
  Files        1106     1106              
  Lines       95914    95852      -62     
==========================================
- Hits        94630    94564      -66     
- Misses       1284     1288       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! Thank you!

@pavoljuhas pavoljuhas added the priority/before-1.5 Finish before the Cirq 1.5 release label Apr 5, 2025
@daxfohl daxfohl enabled auto-merge April 5, 2025 04:03
@daxfohl daxfohl added this pull request to the merge queue Apr 5, 2025
Merged via the queue into quantumlib:main with commit d87d87e Apr 5, 2025
38 checks passed
@daxfohl daxfohl deleted the fix-eigen-equality branch April 5, 2025 04:32
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 6, 2025

@Strilanc can you give this change a quick look? I want to make sure I'm not making any incorrect assumptions before cutting 1.5, especially given the logic changed the results of an equality test.

@daxfohl daxfohl added the BREAKING CHANGE For pull requests that are important to mention in release notes. label Apr 18, 2025
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 18, 2025

Adding BREAKING_CHANGE label since it changes an existing unit test. The behavior of that unit test was ostensibly incorrect, as discussed in #7052. Though the implementation is very different from previous implementation, and hard to compare, the sum effect of this change is:

  • BEFORE: gates are equal iff canonical exponent and absolute (unexponentiated) global phase are equal.
  • AFTER: gates are equal iff canonical exponent and exponentiated/periodized global phase are equal. (This is equivalent to all phased/periodized eigenvalues being the same, which is what the implementation actually does).

Thus, some gates that were previously incorrectly considered unequal are now equal, as issue #7052 describes. There are no cases where gates that were previously considered equal are now unequal (if the canonical exponent is the same and the unexponentiated global phase is the same, then the exponentiated/periodized global phase will be the same).

BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
* Fix equality for EigenGates

* Handle zero-phase with symbolic exponents

* Simplify code

* Remove superfluous _value_equality_values_

* more tests

* more tests

* Fix pauli_interaction_gate change

* Remove unnecessary EigenGate._value_equality_approximate_values_ override

* resort imports

* sort imports

* remove unused import

* Add details to EigenGate._value_equality_values_ docstring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE For pull requests that are important to mention in release notes. priority/before-1.5 Finish before the Cirq 1.5 release size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EigenGate equality does not account for exponent when comparing global phase Optimizer failure
3 participants