Skip to content

fix for issue #4087 #4103

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

Merged
merged 4 commits into from
Jul 2, 2021
Merged

Conversation

NoureldinYosri
Copy link
Collaborator

@NoureldinYosri NoureldinYosri commented May 12, 2021

Problem: the PauliSum add function checks that the type of the other operand is one of numbers.Complex, PauliString, PauliSum and raises NotImplemented otherwise.
Fix: if the other operand is not one of the types try to cast it to PauliString and raise error only if that fails

Fixes #4087.

@NoureldinYosri NoureldinYosri requested review from cduck, vtomole and a team as code owners May 12, 2021 19:13
@NoureldinYosri NoureldinYosri requested a review from dstrain115 May 12, 2021 19:13
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label May 12, 2021
@balopat
Copy link
Contributor

balopat commented May 14, 2021

Problem: the PauliSum add function checks that the type of the other operand is one of numbers.Complex, PauliString, PauliSum and raises NotImplemented otherwise.
Fix: if the other operand is not one of the types try to cast it to PauliString and raise error only if that fails

Hi, thank you for taking this up! Can you please add a test that demonstrates and protects this functionality?

@NoureldinYosri
Copy link
Collaborator Author

Hi, I added a test, please let me know of any suggestions :)

@NoureldinYosri
Copy link
Collaborator Author

@balopat any updates ?

@balopat
Copy link
Contributor

balopat commented May 19, 2021

This looks good, but it seems that there are failing tests...

@NoureldinYosri
Copy link
Collaborator Author

@balopat
looks like the only failing test is this one https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/circuits/circuit_test.py#L299 where in this line https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/circuits/circuit_test.py#L310 it expects the result of addition to be

cirq.Circuit([
     cirq.Moment(
         cirq.X(cirq.NamedQubit('a')),
     ),
 ],
)

but after fixing the addition operator it gives

cirq.PauliSum(cirq.LinearDict({frozenset({(cirq.NamedQubit('a'), cirq.X)}): (1+0j), frozenset(): (1+0j)})

so before I dive deeper in debugging Is this the intended behaviour ?

@NoureldinYosri NoureldinYosri requested a review from wcourtney as a code owner May 25, 2021 21:20
@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@google-cla
Copy link

google-cla bot commented May 25, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no and removed cla: yes Makes googlebot stop complaining. labels May 25, 2021
@NoureldinYosri
Copy link
Collaborator Author

NoureldinYosri commented May 25, 2021

@balopat I restricted the casting to only happen for gate operations, this should fix the issue
PS: I probably did something wrong with git that made it attach unrelated comments to this thread.

@google-cla
Copy link

google-cla bot commented May 25, 2021

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

1 similar comment
@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented May 26, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: yes Makes googlebot stop complaining. and removed cla: no labels May 26, 2021
@google-cla
Copy link

google-cla bot commented May 26, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@maffoo
Copy link
Contributor

maffoo commented May 26, 2021

Ref: #4087

@google-cla
Copy link

google-cla bot commented May 26, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented May 26, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@vtomole vtomole self-requested a review May 26, 2021 19:52
@google-cla
Copy link

google-cla bot commented May 27, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@NoureldinYosri
Copy link
Collaborator Author

@vtomole @balopat a gentle reminder about this PR.

@@ -570,6 +570,8 @@ def __iadd__(self, other):
return self

def __add__(self, other):
if isinstance(other, (gate_operation.GateOperation)):
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed on the Cirq Cynque, we'll need to make this more specific.
Namely, the instance check should be about checking whether this is an instance of identity.IdentityGate. After this I think we're done with this PR :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

but that won't fix the issue in #4087 , the identity there is constructed as cirq.I(cirq.LineQubit(0)) which has type cirq.ops.gate_operation.GateOperation not cirq.ops.identity.IdentityGate, so checking the instance type will fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yes, I meant instance GateOperation, other.gate is instance of IdentityGate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated (y).

@google-cla
Copy link

google-cla bot commented Jun 28, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented Jun 28, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented Jun 28, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@google-cla
Copy link

google-cla bot commented Jun 28, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@balopat balopat self-assigned this Jul 1, 2021
@balopat balopat added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jul 2, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jul 2, 2021
@google-cla
Copy link

google-cla bot commented Jul 2, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@CirqBot CirqBot merged commit 6b6fc98 into quantumlib:master Jul 2, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jul 2, 2021
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Problem: the PauliSum add function checks that the type of the other operand is one of numbers.Complex, PauliString, PauliSum and raises NotImplemented otherwise.
Fix: if the other operand is not one of the types try to cast it to PauliString and raise error only if that fails

Fixes quantumlib#4087.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Problem: the PauliSum add function checks that the type of the other operand is one of numbers.Complex, PauliString, PauliSum and raises NotImplemented otherwise.
Fix: if the other operand is not one of the types try to cast it to PauliString and raise error only if that fails

Fixes quantumlib#4087.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot add identity to PauliSum
6 participants