Skip to content

Cannot add identity to PauliSum #4087

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

Closed
95-martin-orion opened this issue May 5, 2021 · 3 comments · Fixed by #4103
Closed

Cannot add identity to PauliSum #4087

95-martin-orion opened this issue May 5, 2021 · 3 comments · Fixed by #4103
Labels
area/paulis help wanted kind/bug-report Something doesn't seem to work. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on

Comments

@95-martin-orion
Copy link
Collaborator

Description of the issue

Identities cannot be added to PauliSums. This might be related to cirq.IdentityGate not being a cirq.Pauli.

How to reproduce the issue

ps = cirq.PauliSum()
ps += cirq.I(cirq.LineQubit(0))

# Generates the error:
# TypeError: unsupported operand type(s) for +=: 'PauliSum' and 'GateOperation'

Cirq version
0.11.0.dev

@95-martin-orion 95-martin-orion added the kind/bug-report Something doesn't seem to work. label May 5, 2021
@95-martin-orion
Copy link
Collaborator Author

In lieu of a fix, users can wrap identity gates in cirq.PauliStrings before mixing them into PauliSums to avoid this issue.

psum = cirq.PauliSum()
pstr = cirq.PauliString(cirq.I(cirq.LineQubit(0)))
# does not fail
psum += pstr

@BichengYing
Copy link
Collaborator

Another shorter workaround is

ps = cirq.PauliSum()
ps += 1*cirq.I(cirq.LineQubit(0))

but I was also wondering if cirq.XPowerGate(exponent=0) acts same as IdentifyGate or not. (Seems no except global phase difference).

NoureldinYosri added a commit to NoureldinYosri/Cirq that referenced this issue 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
NoureldinYosri added a commit to NoureldinYosri/Cirq that referenced this issue May 26, 2021
@viathor
Copy link
Collaborator

viathor commented May 26, 2021

The PauliString class maps sets of qubits to Pauli gates. This representation makes an implicit assumption that the operator corresponding to any given key-value pair in the dictionary acts as a scalar multiple of the identity on qubits which are not members of its key. This has a special case for scalars: scalar is an operator that acts as a scalar multiple of the identity on all qubits, hence the empty key.

This gives us another workaround for the issue:

ps = cirq.PauliSum()
ps += 1

which actually works fine at the moment. I think that the above code snippet and

ps = cirq.PauliSum()
ps += cirq.I(cirq.LineQubit(0))

should have the same effect once the issue is fixed.

@viathor viathor added area/paulis triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on labels Jun 14, 2021
CirqBot pushed a commit that referenced this issue Jul 2, 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.
rht pushed a commit to rht/Cirq that referenced this issue 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 issue 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
area/paulis help wanted kind/bug-report Something doesn't seem to work. triage/accepted A consensus emerged that this bug report, feature request, or other action should be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants