Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions cirq-core/cirq/circuits/circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,21 @@ def test_add_op_tree(circuit_cls) -> None:


@pytest.mark.parametrize('circuit_cls', [cirq.Circuit, cirq.FrozenCircuit])
def test_radd_op_tree(circuit_cls) -> None:
@pytest.mark.parametrize('gate', [cirq.X, cirq.H])
def test_radd_op_tree(circuit_cls, gate) -> None:
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')

c = circuit_cls()
assert [cirq.X(a), cirq.Y(b)] + c == circuit_cls([cirq.Moment([cirq.X(a), cirq.Y(b)])])
assert [gate(a), cirq.Y(b)] + c == circuit_cls([cirq.Moment([gate(a), cirq.Y(b)])])

assert cirq.X(a) + c == circuit_cls(cirq.X(a))
assert [cirq.X(a)] + c == circuit_cls(cirq.X(a))
assert [[[cirq.X(a)], []]] + c == circuit_cls(cirq.X(a))
assert (cirq.X(a),) + c == circuit_cls(cirq.X(a))
assert (cirq.X(a) for _ in range(1)) + c == circuit_cls(cirq.X(a))
assert gate(a) + c == circuit_cls(gate(a))
assert [gate(a)] + c == circuit_cls(gate(a))
assert [[[gate(a)], []]] + c == circuit_cls(gate(a))
assert (gate(a),) + c == circuit_cls(gate(a))
assert (gate(a) for _ in range(1)) + c == circuit_cls(gate(a))
with pytest.raises(AttributeError):
_ = cirq.X + c
_ = gate + c
with pytest.raises(TypeError):
_ = 0 + c

Expand All @@ -380,9 +381,9 @@ def test_radd_op_tree(circuit_cls) -> None:
else:
d = cirq.Circuit()
d.append(cirq.Y(b))
assert [cirq.X(a)] + d == circuit_cls([cirq.Moment([cirq.X(a)]), cirq.Moment([cirq.Y(b)])])
assert cirq.Moment([cirq.X(a)]) + d == circuit_cls(
[cirq.Moment([cirq.X(a)]), cirq.Moment([cirq.Y(b)])]
assert [gate(a)] + d == circuit_cls([cirq.Moment([gate(a)]), cirq.Moment([cirq.Y(b)])])
assert cirq.Moment([gate(a)]) + d == circuit_cls(
[cirq.Moment([gate(a)]), cirq.Moment([cirq.Y(b)])]
)


Expand Down
5 changes: 5 additions & 0 deletions cirq-core/cirq/ops/gate_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import annotations

import numbers
import re
import warnings
from types import NotImplementedType
Expand Down Expand Up @@ -332,12 +333,16 @@ def __rmul__(self, other: Any) -> Any:
return self.gate._rmul_with_qubits(self._qubits, other)

def __add__(self, other):
if not isinstance(other, (ops.Operation, numbers.Number)):
return NotImplemented
return 1 * self + other

def __radd__(self, other):
return other + 1 * self

def __sub__(self, other):
if not isinstance(other, (ops.Operation, numbers.Number)):
return NotImplemented
return 1 * self - other

def __rsub__(self, other):
Expand Down
4 changes: 4 additions & 0 deletions cirq-core/cirq/ops/linear_combinations.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ def __len__(self) -> int:
return len(self._linear_dict)

def __iadd__(self, other):
if isinstance(other, raw_types.Operation):
other = pauli_string._try_interpret_as_pauli_string(other)
if isinstance(other, numbers.Complex):
other = PauliSum.from_pauli_strings([PauliString(coefficient=other)])
elif isinstance(other, PauliString):
Expand All @@ -766,6 +768,8 @@ def __rsub__(self, other):
return -self.__sub__(other)

def __isub__(self, other):
if isinstance(other, raw_types.Operation):
other = pauli_string._try_interpret_as_pauli_string(other)
if isinstance(other, numbers.Complex):
other = PauliSum.from_pauli_strings([PauliString(coefficient=other)])
elif isinstance(other, PauliString):
Expand Down
24 changes: 24 additions & 0 deletions cirq-core/cirq/ops/linear_combinations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,10 +975,18 @@ def test_paulisum_validation() -> None:
ps += cirq.I(cirq.LineQubit(0))
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))

ps = cirq.PauliSum()
ps = cirq.I(cirq.LineQubit(0)) + ps
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))

ps = cirq.PauliSum()
ps -= cirq.I(cirq.LineQubit(0))
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(-1)}))

ps = cirq.PauliSum()
ps = cirq.I(cirq.LineQubit(0)) - ps
assert ps == cirq.PauliSum(cirq.LinearDict({frozenset(): complex(1)}))


def test_add_number_paulisum() -> None:
q = cirq.LineQubit.range(2)
Expand Down Expand Up @@ -1008,6 +1016,22 @@ def test_add_number_paulistring() -> None:
== cirq.PauliSum.from_pauli_strings([cirq.PauliString() * 2, cirq.PauliString({a: cirq.X})])
)

assert (
cirq.X(a) - 2
== -2 + cirq.X(a)
== cirq.PauliSum.from_pauli_strings(
[cirq.PauliString() * -2, cirq.PauliString({a: cirq.X})]
)
)

assert (
2 - cirq.X(a)
== 2 + -cirq.X(a)
== cirq.PauliSum.from_pauli_strings(
[cirq.PauliString() * 2, -cirq.PauliString({a: cirq.X})]
)
)


def test_pauli_sum_formatting() -> None:
q = cirq.LineQubit.range(2)
Expand Down