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

Legacy device API handles Prod observables #5475

Merged
merged 20 commits into from
Apr 12, 2024
Merged

Legacy device API handles Prod observables #5475

merged 20 commits into from
Apr 12, 2024

Conversation

astralcai
Copy link
Contributor

@astralcai astralcai commented Apr 4, 2024

Context:
The legacy device API has special handling of Tensor and Sum as observables, but Prod is not covered by that.

Description of the Change:

  • Prod observables are expanded if they simplify to a Sum
  • In check_validity, the operands of Prod observables are checked.

Possible Drawbacks:
simplify is called on Prod observables many times, which might be inefficient

Related Shortcut Story:
[sc-60584]

Copy link
Contributor

github-actions bot commented Apr 4, 2024

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented Apr 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.66%. Comparing base (b2b0c36) to head (d946dd2).
Report is 5 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5475      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         404      404              
  Lines       37833    37567     -266     
==========================================
- Hits        37709    37442     -267     
- Misses        124      125       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@astralcai astralcai marked this pull request as ready for review April 8, 2024 13:11
@lillian542 lillian542 self-requested a review April 9, 2024 14:58
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

Great work! Thanks @astralcai !

I left mostly questions for now :)

Concerning the renaming of the mock devices in the test I dont have an opinion and would refer to someone from core to judge whether changing all of them is a good idea or not.

pennylane/_device.py Show resolved Hide resolved
pennylane/_device.py Show resolved Hide resolved
pennylane/_device.py Show resolved Hide resolved
pennylane/devices/tests/test_measurements.py Show resolved Hide resolved
tests/test_device.py Show resolved Hide resolved
@astralcai
Copy link
Contributor Author

Concerning the renaming of the mock devices in the test I dont have an opinion and would refer to someone from core to judge whether changing all of them is a good idea or not.

@Qottmann The renaming was because multiple test cases were using different mock devices that were exact duplicates of one another. I removed the duplicates, and changed all these test cases to use just the one that was left.

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 @astralcai! Left a couple of small comments about tests.

tests/test_device.py Show resolved Hide resolved
pennylane/_device.py Show resolved Hide resolved
pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
Copy link
Contributor

@Qottmann Qottmann left a comment

Choose a reason for hiding this comment

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

Thanks @astralcai

My questions are mostly answered and I am overall happy with the PR.
The only thing left to discuss is the simplify. Why do we need it?

pennylane/transforms/hamiltonian_expand.py Show resolved Hide resolved
@astralcai
Copy link
Contributor Author

Thanks @astralcai

My questions are mostly answered and I am overall happy with the PR. The only thing left to discuss is the simplify. Why do we need it?

@Qottmann The purpose of this is to expand a nested Prod out into either a Prod or a Sum. For example if the observable was qml.X(0) @ qml.Y(1) @ qml.Z(2), we get a Prod with two operands, a Prod and a Z, or if the observable was qml.X(0) @ (qml.Y(1) + qml.Z(2)), we would also want to flatten out the nested structure. I know simplify does this, but do you know if there is another less expensive alternative?

Copy link
Contributor

@Qottmann Qottmann 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 for now, happy to move on and revisit later for a better solution with simplify 👍

Thanks @astralcai great work!

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.

What @Qottmann said 🚀

@astralcai astralcai enabled auto-merge (squash) April 12, 2024 19:00
@astralcai astralcai merged commit 8bdf226 into master Apr 12, 2024
38 checks passed
@astralcai astralcai deleted the device-tests branch April 12, 2024 23:34
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.

3 participants