Skip to content

Commit

Permalink
Fix measurement keys in subcircuits with parent_path (#4616)
Browse files Browse the repository at this point in the history
Applies any parent path in subcircuit to measurement key before returning.

Fixes #4620
  • Loading branch information
daxfohl authored Nov 10, 2021
1 parent a4f5561 commit 13e0225
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cirq-core/cirq/circuits/circuit_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def _measurement_key_objs_(self) -> AbstractSet[value.MeasurementKey]:
for repetition_id in self.repetition_ids
for key in circuit_keys
}
circuit_keys = {
protocols.with_key_path_prefix(key, self.parent_path) for key in circuit_keys
}
object.__setattr__(
self,
'_cached_measurement_key_objs',
Expand Down
12 changes: 11 additions & 1 deletion cirq-core/cirq/circuits/circuit_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ def test_decompose_repeated_nested_measurements():
assert op3.mapped_circuit(deep=True) == expected_circuit


def test_keys_under_parent_path():
a = cirq.LineQubit(0)
op1 = cirq.CircuitOperation(cirq.FrozenCircuit(cirq.measure(a, key='A')))
assert cirq.measurement_key_names(op1) == {'A'}
op2 = op1.with_key_path(('B',))
assert cirq.measurement_key_names(op2) == {'B:A'}
op3 = op2.repeat(2)
assert cirq.measurement_key_names(op3) == {'B:0:A', 'B:1:A'}


def test_mapped_circuit_preserves_moments():
q0, q1 = cirq.LineQubit.range(2)
fc = cirq.FrozenCircuit(cirq.Moment(cirq.X(q0)), cirq.Moment(cirq.X(q1)))
Expand Down Expand Up @@ -802,7 +812,7 @@ def test_tag_propagation():
assert test_tag not in op.tags


def test_keys_under_parent_path():
def test_mapped_circuit_keeps_keys_under_parent_path():
q = cirq.LineQubit(0)
op1 = cirq.CircuitOperation(
cirq.FrozenCircuit(
Expand Down

0 comments on commit 13e0225

Please sign in to comment.