qiskit-documentation/docs/guides/classical-feedforward-and-c...

443 lines
102 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "4d6203af-01d0-4362-a44c-66c2128e63d1",
"metadata": {},
"source": [
"# Classical feedforward and control flow"
]
},
{
"cell_type": "markdown",
"id": "e64d4278-879b-4319-9fbe-85a64327bcac",
"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": "0767b203-ad69-428f-b8c1-50ceab758b35",
"metadata": {},
"source": [
"This guide demonstrates the functionality available in the Qiskit SDK for performing classical feedforward and control flow. These features are sometimes referred to collectively as \"dynamic circuits.\" Classical feedforward refers to the ability to measure qubits in the middle of a circuit and perform additional quantum operations that depend on the measurement outcome. Qiskit supports four control flow constructs for classical feedforward, each implemented as a method on [`QuantumCircuit`](../api/qiskit/qiskit.circuit.QuantumCircuit). The constructs and their corresponding methods are:\n",
"\n",
"- If statement - [`QuantumCircuit.if_test`](../api/qiskit/qiskit.circuit.QuantumCircuit#if_test)\n",
"- Switch statement - [`QuantumCircuit.switch`](../api/qiskit/qiskit.circuit.QuantumCircuit#switch)\n",
"- For loop - [`QuantumCircuit.for_loop`](../api/qiskit/qiskit.circuit.QuantumCircuit#for_loop)\n",
"- While loop - [`QuantumCircuit.while_loop`](../api/qiskit/qiskit.circuit.QuantumCircuit#while_loop)\n",
"\n",
"Each of these methods returns a [context manager](https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers) and is typically used in a `with` statement. In the rest of this guide, we will explain each of these constructs and how to use them.\n",
"\n",
"<Admonition type=\"caution\">\n",
" There are some limitations of classical feedforward and control flow operations on quantum hardware that might impact your program. For more information, see [Hardware considerations and limitations for classical feedforward and control flow](./dynamic-circuits-considerations).\n",
"</Admonition>\n",
"\n",
"## If statement\n",
"\n",
"The if statement is used to conditionally perform operations based on the value of a classical bit or register.\n",
"\n",
"In the example below, we apply a Hadamard gate to a qubit and measure it. If the result is 1, then we apply an X gate on the qubit, which has the effect of flipping it back to the 0 state. We then measure the qubit again. The resulting measurement outcome should be 0 with 100% probability."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "008f85d9-5b48-4e84-b250-846b07901607",
"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.63pt\" height=\"125.65pt\" version=\"1.1\" viewBox=\"0 0 312.63 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=\"m0 125.65h312.63v-125.65h-312.63z\" fill=\"#ffffff\"/><path d=\"m64.385 44.283h236.41\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m73.656 95.273 4.6354-9.2708\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m64.385 89.131h236.41\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.385 92.144h236.41\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m129.74 80.092h12.052l-6.026 8.6914z\" clip-path=\"url(#f778ef05d47)\" fill=\"#778899\"/><path d=\"m268.81 80.092h12.052l-6.026 8.6914z\" clip-path=\"url(#f778ef05d47)\" fill=\"#778899\"/><path d=\"m170.54 63.984h76.484q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-76.484q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(166.83 38.6) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-49\" transform=\"scale(.015625)\" d=\"m628 4666h631v-4666h-631v4666z\"/><path id=\"DejaVuSans-66\" transform=\"scale(.015625)\" d=\"m2375 4863v-479h-550q-309 0-430-125-120-125-120-450v-309h947v-447h-947v-3053h-578v3053h-550v447h550v244q0 584 272 851 272 268 862 268h544z\"/></defs><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><path d=\"m-911.83 203.05q4.6354 0 4.6354-4.6354 0-34.765-4.6354-34.765-81.119 0-81.119 4.6354 0 34.765 4.6354 34.765\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(-992.02 177.66) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><path d=\"m59.75 21.106h-1158.8v139.06h1158.8z\" clip-path=\"url(#f778ef05d47)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m137.28 44.283v35.808\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m134.26 44.283v35.808\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m172.04 65.838v24.799\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m169.03 65.838v24.799\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m276.34 44.283v35.808\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m273.33 44.283v35.808\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.351 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f778ef05d47)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m120.71 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f778ef05d47)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m146.32 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887c-1.9769 1.9769-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m135.77 48.803 10.546-10.546\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m170.54 95.157c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958s-0.4762-2.3482-1.3237-3.1958c-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.1958s0.4762 2.3482 1.3237 3.1958c0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#f778ef05d47)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m259.77 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f778ef05d47)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m285.38 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m274.83 48.803 10.546-10.546\" clip-path=\"url(#f778ef05d47)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m213.41 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f778ef05d47)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(34.315 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 transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(69.021 83.839) scale(.104 -.104)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(35.84 95.121) scale(.1625 -.1625)\"><defs><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\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(84.528 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><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(147.36 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(150.44 105.61) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-5f\" transform=\"scale(.015625)\" d=\"m3263-1063v-446h-3326v446h3326z\"/><path id=\"DejaVuSans-3d\" transform=\"scale(.015625)\" d=\"m678 2906h4006v-525h-4006v525zm0-1275h4006v-531h-4006v531z\"/><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-30\"/><use x=\"118.603516\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.603516\" xlink:href=\"#DejaVuSans-30\"/><use x=\"232.226562\" xlink:href=\"#DejaVuSans-3d\"/><use x=\"316.015625\" xlink:href=\"#DejaVuSans-30\"/><use x=\"379.638672\" xlink:href=\"#DejaVuSans-78\"/><use x=\"438.818359\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(286.42 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f778ef05d47)\"><g transform=\"translate(224.03 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=\"f778ef05d47\"><rect x=\"7.2\" y=\"7.2\" width=\"298.23\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 537.926x200.667 with 1 Axes>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from qiskit.circuit import QuantumCircuit, QuantumRegister, ClassicalRegister\n",
"\n",
"qubits = QuantumRegister(1)\n",
"clbits = ClassicalRegister(1)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0,) = qubits\n",
"(c0,) = clbits\n",
"\n",
"circuit.h(q0)\n",
"circuit.measure(q0, c0)\n",
"with circuit.if_test((c0, 1)):\n",
" circuit.x(q0)\n",
"circuit.measure(q0, c0)\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'0': 1024}"
]
},
{
"cell_type": "markdown",
"id": "fdc99e4d-fcd0-4397-b6da-2e36a51f90a8",
"metadata": {},
"source": [
"The `with` statement can be given an assignment target which is itself a context manager that can be stored and subsequently used to create an else block, which is executed whenever the contents of the if block are *not* executed.\n",
"\n",
"In the example below, we initialize registers with two qubits and two classical bits. We apply a Hadamard gate to the first qubit and measure it. If the result is 1, then we apply a Hadamard gate on the second qubit; otherwise, we apply an X gate on the second qubit. Finally, we measure the second qubit as well."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5b22ae93-27d1-4a0c-89e2-9f88383039d6",
"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=\"415.55pt\" height=\"172pt\" version=\"1.1\" viewBox=\"0 0 415.55 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 172h415.55v-172h-415.55z\" fill=\"#ffffff\"/><path d=\"m74.604 44.283h329.11\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.604 90.637h329.11\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m83.875 141.63 4.6354-9.2708\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m74.604 135.48h329.11\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.604 138.5h329.11\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m139.96 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#778899\"/><path d=\"m371.73 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#778899\"/><path d=\"m180.75 110.34h168.96q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-168.96q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(177.05 84.954) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-49\" transform=\"scale(.015625)\" d=\"m628 4666h631v-4666h-631v4666z\"/><path id=\"DejaVuSans-66\" transform=\"scale(.015625)\" d=\"m2375 4863v-479h-550q-309 0-430-125-120-125-120-450v-309h947v-447h-947v-3053h-578v3053h-550v447h550v244q0 584 272 851 272 268 862 268h544z\"/></defs><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><path d=\"m271.15 109.41v-38.706\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(275.78 84.954) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-45\" transform=\"scale(.015625)\" d=\"m628 4666h2950v-532h-2319v-1381h2222v-531h-2222v-1691h2375v-531h-3006v4666z\"/><path id=\"DejaVuSans-6c\" transform=\"scale(.015625)\" d=\"m603 4863h575v-4863h-575v4863z\"/><path id=\"DejaVuSans-73\" transform=\"scale(.015625)\" d=\"m2834 3397v-544q-243 125-506 187-262 63-544 63-428 0-642-131t-214-394q0-200 153-314t616-217l197-44q612-131 870-370t258-667q0-488-386-773-386-284-1061-284-281 0-586 55t-642 164v594q319-166 628-249 309-82 613-82 406 0 624 139 219 139 219 392 0 234-158 359-157 125-692 241l-200 47q-534 112-772 345-237 233-237 639 0 494 350 762 350 269 994 269 318 0 599-47 282-46 519-140z\"/><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\"/></defs><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"90.966797\" xlink:href=\"#DejaVuSans-73\"/><use x=\"143.066406\" xlink:href=\"#DejaVuSans-65\"/></g></g><path d=\"m-809.13 295.75q4.6354 0 4.6354-4.6354 0-34.765-4.6354-34.765-173.6 0-173.6 4.6354 0 34.765 4.6354 34.765\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(-981.8 270.37) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(-883.07 270.37) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"90.966797\" xlink:href=\"#DejaVuSans-73\"/><use x=\"143.066406\" xlink:href=\"#DejaVuSans-65\"/></g></g><path d=\"m69.969 21.106h-1158.8v185.42h1158.8z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m147.5 44.283v82.162\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m144.48 44.283v82.162\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m182.26 112.19v24.799\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m179.25 112.19v24.799\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m379.27 90.637v35.808\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m376.25 90.637v35.808\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.57 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m130.92 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m156.53 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m145.99 48.803 10.546-10.546\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m180.75 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(#e2ffbb00bf0)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m362.69 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m388.3 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m377.76 95.157 10.546-10.546\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m223.63 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m316.34 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#e2ffbb00bf0)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(36.733 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-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/><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 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-31\"/><use transform=\"translate(128.06 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(36.733 95.088) scale(.1625 -.1625)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-31\"/><use transform=\"translate(128.06 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(79.24 130.19) scale(.104 -.104)\"><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-32\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(46.059 141.48) scale(.1625 -.1625)\"><defs><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\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(94.747 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><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(157.58 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(160.66 151.97) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-5f\" transform=\"scale(.015625)\" d=\"m3263-1063v-446h-3326v446h3326z\"/><path id=\"DejaVuSans-3d\" transform=\"scale(.015625)\" d=\"m678 2906h4006v-525h-4006v525zm0-1275h4006v-531h-4006v531z\"/><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-31\"/><use x=\"118.603516\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.603516\" xlink:href=\"#DejaVuSans-30\"/><use x=\"232.226562\" xlink:href=\"#DejaVuSans-3d\"/><use x=\"316.015625\" xlink:href=\"#DejaVuSans-30\"/><use x=\"379.638672\" xlink:href=\"#DejaVuSans-78\"/><use x=\"438.818359\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(389.35 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(233.81 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#e2ffbb00bf0)\"><g transform=\"translate(326.95 94.224) 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=\"e2ffbb00bf0\"><rect x=\"7.2\" y=\"7.2\" width=\"401.15\" height=\"157.6\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 723.581x284.278 with 1 Axes>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(2)\n",
"clbits = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0, q1) = qubits\n",
"(c0, c1) = clbits\n",
"\n",
"circuit.h(q0)\n",
"circuit.measure(q0, c0)\n",
"with circuit.if_test((c0, 1)) as else_:\n",
" circuit.h(q1)\n",
"with else_:\n",
" circuit.x(q1)\n",
"circuit.measure(q1, c1)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'01': 260, '11': 272, '10': 492}"
]
},
{
"cell_type": "markdown",
"id": "d70a3dec-33fe-4a2f-942e-592770c4868e",
"metadata": {},
"source": [
"In addition to conditioning on a single classical bit, it's also possible to condition on the value of a classical register composed of multiple bits.\n",
"\n",
"In the example below, we apply Hadamard gates to two qubits and measure them. If the result is `01`, that is, the first qubit is 1 and the second qubit is 0, then we apply an X gate to a third qubit. Finally, we measure the third qubit. Note that for clarity, we chose to specify the state of the third classical bit, which is 0, in the if condition. In the circuit drawing, the condition is indicated by the circles on the classical bits being conditioned on. A black circle indicates conditioning on 1, while a white circle indicates conditioning on 0."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "15ac9465-c61f-4de1-81e8-e4edfafb07b4",
"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=\"369.42pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 369.42 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.36h369.42v-218.36h-369.42z\" fill=\"#ffffff\"/><path d=\"m74.828 44.283h282.76\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 90.637h282.76\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 136.99h282.76\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.099 187.98 4.6354-9.2708\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m74.828 181.84h282.76\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 184.85h282.76\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m140.19 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#778899\"/><path d=\"m186.54 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#778899\"/><path d=\"m325.6 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#778899\"/><path d=\"m227.33 156.69h76.484q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-76.484q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(223.62 131.31) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-49\" transform=\"scale(.015625)\" d=\"m628 4666h631v-4666h-631v4666z\"/><path id=\"DejaVuSans-66\" transform=\"scale(.015625)\" d=\"m2375 4863v-479h-550q-309 0-430-125-120-125-120-450v-309h947v-447h-947v-3053h-578v3053h-550v447h550v244q0 584 272 851 272 268 862 268h544z\"/></defs><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><path d=\"m-855.03 388.46q4.6354 0 4.6354-4.6354 0-34.766-4.6354-34.766-81.119 0-81.119 4.6354 0 34.766 4.6354 34.766\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(-935.23 363.08) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"63.574219\" xlink:href=\"#DejaVuSans-49\"/><use x=\"93.066406\" xlink:href=\"#DejaVuSans-66\"/></g></g><path d=\"m70.193 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m147.72 44.283v128.52\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m144.71 44.283v128.52\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m194.07 90.637v82.162\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m191.06 90.637v82.162\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.84 158.55v24.799\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m225.83 158.55v24.799\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m333.14 136.99v35.808\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m330.12 136.99v35.808\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.794 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m84.794 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m131.15 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m156.76 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m146.21 48.803 10.546-10.546\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m177.5 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m203.11 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m192.57 95.157 10.546-10.546\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m227.33 187.86c1.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.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(#6afa6f0be9a)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m316.56 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m342.17 141.51c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m331.63 141.51 10.546-10.546\" clip-path=\"url(#6afa6f0be9a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m270.21 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#6afa6f0be9a)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(36.957 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-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\"/><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-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(128.06 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(36.957 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(128.06 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(36.957 141.44) scale(.1625 -.1625)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(128.06 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(79.464 176.55) scale(.104 -.104)\"><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-33\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(46.283 187.83) scale(.1625 -.1625)\"><defs><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\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(94.971 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><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(94.971 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(157.8 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(204.16 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(219.88 198.46) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/></defs><use xlink:href=\"#DejaVuSans-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-78\"/><use x=\"122.802734\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(343.22 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#6afa6f0be9a)\"><g transform=\"translate(280.82 140.58) 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=\"6afa6f0be9a\"><rect x=\"7.2\" y=\"7.2\" width=\"355.02\" height=\"203.96\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 640.373x367.889 with 1 Axes>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(3)\n",
"clbits = ClassicalRegister(3)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0, q1, q2) = qubits\n",
"(c0, c1, c2) = clbits\n",
"\n",
"circuit.h([q0, q1])\n",
"circuit.measure(q0, c0)\n",
"circuit.measure(q1, c1)\n",
"with circuit.if_test((clbits, 0b001)):\n",
" circuit.x(q2)\n",
"circuit.measure(q2, c2)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'101': 269, '011': 260, '000': 252, '010': 243}"
]
},
{
"cell_type": "markdown",
"id": "ce92fa5f-6b30-4b4c-aa37-5d894e4a40db",
"metadata": {},
"source": [
"## Switch statement\n",
"\n",
"The switch statement is used to select actions based on the value of a classical bit or register. It is similar to an if statement, but allows one to specify more cases for the branching logic. In the example below, we apply a Hadamard gate to a qubit and measure it. If the result is 0, we apply an X gate on the qubit, and if the result is 1, we apply a Z gate. The resulting measurement outcome should be 1 with 100% probability."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6725ba53-c33e-479b-9413-ef3d55a8448c",
"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=\"451.91pt\" height=\"125.65pt\" version=\"1.1\" viewBox=\"0 0 451.91 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=\"m0 125.65h451.91v-125.65h-451.91z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h375.47\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m73.88 95.273 4.6354-9.2708\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m64.609 89.131h375.47\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 92.144h375.47\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m129.97 80.092h12.052l-6.026 8.6914z\" clip-path=\"url(#14ef81e0945)\" fill=\"#778899\"/><path d=\"m408.09 80.092h12.052l-6.026 8.6914z\" clip-path=\"url(#14ef81e0945)\" fill=\"#778899\"/><path d=\"m170.76 63.984h215.08q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-215.08q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(168.91 38.6) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-53\" transform=\"scale(.015625)\" d=\"m3425 4513v-616q-359 172-678 256-319 85-616 85-515 0-795-200t-280-569q0-310 186-468 186-157 705-254l381-78q706-135 1042-474t336-907q0-679-455-1029-454-350-1332-350-331 0-705 75-373 75-773 222v650q384-215 753-325 369-109 725-109 540 0 834 212 294 213 294 607 0 343-211 537t-692 291l-385 75q-706 140-1022 440-315 300-315 835 0 619 436 975t1201 356q329 0 669-60 341-59 697-177z\"/><path id=\"DejaVuSans-77\" transform=\"scale(.015625)\" d=\"m269 3500h575l719-2731 715 2731h678l719-2731 716 2731h575l-916-3500h-678l-753 2869-756-2869h-679l-915 3500z\"/><path id=\"DejaVuSans-69\" transform=\"scale(.015625)\" d=\"m603 3500h575v-3500h-575v3500zm0 1363h575v-729h-575v729z\"/><path id=\"DejaVuSans-74\" transform=\"scale(.015625)\" d=\"m1172 4494v-994h1184v-447h-1184v-1900q0-428 117-550t477-122h590v-481h-590q-666 0-919 248-253 249-253 905v1900h-422v447h422v994h578z\"/><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-68\" transform=\"scale(.015625)\" d=\"m3513 2113v-2113h-575v2094q0 497-194 743-194 247-581 247-466 0-735-297-269-296-269-809v-1978h-578v4863h578v-1907q207 316 486 472 280 156 646 156 603 0 912-373 310-373 310-1098z\"/></defs><use xlink:href=\"#DejaVuSans-53\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-77\"/><use x=\"145.263672\" xlink:href=\"#DejaVuSans-69\"/><use x=\"173.046875\" xlink:href=\"#DejaVuSans-74\"/><use x=\"212.255859\" xlink:href=\"#DejaVuSans-63\"/><use x=\"267.236328\" xlink:href=\"#DejaVuSans-68\"/></g></g><path d=\"m214.8 63.057v-38.706\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(219.43 38.6) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-43\" transform=\"scale(.015625)\" d=\"m4122 4306v-665q-319 297-680 443-361 147-767 147-800 0-1225-489t-425-1414q0-922 425-1411t1225-489q406 0 767 147t680 444v-660q-331-225-702-338-370-112-782-112-1060 0-1670 648-609 649-609 1771 0 1125 609 1773 610 649 1670 649 418 0 788-111 371-111 696-333z\"/><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-73\" transform=\"scale(.015625)\" d=\"m2834 3397v-544q-243 125-506 187-262 63-544 63-428 0-642-131t-214-394q0-200 153-314t616-217l197-44q612-131 870-370t258-667q0-488-386-773-386-284-1061-284-281 0-586 55t-642 164v594q319-166 628-249 309-82 613-82 406 0 624 139 219 139 219 392 0 234-158 359-157 125-692 241l-200 47q-534 112-772 345-237 233-237 639 0 494 350 762 350 269 994 269 318 0 599-47 282-46 519-140z\"/><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\"/></defs><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(219.43 55.761) scale(.08 -.08)\"><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\"/></defs><use xlink:href=\"#DejaVuSans-30\"/></g></g><path d=\"m309.59 63.057v-38.706\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(314.23 38.6) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(314.23 55.761) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-31\"/></g></g><path d=\"m-773.01 203.05q4.6354 0 4.6354-4.6354 0-34.765-4.6354-34.765-219.72 0-219.72 4.6354 0 34.765 4.6354 34.765\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(-989.94 177.66) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-53\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-77\"/><use x=\"145.263672\" xlink:href=\"#DejaVuSans-69\"/><use x=\"173.046875\" xlink:href=\"#DejaVuSans-74\"/><use x=\"212.255859\" xlink:href=\"#DejaVuSans-63\"/><use x=\"267.236328\" xlink:href=\"#DejaVuSans-68\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(-939.42 177.66) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(-939.42 194.82) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(-844.62 177.66) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(-844.62 194.82) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><path d=\"m59.974 21.106h-1158.8v139.06h1158.8z\" clip-path=\"url(#14ef81e0945)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m137.5 44.283v35.808\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m134.49 44.283v35.808\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m172.27 65.838v24.799\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m169.25 65.838v24.799\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m415.62 44.283v35.808\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m412.61 44.283v35.808\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.575 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#14ef81e0945)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m120.93 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#14ef81e0945)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m146.54 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m135.99 48.803 10.546-10.546\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m170.76 95.157c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958s-0.4762-2.3482-1.3237-3.1958c-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.1958s0.4762 2.3482 1.3237 3.1958c0.84753 0.84753 1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#14ef81e0945)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m399.05 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#14ef81e0945)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m424.66 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m414.12 48.803 10.546-10.546\" clip-path=\"url(#14ef81e0945)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m259.99 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#14ef81e0945)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m352.7 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#14ef81e0945)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(34.538 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-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 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(69.245 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(36.064 95.121) scale(.1625 -.1625)\"><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(84.752 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><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(147.58 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(150.66 105.61) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-5f\" transform=\"scale(.015625)\" d=\"m3263-1063v-446h-3326v446h3326z\"/><path id=\"DejaVuSans-3d\" transform=\"scale(.015625)\" d=\"m678 2906h4006v-525h-4006v525zm0-1275h4006v-531h-4006v531z\"/><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-33\"/><use x=\"118.603516\" xlink:href=\"#DejaVuSans-5f\"/><use x=\"168.603516\" xlink:href=\"#DejaVuSans-30\"/><use x=\"232.226562\" xlink:href=\"#DejaVuSans-3d\"/><use x=\"316.015625\" xlink:href=\"#DejaVuSans-30\"/><use x=\"379.638672\" xlink:href=\"#DejaVuSans-78\"/><use x=\"438.818359\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(425.71 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#14ef81e0945)\"><g transform=\"translate(270.6 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(#14ef81e0945)\"><g transform=\"translate(363.31 47.87) 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=\"14ef81e0945\"><rect x=\"7.2\" y=\"7.2\" width=\"437.51\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 789.163x200.667 with 1 Axes>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(1)\n",
"clbits = ClassicalRegister(1)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0,) = qubits\n",
"(c0,) = clbits\n",
"\n",
"circuit.h(q0)\n",
"circuit.measure(q0, c0)\n",
"with circuit.switch(c0) as case:\n",
" with case(0):\n",
" circuit.x(q0)\n",
" with case(1):\n",
" circuit.z(q0)\n",
"circuit.measure(q0, c0)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'1': 1024}"
]
},
{
"cell_type": "markdown",
"id": "a95e44eb-e54a-4ba7-bbc5-da0cc1f9df9a",
"metadata": {},
"source": [
"Because the example above used a single classical bit, there were only two possible cases, so we could have achieved the same result using an if-else statement. The switch case is mainly useful when branching on the value of a classical register composed of multiple bits, as demonstrated in the following example. Here, we also show how to construct a default case, which is executed if none of the preceding cases are. Note that in a switch statement, only one of the blocks are ever executed. There is no fallthrough.\n",
"\n",
"In the example below, we apply Hadamard gates to two qubits and measure them. If the result is either 00 or 11, we apply a Z gate to the third qubit. If the result is 01, we apply a Y gate. If none of the preceding cases matched, we apply an X gate. Finally, measure the third qubit."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8bb350f1-fd66-4ef2-89b4-6168e6ad04b0",
"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=\"601.19pt\" height=\"218.36pt\" version=\"1.1\" viewBox=\"0 0 601.19 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.36h601.19v-218.36h-601.19z\" fill=\"#ffffff\"/><path d=\"m74.828 44.283h514.53\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 90.637h514.53\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 136.99h514.53\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.099 187.98 4.6354-9.2708\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m74.828 181.84h514.53\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.828 184.85h514.53\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m140.19 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#778899\"/><path d=\"m186.54 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#778899\"/><path d=\"m557.37 172.8h12.052l-6.026 8.6914z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#778899\"/><path d=\"m227.33 156.69h307.56q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-307.56q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(225.48 131.31) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-53\" transform=\"scale(.015625)\" d=\"m3425 4513v-616q-359 172-678 256-319 85-616 85-515 0-795-200t-280-569q0-310 186-468 186-157 705-254l381-78q706-135 1042-474t336-907q0-679-455-1029-454-350-1332-350-331 0-705 75-373 75-773 222v650q384-215 753-325 369-109 725-109 540 0 834 212 294 213 294 607 0 343-211 537t-692 291l-385 75q-706 140-1022 440-315 300-315 835 0 619 436 975t1201 356q329 0 669-60 341-59 697-177z\"/><path id=\"DejaVuSans-77\" transform=\"scale(.015625)\" d=\"m269 3500h575l719-2731 715 2731h678l719-2731 716 2731h575l-916-3500h-678l-753 2869-756-2869h-679l-915 3500z\"/><path id=\"DejaVuSans-69\" transform=\"scale(.015625)\" d=\"m603 3500h575v-3500h-575v3500zm0 1363h575v-729h-575v729z\"/><path id=\"DejaVuSans-74\" transform=\"scale(.015625)\" d=\"m1172 4494v-994h1184v-447h-1184v-1900q0-428 117-550t477-122h590v-481h-590q-666 0-919 248-253 249-253 905v1900h-422v447h422v994h578z\"/><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-68\" transform=\"scale(.015625)\" d=\"m3513 2113v-2113h-575v2094q0 497-194 743-194 247-581 247-466 0-735-297-269-296-269-809v-1978h-578v4863h578v-1907q207 316 486 472 280 156 646 156 603 0 912-373 310-373 310-1098z\"/></defs><use xlink:href=\"#DejaVuSans-53\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-77\"/><use x=\"145.263672\" xlink:href=\"#DejaVuSans-69\"/><use x=\"173.046875\" xlink:href=\"#DejaVuSans-74\"/><use x=\"212.255859\" xlink:href=\"#DejaVuSans-63\"/><use x=\"267.236328\" xlink:href=\"#DejaVuSans-68\"/></g></g><path d=\"m271.37 155.76v-38.706\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(276 131.31) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-43\" transform=\"scale(.015625)\" d=\"m4122 4306v-665q-319 297-680 443-361 147-767 147-800 0-1225-489t-425-1414q0-922 425-1411t1225-489q406 0 767 147t680 444v-660q-331-225-702-338-370-112-782-112-1060 0-1670 648-609 649-609 1771 0 1125 609 1773 610 649 1670 649 418 0 788-111 371-111 696-333z\"/><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-73\" transform=\"scale(.015625)\" d=\"m2834 3397v-544q-243 125-506 187-262 63-544 63-428 0-642-131t-214-394q0-200 153-314t616-217l197-44q612-131 870-370t258-667q0-488-386-773-386-284-1061-284-281 0-586 55t-642 164v594q319-166 628-249 309-82 613-82 406 0 624 139 219 139 219 392 0 234-158 359-157 125-692 241l-200 47q-534 112-772 345-237 233-237 639 0 494 350 762 350 269 994 269 318 0 599-47 282-46 519-140z\"/><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\"/></defs><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(276 148.47) scale(.08 -.08)\"><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-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-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-2c\"/><use x=\"95.410156\" xlink:href=\"#DejaVuSans-20\"/><use x=\"127.197266\" xlink:href=\"#DejaVuSans-33\"/></g></g><path d=\"m366.16 155.76v-38.706\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(370.8 131.31) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(370.8 148.47) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-31\"/></g></g><path d=\"m460.96 155.76v-38.706\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#fa4d56\" stroke-linecap=\"square\" stroke-width=\"3\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(465.59 131.31) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(465.59 148.47) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-64\" transform=\"scale(.015625)\" d=\"m2906 2969v1894h575v-4863h-575v525q-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-463zm-1959-1222q0-634 261-995t717-361 718 361q263 361 263 995t-263 995q-262 361-718 361t-717-361-261-995z\"/><path id=\"DejaVuSans-66\" transform=\"scale(.015625)\" d=\"m2375 4863v-479h-550q-309 0-430-125-120-125-120-450v-309h947v-447h-947v-3053h-578v3053h-550v447h550v244q0 584 272 851 272 268 862 268h544z\"/><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\"/></defs><use xlink:href=\"#DejaVuSans-64\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-65\"/><use x=\"125\" xlink:href=\"#DejaVuSans-66\"/><use x=\"160.205078\" xlink:href=\"#DejaVuSans-61\"/><use x=\"221.484375\" xlink:href=\"#DejaVuSans-75\"/><use x=\"284.863281\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"312.646484\" xlink:href=\"#DejaVuSans-74\"/></g></g><path d=\"m-623.96 388.46q4.6354 0 4.6354-4.6354 0-34.766-4.6354-34.766-312.19 0-312.19 4.6354 0 34.766 4.6354 34.766\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-933.37 363.08) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-53\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-77\"/><use x=\"145.263672\" xlink:href=\"#DejaVuSans-69\"/><use x=\"173.046875\" xlink:href=\"#DejaVuSans-74\"/><use x=\"212.255859\" xlink:href=\"#DejaVuSans-63\"/><use x=\"267.236328\" xlink:href=\"#DejaVuSans-68\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-882.85 363.08) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-882.85 380.24) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-2c\"/><use x=\"95.410156\" xlink:href=\"#DejaVuSans-20\"/><use x=\"127.197266\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-788.05 363.08) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-788.05 380.24) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-693.26 363.08) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-43\"/><use x=\"69.824219\" xlink:href=\"#DejaVuSans-61\"/><use x=\"131.103516\" xlink:href=\"#DejaVuSans-73\"/><use x=\"183.203125\" xlink:href=\"#DejaVuSans-65\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(-693.26 380.24) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-64\"/><use x=\"63.476562\" xlink:href=\"#DejaVuSans-65\"/><use x=\"125\" xlink:href=\"#DejaVuSans-66\"/><use x=\"160.205078\" xlink:href=\"#DejaVuSans-61\"/><use x=\"221.484375\" xlink:href=\"#DejaVuSans-75\"/><use x=\"284.863281\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"312.646484\" xlink:href=\"#DejaVuSans-74\"/></g></g><path d=\"m70.193 21.106h-1158.8v231.77h1158.8z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m147.72 44.283v128.52\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m144.71 44.283v128.52\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m194.07 90.637v82.162\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m191.06 90.637v82.162\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.84 158.55v24.799\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m225.83 158.55v24.799\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m564.91 136.99v35.808\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m561.89 136.99v35.808\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.794 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m84.794 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m131.15 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m156.76 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m146.21 48.803 10.546-10.546\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m177.5 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m203.11 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m192.57 95.157 10.546-10.546\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m227.33 187.86c1.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.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(#17eb0eabf49)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m548.33 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m573.94 141.51c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m563.4 141.51 10.546-10.546\" clip-path=\"url(#17eb0eabf49)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m316.56 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m409.27 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m501.98 152.06h30.13v-30.13h-30.13z\" clip-path=\"url(#17eb0eabf49)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(36.957 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-34\" transform=\"scale(.015625)\" d=\"m2419 4116-1594-2491h1594v2491zm-166 550h794v-3041h666v-525h-666v-1100h-628v1100h-2106v609l1940 2957z\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-34\"/><use transform=\"translate(128.06 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(36.957 95.088) scale(.1625 -.1625)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-34\"/><use transform=\"translate(128.06 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(36.957 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 transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-34\"/><use transform=\"translate(128.06 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(79.464 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(46.283 187.83) scale(.1625 -.1625)\"><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(94.971 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><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(94.971 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(157.8 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(204.16 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(219.88 198.46) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/><path id=\"DejaVuSans-37\" transform=\"scale(.015625)\" d=\"m525 4666h3e3v-269l-1694-4397h-659l1594 4134h-2241v532z\"/></defs><use xlink:href=\"#DejaVuSans-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-78\"/><use x=\"122.802734\" xlink:href=\"#DejaVuSans-37\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(574.99 176.55) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#17eb0eabf49)\"><g transform=\"translate(327.18 140.58) 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(#17eb0eabf49)\"><g transform=\"translate(420.37 140.58) 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(#17eb0eabf49)\"><g transform=\"translate(512.59 140.58) 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=\"17eb0eabf49\"><rect x=\"7.2\" y=\"7.2\" width=\"586.79\" height=\"203.96\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 1058.43x367.889 with 1 Axes>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(3)\n",
"clbits = ClassicalRegister(3)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0, q1, q2) = qubits\n",
"(c0, c1, c2) = clbits\n",
"\n",
"circuit.h([q0, q1])\n",
"circuit.measure(q0, c0)\n",
"circuit.measure(q1, c1)\n",
"with circuit.switch(clbits) as case:\n",
" with case(0b000, 0b011):\n",
" circuit.z(q2)\n",
" with case(0b001):\n",
" circuit.y(q2)\n",
" with case(case.DEFAULT):\n",
" circuit.x(q2)\n",
"circuit.measure(q2, c2)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'101': 267, '110': 249, '011': 265, '000': 243}"
]
},
{
"cell_type": "markdown",
"id": "be30c6ab-6aa6-4b06-8322-fecb9500ec6e",
"metadata": {},
"source": [
"## For loop\n",
"\n",
"A for loop is used to iterate over a sequence of classical values and perform some operations during each iteration.\n",
"\n",
"In the example below, we use a for loop to apply 5 X gates to a qubit and then measure it. Because we perform an odd number of X gates, the overall effect is to flip the qubit from the 0 state to the 1 state."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "af3ae593-b759-4b7b-86fc-9b6731a6cfd8",
"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=\"220.14pt\" height=\"125.65pt\" version=\"1.1\" viewBox=\"0 0 220.14 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=\"m0 125.65h220.14v-125.65h-220.14z\" fill=\"#ffffff\"/><path d=\"m64.609 44.283h143.7\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m73.88 95.273 4.6354-9.2708\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m64.609 89.131h143.7\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.609 92.144h143.7\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m176.32 80.092h12.052l-6.026 8.6914z\" clip-path=\"url(#a4fcf00be00)\" fill=\"#778899\"/><path d=\"m78.052 63.984h76.484q4.6354 0 4.6354-4.6354v-30.13q0-4.6354-4.6354-4.6354h-76.484q-4.6354 0-4.6354 4.6354v30.13q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(74.344 38.6) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-46\" transform=\"scale(.015625)\" d=\"m628 4666h2681v-532h-2050v-1375h1850v-531h-1850v-2228h-631v4666z\"/><path id=\"DejaVuSans-6f\" transform=\"scale(.015625)\" d=\"m1959 3097q-462 0-731-361t-269-989 267-989q268-361 733-361 460 0 728 362 269 363 269 988 0 622-269 986-268 364-728 364zm0 487q750 0 1178-488 429-487 429-1349 0-859-429-1349-428-489-1178-489-753 0-1180 489-426 490-426 1349 0 862 426 1349 427 488 1180 488z\"/><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-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-46\"/><use x=\"85.681641\" xlink:href=\"#DejaVuSans-6f\"/><use x=\"146.863281\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(76.198 55.761) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-28\" transform=\"scale(.015625)\" d=\"m1984 4856q-418-718-622-1422-203-703-203-1425 0-721 205-1429t620-1424h-500q-468 735-701 1444t-233 1409q0 697 231 1403 232 707 703 1444h500z\"/><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-35\" transform=\"scale(.015625)\" d=\"m691 4666h2478v-532h-1900v-1143q137 47 274 70 138 23 276 23 781 0 1237-428 457-428 457-1159 0-753-469-1171-469-417-1322-417-294 0-599 50-304 50-629 150v635q281-153 581-228t634-75q541 0 856 284 316 284 316 772 0 487-316 771-315 285-856 285-253 0-505-56-251-56-513-175v2344z\"/><path id=\"DejaVuSans-29\" transform=\"scale(.015625)\" d=\"m513 4856h500q468-737 701-1444 233-706 233-1403 0-700-233-1409t-701-1444h-500q415 716 620 1424t205 1429q0 722-205 1425-205 704-620 1422z\"/></defs><use xlink:href=\"#DejaVuSans-72\"/><use x=\"41.113281\" xlink:href=\"#DejaVuSans-28\"/><use x=\"80.126953\" xlink:href=\"#DejaVuSans-30\"/><use x=\"143.75\" xlink:href=\"#DejaVuSans-2c\"/><use x=\"175.537109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"207.324219\" xlink:href=\"#DejaVuSans-35\"/><use x=\"270.947266\" xlink:href=\"#DejaVuSans-29\"/></g></g><path d=\"m-1004.3 203.05q4.6354 0 4.6354-4.6354 0-34.765-4.6354-34.765-81.119 0-81.119 4.6354 0 34.765 4.6354 34.765\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(-1084.5 177.66) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-46\"/><use x=\"85.681641\" xlink:href=\"#DejaVuSans-6f\"/><use x=\"146.863281\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(-1082.7 194.82) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-72\"/><use x=\"41.113281\" xlink:href=\"#DejaVuSans-28\"/><use x=\"80.126953\" xlink:href=\"#DejaVuSans-30\"/><use x=\"143.75\" xlink:href=\"#DejaVuSans-2c\"/><use x=\"175.537109\" xlink:href=\"#DejaVuSans-20\"/><use x=\"207.324219\" xlink:href=\"#DejaVuSans-35\"/><use x=\"270.947266\" xlink:href=\"#DejaVuSans-29\"/></g></g><path d=\"m59.974 21.106h-1158.8v139.06h1158.8z\" clip-path=\"url(#a4fcf00be00)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m183.85 44.283v35.808\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m180.84 44.283v35.808\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m167.28 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#a4fcf00be00)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m192.89 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m182.35 48.803 10.546-10.546\" clip-path=\"url(#a4fcf00be00)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m120.93 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#a4fcf00be00)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(34.538 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 transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .09375)\" xlink:href=\"#DejaVuSans-35\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(69.245 83.839) scale(.104 -.104)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(36.064 95.121) scale(.1625 -.1625)\"><defs><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\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-35\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(193.94 83.839) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#a4fcf00be00)\"><g transform=\"translate(131.54 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=\"a4fcf00be00\"><rect x=\"7.2\" y=\"7.2\" width=\"205.74\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 371.107x200.667 with 1 Axes>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(1)\n",
"clbits = ClassicalRegister(1)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"(q0,) = qubits\n",
"(c0,) = clbits\n",
"\n",
"with circuit.for_loop(range(5)) as _:\n",
" circuit.x(q0)\n",
"circuit.measure(q0, c0)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'1': 1024}"
]
},
{
"cell_type": "markdown",
"id": "6f1050b0-58a5-46e1-95c6-980db3f09c00",
"metadata": {},
"source": [
"## While loop\n",
"\n",
"A while loop is used to repeat instructions while some condition is satisfied.\n",
"\n",
"In the example below, we apply Hadamard gates to two qubits and measure them. Then, we create a while loop that repeats this procedure while the measurement outcome is 11. As a result, the final measurement should never be 11, with the remaining possibilities appearing with approximately equal frequency."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "95795aa7-9d4d-4c35-ba0e-48e6ba4f41b9",
"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=\"415.55pt\" height=\"172pt\" version=\"1.1\" viewBox=\"0 0 415.55 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 172h415.55v-172h-415.55z\" fill=\"#ffffff\"/><path d=\"m74.604 44.283h329.11\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.604 90.637h329.11\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m83.875 141.63 4.6354-9.2708\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m74.604 135.48h329.11\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.604 138.5h329.11\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m139.96 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#2a064d20708)\" fill=\"#778899\"/><path d=\"m186.32 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#2a064d20708)\" fill=\"#778899\"/><path d=\"m325.38 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#2a064d20708)\" fill=\"#778899\"/><path d=\"m371.73 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#2a064d20708)\" fill=\"#778899\"/><path d=\"m227.11 110.34h169.19q4.6354 0 4.6354-4.6354v-76.484q0-4.6354-4.6354-4.6354h-169.19q-4.6354 0-4.6354 4.6354v76.484q0 4.6354 4.6354 4.6354z\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(223.4 38.6) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-57\" transform=\"scale(.015625)\" d=\"m213 4666h637l981-3944 978 3944h710l981-3944 978 3944h641l-1172-4666h-794l-984 4050-994-4050h-794l-1168 4666z\"/><path id=\"DejaVuSans-68\" transform=\"scale(.015625)\" d=\"m3513 2113v-2113h-575v2094q0 497-194 743-194 247-581 247-466 0-735-297-269-296-269-809v-1978h-578v4863h578v-1907q207 316 486 472 280 156 646 156 603 0 912-373 310-373 310-1098z\"/><path id=\"DejaVuSans-69\" transform=\"scale(.015625)\" d=\"m603 3500h575v-3500h-575v3500zm0 1363h575v-729h-575v729z\"/><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\"/></defs><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-57\"/><use x=\"130.664062\" xlink:href=\"#DejaVuSans-68\"/><use x=\"194.042969\" xlink:href=\"#DejaVuSans-69\"/><use x=\"221.826172\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"249.609375\" xlink:href=\"#DejaVuSans-65\"/></g></g><path d=\"m-762.55 295.75q4.6354 0 4.6354-4.6354 0-81.119-4.6354-81.119-173.83 0-173.83 4.6354 0 81.119 4.6354 81.119\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#fa4d56\" stroke-width=\"3\"/><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(-935.45 224.02) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-20\"/><use x=\"31.787109\" xlink:href=\"#DejaVuSans-57\"/><use x=\"130.664062\" xlink:href=\"#DejaVuSans-68\"/><use x=\"194.042969\" xlink:href=\"#DejaVuSans-69\"/><use x=\"221.826172\" xlink:href=\"#DejaVuSans-6c\"/><use x=\"249.609375\" xlink:href=\"#DejaVuSans-65\"/></g></g><path d=\"m69.969 21.106h-1158.8v185.42h1158.8z\" clip-path=\"url(#2a064d20708)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m147.5 44.283v82.162\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m144.48 44.283v82.162\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m193.85 90.637v35.808\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m190.84 90.637v35.808\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m228.62 112.19v24.799\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m225.6 112.19v24.799\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m332.91 44.283v82.162\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m329.9 44.283v82.162\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m379.27 90.637v35.808\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m376.25 90.637v35.808\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m84.57 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m84.57 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m130.92 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m156.53 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m145.99 48.803 10.546-10.546\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m177.28 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m202.89 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m192.34 95.157 10.546-10.546\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m227.11 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(#2a064d20708)\" stroke=\"#000000\" stroke-width=\"1.5\"/><path d=\"m269.99 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m269.99 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m316.34 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m341.95 48.803c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887-5.4799 1.1118-7.4568 3.0887-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m331.41 48.803 10.546-10.546\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m362.69 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#2a064d20708)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m388.3 95.157c0-2.7958-1.1118-5.4799-3.0887-7.4568s-4.661-3.0887-7.4568-3.0887c-2.7958 0-5.4799 1.1118-7.4568 3.0887s-3.0887 4.661-3.0887 7.4568\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m377.76 95.157 10.546-10.546\" clip-path=\"url(#2a064d20708)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(36.733 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-36\" transform=\"scale(.015625)\" d=\"m2113 2584q-425 0-674-291-248-290-248-796 0-503 248-796 249-292 674-292t673 292q248 293 248 796 0 506-248 796-248 291-673 291zm1253 1979v-575q-238 112-480 171-242 60-480 60-625 0-955-422-329-422-376-1275 184 272 462 417 279 145 613 145 703 0 1111-427 408-426 408-1160 0-719-425-1154-425-434-1131-434-810 0-1238 620-428 621-428 1799 0 1106 525 1764t1409 658q238 0 480-47t505-140z\"/><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-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-36\"/><use transform=\"translate(128.06 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(36.733 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 .78125)\" xlink:href=\"#DejaVuSans-36\"/><use transform=\"translate(128.06 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(79.24 130.19) scale(.104 -.104)\"><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-32\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(46.059 141.48) scale(.1625 -.1625)\"><defs><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\"/></defs><use xlink:href=\"#DejaVuSans-63\"/><use x=\"54.980469\" xlink:href=\"#DejaVuSans-36\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(94.747 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><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(94.747 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(157.58 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(203.93 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(219.65 152.11) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-78\" transform=\"scale(.015625)\" d=\"m3513 3500-1266-1703 1331-1797h-678l-1019 1375-1018-1375h-679l1360 1831-1244 1669h678l928-1247 928 1247h679z\"/><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-30\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-78\"/><use x=\"122.802734\" xlink:href=\"#DejaVuSans-33\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(280.16 47.87) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(280.16 94.224) scale(.13 -.13)\"><use xlink:href=\"#DejaVuSans-48\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(342.99 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#2a064d20708)\"><g transform=\"translate(389.35 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><defs><clipPath id=\"2a064d20708\"><rect x=\"7.2\" y=\"7.2\" width=\"401.15\" height=\"157.6\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 723.581x284.278 with 1 Axes>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qubits = QuantumRegister(2)\n",
"clbits = ClassicalRegister(2)\n",
"circuit = QuantumCircuit(qubits, clbits)\n",
"\n",
"q0, q1 = qubits\n",
"c0, c1 = clbits\n",
"\n",
"circuit.h([q0, q1])\n",
"circuit.measure(q0, c0)\n",
"circuit.measure(q1, c1)\n",
"with circuit.while_loop((clbits, 0b11)):\n",
" circuit.h([q0, q1])\n",
" circuit.measure(q0, c0)\n",
" circuit.measure(q1, c1)\n",
"\n",
"circuit.draw(\"mpl\")\n",
"\n",
"# example output counts: {'01': 334, '10': 368, '00': 322}"
]
},
{
"cell_type": "markdown",
"id": "6aa76130-acc1-4479-8a54-edab6a133e7c",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
" - See an example of dynamic circuits in the [Repeat until success](https://learning.quantum.ibm.com/tutorial/repeat-until-success) tutorial.\n",
" - Review the [circuit library API](/api/qiskit/circuit_library) reference.\n",
"</Admonition>"
]
}
],
"metadata": {
"description": "Use Qiskit for classical feedforward and control flow, otherwise referred to as dynamic circuits",
"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": "Classical feedforward and control flow (a.k.a. dynamic circuits)"
},
"nbformat": 4,
"nbformat_minor": 2
}