-
Notifications
You must be signed in to change notification settings - Fork 615
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
Add __iadd__ method to PauliSentence #4662
Conversation
Thanks for opening this PR @AnuravModak . Just realized a potential clarification. If For example if we have:
|
so here i have made another PR. The idea was to only change the self and the other remains unchanged and the same has been reflected in |
Suppose we have two pauli sentences we want to sum together:
We can use the normal addition:
Or the in-place addition on the larger sentence:
And get the same answer, as expected. But what happens if we try and add the larger sentence onto the smaller one right now?
We end up getting What does this behaviour tell us about what is going on? When we are adding a bigger pauli sentence to a smaller one, we are adding Does that make sense? |
Thanks for your clarification, the behavior you want to achieve, as explained in the provided text, is to always add the other (string2 in your test) to the self (string1 in your test) when performing in-place addition using +=. Currently, my iadd method is designed to add the smaller of the two objects to the larger one, which can lead to the undesired behavior you mentioned. So I am adding the draft code so please confirm if u are looking for something like this: def iadd(self, other): for key in other: if key in self: self[key] += other[key] else: self[key] = other[key] return self |
Exactly 👍 When we had to copy one of the two objects, we wanted to copy the smaller of the two. But we don't have that flexibility of choice with |
Signed-off-by: Anurav Modak <[email protected]>
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.
Once we get the test passing (pending copies in the right place so we don't mutate the test inputs), we will also need to add a changelog entry.
This is located at doc/releases/changelog-dev.md
. You should add an entry under Improvements
and add your name to the list of contributors at the bottom.
Note that we will be creating our release candidate today, so the changelog-dev.md
on master will be getting changed in a few hours. So it might be best to wait a few hours to add a changelog entry to avoid merge conflict issues.
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #4662 +/- ##
==========================================
- Coverage 99.64% 99.64% -0.01%
==========================================
Files 380 380
Lines 34266 34012 -254
==========================================
- Hits 34145 33890 -255
- Misses 121 122 +1
☔ View full report in Codecov by Sentry. |
Hey @albi3ro, the Formatting check / black (pull_request) is still failing, is there anything to do from my side? |
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
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 your work and patience @AnuravModak 🎉
I'm happy to approve, and am tagging the team for the second review, but that should go quickly at this point.
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! 🎉
Thanks @AnuravModak for your contribution! 🏆 This should be released as part of v0.34 in early January. If you would like us to tag you as part of our Twitter/X marketing, feel free to optionally share your handle here. |
You can find me on twitter using this handle: @aakhri_modak. |
So here is the first draft of the changes that i have done: towards #4658
So for the task of defining def __ iadd__(self, other):
The iadd method takes two arguments, self and other.
It checks which of the two sentences (self or other) is smaller in terms of the number of terms.
It iterates over the smaller sentence, adding its terms and coefficients to the larger sentence in-place, without using the copy.
The method returns self to indicate in-place modification.
And for testing part, I have tried to optimize the code as far as possible, and from my understanding it should cover all the conventional test cases, please let me know if there is any loop hole or if something is not wrong.