Skip to content

Break intermediate measurements on 3+ qubits into single qubit measurements in RouteCQC#6293 #6349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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: 16 additions & 1 deletion cirq-core/cirq/transformers/routing/route_circuit_cqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,32 @@ 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.
"""
two_qubit_circuit = circuits.Circuit()
single_qubit_ops: List[List[cirq.Operation]] = []
current_moment = 0

for moment in circuit:
current_moment += 1
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 isinstance(op.gate, ops.MeasurementGate):
if len(circuit.moments) == current_moment:
single_qubit_ops[timestep].append(op)
elif op.gate.key == "":
single_qubit_ops[timestep] += [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'
)
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
47 changes: 47 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,53 @@ 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)
q = cirq.LineQubit.range(2)
hard_coded_mapper = cirq.HardCodedInitialMapper({q[i]: q[i] for i in range(2)})
circuit = cirq.Circuit(cirq.measure_each(*q), cirq.H.on_each(q))
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)
q = cirq.LineQubit.range(3)
hard_coded_mapper = cirq.HardCodedInitialMapper({q[i]: q[i] for i in range(3)})
circuit = cirq.Circuit(
[cirq.Moment(cirq.MeasurementGate(3).on(*q)), cirq.Moment(cirq.H.on_each(q))]
)
routed_circuit = router(
circuit, initial_mapper=hard_coded_mapper, context=cirq.TransformerContext(deep=True)
)
expected = cirq.Circuit(
[
cirq.Moment(cirq.measure(q[0]), cirq.measure(q[1]), cirq.measure(q[2])),
cirq.Moment(cirq.H.on_each(q)),
]
)
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)
q = cirq.LineQubit.range(3)
hard_coded_mapper = cirq.HardCodedInitialMapper({q[i]: q[i] for i in range(3)})
circuit = cirq.Circuit(cirq.MeasurementGate(3, key="key").on(*q), cirq.H.on_each(*q))
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
2 changes: 1 addition & 1 deletion docs/dev/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ See the previous section for instructions.
If you want to pip install cirq in an editable fashion, you'll have to install it per module, e.g.:

```
pip install -e ./cirq-core -e ./cirq-google -e ./cirq-ionq -e ./cirq-aqt
pip install -e . ./cirq-core -e ./cirq-google -e ./cirq-ionq -e ./cirq-aqt ./cirq-ft ./cirq-pasqal ./cirq-rigetti ./cirq-web
```

Note that `pip install -e .` will install the `cirq` metapackage only, and your code changes won't
Expand Down
86 changes: 77 additions & 9 deletions docs/transform/routing_transformer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"id": "EJcUPXkY0K93"
},
Expand Down Expand Up @@ -145,11 +145,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"id": "NafLVgAD34IL"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌──┐\n",
"0: ────X─────X───@───────X───\n",
" │ │ │\n",
"1: ────┼@────@───X───H───@───\n",
" ││ │\n",
"2: ────@┼────X───────H───X───\n",
" │ │\n",
"3: ─────X────────────────@───\n",
" └──┘\n"
]
}
],
"source": [
"# The circuit to be routed\n",
"q = cirq.LineQubit.range(4)\n",
Expand Down Expand Up @@ -183,11 +199,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"id": "NO1dyRbS9-1x"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌──┐ ┌──┐\n",
"(1, 5): ────X─────X────────────@─────────X───\n",
" │ │ │\n",
"(2, 4): ────┼@────@───────×────┼X────────┼───\n",
" ││ │ │ ││ │\n",
"(2, 5): ────@┼────X───H───×────X┼────H───@───\n",
" │ │\n",
"(3, 4): ─────X──────────────────@────────────\n",
" └──┘ └──┘\n"
]
}
],
"source": [
"routed_circuit = router(circuit)\n",
"print(routed_circuit)"
Expand Down Expand Up @@ -240,11 +272,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"id": "DU8lHNiK4eCL"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cirq.HardCodedInitialMapper({cirq.LineQubit(0): cirq.GridQubit(3, 5), cirq.LineQubit(1): cirq.GridQubit(3, 4), cirq.LineQubit(2): cirq.GridQubit(2, 5), cirq.LineQubit(3): cirq.GridQubit(2, 4)})\n"
]
}
],
"source": [
"# Use a hard-coded initial mapping strategy of logical to physical that places q0, q1, q2, q3 onto\n",
"# Grid(3, 5), Grid(3, 6), Grid(4, 5), Grid(4, 6), respectively\n",
Expand All @@ -257,11 +297,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"id": "RMGa1h2uoKui"
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌──┐\n",
"(2, 4): ─────X───────────────────────────────────────────@───────────\n",
" │ │\n",
"(2, 5): ────@┼───────────────────────────────────X───H───X───────────\n",
" ││ │\n",
"(3, 4): ────┼@────────×[cirq.RoutingSwapTag()]───┼───@───────────X───\n",
" │ │ │ │ │\n",
"(3, 5): ────X─────X───×──────────────────────────@───X───────H───@───\n",
" └──┘\n"
]
}
],
"source": [
"routed_circuit = router(circuit, lookahead_radius = 5, tag_inserted_swaps=True, initial_mapper=hc_initial_mapper)\n",
"print(routed_circuit)"
Expand Down Expand Up @@ -465,6 +521,18 @@
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down