-
-
Notifications
You must be signed in to change notification settings - Fork 162
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
resolve flaky mirror QV circuit test #2127
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2127 +/- ##
==========================================
- Coverage 98.21% 98.21% -0.01%
==========================================
Files 87 87
Lines 4150 4145 -5
==========================================
- Hits 4076 4071 -5
Misses 74 74 ☔ View full report in Codecov by Sentry. |
Co-authored-by: Misty Wahl <misty@unitary.fund>
Co-authored-by: Misty Wahl <misty@unitary.fund>
this is needed since the `cirq.inverse` function can introduce tiny deviations (on the order of 1e-15) when computing the inverses of matrix gates. exact equality is awesome, but some numerical inacuracy here seems inevitable
8b55aab
to
28f02be
Compare
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.
Thanks for the detailed summary @natestemen, LGTM!
mirror_half_circ.append(op_inverse, strategy=cirq.InsertStrategy.NEW) | ||
|
||
circ = qv_half_circ + mirror_half_circ | ||
mirror_qv_circuit = qv_circuit + cirq.inverse(qv_circuit) |
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.
Thanks for catching this @natestemen!
I don't remember why I added L68-74, but commit history shows it's due to mypy
? I need to look more into item 3 in the PR description.
Even though this code works, it made debugging the code a bit harder than it would have otherwise.
Apologies!
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.
No worries! Sometimes things get missed, or maybe there was actually a really good reason to have it that way that no longer is needed!
Description
Misty and I took a look at the recent failures we've been seeing with the
test_generate_model_circuit_with_seed
test which was originally raised in #2121. We weren't able to reproduce the failure locally, but we did find that the failure is due to tiny numerical differences in matrix gates coming from the inverse part of the mirror circuit. E.g.While trying to fix this, we simplified some of the code to make it more readable (and probably a tiny bit faster). To fix the flaky test itself we use approximate equality instead of exact with an extremely small absolute tolerance to ensure the only differences are from numerics.
fixes #2121
Why can't we reproduce this locally?
The only ideas I have is that the chip running the computations to compute inverses of matrix gates is given more resources on my machine than the GitHub runner. If others have ideas, please say so!
History
Mirror Quantum Volume circuits were introduced in #1838, and indeed the first commit b9777e3 on the branch uses the simplest way to construct a mirror quantum volume circuit using something like the following.
A few commits later 2bf73b4, this code was substantially modified with commit message "mypy". Presumably this change was made to make mypy happy, but in doing so added a lot of complexity to the code. Even though this code works, it made debugging the code a bit harder than it would have otherwise. For that reason, and the general principle that readable code is (almost) always better, we've reverted to a simpler approach. With this approach we do have to use a
typing.cast
call to ensure mypy knows the correct type of the circuit returned fromgenerate_quantum_volume_circuit
.1Post-Mortem
This issue was made more difficult to debug due to the fact that code was rewritten to appease
mypy
2. This, however, should not be the purpose of using mypy. We should do our best to help mypy understand the code we want to write, not rewrite code because mypy is complaining. At its best, mypy is a tool to help make our code better and more readable and at its worst, it can nudge us to rewrite clear code into something that is less readable despite passing mypy.My recommendation here is to follow something like the following practice.
typing.cast
and# type:ignore
comments, but sometimes they are needed.In particular, though, do not add code that will run (at runtime) only to appease mypy.
cc @purva-thakre since you wrote the original implementation, and @Misty-W since we paired on this together.
Footnotes
Which, BTW, is not known by mypy because the return type is a general
QPROGRAM
. ↩My working theory from looking through the commit history. ↩