-
Notifications
You must be signed in to change notification settings - Fork 603
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
modified __add__
for Observable
and Hamiltonian
to support adding 0
#2603
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2603 +/- ##
=======================================
Coverage 99.58% 99.58%
=======================================
Files 248 248
Lines 19975 19985 +10
=======================================
+ Hits 19892 19902 +10
Misses 83 83
Continue to review full report at Codecov.
|
Hi @Qi-Hu, let us know when the PR is ready for review, or if you have any questions 🙂 |
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.
Hi @Qi-Hu, the core solution is looking great overall! 🎉 🙂 Great to have support for adding 0
to observables both from the left side and the right side.
There's one subtlety that would be great to adjust and a couple of additional things to test.
- The in-place summation (
__iadd__
method) of theqml.Hamiltonian
class has a different implementation to the regular addition (__add__
method).
Although not explicitly mentioned in the original issue, attempting to add 0
to a Hamiltonian object using in-place addition raises an error:
H = sum([qml.PauliX(i) for i in range(10)])
H += 0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-2918b18b0578> in <module>
----> 1 H += 0
~/pennylane/pennylane/ops/qubit/hamiltonian.py in __iadd__(self, H)
620 return self
621
--> 622 raise ValueError(f"Cannot add Hamiltonian and {type(H)}")
623
624 def __imul__(self, a):
ValueError: Cannot add Hamiltonian and <class 'int'>
- As for testing, it would be great to add a couple of further cases:
A. Checking two remaining qubit observables:
- SparseHamiltonian
- Projector
B. Testing the in-place addition support via the +=
operator for a few general observables.
C. Testing that the feature works for continuous-variable (CV) observables too. These observables would subclass CVObservable
.
Co-authored-by: antalszava <antalszava@gmail.com>
Hi @antalszava, thanks a lot for your detailed comments and useful suggestions! I have made a few changes according to your suggestions. I have two questions:
|
Hi @antalszava, I realized the |
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.
Hi @Qi-Hu, this looks good to me! 👍 It will be very useful to have this in. 🙂
Apologies for the delay here, wasn't feeling completely well and then it has been a bit busy.
Thank you for addressing the suggestions.
I realized the compare method is not supported by a few classes: Projector, SparseHamiltonian, QuadOperator and FockStateProjector. To be more specific, their parameters do not have tobytes attribute. I do not know if this is an issue that needs to be fixed, so I just didn't include these test cases in my most recent commit.
Good catch! This should be good as such indeed. 👍
Thanks @antalszava!, please let me know if there is anything else I could do. |
Sure 🙂 Merged it, thank you for this contribution! 🎉 |
Glad to hear that! Thank you for your help! |
🎉 |
Context:
We would like to be able to sum over a list of observables as follows:
Since
sum
starts at integer0
by default, we need to define addition between observables and0
.Description of the Change:
We modified the
__add__
method forObservable
andHamiltonian
classes, so that integer0
can be added to those objects, either from the left side or the right side. Unit tests were added for this feature.Benefits:
Now we can directly
sum
a list of observables in an elegant way.Possible Drawbacks:
None.
Related GitHub Issues:
Closes #2423.