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

Compensate for global phase in MatrixGate decomposition #7118

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Mar 4, 2025

Following up on #7112 (which is the first commit in this PR), this PR fixes the issue at its root: it adds a compensating GlobalPhaseOperation when decomposing a MatrixGate. This allows the controlled decomposition tests to use np.testing.assert_allclose as desired, and also allows controlled MatrixGates to be used in decompositions, so that restriction has been removed.

I think this closes #6523, as the core of that issue is being able to decompose controlled MatrixGates, and the specific analytical decomposition methods do not state that they might return any global phase ops, and doing so could be a breaking change. So, this PR fixes the core issue, but with lower likelihood of introducing a breaking change.

cc @pavoljuhas

@CirqBot CirqBot added the size: M 50< lines changed <250 label Mar 4, 2025
@daxfohl
Copy link
Collaborator Author

daxfohl commented Mar 4, 2025

Looks like this creates a global phase in one of the ionq tests, which is not a supported gate in their gateset. I added it to their gateset just to see if it would allow the test to pass (pending). @tanujkhattar what's the best thing to do here? Add it to their gateset? Create a drop_global_phase_ops transformer and add it to their postprocessor? Add a DecomposeArgs field to ignore global phase? Skip this change entirely?

Copy link

codecov bot commented Mar 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.16%. Comparing base (2ad4136) to head (01be7e0).
Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7118    +/-   ##
========================================
  Coverage   98.15%   98.16%            
========================================
  Files        1089     1093     +4     
  Lines       95251    95439   +188     
========================================
+ Hits        93497    93683   +186     
- Misses       1754     1756     +2     

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

np.testing.assert_allclose(cirq.unitary(cgate), cirq.unitary(circuit), atol=1e-1)
first_op = cirq.IdentityGate(qid_shape=shape).on(*qids) # To ensure same qid order
circuit = cirq.Circuit(first_op, *decomposed)
np.testing.assert_allclose(cirq.unitary(cgate), cirq.unitary(circuit), atol=1e-13)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can confirm the test failure in #7071 (comment) does not happen after this change.

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.

This fixes the test failure on my desktop that happened due to phase difference.
I'd defer to @tanujkhattar if this is an OK change for ionq.

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.

Preserve global phase in analytical decompositions
3 participants