Skip to content

Commit 6b6fc98

Browse files
fix for issue #4087 (#4103)
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.
1 parent 680f897 commit 6b6fc98

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

cirq-core/cirq/ops/linear_combinations.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,10 @@ def __iadd__(self, other):
575575

576576
def __add__(self, other):
577577
if not isinstance(other, (numbers.Complex, PauliString, PauliSum)):
578-
return NotImplemented
578+
if hasattr(other, 'gate') and isinstance(other.gate, identity.IdentityGate):
579+
other = PauliString(other)
580+
else:
581+
return NotImplemented
579582
result = self.copy()
580583
result += other
581584
return result

cirq-core/cirq/ops/linear_combinations_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,10 @@ def test_paulisum_validation():
11371137
ld = cirq.LinearDict({key: 2.0})
11381138
assert cirq.PauliSum(ld) == cirq.PauliSum.from_pauli_strings([2 * cirq.X(q[0])])
11391139

1140+
ps = cirq.PauliSum()
1141+
ps += cirq.I(cirq.LineQubit(0))
1142+
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))
1143+
11401144

11411145
def test_add_number_paulisum():
11421146
q = cirq.LineQubit.range(2)

0 commit comments

Comments
 (0)