Skip to content

Commit 4889170

Browse files
added fix for #4087 and a test to ensure adding identity to a PauliSum is a valid operation
1 parent ce58f42 commit 4889170

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cirq-core/cirq/ops/linear_combinations.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from cirq import linalg, protocols, qis, value
3131
from cirq._doc import document
3232
from cirq.linalg import operator_spaces
33-
from cirq.ops import identity, raw_types, pauli_gates, pauli_string
33+
from cirq.ops import identity, raw_types, pauli_gates, pauli_string, gate_operation
3434
from cirq.ops.pauli_string import PauliString, _validate_qubit_mapping
3535
from cirq.value.linear_dict import _format_terms
3636

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

572572
def __add__(self, other):
573+
if isinstance(other, (gate_operation.GateOperation)):
574+
other = PauliString(other)
573575
if not isinstance(other, (numbers.Complex, PauliString, PauliSum)):
574576
return NotImplemented
575577
result = self.copy()

cirq-core/cirq/ops/linear_combinations_test.py

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

1139+
try:
1140+
ps = cirq.PauliSum()
1141+
ps += cirq.I(cirq.LineQubit(0))
1142+
except:
1143+
raise Exception("Failed to add identity to PauliSum.")
1144+
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))
1145+
11391146

11401147
def test_add_number_paulisum():
11411148
q = cirq.LineQubit.range(2)

0 commit comments

Comments
 (0)