Skip to content

Commit

Permalink
Define moment+moment operation to return a combined moment (#2879)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Apr 1, 2020
1 parent 3fee6d5 commit 230249c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cirq/circuits/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def _decompose_(self) -> 'cirq.OP_TREE':

# pylint: disable=function-redefined
@overload
def __getitem__(self, key: slice) -> 'Circuit':
def __getitem__(self, key: slice) -> 'cirq.Circuit':
pass

@overload
Expand Down
14 changes: 9 additions & 5 deletions cirq/ops/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def operates_on(self, qubits: Iterable[raw_types.Qid]) -> bool:
"""
return bool(set(qubits) & self.qubits)

def with_operation(self, operation: 'cirq.Operation'):
def with_operation(self, operation: 'cirq.Operation') -> 'cirq.Moment':
"""Returns an equal moment, but with the given op added.
Args:
Expand All @@ -108,7 +108,8 @@ def with_operation(self, operation: 'cirq.Operation'):

return m

def without_operations_touching(self, qubits: Iterable[raw_types.Qid]):
def without_operations_touching(self, qubits: Iterable['cirq.Qid']
) -> 'cirq.Moment':
"""Returns an equal moment, but without ops on the given qubits.
Args:
Expand Down Expand Up @@ -194,8 +195,8 @@ def __str__(self):
return ' and '.join(str(op) for op in self.operations)

def transform_qubits(self: TSelf_Moment,
func: Callable[[raw_types.Qid], raw_types.Qid]
) -> TSelf_Moment:
func: Callable[['cirq.Qid'], 'cirq.Qid']
) -> TSelf_Moment:
"""Returns the same moment, but with different qubits.
Args:
Expand All @@ -212,9 +213,12 @@ def transform_qubits(self: TSelf_Moment,
def _json_dict_(self):
return protocols.obj_to_dict_helper(self, ['operations'])

def __add__(self, other):
def __add__(self,
other: Union['cirq.Operation', 'cirq.Moment']) -> 'cirq.Moment':
if isinstance(other, raw_types.Operation):
return self.with_operation(other)
if isinstance(other, Moment):
return Moment(self.operations + other.operations)
return NotImplemented

# pylint: disable=function-redefined
Expand Down
10 changes: 9 additions & 1 deletion cirq/ops/moment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_immutable_moment():


def test_add():
a, b = cirq.LineQubit.range(2)
a, b, c = cirq.LineQubit.range(3)
expected_circuit = cirq.Circuit([cirq.CNOT(a, b), cirq.X(a), cirq.Y(b)])

circuit1 = cirq.Circuit([cirq.CNOT(a, b), cirq.X(a)])
Expand All @@ -275,6 +275,14 @@ def test_add():
circuit2[1] += cirq.X(a)
assert circuit2 == expected_circuit

m1 = cirq.Moment([cirq.X(a)])
m2 = cirq.Moment([cirq.CNOT(a, b)])
m3 = cirq.Moment([cirq.X(c)])
assert m1 + m3 == cirq.Moment([cirq.X(a), cirq.X(c)])
assert m2 + m3 == cirq.Moment([cirq.CNOT(a, b), cirq.X(c)])
with pytest.raises(ValueError, match='Overlap'):
_ = m1 + m2


def test_indexes_by_qubit():
a, b, c = cirq.LineQubit.range(3)
Expand Down

0 comments on commit 230249c

Please sign in to comment.