Skip to content

Commit cb45c80

Browse files
restricting the casting in PauliString's add method to GateOperation
1 parent cf189c8 commit cb45c80

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

cirq-core/cirq/ops/linear_combinations.py

+8-5
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,11 +570,14 @@ 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)):
574-
try:
575-
other = PauliString(other)
576-
except:
577-
return NotImplemented
576+
return NotImplemented
577+
# try:
578+
# other = PauliSum.from_pauli_strings([PauliString(other)])
579+
# except:
580+
# return NotImplemented
578581
result = self.copy()
579582
result += other
580583
return result

cirq-core/cirq/ops/linear_combinations_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1139,8 +1139,8 @@ def test_paulisum_validation():
11391139
try:
11401140
ps = cirq.PauliSum()
11411141
ps += cirq.I(cirq.LineQubit(0))
1142-
except e:
1143-
raise Exeption("Failed to add identity to PauliSum.", e)
1142+
except:
1143+
raise Exception("Failed to add identity to PauliSum.")
11441144
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))
11451145

11461146

0 commit comments

Comments
 (0)