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
composite ops auto-upgrade tensors and hamiltonians #5031
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #5031 +/- ##
==========================================
- Coverage 99.67% 99.66% -0.01%
==========================================
Files 394 394
Lines 35582 35322 -260
==========================================
- Hits 35467 35205 -262
- Misses 115 117 +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.
Thanks for adding in that additional fix. Looks good 👍
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 👍
# Conflicts: # doc/releases/changelog-dev.md
**Context:** Started in #5031, but missed this upgrade behaviour for `SProd` **Description of the Change:** If the base provided to `SProd.__init__` is a Tensor or Hamiltonian, auto-upgrade it to new arithmetic operators using `qml.operation.convert_to_opmath` **Benefits:** No confusing errors or unexpected behaviour when working with new operator arithmetic. **Possible Drawbacks:** N/A **Related GitHub Issues:** Fixes #5060
**Context:** We can get some spooky-looking Franken-Operators today, and it often happens by accident. For example: ```pycon >>> qml.prod(qml.PauliX(0), 3 * qml.PauliZ(1)) PauliX(wires=[0]) @ (3) [Z1] ``` The second operand became a Hamiltonian instead of an SProd! This makes sense if you think about the order of python execution and how we implemented things, but users shouldn't have to think about that. Moreover, calling `simplify()` on that op won't work so well because things are all mixed up. **Description of the Change:** Update `CompositeOp.__init__` to replace any `Tensor` or `Hamiltonian` operands with `Prod`, `Sum`, or `SProd` operands as required. **Benefits:** User expectations are met, Operator simplification works **Possible Drawbacks:** I suspect this will bring far more benefit than drawback, but it's possible that a user is expecting some weird hybrid operator and is disappointed by this standardization [sc-53356]
**Context:** Started in #5031, but missed this upgrade behaviour for `SProd` **Description of the Change:** If the base provided to `SProd.__init__` is a Tensor or Hamiltonian, auto-upgrade it to new arithmetic operators using `qml.operation.convert_to_opmath` **Benefits:** No confusing errors or unexpected behaviour when working with new operator arithmetic. **Possible Drawbacks:** N/A **Related GitHub Issues:** Fixes #5060
Context:
We can get some spooky-looking Franken-Operators today, and it often happens by accident. For example:
The second operand became a Hamiltonian instead of an SProd! This makes sense if you think about the order of python execution and how we implemented things, but users shouldn't have to think about that. Moreover, calling
simplify()
on that op won't work so well because things are all mixed up.Description of the Change:
Update
CompositeOp.__init__
to replace anyTensor
orHamiltonian
operands withProd
,Sum
, orSProd
operands as required.Benefits:
User expectations are met, Operator simplification works
Possible Drawbacks:
I suspect this will bring far more benefit than drawback, but it's possible that a user is expecting some weird hybrid operator and is disappointed by this standardization
[sc-53356]