Skip to content

Commit

Permalink
Support decompose protocol on cirq.Moment (#4251)
Browse files Browse the repository at this point in the history
This also enables other protocols on Moment, such as the measurement keys protocol, which currently does not work.
  • Loading branch information
maffoo committed Jun 22, 2021
1 parent 81436fe commit 81d2097
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cirq-core/cirq/ops/moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return self.to_text_diagram()

def _decompose_(self) -> 'cirq.OP_TREE':
"""See `cirq.SupportsDecompose`."""
return self._operations

def transform_qubits(
self: TSelf_Moment,
qubit_map: Union[Dict['cirq.Qid', 'cirq.Qid'], Callable[['cirq.Qid'], 'cirq.Qid']],
Expand Down
19 changes: 19 additions & 0 deletions cirq-core/cirq/ops/moment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,25 @@ def test_container_methods():
assert len(m) == 2


def test_decompose():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
m = cirq.Moment(cirq.X(a), cirq.X(b))
assert list(cirq.decompose(m)) == list(m.operations)


def test_measurement_keys():
a = cirq.NamedQubit('a')
b = cirq.NamedQubit('b')
m = cirq.Moment(cirq.X(a), cirq.X(b))
assert cirq.measurement_keys(m) == set()
assert not cirq.is_measurement(m)

m2 = cirq.Moment(cirq.measure(a, b, key='foo'))
assert cirq.measurement_keys(m2) == {'foo'}
assert cirq.is_measurement(m2)


def test_bool():
assert not cirq.Moment()
a = cirq.NamedQubit('a')
Expand Down

0 comments on commit 81d2097

Please sign in to comment.