540 lines
63 KiB
Plaintext
540 lines
63 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bc54d7bc-b2d8-4b30-9b0b-05689e07a463",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Construct circuits"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "e11dd8ad-7edc-4cf8-bc13-388fe04e84fa",
|
|
"metadata": {
|
|
"tags": [
|
|
"version-info"
|
|
]
|
|
},
|
|
"source": [
|
|
"<details>\n",
|
|
"<summary><b>Package versions</b></summary>\n",
|
|
"\n",
|
|
"The code on this page was developed using the following requirements.\n",
|
|
"We recommend using these versions or newer.\n",
|
|
"\n",
|
|
"```\n",
|
|
"qiskit[all]~=1.2.4\n",
|
|
"qiskit-aer~=0.15.1\n",
|
|
"qiskit-ibm-runtime~=0.31.0\n",
|
|
"qiskit-serverless~=0.17.1\n",
|
|
"qiskit-ibm-catalog~=0.1\n",
|
|
"```\n",
|
|
"</details>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c50d8e43-ae82-4e41-8d17-a37332d1bf6d",
|
|
"metadata": {},
|
|
"source": [
|
|
"This page takes a closer look at the [`QuantumCircuit`](/api/qiskit/qiskit.circuit.QuantumCircuit) class in the Qiskit SDK, including some more advanced methods you can use to create quantum circuits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2664d407-aa95-43a3-9101-d3ad58c2df58",
|
|
"metadata": {},
|
|
"source": [
|
|
"## What is a quantum circuit?\n",
|
|
"\n",
|
|
"A simple quantum circuit is a collection of qubits and a list of instructions that act on those qubits. To demonstrate, the following cell creates a new circuit with two new qubits, then displays the circuit's [`qubits`](/api/qiskit/qiskit.circuit.QuantumCircuit#qubits) attribute."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "b410d397-b67d-4f31-90cf-9c1c34c157c5",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[Qubit(QuantumRegister(2, 'q'), 0), Qubit(QuantumRegister(2, 'q'), 1)]"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(2)\n",
|
|
"qc.qubits"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f5c95cb2-a94f-48f3-b2a6-8ec6c25da5cd",
|
|
"metadata": {},
|
|
"source": [
|
|
"Adding an instruction to the circuit appends the instruction to the circuit's [`data`](/api/qiskit/qiskit.circuit.QuantumCircuit#data) attribute. The following cell output shows `data` is a list of [`CircuitInstruction`](/api/qiskit/qiskit.circuit.CircuitInstruction) objects, each of which has an `operation` attribute, and a `qubits` attribute."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "f7b5573c-b2b2-4cbf-ba55-c53c9221ce71",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[CircuitInstruction(operation=Instruction(name='x', num_qubits=1, num_clbits=0, params=[]), qubits=(Qubit(QuantumRegister(2, 'q'), 0),), clbits=())]"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc.x(0) # Add X-gate to qubit 0\n",
|
|
"qc.data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "17a82d8a-b717-44b8-b3f8-ce89e2588261",
|
|
"metadata": {},
|
|
"source": [
|
|
"The easiest way to view this information is through the [`draw`](/api/qiskit/qiskit.circuit.QuantumCircuit#draw) method, which returns a visualization of a circuit. See [Visualize circuits](./visualize-circuits) for different ways of displaying quantum circuits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "43a57258-3e33-4071-8a48-2bf127c8a5be",
|
|
"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=\"127.32pt\" height=\"125.65pt\" version=\"1.1\" viewBox=\"0 0 127.32 125.65\" 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=\"m-0 125.65h127.32v-125.65h-127.32z\" fill=\"#ffffff\"/><path d=\"m64.497 44.283h50.989\" clip-path=\"url(#422941849ec)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.497 90.637h50.989\" clip-path=\"url(#422941849ec)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.862 21.106h-1158.8v139.06h1158.8z\" clip-path=\"url(#422941849ec)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m74.463 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#422941849ec)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#422941849ec)\"><g transform=\"translate(37.189 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(#422941849ec)\"><g transform=\"translate(37.189 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(#422941849ec)\"><g transform=\"translate(85.076 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><defs><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-58\"/></g></g><defs><clipPath id=\"422941849ec\"><rect x=\"7.2\" y=\"7.2\" width=\"112.92\" height=\"111.25\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 203.683x200.667 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ab5f4bc9-7d7c-4ee7-b1bc-70a313b4fe29",
|
|
"metadata": {},
|
|
"source": [
|
|
"Circuit instruction objects can contain \"definition\" circuits that describe the instruction in terms of more fundamental instructions. For example, the [X-gate](/api/qiskit/qiskit.circuit.library.XGate) is defined as a specific case of the [U3-gate](/api/qiskit/qiskit.circuit.library.U3Gate), a more general single-qubit gate."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "653e2427-e301-4d2f-84de-1959185ace8e",
|
|
"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=\"117.22pt\" height=\"79.296pt\" version=\"1.1\" viewBox=\"0 0 117.22 79.296\" 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 79.296h117.22v-79.296h-117.22z\" fill=\"#ffffff\"/><path d=\"m54.39 44.283h50.989\" clip-path=\"url(#4212acb5954)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m49.755 21.106h-1158.8v92.708h1158.8z\" clip-path=\"url(#4212acb5954)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m64.356 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#4212acb5954)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#4212acb5954)\"><g transform=\"translate(34.719 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\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/></g></g><g clip-path=\"url(#4212acb5954)\"><g transform=\"translate(66.941 55.53) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2c\" transform=\"scale(.015625)\" d=\"m750 794h659v-538l-512-1e3h-403l256 1e3v538z\"/><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 transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(91.992 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(123.78 .78125)\" xlink:href=\"#DejaVuSans-30\"/><use transform=\"translate(187.4 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(219.19 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(250.98 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/></g></g><g clip-path=\"url(#4212acb5954)\"><g transform=\"translate(71.491 43.351) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-55\" transform=\"scale(.015625)\" d=\"m556 4666h635v-2835q0-750 271-1080 272-329 882-329 606 0 878 329 272 330 272 1080v2835h634v-2913q0-912-452-1378-451-466-1332-466-885 0-1337 466-451 466-451 1378v2913z\"/><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-55\"/><use transform=\"translate(74.15 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><defs><clipPath id=\"4212acb5954\"><rect x=\"7.2\" y=\"7.2\" width=\"102.82\" height=\"64.896\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 185.453x117.056 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# Draw definition circuit of 0th instruction in `qc`\n",
|
|
"qc.data[0].operation.definition.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"id": "ee893af1-db43-449f-bfc4-8f636bd98546",
|
|
"metadata": {},
|
|
"source": [
|
|
"Instructions and circuits are similar in that they both describe operations on bits and qubits, but they have different purposes:\n",
|
|
"\n",
|
|
"- Instructions are treated as fixed, and their methods will usually return new instructions (without mutating the original object).\n",
|
|
"- Circuits are designed to be built over many lines of code, and [`QuantumCircuit`](/api/qiskit/qiskit.circuit.QuantumCircuit) methods often mutate the existing object.\n",
|
|
"\n",
|
|
"The rest of this page illustrates how to manipulate quantum circuits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ff4f08f3-48eb-454c-9647-2af505bf79bc",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Build circuits\n",
|
|
"\n",
|
|
"Methods such as [`QuantumCircuit.h`](/api/qiskit/qiskit.circuit.QuantumCircuit#h) and [`QuantumCircuit.cx`](/api/qiskit/qiskit.circuit.QuantumCircuit#cx) add specific instructions to circuits. To add instructions to a circuit more generally, use the [`append`](/api/qiskit/qiskit.circuit.QuantumCircuit#append) method. This takes an instruction and a list of qubits to apply the instruction to. See the [Circuit Library API documentation](/api/qiskit/circuit_library) for a list of supported instructions."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "66813cae-9841-47ea-96b7-8fd7b82e9759",
|
|
"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=\"117.22pt\" height=\"79.296pt\" version=\"1.1\" viewBox=\"0 0 117.22 79.296\" 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 79.296h117.22v-79.296h-117.22z\" fill=\"#ffffff\"/><path d=\"m54.39 44.283h50.989\" clip-path=\"url(#3e1ec2756d5)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m49.755 21.106h-1158.8v92.708h1158.8z\" clip-path=\"url(#3e1ec2756d5)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m64.356 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#3e1ec2756d5)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><g clip-path=\"url(#3e1ec2756d5)\"><g transform=\"translate(34.719 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\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/></g></g><g clip-path=\"url(#3e1ec2756d5)\"><g transform=\"translate(74.533 47.87) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-48\" transform=\"scale(.015625)\" d=\"m628 4666h631v-1913h2294v1913h631v-4666h-631v2222h-2294v-2222h-631v4666z\"/></defs><use xlink:href=\"#DejaVuSans-48\"/></g></g><defs><clipPath id=\"3e1ec2756d5\"><rect x=\"7.2\" y=\"7.2\" width=\"102.82\" height=\"64.896\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 185.453x117.056 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit.circuit.library import HGate\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(1)\n",
|
|
"qc.append(\n",
|
|
" HGate(), # New HGate instruction\n",
|
|
" [0], # Apply to qubit 0\n",
|
|
")\n",
|
|
"qc.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bbdbef08-07c0-4fb0-ab3e-ed6efb7a3dc3",
|
|
"metadata": {},
|
|
"source": [
|
|
"To combine two circuits, use the [`compose`](/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method. This accepts another [`QuantumCircuit`](/api/qiskit/qiskit.circuit.QuantumCircuit) and an optional list of qubit mappings.\n",
|
|
"\n",
|
|
"<Admonition type=\"note\">\n",
|
|
" The [`compose`](/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method returns a new circuit and does **not** mutate either circuit it acts on. To mutate the circuit on which you're calling the [`compose`](/api/qiskit/qiskit.circuit.QuantumCircuit#compose) method, use the argument `inplace=True`.\n",
|
|
"</Admonition>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "29152dfa-2275-4bc4-aadb-82185b9e0e86",
|
|
"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=\"127.43pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 127.43 218.36\" 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 218.36h127.43v-218.36h-127.43z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h50.989\" clip-path=\"url(#1a2ee44b51d)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h50.989\" clip-path=\"url(#1a2ee44b51d)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h50.989\" clip-path=\"url(#1a2ee44b51d)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 183.35h50.989\" clip-path=\"url(#1a2ee44b51d)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#1a2ee44b51d)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m74.575 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#1a2ee44b51d)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m74.575 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#1a2ee44b51d)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m74.575 198.41h30.13v-30.13h-30.13z\" clip-path=\"url(#1a2ee44b51d)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><g clip-path=\"url(#1a2ee44b51d)\"><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(#1a2ee44b51d)\"><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(#1a2ee44b51d)\"><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(#1a2ee44b51d)\"><g transform=\"translate(37.301 187.8) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#1a2ee44b51d)\"><g transform=\"translate(85.188 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><defs><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-58\"/></g></g><g clip-path=\"url(#1a2ee44b51d)\"><g transform=\"translate(85.67 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-59\" transform=\"scale(.015625)\" d=\"m-13 4666h679l1293-1919 1285 1919h678l-1650-2444v-2222h-634v2222l-1651 2444z\"/></defs><use xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#1a2ee44b51d)\"><g transform=\"translate(85.188 186.93) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-5a\" transform=\"scale(.015625)\" d=\"m359 4666h3666v-482l-2950-3653h3022v-531h-3809v481l2950 3653h-2879v532z\"/></defs><use xlink:href=\"#DejaVuSans-5a\"/></g></g><defs><clipPath id=\"1a2ee44b51d\"><rect x=\"7.2\" y=\"7.2\" width=\"113.03\" height=\"203.96\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 203.885x367.889 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc_a = QuantumCircuit(4)\n",
|
|
"qc_a.x(0)\n",
|
|
"\n",
|
|
"qc_b = QuantumCircuit(2, name=\"qc_b\")\n",
|
|
"qc_b.y(0)\n",
|
|
"qc_b.z(1)\n",
|
|
"\n",
|
|
"# compose qubits (0, 1) of qc_a to qubits (1, 3) of qc_b respectively\n",
|
|
"combined = qc_a.compose(qc_b, qubits=[1, 3])\n",
|
|
"combined.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d4529506-8397-4208-9cfd-31e1a1978741",
|
|
"metadata": {},
|
|
"source": [
|
|
"You might also want to compile circuits into instructions to keep your circuits organized. You can convert a circuit to an instruction by using the [`to_instruction`](/api/qiskit/qiskit.circuit.QuantumCircuit#to_instruction) method, then append this to another circuit as you would any other instruction. The circuit drawn in the following cell is functionally equivalent to the circuit drawn in the previous cell."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "81b682dd-45cb-4492-809e-d9e8ebbf5600",
|
|
"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=\"173.79pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 173.79 218.36\" 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 218.36h173.79v-218.36h-173.79z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h97.343\" clip-path=\"url(#df3c89e2f56)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h97.343\" clip-path=\"url(#df3c89e2f56)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h97.343\" clip-path=\"url(#df3c89e2f56)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 183.35h97.343\" clip-path=\"url(#df3c89e2f56)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#df3c89e2f56)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m97.752 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#df3c89e2f56)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m91.773 198.41h42.089v-122.84h-42.089z\" clip-path=\"url(#df3c89e2f56)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#df3c89e2f56)\"><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(#df3c89e2f56)\"><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(#df3c89e2f56)\"><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(#df3c89e2f56)\"><g transform=\"translate(37.301 187.8) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#df3c89e2f56)\"><g transform=\"translate(108.36 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><defs><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-58\"/></g></g><g clip-path=\"url(#df3c89e2f56)\"><g transform=\"translate(95.017 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#df3c89e2f56)\"><g transform=\"translate(95.017 186.93) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#df3c89e2f56)\"><g transform=\"translate(102.84 140.4) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-71\" transform=\"scale(.015625)\" d=\"m947 1747q0-634 261-995t717-361 718 361q263 361 263 995t-263 995q-262 361-718 361t-717-361-261-995zm1959-1222q-181-312-458-464-276-152-664-152-634 0-1033 506-398 507-398 1332t398 1331q399 506 1033 506 388 0 664-152 277-151 458-463v531h575v-4831h-575v1856z\"/><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-5f\" transform=\"scale(.015625)\" d=\"m3263-1063v-446h-3326v446h3326z\"/><path id=\"DejaVuSans-62\" transform=\"scale(.015625)\" d=\"m3116 1747q0 634-261 995t-717 361q-457 0-718-361t-261-995 261-995 718-361q456 0 717 361t261 995zm-1957 1222q182 312 458 463 277 152 661 152 638 0 1036-506 399-506 399-1331t-399-1332q-398-506-1036-506-384 0-661 152-276 152-458 464v-525h-578v4863h578v-1894z\"/></defs><use xlink:href=\"#DejaVuSans-71\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.457031\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.457031\" xlink:href=\"#DejaVuSans-62\"/></g></g><defs><clipPath id=\"df3c89e2f56\"><rect x=\"7.2\" y=\"7.2\" width=\"159.39\" height=\"203.96\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 287.496x367.889 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"inst = qc_b.to_instruction()\n",
|
|
"qc_a.append(inst, [1, 3])\n",
|
|
"qc_a.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6e81c46b-a2d2-430e-9d3d-73e5030e3548",
|
|
"metadata": {},
|
|
"source": [
|
|
"If your circuit is unitary, you can convert it to a [`Gate`](/api/qiskit/qiskit.circuit.Gate) by using the [`to_gate`](/api/qiskit/qiskit.circuit.QuantumCircuit#to_gate) method. [`Gate`](/api/qiskit/qiskit.circuit.Gate) objects are specific types of instructions that have some extra features, such as the [`control`](/api/qiskit/qiskit.circuit.Gate#control) method, which adds a quantum control."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"id": "ed362e64-d6a4-4dfd-a5cf-5e6bdc7a81b5",
|
|
"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=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 266.5 218.36\" 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 218.36h266.5v-218.36h-266.5z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h190.05\" clip-path=\"url(#e86fad715c7)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h190.05\" clip-path=\"url(#e86fad715c7)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h190.05\" clip-path=\"url(#e86fad715c7)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 183.35h190.05\" clip-path=\"url(#e86fad715c7)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#e86fad715c7)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m205.53 183.35v-139.06\" clip-path=\"url(#e86fad715c7)\" fill=\"none\" stroke=\"#9f1853\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m97.752 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#e86fad715c7)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m91.773 198.41h42.089v-122.84h-42.089z\" clip-path=\"url(#e86fad715c7)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m205.53 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.3237s-1.3237 1.9972-1.3237 3.1958c0 1.1986 0.4762 2.3482 1.3237 3.1958s1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#e86fad715c7)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m184.48 198.41h42.089v-122.84h-42.089z\" clip-path=\"url(#e86fad715c7)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#e86fad715c7)\"><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(#e86fad715c7)\"><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(#e86fad715c7)\"><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(#e86fad715c7)\"><g transform=\"translate(37.301 187.8) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(108.36 47.87) scale(.13 -.13)\" fill=\"#ffffff\"><defs><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-58\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(95.017 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(95.017 186.93) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(102.84 140.4) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-71\" transform=\"scale(.015625)\" d=\"m947 1747q0-634 261-995t717-361 718 361q263 361 263 995t-263 995q-262 361-718 361t-717-361-261-995zm1959-1222q-181-312-458-464-276-152-664-152-634 0-1033 506-398 507-398 1332t398 1331q399 506 1033 506 388 0 664-152 277-151 458-463v531h575v-4831h-575v1856z\"/><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-5f\" transform=\"scale(.015625)\" d=\"m3263-1063v-446h-3326v446h3326z\"/><path id=\"DejaVuSans-62\" transform=\"scale(.015625)\" d=\"m3116 1747q0 634-261 995t-717 361q-457 0-718-361t-261-995 261-995 718-361q456 0 717 361t261 995zm-1957 1222q182 312 458 463 277 152 661 152 638 0 1036-506 399-506 399-1331t-399-1332q-398-506-1036-506-384 0-661 152-276 152-458 464v-525h-578v4863h578v-1894z\"/></defs><use xlink:href=\"#DejaVuSans-71\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.457031\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.457031\" xlink:href=\"#DejaVuSans-62\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(187.73 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(187.73 186.93) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e86fad715c7)\"><g transform=\"translate(195.55 140.4) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-71\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.457031\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.457031\" xlink:href=\"#DejaVuSans-62\"/></g></g><defs><clipPath id=\"e86fad715c7\"><rect x=\"7.2\" y=\"7.2\" width=\"252.1\" height=\"203.96\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 454.719x367.889 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"gate = qc_b.to_gate().control()\n",
|
|
"qc_a.append(gate, [0, 1, 3])\n",
|
|
"qc_a.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae850362-84eb-4998-a2cf-a93a1be4ac2a",
|
|
"metadata": {},
|
|
"source": [
|
|
"To see what's going on, you can use the [`decompose`](/api/qiskit/qiskit.circuit.QuantumCircuit#decompose) method to expand each instruction into its definition.\n",
|
|
"\n",
|
|
"<Admonition type=\"note\">\n",
|
|
" The [`decompose`](/api/qiskit/qiskit.circuit.QuantumCircuit#decompose) method returns a new circuit and does **not** mutate the circuit it acts on.\n",
|
|
"</Admonition>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "3c0633db-929b-4428-a888-7a3d493bd6dd",
|
|
"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=\"312.85pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 312.85 218.36\" 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 218.36h312.85v-218.36h-312.85z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h236.41\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 90.637h236.41\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 136.99h236.41\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 183.35h236.41\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.974 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m159.17 90.637v-46.354\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#9f1853\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.7 183.35v-139.06\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.575 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m74.575 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m74.575 198.41h30.13v-30.13h-30.13z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m159.17 48.803c1.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.1958s-1.9972-1.3237-3.1958-1.3237c-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(#a991c1bf1c4)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m132.86 105.7h52.63v-30.13h-52.63z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m144.11 198.41h30.13v-30.13h-30.13z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m228.7 48.803c1.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.1958s-1.9972-1.3237-3.1958-1.3237c-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(#a991c1bf1c4)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m228.7 193.89c2.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(#a991c1bf1c4)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m259.99 198.41h30.13v-30.13h-30.13z\" clip-path=\"url(#a991c1bf1c4)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m228.7 189.37v-12.052\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m222.68 183.35h12.052\" clip-path=\"url(#a991c1bf1c4)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#a991c1bf1c4)\"><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(#a991c1bf1c4)\"><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(#a991c1bf1c4)\"><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(#a991c1bf1c4)\"><g transform=\"translate(37.301 187.8) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-33\" transform=\"scale(.015625)\" d=\"m2597 2516q453-97 707-404 255-306 255-756 0-690-475-1069-475-378-1350-378-293 0-604 58t-642 174v609q262-153 574-231 313-78 654-78 593 0 904 234t311 681q0 413-289 645-289 233-804 233h-544v519h569q465 0 712 186t247 536q0 359-255 551-254 193-729 193-260 0-557-57-297-56-653-174v562q360 100 674 150t592 50q719 0 1137-327 419-326 419-882 0-388-222-655t-631-370z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(77.16 55.53) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2c\" transform=\"scale(.015625)\" d=\"m750 794h659v-538l-512-1e3h-403l256 1e3v538z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(91.992 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(123.78 .78125)\" xlink:href=\"#DejaVuSans-30\"/><use transform=\"translate(187.4 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(219.19 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(250.98 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(81.71 43.351) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-55\" transform=\"scale(.015625)\" d=\"m556 4666h635v-2835q0-750 271-1080 272-329 882-329 606 0 878 329 272 330 272 1080v2835h634v-2913q0-912-452-1378-451-466-1332-466-885 0-1337 466-451 466-451 1378v2913z\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-55\"/><use transform=\"translate(74.15 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(85.67 94.224) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-59\" transform=\"scale(.015625)\" d=\"m-13 4666h679l1293-1919 1285 1919h678l-1650-2444v-2222h-634v2222l-1651 2444z\"/></defs><use xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(85.188 186.93) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-5a\" transform=\"scale(.015625)\" d=\"m359 4666h3666v-482l-2950-3653h3022v-531h-3809v481l2950 3653h-2879v532z\"/></defs><use xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(134.25 101.88) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-2f\" transform=\"scale(.015625)\" d=\"m1625 4666h531l-1625-5260h-531l1625 5260z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(91.992 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(123.78 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(183.98 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(214.05 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(277.67 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(309.46 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(341.25 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(401.45 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(431.52 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(495.14 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(526.93 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(558.72 .78125)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(154.41 89.705) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-55\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(154.28 186.93) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-48\" transform=\"scale(.015625)\" d=\"m628 4666h631v-1913h2294v1913h631v-4666h-631v2222h-2294v-2222h-631v4666z\"/></defs><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#a991c1bf1c4)\"><g transform=\"translate(270.17 186.93) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><defs><clipPath id=\"a991c1bf1c4\"><rect x=\"7.2\" y=\"7.2\" width=\"298.45\" height=\"203.96\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 538.33x367.889 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc_a.decompose().draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "649fc3fd-caf1-45f1-ad8e-3b5d26ca859b",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Parameterized circuits\n",
|
|
"\n",
|
|
"Many near-term quantum algorithms involve executing many variations of a quantum circuit. Since constructing and optimizing large circuits can be computationally expensive, Qiskit supports **parameterized** circuits. These circuits have undefined parameters, and their values do not need to be defined until just before executing the circuit. This lets you move circuit construction and optimization out of the main program loop. The following cell creates and displays a parameterized circuit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "a580552c-d585-4047-99f0-32aafd06e4f3",
|
|
"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=\"163.57pt\" height=\"79.296pt\" version=\"1.1\" viewBox=\"0 0 163.57 79.296\" 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 79.296h163.57v-79.296h-163.57z\" fill=\"#ffffff\"/><path d=\"m54.39 44.283h97.343\" clip-path=\"url(#f8e1f8c3569)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m49.755 21.106h-1158.8v92.708h1158.8z\" clip-path=\"url(#f8e1f8c3569)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m68.41 59.348h68.377v-30.13h-68.377z\" clip-path=\"url(#f8e1f8c3569)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#f8e1f8c3569)\"><g transform=\"translate(34.719 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\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/></g></g><g clip-path=\"url(#f8e1f8c3569)\"><g transform=\"translate(70.718 55.522) scale(.08 -.08)\" fill=\"#ffffff\"><defs><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-6e\" transform=\"scale(.015625)\" d=\"m3513 2113v-2113h-575v2094q0 497-194 743-194 247-581 247-466 0-735-297-269-296-269-809v-1978h-578v3500h578v-544q207 316 486 472 280 156 646 156 603 0 912-373 310-373 310-1098z\"/><path id=\"DejaVuSans-67\" transform=\"scale(.015625)\" d=\"m2906 1791q0 625-258 968-257 344-723 344-462 0-720-344-258-343-258-968 0-622 258-966t720-344q466 0 723 344 258 344 258 966zm575-1357q0-893-397-1329-396-436-1215-436-303 0-572 45t-522 139v559q253-137 500-202 247-66 503-66 566 0 847 295t281 892v285q-178-310-456-463t-666-153q-643 0-1037 490-394 491-394 1301 0 812 394 1302 394 491 1037 491 388 0 666-153t456-462v531h575v-3066z\"/><path id=\"DejaVuSans-6c\" transform=\"scale(.015625)\" d=\"m603 4863h575v-4863h-575v4863z\"/><path id=\"DejaVuSans-65\" transform=\"scale(.015625)\" d=\"m3597 1894v-281h-2644q38-594 358-905t892-311q331 0 642 81t618 244v-544q-310-131-635-200t-659-69q-838 0-1327 487-489 488-489 1320 0 859 464 1363 464 505 1252 505 706 0 1117-455 411-454 411-1235zm-575 169q-6 471-264 752-258 282-683 282-481 0-770-272t-333-766l2050 4z\"/><path id=\"DejaVuSans-2c\" transform=\"scale(.015625)\" d=\"m750 794h659v-538l-512-1e3h-403l256 1e3v538z\"/><path id=\"DejaVuSans-2212\" transform=\"scale(.015625)\" d=\"m678 2272h4006v-531h-4006v531z\"/><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2f\" transform=\"scale(.015625)\" d=\"m1625 4666h531l-1625-5260h-531l1625 5260z\"/><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 transform=\"translate(0 .015625)\" xlink:href=\"#DejaVuSans-61\"/><use transform=\"translate(61.279 .015625)\" xlink:href=\"#DejaVuSans-6e\"/><use transform=\"translate(124.66 .015625)\" xlink:href=\"#DejaVuSans-67\"/><use transform=\"translate(188.13 .015625)\" xlink:href=\"#DejaVuSans-6c\"/><use transform=\"translate(215.92 .015625)\" xlink:href=\"#DejaVuSans-65\"/><use transform=\"translate(277.44 .015625)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(309.23 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(341.02 .015625)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(424.8 .015625)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(485.01 .015625)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(515.08 .015625)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(578.7 .015625)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(610.49 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(642.27 .015625)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(702.48 .015625)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(732.54 .015625)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#f8e1f8c3569)\"><g transform=\"translate(97.841 43.351) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-55\" transform=\"scale(.015625)\" d=\"m556 4666h635v-2835q0-750 271-1080 272-329 882-329 606 0 878 329 272 330 272 1080v2835h634v-2913q0-912-452-1378-451-466-1332-466-885 0-1337 466-451 466-451 1378v2913z\"/></defs><use xlink:href=\"#DejaVuSans-55\"/></g></g><defs><clipPath id=\"f8e1f8c3569\"><rect x=\"7.2\" y=\"7.2\" width=\"149.17\" height=\"64.896\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 269.064x117.056 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
|
"from qiskit.circuit import Parameter\n",
|
|
"\n",
|
|
"angle = Parameter(\"angle\") # undefined number\n",
|
|
"\n",
|
|
"# Create and optimize circuit once\n",
|
|
"qc = QuantumCircuit(1)\n",
|
|
"qc.rx(angle, 0)\n",
|
|
"qc = generate_preset_pass_manager(\n",
|
|
" optimization_level=3, basis_gates=[\"u\", \"cx\"]\n",
|
|
").run(qc)\n",
|
|
"\n",
|
|
"qc.draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a174600b-8f4a-47ca-aa4c-cd16dcbecb7c",
|
|
"metadata": {},
|
|
"source": [
|
|
"The following cell creates many variations of this circuit and displays one of the variations."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "85af6231-921a-4130-99d3-f6998f761df8",
|
|
"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=\"163.57pt\" height=\"79.296pt\" version=\"1.1\" viewBox=\"0 0 163.57 79.296\" 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 79.296h163.57v-79.296h-163.57z\" fill=\"#ffffff\"/><path d=\"m54.39 44.283h97.343\" clip-path=\"url(#4d9df483a90)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m49.755 21.106h-1158.8v92.708h1158.8z\" clip-path=\"url(#4d9df483a90)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m77.732 59.348h49.733v-30.13h-49.733z\" clip-path=\"url(#4d9df483a90)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#4d9df483a90)\"><g transform=\"translate(34.719 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\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/></g></g><g clip-path=\"url(#4d9df483a90)\"><g transform=\"translate(79.278 55.53) scale(.08 -.08)\" fill=\"#ffffff\"><defs><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\"/><path id=\"DejaVuSans-2c\" transform=\"scale(.015625)\" d=\"m750 794h659v-538l-512-1e3h-403l256 1e3v538z\"/><path id=\"DejaVuSans-2212\" transform=\"scale(.015625)\" d=\"m678 2272h4006v-531h-4006v531z\"/><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2f\" transform=\"scale(.015625)\" d=\"m1625 4666h531l-1625-5260h-531l1625 5260z\"/><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 transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-30\"/><use transform=\"translate(63.623 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(95.41 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(127.2 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(210.99 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(271.19 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(301.26 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(364.88 .78125)\" xlink:href=\"#DejaVuSans-2c\"/><use transform=\"translate(396.67 .78125)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(428.46 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(488.66 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(518.73 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#4d9df483a90)\"><g transform=\"translate(97.841 43.351) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-55\" transform=\"scale(.015625)\" d=\"m556 4666h635v-2835q0-750 271-1080 272-329 882-329 606 0 878 329 272 330 272 1080v2835h634v-2913q0-912-452-1378-451-466-1332-466-885 0-1337 466-451 466-451 1378v2913z\"/></defs><use xlink:href=\"#DejaVuSans-55\"/></g></g><defs><clipPath id=\"4d9df483a90\"><rect x=\"7.2\" y=\"7.2\" width=\"149.17\" height=\"64.896\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 269.064x117.056 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"circuits = []\n",
|
|
"for value in range(100):\n",
|
|
" circuits.append(qc.assign_parameters({angle: value}))\n",
|
|
"\n",
|
|
"circuits[0].draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ede2ff7c-5e68-4458-87b3-887b5b027ee1",
|
|
"metadata": {},
|
|
"source": [
|
|
"You can find a list of a circuit's undefined parameters in its `parameters` attribute."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "0990b840-f8c4-4416-b2b5-9861173a9471",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"ParameterView([Parameter(angle)])"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"qc.parameters"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae19e48d",
|
|
"metadata": {},
|
|
"source": [
|
|
"<CodeAssistantAdmonition\n",
|
|
" tagLine=\"Forgotten the method name? Try asking Qiskit Code Assistant.\"\n",
|
|
" prompts={[\n",
|
|
" \"# Assign all parameters in qc to 0\"\n",
|
|
" ]}\n",
|
|
"/>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5af44bc3",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Next steps\n",
|
|
"\n",
|
|
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
|
|
" - To learn about near-term quantum algorithms, take our [Variational algorithm design](https://learning.quantum.ibm.com/course/variational-algorithm-design) course.\n",
|
|
" - See an example of circuits being used in the [Grover's Algorithm](https://learning.quantum.ibm.com/tutorial/grovers-algorithm) tutorial.\n",
|
|
" - Work with simple circuits in the [Explore gates and circuits with the Quantum Composer](https://learning.quantum.ibm.com/tutorial/explore-gates-and-circuits-with-the-quantum-composer) tutorial.\n",
|
|
"</Admonition>"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"description": "How to construct and visualize quantum circuits in Qiskit.",
|
|
"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": "Construct circuits"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|