Skip to content
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

Merged
merged 6 commits into from
May 10, 2024
Merged

Conversation

dwierichs
Copy link
Contributor

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 like argsort 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]

@dwierichs dwierichs marked this pull request as ready for review April 29, 2024 14:33
Copy link

codecov bot commented Apr 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.68%. Comparing base (9f7e8ba) to head (6fa7ac8).

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.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@lillian542 lillian542 left a 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!

@dwierichs dwierichs enabled auto-merge (squash) May 10, 2024 15:20
@dwierichs dwierichs merged commit ebcb29f into master May 10, 2024
38 checks passed
@dwierichs dwierichs deleted the fix-parshift-order branch May 10, 2024 15:46
@@ -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)
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear. Yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants