Skip to content

Commit 266491a

Browse files
fedimservtomole
authored andcommitted
Create __add__ method for Moment (#2665)
* Added __add__ to moment * Added test. * remove else * add test for non-list circuit * Return NotImplemented if right operand is not Operation
1 parent de6ca2d commit 266491a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Diff for: cirq/ops/moment.py

+5
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ def transform_qubits(self: TSelf_Moment,
193193
def _json_dict_(self):
194194
return protocols.obj_to_dict_helper(self, ['operations'])
195195

196+
def __add__(self, other):
197+
if isinstance(other, raw_types.Operation):
198+
return self.with_operation(other)
199+
return NotImplemented
200+
196201

197202
def _list_repr_with_indented_item_lines(items: Sequence[Any]) -> str:
198203
block = '\n'.join([repr(op) + ',' for op in items])

Diff for: cirq/ops/moment_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,16 @@ def test_immutable_moment():
261261
circuit = cirq.Circuit(cirq.X(q1))
262262
moment = circuit.moments[0]
263263
moment.operations += (cirq.Y(q2),)
264+
265+
266+
def test_add():
267+
a, b = cirq.LineQubit.range(2)
268+
expected_circuit = cirq.Circuit([cirq.CNOT(a, b), cirq.X(a), cirq.Y(b)])
269+
270+
circuit1 = cirq.Circuit([cirq.CNOT(a, b), cirq.X(a)])
271+
circuit1[1] += cirq.Y(b)
272+
assert circuit1 == expected_circuit
273+
274+
circuit2 = cirq.Circuit(cirq.CNOT(a, b), cirq.Y(b))
275+
circuit2[1] += cirq.X(a)
276+
assert circuit2 == expected_circuit

0 commit comments

Comments
 (0)