Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
17 changes: 15 additions & 2 deletions cirq-core/cirq/transformers/routing/route_circuit_cqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,30 @@ def _get_one_and_two_qubit_ops_as_timesteps(
The i'th entry in the nested two-qubit and single-qubit ops correspond to the two-qubit
gates and single-qubit gates of the i'th timesteps respectively. When constructing the
output routed circuit, single-qubit operations are inserted before two-qubit operations.

Raises:
ValueError: if circuit has intermediate measurement op's that act on three or more qubits and result is stored.
Comment thread
This conversation was marked as resolved.
Outdated
"""
two_qubit_circuit = circuits.Circuit()
single_qubit_ops: List[List[cirq.Operation]] = []
for moment in circuit:

for i, moment in enumerate(circuit):
for op in moment:
timestep = two_qubit_circuit.earliest_available_moment(op)
single_qubit_ops.extend([] for _ in range(timestep + 1 - len(single_qubit_ops)))
two_qubit_circuit.append(
circuits.Moment() for _ in range(timestep + 1 - len(two_qubit_circuit))
)
if protocols.num_qubits(op) == 2 and not protocols.is_measurement(op):
if protocols.num_qubits(op) > 2 and protocols.is_measurement(op):
if len(circuit.moments) == i + 1:
single_qubit_ops[timestep].append(op)
elif op.gate.key in ('', ops.measure(op.qubits).gate.key):
Comment thread
This conversation was marked as resolved.
Outdated
single_qubit_ops[timestep].extend(ops.measure(qubit) for qubit in op.qubits)
else:
raise ValueError(
'Non-terminal measurements on three or more qubits when result is stored are not supported'
Comment thread
This conversation was marked as resolved.
Outdated
)
elif protocols.num_qubits(op) == 2:
two_qubit_circuit[timestep] = two_qubit_circuit[timestep].with_operation(op)
else:
single_qubit_ops[timestep].append(op)
Expand Down
42 changes: 42 additions & 0 deletions cirq-core/cirq/transformers/routing/route_circuit_cqc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,48 @@ def test_circuit_with_measurement_gates():
cirq.testing.assert_same_circuits(routed_circuit, circuit)


def test_circuit_with_two_qubit_intermediate_measurement_gate():
device = cirq.testing.construct_ring_device(2)
device_graph = device.metadata.nx_graph
router = cirq.RouteCQC(device_graph)
qs = cirq.LineQubit.range(2)
hard_coded_mapper = cirq.HardCodedInitialMapper({qs[i]: qs[i] for i in range(2)})
circuit = cirq.Circuit([cirq.Moment(cirq.measure(qs)), cirq.Moment(cirq.H.on_each(qs))])
routed_circuit = router(
circuit, initial_mapper=hard_coded_mapper, context=cirq.TransformerContext(deep=True)
)
device.validate_circuit(routed_circuit)


def test_circuit_with_multi_qubit_intermediate_measurement_gate_and_result_not_stored():
device = cirq.testing.construct_ring_device(3)
device_graph = device.metadata.nx_graph
router = cirq.RouteCQC(device_graph)
qs = cirq.LineQubit.range(3)
hard_coded_mapper = cirq.HardCodedInitialMapper({qs[i]: qs[i] for i in range(3)})
circuit = cirq.Circuit([cirq.Moment(cirq.measure(qs)), cirq.Moment(cirq.H.on_each(qs))])
routed_circuit = router(
circuit, initial_mapper=hard_coded_mapper, context=cirq.TransformerContext(deep=True)
)
expected = cirq.Circuit([cirq.Moment(cirq.measure_each(qs)), cirq.Moment(cirq.H.on_each(qs))])
cirq.testing.assert_same_circuits(routed_circuit, expected)


def test_circuit_with_multi_qubit_intermediate_measurement_gate_and_result_stored():
device = cirq.testing.construct_ring_device(3)
device_graph = device.metadata.nx_graph
router = cirq.RouteCQC(device_graph)
qs = cirq.LineQubit.range(3)
hard_coded_mapper = cirq.HardCodedInitialMapper({qs[i]: qs[i] for i in range(3)})
circuit = cirq.Circuit(
[cirq.Moment(cirq.measure(qs, key="key_name")), cirq.Moment(cirq.H.on_each(qs))]
Comment thread
This conversation was marked as resolved.
Outdated
)
with pytest.raises(ValueError):
_ = router(
circuit, initial_mapper=hard_coded_mapper, context=cirq.TransformerContext(deep=True)
)


def test_circuit_with_non_unitary_and_global_phase():
device = cirq.testing.construct_ring_device(4)
device_graph = device.metadata.nx_graph
Expand Down