340 lines
40 KiB
Plaintext
340 lines
40 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "710d86a8-3243-4133-b7bc-093306598512",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Write a custom transpiler pass\n",
|
|
"\n",
|
|
"The Qiskit SDK lets you create custom transpilation passes and run them in the `PassManager` object or add them to a `StagedPassManager`. Here we will demonstrate how to write a transpiler pass, focusing on building a pass that performs [Pauli twirling](https://arxiv.org/abs/quant-ph/0606161) on the noisy quantum gates in a quantum circuit. This example uses the DAG, which is the object manipulated by the `TransformationPass` type of pass."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c49a2046-39b0-4e63-aa63-911845ffd315",
|
|
"metadata": {},
|
|
"source": [
|
|
"<details>\n",
|
|
" <summary>\n",
|
|
" Background: DAG representation\n",
|
|
" </summary>\n",
|
|
"\n",
|
|
"Before building a pass, it is important to introduce the internal representation of quantum circuits in Qiskit, the [directed acyclic graph (DAG)](../api/qiskit/qiskit.dagcircuit.DAGCircuit) (see [this tutorial](https://qiskit.org/ecosystem/rustworkx/tutorial/dags.html) for an overview). To follow these steps, install the [`graphviz` library](https://graphviz.org/download/) for the DAG plotting functions.\n",
|
|
"\n",
|
|
"In Qiskit, within the transpilation stages, circuits are represented using a DAG. In general, a DAG is composed of *vertices* (also known as \"nodes\") and directed *edges* that connect pairs of vertices in a particular orientation. This representation is stored using `qiskit.dagcircuit.DAGCircuit` objects that are composed of invididual `DagNode` objects. The advantage of this representation over a pure list of gates (that is, a *netlist*) is that the flow of information between operations is explicit, making it easier to make transformation decisions.\n",
|
|
"\n",
|
|
"This example illustrates the DAG by creating a simple circuit that prepares a Bell state and applies an $R_Z$ rotation, depending on the measurement outcome.\n",
|
|
"\n",
|
|
"```python\n",
|
|
" from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit\n",
|
|
" import numpy as np\n",
|
|
"\n",
|
|
" qr = QuantumRegister(3, 'qr')\n",
|
|
" cr = ClassicalRegister(3, 'cr')\n",
|
|
" qc = QuantumCircuit(qr, cr)\n",
|
|
"\n",
|
|
" qc.h(qr[0])\n",
|
|
" qc.cx(qr[0], qr[1])\n",
|
|
" qc.measure(qr[0], cr[0])\n",
|
|
" qc.rz(np.pi/2, qr[1]).c_if(cr, 2)\n",
|
|
" qc.draw(output='mpl')\n",
|
|
"\n",
|
|
"```\n",
|
|
"\n",
|
|
"\n",
|
|
"Use the `qiskit.tools.visualization.dag_drawer()` function to view this circuit's DAG. There are three kinds of graph nodes: qubit/clbit nodes (green), operation nodes (blue), and output nodes (red). Each edge indicates data flow (or dependency) between two nodes.\n",
|
|
"\n",
|
|
"```python\n",
|
|
"from qiskit.converters import circuit_to_dag\n",
|
|
"from qiskit.tools.visualization import dag_drawer\n",
|
|
"\n",
|
|
"dag = circuit_to_dag(qc)\n",
|
|
"dag_drawer(dag)\n",
|
|
"```\n",
|
|
"\n",
|
|
"</details>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "7e2e2bc6-c900-4d3b-a948-3f5d154cdea2",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Transpiler passes\n",
|
|
"\n",
|
|
"Transpiler passes are classified either as an [`AnalysisPass`](../api/qiskit/qiskit.transpiler.AnalysisPass) or a [`TransformationPass`](../api/qiskit/qiskit.transpiler.TransformationPass). Passes in general work with the [DAG](../api/qiskit/qiskit.dagcircuit.DAGCircuit) and the `property_set`, a dictionary-like object for storing properties determined by analysis passes. Analysis passes work with both the DAG and its `property_set`. They cannot modify the DAG, but can modify the `property_set`. This contrasts with transformation passes, which do modify the DAG, and can read (but not write to) `property_set`. For example, transformation passes translate a circuit to its [ISA](./transpile#instruction-set-architecture) or perform routing passes to insert SWAP gates where needed."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f8ead728-b2b7-47b9-b3b1-f3578b594ffe",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Create a `PauliTwirl` transpiler pass\n",
|
|
"\n",
|
|
"The following example constructs a transpiler pass that adds Pauli twirls. [Pauli twirling](https://arxiv.org/abs/quant-ph/0606161) is an error suppression strategy that randomizes how qubits experience noisy channels, which we assume to be two-qubit gates in this example (because they are much more error-prone than single-qubit gates). The Pauli twirls do not affect the two-qubit gates' operation. They are chosen such that those applied *before* the two-qubit gate (to the left) are countered by those applied *after* the two-qubit gate (to the right). In this sense, the two-qubit operations are identical, but the way they are performed is different. One benefit of Pauli twirling is that it turns coherent errors into stochastic errors, which can be improved by averaging more.\n",
|
|
"\n",
|
|
"Transpiler passes act on the [DAG](../api/qiskit/qiskit.dagcircuit.DAGCircuit), so the important method to override is `.run()`, which takes the DAG as input. Initializing pairs of Paulis as shown preserves the operation of each two-qubit gate. This is done with the helper method `build_twirl_set`, which goes through each two-qubit Pauli (as obtained from `pauli_basis(2)`) and finds the other Pauli that preserves the operation.\n",
|
|
"\n",
|
|
"From the DAG, use the `op_nodes()` method to return all of its nodes. The DAG can also be used to collect runs, which are sequences of nodes that run uninterrupted on a qubit. These can be collected as single-qubit runs with `collect_1q_runs`, two-qubit runs with `collect_2q_runs`, and runs of nodes where the instruction names are in a namelist with `collect_runs`. The `DAGCircuit` has many methods for searching and traversing a graph. One commonly used method is `topological_op_nodes`, which provides the nodes in a dependency ordering. Other methods such as `bfs_successors` are used primarily to determine how nodes interact with subsequent operations on a DAG.\n",
|
|
"\n",
|
|
"In the example, we want to replace each node, representing an instruction, with a subcircuit built as a mini DAG. The mini DAG has a two-qubit quantum register added to it. Operations are added to the mini DAG by using `apply_operation_back`, which places the `Instruction` on the mini DAG's output (whereas `apply_operation_front` would place it on the mini DAG's input). The node is then substituted by the mini DAG by using `substitute_node_with_dag`, and the process continues over each instance of `CXGate` and `ECRGate` in the DAG (corresponding to the two-qubit basis gates on IBM® backends)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "80546e59-dcd8-4b61-88b5-00b220409f6d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit.dagcircuit import DAGCircuit\n",
|
|
"from qiskit.circuit import QuantumCircuit, QuantumRegister, Gate\n",
|
|
"from qiskit.circuit.library import CXGate, ECRGate\n",
|
|
"from qiskit.transpiler import PassManager\n",
|
|
"from qiskit.transpiler.basepasses import TransformationPass\n",
|
|
"from qiskit.quantum_info import Operator, pauli_basis\n",
|
|
"\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"from typing import Iterable, Optional"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "6bb6ff72-43dd-4f8c-a67c-b43af2548b2a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class PauliTwirl(TransformationPass):\n",
|
|
" \"\"\"Add Pauli twirls to two-qubit gates.\"\"\"\n",
|
|
"\n",
|
|
" def __init__(\n",
|
|
" self,\n",
|
|
" gates_to_twirl: Optional[Iterable[Gate]] = None,\n",
|
|
" ):\n",
|
|
" \"\"\"\n",
|
|
" Args:\n",
|
|
" gates_to_twirl: Names of gates to twirl. The default behavior is to twirl all\n",
|
|
" two-qubit basis gates, `cx` and `ecr` for IBM backends.\n",
|
|
" \"\"\"\n",
|
|
" if gates_to_twirl is None:\n",
|
|
" gates_to_twirl = [CXGate(), ECRGate()]\n",
|
|
" self.gates_to_twirl = gates_to_twirl\n",
|
|
" self.build_twirl_set()\n",
|
|
" super().__init__()\n",
|
|
"\n",
|
|
" def build_twirl_set(self):\n",
|
|
" \"\"\"\n",
|
|
" Build a set of Paulis to twirl for each gate and store internally as .twirl_set.\n",
|
|
" \"\"\"\n",
|
|
" self.twirl_set = {}\n",
|
|
"\n",
|
|
" # iterate through gates to be twirled\n",
|
|
" for twirl_gate in self.gates_to_twirl:\n",
|
|
" twirl_list = []\n",
|
|
"\n",
|
|
" # iterate through Paulis on left of gate to twirl\n",
|
|
" for pauli_left in pauli_basis(2):\n",
|
|
"\n",
|
|
" # iterature through Paulis on right of gate to twirl\n",
|
|
" for pauli_right in pauli_basis(2):\n",
|
|
"\n",
|
|
" # save pairs that produce identical operation as gate to twirl\n",
|
|
" if (Operator(pauli_left) @ Operator(twirl_gate)).equiv(Operator(twirl_gate) @ pauli_right):\n",
|
|
" twirl_list.append((pauli_left, pauli_right))\n",
|
|
"\n",
|
|
" self.twirl_set[twirl_gate.name] = twirl_list\n",
|
|
"\n",
|
|
" def run(\n",
|
|
" self,\n",
|
|
" dag: DAGCircuit,\n",
|
|
" ) -> DAGCircuit:\n",
|
|
"\n",
|
|
" # collect all nodes in DAG and proceed if it is to be twirled\n",
|
|
" twirling_gate_classes = tuple(gate.base_class for gate in self.gates_to_twirl)\n",
|
|
" for node in dag.op_nodes():\n",
|
|
" if not isinstance(node.op, twirling_gate_classes):\n",
|
|
" continue\n",
|
|
"\n",
|
|
" # random integer to select Pauli twirl pair\n",
|
|
" pidx = np.random.randint(0, len(self.twirl_set[node.op.name]),)\n",
|
|
" twirl_pair = self.twirl_set[node.op.name][pidx]\n",
|
|
"\n",
|
|
" # instantiate mini_dag and attach quantum register\n",
|
|
" mini_dag = DAGCircuit()\n",
|
|
" register = QuantumRegister(2)\n",
|
|
" mini_dag.add_qreg(register)\n",
|
|
"\n",
|
|
" # apply left Pauli, gate to twirl, and right Pauli to empty mini-DAG\n",
|
|
" mini_dag.apply_operation_back(twirl_pair[0].to_instruction(), [register[0], register[1]])\n",
|
|
" mini_dag.apply_operation_back(node.op, [register[0], register[1]])\n",
|
|
" mini_dag.apply_operation_back(twirl_pair[1].to_instruction(), [register[0], register[1]])\n",
|
|
"\n",
|
|
" # substitute gate to twirl node with twirling mini-DAG\n",
|
|
" dag.substitute_node_with_dag(node, mini_dag)\n",
|
|
"\n",
|
|
" return dag"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "da136beb-d1b2-4f68-8274-386e80a49860",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Use the `PauliTwirl` transpiler pass\n",
|
|
"\n",
|
|
"The following code uses the pass created above to transpile a circuit. Consider a simple circuit with `cx`s and `ecr`s."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "9123905d-b4cb-4ae9-9695-4ad77e70bdab",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"image/svg+xml": [
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg width=\"266.5pt\" height=\"172pt\" version=\"1.1\" viewBox=\"0 0 266.5 172\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style></defs><path d=\"m0 172h266.5v-172h-266.5z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h190.05\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h190.05\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h190.05\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v185.42h1158.8z\" clip-path=\"url(#f888e912308)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m89.64 90.637v-46.354\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.7 136.99v-46.354\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m89.64 48.803c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958 0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237-0.84753 0.84753-1.3237 1.9972-1.3237 3.1958 0 1.1986 0.4762 2.3482 1.3237 3.1958 0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#f888e912308)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m89.64 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#f888e912308)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m120.07 152.06h31.85v-76.484h-31.85z\" clip-path=\"url(#f888e912308)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m166.42 105.7h31.85v-76.484h-31.85z\" clip-path=\"url(#f888e912308)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m228.7 141.51c1.1986 0 2.3482-0.4762 3.1958-1.3237s1.3237-1.9972 1.3237-3.1958c0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237-0.84753 0.84753-1.3237 1.9972-1.3237 3.1958 0 1.1986 0.4762 2.3482 1.3237 3.1958 0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#f888e912308)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m228.7 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#f888e912308)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m89.64 96.663v-12.052\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m83.614 90.637h12.052\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.7 96.663v-12.052\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m222.68 90.637h12.052\" clip-path=\"url(#f888e912308)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(37.301 48.734) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-Oblique-71\" transform=\"scale(.015625)\" d=\"m2669 525q-231-303-546-460-314-156-695-156-531 0-833 358-301 358-301 986 0 506 186 978t533 847q225 244 517 375t614 131q387 0 637-153t363-462l100 525h578l-934-4813h-579l360 1844zm-1778 813q0-463 193-705 194-242 560-242 544 0 928 520t384 1264q0 450-199 689-198 239-569 239-272 0-504-127-231-126-403-370-181-256-286-600-104-343-104-668z\"/><path id=\"DejaVuSans-30\" transform=\"scale(.015625)\" d=\"m2034 4250q-487 0-733-480-245-479-245-1442 0-959 245-1439 246-480 733-480 491 0 736 480 246 480 246 1439 0 963-246 1442-245 480-736 480zm0 500q785 0 1199-621 414-620 414-1801 0-1178-414-1799-414-620-1199-620-784 0-1198 620-414 621-414 1799 0 1181 414 1801 414 621 1198 621z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(37.301 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(37.301 141.44) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-32\" transform=\"scale(.015625)\" d=\"m1228 531h2203v-531h-2962v531q359 372 979 998 621 627 780 809 303 340 423 576 121 236 121 464 0 372-261 606-261 235-680 235-297 0-627-103-329-103-704-313v638q381 153 712 231 332 78 607 78 725 0 1156-363 431-362 431-968 0-288-108-546-107-257-392-607-78-91-497-524-418-433-1181-1211z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(123.31 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(123.31 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(130.74 117.4) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-45\" transform=\"scale(.015625)\" d=\"m628 4666h2950v-532h-2319v-1381h2222v-531h-2222v-1691h2375v-531h-3006v4666z\"/><path id=\"DejaVuSans-63\" transform=\"scale(.015625)\" d=\"m3122 3366v-538q-244 135-489 202t-495 67q-560 0-870-355-309-354-309-995t309-996q310-354 870-354 250 0 495 67t489 202v-532q-241-112-499-168-257-57-548-57-791 0-1257 497-465 497-465 1341 0 856 470 1346 471 491 1290 491 265 0 518-55 253-54 491-163z\"/><path id=\"DejaVuSans-72\" transform=\"scale(.015625)\" d=\"m2631 2963q-97 56-211 82-114 27-251 27-488 0-749-317t-261-911v-1844h-578v3500h578v-544q182 319 472 473 291 155 707 155 59 0 131-8 72-7 159-23l3-590z\"/></defs><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(169.67 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(169.67 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#f888e912308)\"><g transform=\"translate(177.09 71.047) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><defs><clipPath id=\"f888e912308\"><rect x=\"7.2\" y=\"7.2\" width=\"252.1\" height=\"157.6\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 454.719x284.278 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc = QuantumCircuit(3)\n",
|
|
"qc.cx(0, 1)\n",
|
|
"qc.ecr(1, 2)\n",
|
|
"qc.ecr(1, 0)\n",
|
|
"qc.cx(2, 1)\n",
|
|
"qc.draw('mpl')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f112c2af-c99a-4733-8118-4786e2dfdfd3",
|
|
"metadata": {},
|
|
"source": [
|
|
"To apply the custom pass, build a pass manager using the `PauliTwirl` pass and run it on 50 circuits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "ea884cd2-2446-4b2d-a0f7-82ef574b11ee",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"pm = PassManager([PauliTwirl()])\n",
|
|
"twirled_qcs = [pm.run(qc) for _ in range(50)]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3d4d921e-1a43-4f9a-bc30-6c64092d8f6b",
|
|
"metadata": {},
|
|
"source": [
|
|
"Each two-qubit gate is now sandwiched between two Paulis."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "e2515cf3-f8d9-4281-9673-d5a955d7aab9",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"image/svg+xml": [
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg width=\"1008.2pt\" height=\"172pt\" version=\"1.1\" viewBox=\"0 0 1008.2 172\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs><style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style></defs><path d=\"m0 172h1008.2v-172h-1008.2z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h931.72\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h931.72\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h931.72\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v185.42h1158.8z\" clip-path=\"url(#851752f9d54)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m182.35 90.637v-46.354\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m877.66 136.99v-46.354\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m91.101 105.7h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m182.35 48.803c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958 0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237s-2.3482 0.4762-3.1958 1.3237c-0.84753 0.84753-1.3237 1.9972-1.3237 3.1958 0 1.1986 0.4762 2.3482 1.3237 3.1958 0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#851752f9d54)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m182.35 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#851752f9d54)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m230.16 105.7h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m322.87 152.06h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m398.19 152.06h31.85v-76.484h-31.85z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m461.93 152.06h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m554.64 105.7h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m629.96 105.7h31.85v-76.484h-31.85z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m693.7 105.7h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m786.41 152.06h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m877.66 141.51c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958 0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237-0.84753 0.84753-1.3237 1.9972-1.3237 3.1958 0 1.1986 0.4762 2.3482 1.3237 3.1958s1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#851752f9d54)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m877.66 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#851752f9d54)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m925.47 152.06h43.434v-76.484h-43.434z\" clip-path=\"url(#851752f9d54)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m182.35 96.663v-12.052\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m176.32 90.637h12.052\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m877.66 96.663v-12.052\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m871.63 90.637h12.052\" clip-path=\"url(#851752f9d54)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(37.301 48.734) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-Oblique-71\" transform=\"scale(.015625)\" d=\"m2669 525q-231-303-546-460-314-156-695-156-531 0-833 358-301 358-301 986 0 506 186 978t533 847q225 244 517 375t614 131q387 0 637-153t363-462l100 525h578l-934-4813h-579l360 1844zm-1778 813q0-463 193-705 194-242 560-242 544 0 928 520t384 1264q0 450-199 689-198 239-569 239-272 0-504-127-231-126-403-370-181-256-286-600-104-343-104-668z\"/><path id=\"DejaVuSans-30\" transform=\"scale(.015625)\" d=\"m2034 4250q-487 0-733-480-245-479-245-1442 0-959 245-1439 246-480 733-480 491 0 736 480 246 480 246 1439 0 963-246 1442-245 480-736 480zm0 500q785 0 1199-621 414-620 414-1801 0-1178-414-1799-414-620-1199-620-784 0-1198 620-414 621-414 1799 0 1181 414 1801 414 621 1198 621z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(37.301 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(37.301 141.44) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-32\" transform=\"scale(.015625)\" d=\"m1228 531h2203v-531h-2962v531q359 372 979 998 621 627 780 809 303 340 423 576 121 236 121 464 0 372-261 606-261 235-680 235-297 0-627-103-329-103-704-313v638q381 153 712 231 332 78 607 78 725 0 1156-363 431-362 431-968 0-288-108-546-107-257-392-607-78-91-497-524-418-433-1181-1211z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(94.345 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(94.345 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(112.73 77.548) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-59\" transform=\"scale(.015625)\" d=\"m-13 4666h679l1293-1919 1285 1919h678l-1650-2444v-2222h-634v2222l-1651 2444z\"/><path id=\"DejaVuSans-58\" transform=\"scale(.015625)\" d=\"m403 4666h678l1160-1735 1165 1735h678l-1500-2241 1600-2425h-678l-1312 1984-1322-1984h-681l1665 2491-1453 2175z\"/></defs><use xlink:href=\"#DejaVuSans-59\"/><use x=\"61.083984\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(102.58 63.631) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-50\" transform=\"scale(.015625)\" d=\"m1259 4147v-1753h794q441 0 681 228 241 228 241 650 0 419-241 647-240 228-681 228h-794zm-631 519h1425q785 0 1186-355 402-355 402-1039 0-691-402-1044-401-353-1186-353h-794v-1875h-631v4666z\"/><path id=\"DejaVuSans-61\" transform=\"scale(.015625)\" d=\"m2194 1759q-697 0-966-159t-269-544q0-306 202-486 202-179 548-179 479 0 768 339t289 901v128h-572zm1147 238v-1997h-575v531q-197-318-491-470t-719-152q-537 0-855 302-317 302-317 808 0 590 395 890 396 300 1180 300h807v57q0 397-261 614t-733 217q-300 0-585-72-284-72-546-216v532q315 122 612 182 297 61 578 61 760 0 1135-394 375-393 375-1193z\"/><path id=\"DejaVuSans-75\" transform=\"scale(.015625)\" d=\"m544 1381v2119h575v-2097q0-497 193-746 194-248 582-248 465 0 735 297 271 297 271 810v1984h575v-3500h-575v538q-209-319-486-474-276-155-642-155-603 0-916 375-312 375-312 1097zm1447 2203z\"/><path id=\"DejaVuSans-6c\" transform=\"scale(.015625)\" d=\"m603 4863h575v-4863h-575v4863z\"/><path id=\"DejaVuSans-69\" transform=\"scale(.015625)\" d=\"m603 3500h575v-3500h-575v3500zm0 1363h575v-729h-575v729z\"/></defs><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(233.41 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(233.41 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(251.8 77.548) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-5a\" transform=\"scale(.015625)\" d=\"m359 4666h3666v-482l-2950-3653h3022v-531h-3809v481l2950 3653h-2879v532z\"/></defs><use xlink:href=\"#DejaVuSans-5a\"/><use x=\"68.505859\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(241.64 63.631) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(326.12 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(326.12 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(344.21 123.9) scale(.08 -.08)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-58\"/><use x=\"68.505859\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(334.35 109.98) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(401.44 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(401.44 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(408.86 117.4) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-45\" transform=\"scale(.015625)\" d=\"m628 4666h2950v-532h-2319v-1381h2222v-531h-2222v-1691h2375v-531h-3006v4666z\"/><path id=\"DejaVuSans-63\" transform=\"scale(.015625)\" d=\"m3122 3366v-538q-244 135-489 202t-495 67q-560 0-870-355-309-354-309-995t309-996q310-354 870-354 250 0 495 67t489 202v-532q-241-112-499-168-257-57-548-57-791 0-1257 497-465 497-465 1341 0 856 470 1346 471 491 1290 491 265 0 518-55 253-54 491-163z\"/><path id=\"DejaVuSans-72\" transform=\"scale(.015625)\" d=\"m2631 2963q-97 56-211 82-114 27-251 27-488 0-749-317t-261-911v-1844h-578v3500h578v-544q182 319 472 473 291 155 707 155 59 0 131-8 72-7 159-23l3-590z\"/></defs><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(465.18 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(465.18 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(483.27 123.9) scale(.08 -.08)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-58\"/><use x=\"68.505859\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(473.41 109.98) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(557.89 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(557.89 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(579.1 77.548) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-49\" transform=\"scale(.015625)\" d=\"m628 4666h631v-4666h-631v4666z\"/></defs><use xlink:href=\"#DejaVuSans-49\"/><use x=\"29.492188\" xlink:href=\"#DejaVuSans-49\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(566.12 63.631) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(633.21 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(633.21 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(640.63 71.047) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(696.95 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(696.95 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(718.16 77.548) scale(.08 -.08)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-49\"/><use x=\"29.492188\" xlink:href=\"#DejaVuSans-49\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(705.18 63.631) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(789.66 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(789.66 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(807.75 123.9) scale(.08 -.08)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-5a\"/><use x=\"68.505859\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(797.89 109.98) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(928.72 140.58) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(928.72 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(947.4 123.9) scale(.08 -.08)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-59\"/><use x=\"61.083984\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#851752f9d54)\"><g transform=\"translate(936.95 109.98) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-50\"/><use x=\"55.802734\" xlink:href=\"#DejaVuSans-61\"/><use x=\"117.082031\" xlink:href=\"#DejaVuSans-75\"/><use x=\"180.460938\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"208.244141\" xlink:href=\"#DejaVuSans-69\"/></g></g><defs><clipPath id=\"851752f9d54\"><rect x=\"7.2\" y=\"7.2\" width=\"993.76\" height=\"157.6\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 1792.5x284.278 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"twirled_qcs[-1].draw('mpl')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "41cccfba-8b65-401c-9d30-66396a3fe3a1",
|
|
"metadata": {},
|
|
"source": [
|
|
"The operators are the same if `Operator` from `qiskit.quantum_info` is used:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "0f8b4ca1-4205-4b71-941d-0852eebbe917",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"np.True_"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"np.all([Operator(twirled_qc).equiv(qc) for twirled_qc in twirled_qcs])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "0d680854-13f8-4ccd-9be1-595edb4f942c",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Next steps\n",
|
|
"\n",
|
|
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
|
|
" - To learn how to use the `transpile` function, start with the [Transpilation default settings and configuration options](defaults-and-configuration-options) topic.\n",
|
|
" - Try the [Submit transpiled circuits](https://learning.quantum.ibm.com/tutorial/submit-transpiled-circuits) tutorial.\n",
|
|
" - Review the [Transpile API documentation.](https://docs.quantum-computing.ibm.com/api/qiskit/transpiler)\n",
|
|
"</Admonition>"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"description": "Learn how to write your own transpiler pass using Qiskit, including DAG circuit representation",
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"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"
|
|
},
|
|
"title": "Write your own transpiler pass"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|