-
Notifications
You must be signed in to change notification settings - Fork 571
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
Fix a sorting order in parameter-shift terms #5583
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5583 +/- ##
==========================================
- Coverage 99.68% 99.68% -0.01%
==========================================
Files 412 412
Lines 38645 38354 -291
==========================================
- Hits 38525 38232 -293
- Misses 120 122 +2 ☔ 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.
Looks good to me!
@@ -6,6 +6,9 @@ | |||
|
|||
<h3>Improvements 🛠</h3> | |||
|
|||
* The sorting order of parameter-shift terms is now guaranteed to resolve ties in the absolute value with the sign of the shifts. | |||
[(#5582)](https://github.com/PennyLaneAI/pennylane/pull/5582) |
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.
Was this the wrong PR number?
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.
Oh dear. Yes!
Context:
Currently,
process_shifts
sorts shifts according to the absolute value of the shift.It uses
kind="stable"
, which guarantees to keep inputs in their input order if they have same absolute value.While this is stable against repeated calls, the stable order is somewhat arbitrarily depending on how a user/PennyLane
arrived at the input shifts.
This is visible in
tests/gradients/core/test_general_shift_rules.py
, where the expected output values sometimes have positive before negative shifts with same magnitude, and sometimes the other way around.Description of the Change:
This PR uses
lexsort
, which is likeargsort
but allows us to use the sign of the shift as a secondary sorting key, producing not only stable but also well-defined sorting results.Benefits:
Predictable sorting results beyond the absolute value alone.
Possible Drawbacks:
Related GitHub Issues:
[sc-45043]