-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Compensate for global phase in MatrixGate decomposition #7118
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
Conversation
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 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7118 +/- ##
==========================================
- Coverage 98.65% 98.65% -0.01%
==========================================
Files 1106 1106
Lines 95870 95885 +15
==========================================
+ Hits 94583 94592 +9
- Misses 1287 1293 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
@tanujkhattar do you have a recommendation here? This PR compensates for global phase in MatrixGate decomposition, fixing the associated issues regarding the broken decomposition of certain controlled gates that neglect the controlled phase. The change adds global phase ops to compensate, which is straightforward enough, but the remaining concern is that global phase op is not part of the ionq gateset, so the addition of this op during decomposition causes some of their unit tests to break. For now, to allow their tests to pass, I added global phase ops to their gateset. Is this okay or is there something else we should do? One alternative is we could add a postprocessor to their gateset rules that drops global phase ops from the final circuit (seems straightforward). Alternately we could add a flag to |
I think this is a much needed fix and I'm very happy to see this finally happening. Thanks for doing this!
I think this is fine and we can merge the PR as is. Thanks! |
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 aMatrixGate
. This allows the controlled decomposition tests to usenp.testing.assert_allclose
as desired, and also allows controlledMatrixGates
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