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

composite ops auto-upgrade tensors and hamiltonians #5031

Merged
merged 5 commits into from Jan 10, 2024

Conversation

timmysilv
Copy link
Contributor

@timmysilv timmysilv commented Jan 9, 2024

Context:
We can get some spooky-looking Franken-Operators today, and it often happens by accident. For example:

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

@timmysilv timmysilv requested a review from a team January 9, 2024 20:54
Copy link

codecov bot commented Jan 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (9a6419c) 99.67% compared to head (8017ee9) 99.66%.

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

@lillian542 lillian542 self-requested a review January 10, 2024 14:48
Copy link
Contributor

@albi3ro albi3ro left a 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 👍

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 👍

# Conflicts:
#	doc/releases/changelog-dev.md
@timmysilv timmysilv enabled auto-merge (squash) January 10, 2024 20:33
@timmysilv timmysilv merged commit 919b6b3 into master Jan 10, 2024
35 checks passed
@timmysilv timmysilv deleted the stop-mixing-op-types branch January 10, 2024 21:22
timmysilv added a commit that referenced this pull request Jan 16, 2024
**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
mudit2812 pushed a commit that referenced this pull request Jan 19, 2024
**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]
mudit2812 pushed a commit that referenced this pull request Jan 19, 2024
**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
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