-
Notifications
You must be signed in to change notification settings - Fork 603
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
Upgrade fuse_rot_angles
to preserve derivatives and global phases
#6031
Conversation
…and test for singular points of Jacobian
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #6031 +/- ##
==========================================
- Coverage 99.66% 99.65% -0.01%
==========================================
Files 431 431
Lines 41889 41572 -317
==========================================
- Hits 41748 41430 -318
- Misses 141 142 +1 ☔ View full report in Codecov by Sentry. |
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.
Nice work @dwierichs . I have a couple questions, but this looks ready to merge to me :)
Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai>
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.
Congrats @dwierichs , this looks like a lot of tedious work to get right. Really appreciate the detailed derivation and commentary for devs and users 👌 👌 👌
I dont have anything to complain, but will have to take a second look at tests before approving.
Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com>
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, nice one @dwierichs !
Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
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.
Had a look at the tests now as well. Looks good so far, but aren't we missing tests for differentiating with autograd, torch and tf?
Yes, you're right. I was happy to include derivatives in the tests at all, and noticed they started taking quite some time already. But I can add the other interfaces as well 👍 |
I'm working on extending the tests to other interfaces, and the tests start hitting O(10) seconds for this function. |
I think it is fine now for all interfaces, but I can't get TensorFlow to work with broadcasting and the non-broadcasted version takes ages. @Qottmann would it be fine to skip the TensorFlow interface 😬 |
TensorFlow is always being a pain 😢 In general we're happy for some things to slip in terms of feature parity for TensorFlow. But we want to be avoiding giving wrong answers - would that be the case here? |
So I think the code should in principle work with full features also with Tensorflow, but I can't seem to make it work with TF's However, other interfaces like Autograd also need to iterate manually within the test, instead of using something like JAX's |
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.
Awesome job @dwierichs !
That was an impressive upgrade in test coverage, which makes me almost wonder if this shooting over the top 😅 For now, happy with it as it covers all 👍
**Context:** As reported in #5715, `merge_rotations` and `single_qubit_fusion` have problems with differentiability at specific points. #6031 takes care of upgrading the Rot-angle fusion to only ever return NaNs at mathematically non-differentiable points, rather than wrong results. However, the transforms add additional points, based on internal optimizations, where the derivative is flawed. **Description of the Change:** This PR fixes the flawed derivatives caused by the code of the transforms themselves. **Benefits:** Fixes derivatives of `merge_rotations` and `single_qubit_fusion` (where mathematically defined) **Possible Drawbacks:** N/A **Related GitHub Issues:** Fixes another part of #5715, still not all of it. --------- Co-authored-by: Vincent Michaud-Rioux <vincent.michaud-rioux@xanadu.ai> Co-authored-by: Korbinian Kottmann <43949391+Qottmann@users.noreply.github.com> Co-authored-by: Thomas R. Bromley <49409390+trbromley@users.noreply.github.com>
Context:
The current implementation of
fuse_rot_angles
, which is used bymerge_rotations
andsingle_qubit_fusion
, has the following issues:fuse_rot_angles
at singular points leads to wrong derivatives, rather than NaN values, which are mathematically well-motivated.Description of the Change:
The implementation of
fuse_rot_angles
is remade entirely.Benefits:
The remade code uses a comparably simple mathematical expression to compute the fused rotation angles that
qml.math.arctan2
anyways, and thus available in all ML interfaces, including JAX with JIT-compatibility.In summary, the global phases are fixed, Jacobians are only ever NaNs, rather than wrong, and Jacobians are only NaNs when they were NaN or wrong in the current implementation.
Possible Drawbacks:
N/A
Related GitHub Issues:
#5715 (not fixed entirely, just partially)
[sc-63642]