-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add IdentityGate._commutes_
#6702
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
Conversation
It occurs to me that there are other places in the changed file that check for commutation of two Pauli operators. Perhaps I should just add a EDIT: done |
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.
It's better to move this optimization to the commutes_protocol in a section that checks if the inputs are Pauli and leave these files as-is. That way any time protocols.commute
is called this optimization can be applied if the inputs are paulis.
If I work with the commutes protocol, it looks like one neat option is to add something like def _commutes_(self, other: Any, *, atol: float = 1e-8) -> bool:
return True to This change seems reasonable because an identity gate commutes with everything, but I'm not sure whether |
What sorts of "invalid" types come to mind? |
None come to mind for me 🙂 If you think that addition to IdentityGate is good, I'll just go ahead and add it. |
The change to |
Using the IdentityGate option sounds great. The current We don't need to redo the Cirq/cirq-core/cirq/ops/raw_types.py Lines 472 to 481 in 8d8a6c5
|
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.
The IdentityGate variant sounds great, thanks!
MutablePauliString.inplace_after
IdentityGate._commutes_
@pavoljuhas great! I think checking for How are the current proposed changes? I'm not sure what kind of test makes sense for |
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.
One small suggestion, otherwise LGTM.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #6702 +/- ##
=======================================
Coverage 97.83% 97.83%
=======================================
Files 1077 1077
Lines 92493 92502 +9
=======================================
+ Hits 90492 90501 +9
Misses 2001 2001 ☔ View full report in Codecov by Sentry. |
Co-authored-by: Pavol Juhas <[email protected]>
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.
Why revert cirq.commutes(cirq.I, cirq.X)
?
My thinking was to ensure that this test calls |
Let's do that. We try to write tests the way an external user would call the API. |
This reverts commit b5282da.
Makes sense. Done. |
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.
LGTM!
* faster commutation * add _paulis_commute * Revert "add _paulis_commute" This reverts commit 49104f4. * Revert "faster commutation" This reverts commit 05a1efe. * add IdentityGate._commutes_ * add comment * commute with gates only * fix test * fix test * Update cirq-core/cirq/ops/identity_test.py Co-authored-by: Pavol Juhas <[email protected]> * type fix * import fix * minor test change * Revert "minor test change" This reverts commit b5282da. --------- Co-authored-by: Pavol Juhas <[email protected]>
I wrote a custom stabilizer-like simulator in Cirq for this paper (arXiv), which involved commuting Pauli strings through circuit operations. The speed of my simulations turned out to be limited by calls to
protocols.commutes
insideMutablePauliString.inplace_after
. I found that checking conditions for commutation "manually" led to considerable speedups, and figured I could push this change upstream.Long story short: two Pauli operators
P
andQ
commute iff (one ofP
orQ
is the identity operator, orP == Q
).As an example, on my laptop the changes in this PR reduce the runtime of the script below from ~2 minutes to ~39 seconds.