Skip to content

Commit

Permalink
router mishandling mid-circuit measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
shef4 committed Sep 28, 2023
1 parent 0a68ee5 commit 9698377
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cirq-core/cirq/transformers/routing/route_circuit_cqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,10 @@ def route_circuit(
# 2. Construct a mapping manager that implicitly keeps track of this mapping and provides
# convinience methods over the image of the map on the device graph.
mm = mapping_manager.MappingManager(self.device_graph, initial_mapping)

# 3. Get two_qubit_ops and single-qubit operations.
two_qubit_ops, single_qubit_ops = self._get_one_and_two_qubit_ops_as_timesteps(circuit)

print(mm,two_qubit_ops, single_qubit_ops)
# 4. Do the routing and save the routed circuit as a list of moments.
routed_ops = self._route(
mm,
Expand Down
242 changes: 233 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 @@ -454,6 +510,162 @@
"\n",
"The use cases for this are also similar to overriding the cost function. For example, placing logical qubits on a subset of the physical qubits that are well calibrated or taking advantage of certain device topologies to come up with a initial mapping strategy that yields a more highly connected image under the initial mapping."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (4216171554.py, line 2)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[7], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m pip install -e ./cirq-core -e ./cirq-google -e ./cirq-ionq -e ./cirq-aqt\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"#Degub cirq.RouteCQC()\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0: ───@───────@───M('key')───H───M───\n",
" │ │ │\n",
"1: ───@───@───┼───M──────────H───M───\n",
" │ │ │\n",
"2: ───────@───@───M──────────H───M───\n",
"0: ───×───────@───────────────@───M──────────H───M('q(2)')───\n",
" │ │ │ │\n",
"1: ───×───@───@───H───M───×───@───M('key')───H───M('q(0)')───\n",
" │ │ │\n",
"2: ───────@───────────────×───────M──────────────────────────\n"
]
}
],
"source": [
"import cirq \n",
"import networkx as nx\n",
"q0, q1, q2 = cirq.LineQubit.range(3)\n",
"\n",
"circuit = cirq.Circuit(\n",
" cirq.CZ(q0, q1),\n",
" cirq.CZ(q1, q2),\n",
" cirq.CZ(q0, q2),\n",
" cirq.measure(q0, q1, q2, key=\"key\"),\n",
" cirq.H.on_each(q0, q1, q2),\n",
" cirq.measure_each(q0, q1, q2),\n",
")\n",
"\n",
"\n",
"graph = nx.Graph([[q0, q1], [q1, q2]])\n",
"\n",
"router = cirq.RouteCQC(graph)\n",
"rcirc, initial_map, swap_map = router.route_circuit(circuit)\n",
"print(circuit)\n",
"print(router.route_circuit(circuit)[0])\n",
"\n",
"#cirq.testing.assert_same_circuits(routed_circuit, circuit)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0: ───@───────@───M('')───H───M───\n",
" │ │ │\n",
"1: ───@───@───┼───M───────H───M───\n",
" │ │ │\n",
"2: ───────@───@───M───────H───M───\n",
" ┌──┐\n",
"0: ───@─────────@────────M('')───H───M───\n",
" │ │ │\n",
"1: ───@───@────H┼────M───M───────────────\n",
" │ │ │\n",
"2: ───────@─────@────────M───────H───M───\n",
" └──┘\n"
]
}
],
"source": [
"import cirq \n",
"import networkx as nx\n",
"\n",
"device = cirq.testing.construct_ring_device(3)\n",
"device_graph = device.metadata.nx_graph\n",
"\n",
"q = cirq.LineQubit.range(3)\n",
"\n",
"circuit = cirq.Circuit(cirq.CZ(q[0], q[1]), cirq.CZ(q[1], q[2]), cirq.CZ(q[0], q[2]),cirq.MeasurementGate(3).on(*q),cirq.H.on_each(*q), cirq.measure_each(*q))\n",
"\n",
"hard_coded_mapper = cirq.HardCodedInitialMapper({q[i]: q[i] for i in range(3)})\n",
"\n",
"router = cirq.RouteCQC(device_graph)\n",
"routed_circuit = router(circuit, initial_mapper=hard_coded_mapper)\n",
"\n",
"print(circuit)\n",
"print(routed_circuit)\n",
"\n",
"#cirq.testing.assert_same_circuits(routed_circuit, circuit)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0: ───@───M('')───H───M───\n",
" │ │\n",
"1: ───@───M───────H───M───\n",
"0: ───@───M('')───H───M───\n",
" │ │\n",
"1: ───@───M───────H───M───\n"
]
}
],
"source": [
"import cirq \n",
"import networkx as nx\n",
"\n",
"device = cirq.testing.construct_ring_device(2)\n",
"device_graph = device.metadata.nx_graph\n",
"\n",
"q = cirq.LineQubit.range(2)\n",
"\n",
"circuit = cirq.Circuit(cirq.CZ(q[0], q[1]),cirq.MeasurementGate(2).on(*q),cirq.H.on_each(*q), cirq.measure_each(*q))\n",
"\n",
"hard_coded_mapper = cirq.HardCodedInitialMapper({q[i]: q[i] for i in range(2)})\n",
"\n",
"router = cirq.RouteCQC(device_graph)\n",
"routed_circuit = router(circuit, initial_mapper=hard_coded_mapper)\n",
"\n",
"print(circuit)\n",
"print(routed_circuit)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -465,6 +677,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

0 comments on commit 9698377

Please sign in to comment.