qiskit-documentation/docs/guides/transpile-with-pass-manager...

382 lines
68 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "7dd44f6d-44f7-4613-bf3c-65a1fb7d100b",
"metadata": {},
"source": [
"# Transpile with pass managers"
]
},
{
"cell_type": "markdown",
"id": "0fcd3656-40ad-4f84-b83e-9e6506dff50e",
"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": "8f4f67e5-efba-45e4-ab42-073f99043c3c",
"metadata": {},
"source": [
"The recommended way to transpile a circuit is to create a staged pass manager and then execute its `run` method with the circuit as input. This page explains how to transpile quantum circuits this way."
]
},
{
"cell_type": "markdown",
"id": "33f59120-5486-4430-8a8f-64e3e75f0667",
"metadata": {},
"source": [
"## What is a (staged) pass manager?\n",
"\n",
"In the context of the Qiskit SDK, transpilation refers to the process of transforming an input circuit into a form that is suitable for execution on a quantum device. Transpilation typically occurs in a sequence of steps called transpiler passes. The circuit is processed by each transpiler pass in sequence, with the output of one pass becoming the input to the next. For example, one pass could go through the circuit and merge all consecutive sequences of single-qubit gates, and then the next pass could synthesize these gates into the basis set of the target device. The transpiler passes included with Qiskit are located in the [qiskit.transpiler.passes](/api/qiskit/transpiler_passes) module.\n",
"\n",
"A pass manager is an object that stores a list of transpiler passes and can execute them on a circuit. Create a pass manager by initializing a [`PassManager`](/api/qiskit/qiskit.transpiler.PassManager) with a list of transpiler passes. To run the transpilation on a circuit, call the [`run`](/api/qiskit/qiskit.transpiler.PassManager#run) method with a circuit as input.\n",
"\n",
"A staged pass manager is a special kind of pass manager that represents a level of abstraction above that of a normal pass manager. While a normal pass manager is composed of several transpiler passes, a staged pass manager is composed of several *pass managers*. This is a useful abstraction because transpilation typically happens in discrete stages, as described in [Transpiler stages](transpiler-stages), with each stage being represented by a pass manager. Staged pass managers are represented by the [`StagedPassManager`](/api/qiskit/qiskit.transpiler.StagedPassManager) class. The rest of this page describes how to create and customize (staged) pass managers."
]
},
{
"cell_type": "markdown",
"id": "cd56fbbe-ef00-4831-b63c-be5fadf878d9",
"metadata": {},
"source": [
"## Generate a preset staged pass manager\n",
"\n",
"To create a preset staged pass manager with reasonable defaults, use the [`generate_preset_pass_manager`](/api/qiskit/transpiler_preset#qiskit.transpiler.preset_passmanagers.generate_preset_pass_manager) function:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ba642e68-81fb-4011-bcf7-251ec7194018",
"metadata": {},
"outputs": [],
"source": [
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
"\n",
"service = QiskitRuntimeService()\n",
"backend = service.backend(\"ibm_brisbane\")\n",
"pass_manager = generate_preset_pass_manager(\n",
" optimization_level=3, backend=backend\n",
")"
]
},
{
"cell_type": "markdown",
"id": "d24983e4-e0e1-4c17-8204-1348bf5ab896",
"metadata": {},
"source": [
"To transpile a circuit or list of circuits with a pass manager, pass the circuit or list of circuits to the `run` method. Let's do this on a two-qubit circuit consisting of a Hadamard followed by two adjacent CX gates:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dcc69b72-e13b-4df6-a51f-a5ef2108bae7",
"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=\"619.96pt\" height=\"133.25pt\" version=\"1.1\" viewBox=\"0 0 619.96 133.25\" 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 133.25h619.96v-133.25h-619.96z\" fill=\"#ffffff\"/><path d=\"m93.592 51.882h514.53\" clip-path=\"url(#64f8f15ea8a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m93.592 98.236h514.53\" clip-path=\"url(#64f8f15ea8a)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><g transform=\"translate(7.2 14.798) scale(.1 -.1)\" fill=\"#343a3f\"><defs><path id=\"DejaVuSans-47\" transform=\"scale(.015625)\" d=\"m3809 666v1253h-1031v519h1656v-2004q-365-259-806-392-440-133-940-133-1094 0-1712 639-617 640-617 1780 0 1144 617 1783 618 639 1712 639 456 0 867-113 411-112 758-331v-672q-350 297-744 447t-828 150q-857 0-1287-478-429-478-429-1425 0-944 429-1422 430-478 1287-478 334 0 596 58 263 58 472 180z\"/><path id=\"DejaVuSans-6c\" transform=\"scale(.015625)\" d=\"m603 4863h575v-4863h-575v4863z\"/><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-62\" transform=\"scale(.015625)\" d=\"m3116 1747q0 634-261 995t-717 361q-457 0-718-361t-261-995 261-995 718-361q456 0 717 361t261 995zm-1957 1222q182 312 458 463 277 152 661 152 638 0 1036-506 399-506 399-1331t-399-1332q-398-506-1036-506-384 0-661 152-276 152-458 464v-525h-578v4863h578v-1894z\"/><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-50\" transform=\"scale(.015625)\" d=\"m1259 4147v-1753h794q441 0 681 228 241 228 241 650 0 419-241 647-240 228-681 228h-794zm-631 519h1425q785 0 1186-355 402-355 402-1039 0-691-402-1044-401-353-1186-353h-794v-1875h-631v4666z\"/><path id=\"DejaVuSans-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-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\"/><path id=\"DejaVuSans-3a\" transform=\"scale(.015625)\" d=\"m750 794h659v-794h-659v794zm0 2515h659v-793h-659v793z\"/><path id=\"DejaVuSans-37\" transform=\"scale(.015625)\" d=\"m525 4666h3e3v-269l-1694-4397h-659l1594 4134h-2241v532z\"/><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2f\" transform=\"scale(.015625)\" d=\"m1625 4666h531l-1625-5260h-531l1625 5260z\"/><path id=\"DejaVuSans-34\" transform=\"scale(.015625)\" d=\"m2419 4116-1594-2491h1594v2491zm-166 550h794v-3041h666v-525h-666v-1100h-628v1100h-2106v609l1940 2957z\"/></defs><use transform=\"translate(0 .015625)\" xlink:href=\"#DejaVuSans-47\"/><use transform=\"translate(77.49 .015625)\" xlink:href=\"#DejaVuSans-6c\"/><use transform=\"translate(105.27 .015625)\" xlink:href=\"#DejaVuSans-6f\"/><use transform=\"translate(166.46 .015625)\" xlink:href=\"#DejaVuSans-62\"/><use transform=\"translate(229.93 .015625)\" xlink:href=\"#DejaVuSans-61\"/><use transform=\"translate(291.21 .015625)\" xlink:href=\"#DejaVuSans-6c\"/><use transform=\"translate(318.99 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(350.78 .015625)\" xlink:href=\"#DejaVuSans-50\"/><use transform=\"translate(411.08 .015625)\" xlink:href=\"#DejaVuSans-68\"/><use transform=\"translate(474.46 .015625)\" xlink:href=\"#DejaVuSans-61\"/><use transform=\"translate(535.74 .015625)\" xlink:href=\"#DejaVuSans-73\"/><use transform=\"translate(587.84 .015625)\" xlink:href=\"#DejaVuSans-65\"/><use transform=\"translate(649.37 .015625)\" xlink:href=\"#DejaVuSans-3a\"/><use transform=\"translate(683.06 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(714.84 .015625)\" xlink:href=\"#DejaVuSans-37\"/><use transform=\"translate(778.47 .015625)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(838.67 .015625)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(872.36 .015625)\" xlink:href=\"#DejaVuSans-34\"/></g><path d=\"m88.957 28.705h-1158.8v139.06h1158.8z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m103.56 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m103.56 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m149.91 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m149.91 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m195.41 113.3h31.85v-76.484h-31.85z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m242.62 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m242.62 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m288.97 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m288.97 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m335.33 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m335.33 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m380.82 113.3h31.85v-76.484h-31.85z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m428.04 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m428.04 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m473.57 113.3h31.776v-30.13h-31.776z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m474.39 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><path d=\"m520.74 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m567.1 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#64f8f15ea8a)\" fill=\"#33b1ff\" stroke=\"#33b1ff\" stroke-width=\"1.5\"/><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(27.121 56.333) 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\"/><path id=\"DejaVuSans-21a6\" transform=\"scale(.015625)\" d=\"m4175 1741h-3278v-1078h-531v2687h531v-1078h3278l-728 728 375 375 1228-1228v-281l-1228-1228-375 375 728 728z\"/><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\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/><use transform=\"translate(157.29 .09375)\" xlink:href=\"#DejaVuSans-21a6\"/><use transform=\"translate(287.62 .09375)\" xlink:href=\"#DejaVuSans-35\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(27.121 102.69) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/><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\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -15.625) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/><use transform=\"translate(157.29 .78125)\" xlink:href=\"#DejaVuSans-21a6\"/><use transform=\"translate(287.62 .78125)\" xlink:href=\"#DejaVuSans-36\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(107.17 63.128) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-2e\" transform=\"scale(.015625)\" d=\"m684 794h660v-794h-660v794z\"/><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-2e\"/><use x=\"95.410156\" xlink:href=\"#DejaVuSans-33\"/><use x=\"159.033203\" xlink:href=\"#DejaVuSans-33\"/><use x=\"222.65625\" xlink:href=\"#DejaVuSans-37\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(110.69 50.949) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-52\" transform=\"scale(.015625)\" d=\"m2841 2188q203-69 395-294t386-619l641-1275h-679l-596 1197q-232 469-449 622t-592 153h-688v-1972h-631v4666h1425q800 0 1194-335 394-334 394-1009 0-441-205-732-205-290-595-402zm-1582 1959v-1656h794q456 0 689 211t233 620-233 617-689 208h-794z\"/><path id=\"DejaVuSans-5a\" transform=\"scale(.015625)\" d=\"m359 4666h3666v-482l-2950-3653h3022v-531h-3809v481l2950 3653h-2879v532z\"/></defs><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(106.42 109.48) scale(.08 -.08)\"><defs><path id=\"DejaVuSans-2212\" transform=\"scale(.015625)\" d=\"m678 2272h4006v-531h-4006v531z\"/><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-39\" transform=\"scale(.015625)\" d=\"m703 97v575q238-113 481-172 244-59 479-59 625 0 954 420 330 420 377 1277-181-269-460-413-278-144-615-144-700 0-1108 423-408 424-408 1159 0 718 425 1152 425 435 1131 435 810 0 1236-621 427-620 427-1801 0-1103-524-1761-523-658-1407-658-238 0-482 47-243 47-506 141zm1256 1978q425 0 673 290 249 291 249 798 0 503-249 795-248 292-673 292t-673-292-248-795q0-507 248-798 248-290 673-290z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-32\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-2e\"/><use transform=\"translate(179.2 .78125)\" xlink:href=\"#DejaVuSans-39\"/><use transform=\"translate(241.07 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(110.69 97.303) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(154.97 56.328) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"STIXSizeOneSym-Regular-221a\" transform=\"scale(.015625)\" d=\"m6970 9933-3597-11821h-269l-1478 4806q-71 231-161 333-89 103-236 103-218 0-435-173l-77 128 1049 806h160l1453-4717h26l3200 10535h365z\"/><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 transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(154.97 102.68) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(198.65 101.82) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(198.65 55.469) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(206.08 78.646) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-45\" transform=\"scale(.015625)\" d=\"m628 4666h2950v-532h-2319v-1381h2222v-531h-2222v-1691h2375v-531h-3006v4666z\"/><path id=\"DejaVuSans-63\" transform=\"scale(.015625)\" d=\"m3122 3366v-538q-244 135-489 202t-495 67q-560 0-870-355-309-354-309-995t309-996q310-354 870-354 250 0 495 67t489 202v-532q-241-112-499-168-257-57-548-57-791 0-1257 497-465 497-465 1341 0 856 470 1346 471 491 1290 491 265 0 518-55 253-54 491-163z\"/><path id=\"DejaVuSans-72\" transform=\"scale(.015625)\" d=\"m2631 2963q-97 56-211 82-114 27-251 27-488 0-749-317t-261-911v-1844h-578v3500h578v-544q182 319 472 473 291 155 707 155 59 0 131-8 72-7 159-23l3-590z\"/></defs><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(251.53 109.48) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(249.76 97.303) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(248.17 63.128) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(174.06 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(249.76 50.949) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(294.03 102.68) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(294.03 56.328) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(340.87 109.48) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(174.06 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(342.46 97.303) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(344.23 63.128) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(342.46 50.949) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(384.07 101.82) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(384.07 55.469) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(391.49 78.646) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-45\"/><use x=\"63.183594\" xlink:href=\"#DejaVuSans-63\"/><use x=\"118.164062\" xlink:href=\"#DejaVuSans-72\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(433.09 102.68) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(433.09 56.328) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(474.62 109.48) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-30\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-2e\"/><use transform=\"translate(179.2 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(242.82 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(306.45 .78125)\" xlink:href=\"#DejaVuSans-37\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(481.53 97.303) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(480.55 63.128) scale(.08 -.08)\"><use xlink:href=\"#DejaVuSans-31\"/><use x=\"63.623047\" xlink:href=\"#DejaVuSans-2e\"/><use x=\"95.410156\" xlink:href=\"#DejaVuSans-33\"/><use x=\"159.033203\" xlink:href=\"#DejaVuSans-35\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(481.53 50.949) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(525.8 102.68) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 17.094) scale(.56391)\" xlink:href=\"#STIXSizeOneSym-Regular-221a\"/><use transform=\"translate(72.088 .84375)\" xlink:href=\"#DejaVuSans-58\"/><path d=\"m59.588 97.5v6.25h93.506v-6.25h-93.506z\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(576 109.48) scale(.08 -.08)\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#64f8f15ea8a)\"><g transform=\"translate(574.23 97.303) scale(.13 -.13)\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-5a\"/></g></g><defs><clipPath id=\"64f8f15ea8a\"><rect x=\"7.2\" y=\"14.798\" width=\"605.56\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 1092.27x200.667 with 1 Axes>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from qiskit import QuantumRegister, QuantumCircuit\n",
"\n",
"# Create a circuit\n",
"qubits = QuantumRegister(2, name=\"q\")\n",
"circuit = QuantumCircuit(qubits)\n",
"a, b = qubits\n",
"circuit.h(a)\n",
"circuit.cx(a, b)\n",
"circuit.cx(b, a)\n",
"\n",
"# Transpile it by calling the run method of the pass manager\n",
"transpiled = pass_manager.run(circuit)\n",
"\n",
"# Draw it, excluding idle qubits from the diagram\n",
"transpiled.draw(\"mpl\", idle_wires=False)"
]
},
{
"cell_type": "markdown",
"id": "f8fc4517-12b8-43ec-8786-fab82fcb66cd",
"metadata": {},
"source": [
"See [Transpilation defaults and configuration options](defaults-and-configuration-options) for a description of the possible arguments to the `generate_preset_pass_manager` function. The arguments to `generate_preset_pass_manager` match the arguments to the [`transpile`](/api/qiskit/compiler#qiskit.compiler.transpile) function.\n",
"\n",
"<CodeAssistantAdmonition\n",
" tagLine=\"Having trouble remembering pass manager details? Try asking Qiskit Code Assistant.\"\n",
" prompts={[\n",
" \"# Create a pass manager with the highest optimization level\",\n",
" \"# Transpile 'circuit' with the highest optimization level\"\n",
" ]}\n",
"/>\n",
"\n",
"\n",
"If the preset pass managers don't fulfill your needs, customize transpilation by creating (staged) pass managers or even transpilation passes. The rest of this page describes how to create pass managers. For instructions on how to create transpilation passes, see [Write your own transpiler pass](custom-transpiler-pass)."
]
},
{
"cell_type": "markdown",
"id": "191ec295-b5ba-41d8-9bd0-8d70147359c7",
"metadata": {},
"source": [
"## Create your own pass manager\n",
"\n",
"The [qiskit.transpiler.passes](/api/qiskit/transpiler_passes) module includes many transpiler passes that can be used to create pass managers. To create a pass manager, initialize a `PassManager` with a list of passes. For example, the following code creates a transpiler pass that merges adjacent two-qubit gates and then synthesizes them into a basis of [$R_y$](/api/qiskit/qiskit.circuit.library.RYGate), [$R_z$](/api/qiskit/qiskit.circuit.library.RZGate), and [$R_{xx}$](/api/qiskit/qiskit.circuit.library.RXXGate), gates."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "77faa30c-2b70-45a5-b161-d214be508cdc",
"metadata": {},
"outputs": [],
"source": [
"from qiskit.transpiler import PassManager\n",
"from qiskit.transpiler.passes import (\n",
" Collect2qBlocks,\n",
" ConsolidateBlocks,\n",
" UnitarySynthesis,\n",
")\n",
"\n",
"basis_gates = [\"rx\", \"ry\", \"rxx\"]\n",
"translate = PassManager(\n",
" [\n",
" Collect2qBlocks(),\n",
" ConsolidateBlocks(basis_gates=basis_gates),\n",
" UnitarySynthesis(basis_gates),\n",
" ]\n",
")"
]
},
{
"cell_type": "markdown",
"id": "8cf1493d-d4a2-41d4-87f6-687f94e094ab",
"metadata": {},
"source": [
"To demonstrate this pass manager in action, test it on a two-qubit circuit consisting of a Hadamard followed by two adjacent CX gates:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bc208935-e63c-461b-90d0-a6f4cabc16b6",
"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.03pt\" height=\"125.65pt\" version=\"1.1\" viewBox=\"0 0 220.03 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.03v-125.65h-220.03z\" fill=\"#ffffff\"/><path d=\"m64.497 44.283h143.7\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.497 90.637h143.7\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m59.862 21.106h-1158.8v139.06h1158.8z\" clip-path=\"url(#2e210a0031f)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m135.88 90.637v-46.354\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m182.24 90.637v-46.354\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m74.463 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#2e210a0031f)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m135.88 48.803c1.1986 0 2.3482-0.4762 3.1958-1.3237 0.84753-0.84753 1.3237-1.9972 1.3237-3.1958 0-1.1986-0.4762-2.3482-1.3237-3.1958-0.84753-0.84753-1.9972-1.3237-3.1958-1.3237-1.1986 0-2.3482 0.4762-3.1958 1.3237s-1.3237 1.9972-1.3237 3.1958c0 1.1986 0.4762 2.3482 1.3237 3.1958s1.9972 1.3237 3.1958 1.3237z\" clip-path=\"url(#2e210a0031f)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m135.88 101.18c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887-2.7967 0-5.4792 1.1111-7.4568 3.0887-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#2e210a0031f)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m182.24 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(#2e210a0031f)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m182.24 54.829c2.7967 0 5.4792-1.1111 7.4568-3.0887 1.9776-1.9776 3.0887-4.6601 3.0887-7.4568 0-2.7967-1.1111-5.4792-3.0887-7.4568-1.9776-1.9776-4.6601-3.0887-7.4568-3.0887s-5.4792 1.1111-7.4568 3.0887c-1.9776 1.9776-3.0887 4.6601-3.0887 7.4568 0 2.7967 1.1111 5.4792 3.0887 7.4568 1.9776 1.9776 4.6601 3.0887 7.4568 3.0887z\" clip-path=\"url(#2e210a0031f)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m135.88 96.663v-12.052\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m129.86 90.637h12.052\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m182.24 50.309v-12.052\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m176.21 44.283h12.052\" clip-path=\"url(#2e210a0031f)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#2e210a0031f)\"><g transform=\"translate(37.189 48.734) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-Oblique-71\" transform=\"scale(.015625)\" d=\"m2669 525q-231-303-546-460-314-156-695-156-531 0-833 358-301 358-301 986 0 506 186 978t533 847q225 244 517 375t614 131q387 0 637-153t363-462l100 525h578l-934-4813h-579l360 1844zm-1778 813q0-463 193-705 194-242 560-242 544 0 928 520t384 1264q0 450-199 689-198 239-569 239-272 0-504-127-231-126-403-370-181-256-286-600-104-343-104-668z\"/><path id=\"DejaVuSans-30\" transform=\"scale(.015625)\" d=\"m2034 4250q-487 0-733-480-245-479-245-1442 0-959 245-1439 246-480 733-480 491 0 736 480 246 480 246 1439 0 963-246 1442-245 480-736 480zm0 500q785 0 1199-621 414-620 414-1801 0-1178-414-1799-414-620-1199-620-784 0-1198 620-414 621-414 1799 0 1181 414 1801 414 621 1198 621z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#2e210a0031f)\"><g transform=\"translate(37.189 95.088) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#2e210a0031f)\"><g transform=\"translate(84.64 47.87) scale(.13 -.13)\"><defs><path id=\"DejaVuSans-48\" transform=\"scale(.015625)\" d=\"m628 4666h631v-1913h2294v1913h631v-4666h-631v2222h-2294v-2222h-631v4666z\"/></defs><use xlink:href=\"#DejaVuSans-48\"/></g></g><defs><clipPath id=\"2e210a0031f\"><rect x=\"7.2\" y=\"7.2\" width=\"205.63\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 370.906x200.667 with 1 Axes>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from qiskit import QuantumRegister, QuantumCircuit\n",
"\n",
"qubits = QuantumRegister(2, name=\"q\")\n",
"circuit = QuantumCircuit(qubits)\n",
"\n",
"a, b = qubits\n",
"circuit.h(a)\n",
"circuit.cx(a, b)\n",
"circuit.cx(b, a)\n",
"\n",
"circuit.draw(\"mpl\")"
]
},
{
"cell_type": "markdown",
"id": "7a4eb2f3-63ac-41bb-91f0-119e4fbd303f",
"metadata": {},
"source": [
"To run the pass manager on the circuit, call the `run` method."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "adb5c242-5cba-4878-a00d-4ec47737d029",
"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=\"590.86pt\" height=\"133.25pt\" version=\"1.1\" viewBox=\"0 0 590.86 133.25\" 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 133.25h590.86v-133.25h-590.86z\" fill=\"#ffffff\"/><path d=\"m64.497 51.882h514.53\" clip-path=\"url(#28ebc1c0e96)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m64.497 98.236h514.53\" clip-path=\"url(#28ebc1c0e96)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><g transform=\"translate(7.2 14.798) scale(.1 -.1)\" fill=\"#343a3f\"><defs><path id=\"DejaVuSans-47\" transform=\"scale(.015625)\" d=\"m3809 666v1253h-1031v519h1656v-2004q-365-259-806-392-440-133-940-133-1094 0-1712 639-617 640-617 1780 0 1144 617 1783 618 639 1712 639 456 0 867-113 411-112 758-331v-672q-350 297-744 447t-828 150q-857 0-1287-478-429-478-429-1425 0-944 429-1422 430-478 1287-478 334 0 596 58 263 58 472 180z\"/><path id=\"DejaVuSans-6c\" transform=\"scale(.015625)\" d=\"m603 4863h575v-4863h-575v4863z\"/><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-62\" transform=\"scale(.015625)\" d=\"m3116 1747q0 634-261 995t-717 361q-457 0-718-361t-261-995 261-995 718-361q456 0 717 361t261 995zm-1957 1222q182 312 458 463 277 152 661 152 638 0 1036-506 399-506 399-1331t-399-1332q-398-506-1036-506-384 0-661 152-276 152-458 464v-525h-578v4863h578v-1894z\"/><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-50\" transform=\"scale(.015625)\" d=\"m1259 4147v-1753h794q441 0 681 228 241 228 241 650 0 419-241 647-240 228-681 228h-794zm-631 519h1425q785 0 1186-355 402-355 402-1039 0-691-402-1044-401-353-1186-353h-794v-1875h-631v4666z\"/><path id=\"DejaVuSans-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-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\"/><path id=\"DejaVuSans-3a\" transform=\"scale(.015625)\" d=\"m750 794h659v-794h-659v794zm0 2515h659v-793h-659v793z\"/><path id=\"DejaVuSans-Oblique-3c0\" transform=\"scale(.015625)\" d=\"m584 3500h3354l-113-575h-441l-418-2150q-44-225 15-325 57-97 228-97 47 0 116 10 72 6 94 9l-81-416q-116-40-235-59-122-19-237-19-375 0-478 203-104 207 3 757l406 2087h-1291l-568-2925h-588l569 2925h-447l112 575z\"/><path id=\"DejaVuSans-2f\" transform=\"scale(.015625)\" d=\"m1625 4666h531l-1625-5260h-531l1625 5260z\"/><path id=\"DejaVuSans-32\" transform=\"scale(.015625)\" d=\"m1228 531h2203v-531h-2962v531q359 372 979 998 621 627 780 809 303 340 423 576 121 236 121 464 0 372-261 606-261 235-680 235-297 0-627-103-329-103-704-313v638q381 153 712 231 332 78 607 78 725 0 1156-363 431-362 431-968 0-288-108-546-107-257-392-607-78-91-497-524-418-433-1181-1211z\"/></defs><use transform=\"translate(0 .015625)\" xlink:href=\"#DejaVuSans-47\"/><use transform=\"translate(77.49 .015625)\" xlink:href=\"#DejaVuSans-6c\"/><use transform=\"translate(105.27 .015625)\" xlink:href=\"#DejaVuSans-6f\"/><use transform=\"translate(166.46 .015625)\" xlink:href=\"#DejaVuSans-62\"/><use transform=\"translate(229.93 .015625)\" xlink:href=\"#DejaVuSans-61\"/><use transform=\"translate(291.21 .015625)\" xlink:href=\"#DejaVuSans-6c\"/><use transform=\"translate(318.99 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(350.78 .015625)\" xlink:href=\"#DejaVuSans-50\"/><use transform=\"translate(411.08 .015625)\" xlink:href=\"#DejaVuSans-68\"/><use transform=\"translate(474.46 .015625)\" xlink:href=\"#DejaVuSans-61\"/><use transform=\"translate(535.74 .015625)\" xlink:href=\"#DejaVuSans-73\"/><use transform=\"translate(587.84 .015625)\" xlink:href=\"#DejaVuSans-65\"/><use transform=\"translate(649.37 .015625)\" xlink:href=\"#DejaVuSans-3a\"/><use transform=\"translate(683.06 .015625)\" xlink:href=\"#DejaVuSans-20\"/><use transform=\"translate(714.84 .015625)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(775.05 .015625)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(805.12 .015625)\" xlink:href=\"#DejaVuSans-32\"/></g><path d=\"m59.862 28.705h-1158.8v139.06h1158.8z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m74.463 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m74.463 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m120.82 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m120.82 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m167.17 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m167.17 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m209.34 113.3h38.506v-76.484h-38.506z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m259.88 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m259.88 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m306.23 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m306.23 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m352.59 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m352.59 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m394.75 113.3h38.506v-76.484h-38.506z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m445.3 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m445.3 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m491.65 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m491.65 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m538 66.947h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><path d=\"m538 113.3h30.13v-30.13h-30.13z\" clip-path=\"url(#28ebc1c0e96)\" fill=\"#9f1853\" stroke=\"#9f1853\" stroke-width=\"1.5\"/><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(37.189 56.333) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-Oblique-71\" transform=\"scale(.015625)\" d=\"m2669 525q-231-303-546-460-314-156-695-156-531 0-833 358-301 358-301 986 0 506 186 978t533 847q225 244 517 375t614 131q387 0 637-153t363-462l100 525h578l-934-4813h-579l360 1844zm-1778 813q0-463 193-705 194-242 560-242 544 0 928 520t384 1264q0 450-199 689-198 239-569 239-272 0-504-127-231-126-403-370-181-256-286-600-104-343-104-668z\"/><path id=\"DejaVuSans-30\" transform=\"scale(.015625)\" d=\"m2034 4250q-487 0-733-480-245-479-245-1442 0-959 245-1439 246-480 733-480 491 0 736 480 246 480 246 1439 0 963-246 1442-245 480-736 480zm0 500q785 0 1199-621 414-620 414-1801 0-1178-414-1799-414-620-1199-620-784 0-1198 620-414 621-414 1799 0 1181 414 1801 414 621 1198 621z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(37.189 102.69) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-31\" transform=\"scale(.015625)\" d=\"m794 531h1031v3560l-1122-225v575l1116 225h631v-4135h1031v-531h-2687v531z\"/></defs><use xlink:href=\"#DejaVuSans-Oblique-71\"/><use transform=\"translate(63.477 -16.406) scale(.7)\" xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(80.008 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-2212\" transform=\"scale(.015625)\" d=\"m678 2272h4006v-531h-4006v531z\"/></defs><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(174.06 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(81.598 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><defs><path id=\"DejaVuSans-52\" transform=\"scale(.015625)\" d=\"m2841 2188q203-69 395-294t386-619l641-1275h-679l-596 1197q-232 469-449 622t-592 153h-688v-1972h-631v4666h1425q800 0 1194-335 394-334 394-1009 0-441-205-732-205-290-595-402zm-1582 1959v-1656h794q456 0 689 211t233 620-233 617-689 208h-794z\"/><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 transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(80.008 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(174.06 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(81.598 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(129.56 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><defs><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-3c0\"/><use transform=\"translate(60.205 .09375)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(93.896 .09375)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(128.34 50.949) 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 transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(129.56 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .09375)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(93.896 .09375)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(128.34 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(170.04 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><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 transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(207.62 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(241.31 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(174.31 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(170.04 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(207.62 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(241.31 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(174.31 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(212.58 55.469) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(212.58 101.82) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(227.53 85.146) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(222.64 71.229) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/><use transform=\"translate(118.39 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(266.06 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(63.623 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(123.83 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(157.52 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(267.01 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(265.26 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .09375)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .09375)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(177.69 .09375)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(267.01 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(315.14 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(313.76 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(315.14 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(313.76 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(355.45 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(207.62 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(241.31 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(359.72 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(355.45 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(207.62 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(241.31 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(359.72 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(398 55.469) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(398 101.82) scale(.13 -.13)\" fill=\"#ffffff\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(412.95 85.146) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(408.06 71.229) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/><use transform=\"translate(118.39 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(451.48 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(63.623 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(123.83 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(157.52 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(452.43 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(448.16 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-33\"/><use transform=\"translate(147.41 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(207.62 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(241.31 .78125)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(452.43 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(500.39 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .09375)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(93.896 .09375)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(499.17 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(500.55 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(60.205 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(90.271 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(499.17 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-59\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(543.55 63.128) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .78125)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .78125)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .78125)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(174.06 .78125)\" xlink:href=\"#DejaVuSans-32\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(545.14 50.949) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(543.39 109.48) scale(.08 -.08)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-2212\"/><use transform=\"translate(83.789 .09375)\" xlink:href=\"#DejaVuSans-Oblique-3c0\"/><use transform=\"translate(143.99 .09375)\" xlink:href=\"#DejaVuSans-2f\"/><use transform=\"translate(177.69 .09375)\" xlink:href=\"#DejaVuSans-34\"/></g></g><g clip-path=\"url(#28ebc1c0e96)\"><g transform=\"translate(545.14 97.303) scale(.13 -.13)\" fill=\"#ffffff\"><use transform=\"translate(0 .09375)\" xlink:href=\"#DejaVuSans-52\"/><use transform=\"translate(70.439 -16.312) scale(.7)\" xlink:href=\"#DejaVuSans-58\"/></g></g><defs><clipPath id=\"28ebc1c0e96\"><rect x=\"7.2\" y=\"14.798\" width=\"576.46\" height=\"111.25\"/></clipPath></defs></svg>"
],
"text/plain": [
"<Figure size 1039.79x200.667 with 1 Axes>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"translated = translate.run(circuit)\n",
"translated.draw(\"mpl\")"
]
},
{
"cell_type": "markdown",
"id": "a13649b3-e22c-4fbd-8ee8-44a1ea47ffac",
"metadata": {},
"source": [
"For a more advanced example that shows how to create a pass manager to implement the error suppression technique known as dynamical decoupling, see [Create a pass manager for dynamical decoupling](dynamical-decoupling-pass-manager)."
]
},
{
"cell_type": "markdown",
"id": "3739fc8a-575e-48c6-a4ec-f2df4363b328",
"metadata": {},
"source": [
"## Create a staged pass manager\n",
"\n",
"A `StagedPassManager` is a pass manager that is composed of individual stages, where each stage is defined by a `PassManager` instance. You can create a `StagedPassManager` by specifying the desired stages. For example, the following code creates a staged pass manager with two stages, `init` and `translation`. The `translation` stage is defined by the pass manager that was created previously."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "934e6a01-2fe5-4882-90a3-c53485bd4046",
"metadata": {},
"outputs": [],
"source": [
"from qiskit.transpiler import PassManager, StagedPassManager\n",
"from qiskit.transpiler.passes import UnitarySynthesis, Unroll3qOrMore\n",
"\n",
"basis_gates = [\"rx\", \"ry\", \"rxx\"]\n",
"init = PassManager(\n",
" [UnitarySynthesis(basis_gates, min_qubits=3), Unroll3qOrMore()]\n",
")\n",
"staged_pm = StagedPassManager(\n",
" stages=[\"init\", \"translation\"], init=init, translation=translate\n",
")"
]
},
{
"cell_type": "markdown",
"id": "cbf0e3e9-56cd-42b8-bb90-bc2645ef79c6",
"metadata": {},
"source": [
"There is no limit on the number of stages you can put in a staged pass manager.\n",
"\n",
"Another useful way to create a staged pass manager is to begin with a preset staged pass manager and then swap out some of the stages. For example, the following code generates a preset pass manager with optimization level 3, and then specifies a custom `pre_layout` stage."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "41929693-eff2-4389-a127-cb299f021f6d",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from qiskit.circuit.library import HGate, PhaseGate, RXGate, TdgGate, TGate\n",
"from qiskit.transpiler.passes import CXCancellation, InverseCancellation\n",
"\n",
"pass_manager = generate_preset_pass_manager(3, backend)\n",
"inverse_gate_list = [\n",
" HGate(),\n",
" (RXGate(np.pi / 4), RXGate(-np.pi / 4)),\n",
" (PhaseGate(np.pi / 4), PhaseGate(-np.pi / 4)),\n",
" (TGate(), TdgGate()),\n",
"]\n",
"logical_opt = PassManager(\n",
" [\n",
" CXCancellation(),\n",
" InverseCancellation(inverse_gate_list),\n",
" ]\n",
")\n",
"\n",
"# Add pre-layout stage to run extra logical optimization\n",
"pass_manager.pre_layout = logical_opt"
]
},
{
"cell_type": "markdown",
"id": "b1b35114-458e-4ac6-9829-9bbeabfa2d17",
"metadata": {},
"source": [
"The [stage generator functions](/api/qiskit/transpiler_preset#stage-generator-functions) might be useful for constructing custom pass managers.\n",
"They generate stages that provide common functionality used in many pass managers.\n",
"For example, [`generate_embed_passmanager`](/api/qiskit/transpiler_preset#qiskit.transpiler.preset_passmanagers.common.generate_embed_passmanager) can be used to generate a stage\n",
"to \"embed\" a selected initial `Layout` from a layout pass to the specified target device.\n",
"\n",
"## Next steps\n",
"\n",
"<Admonition type=\"tip\" title=\"Recommendation\">\n",
" - [Write a custom transpiler pass](custom-transpiler-pass).\n",
" - [Create a pass manager for dynamical decoupling](dynamical-decoupling-pass-manager).\n",
" - To learn more about the `generate_preset_passmanager` function, see the [Transpilation default settings and configuration options](defaults-and-configuration-options) topic.\n",
" - Try the [Submit transpiled circuits](https://learning.quantum.ibm.com/tutorial/submit-transpiled-circuits) tutorial.\n",
" - See the [Transpile API documentation.](https://docs.quantum-computing.ibm.com/api/qiskit/transpiler)\n",
"</Admonition>"
]
}
],
"metadata": {
"celltoolbar": "Raw Cell Format",
"description": "How to transpile quantum circuits using pass managers in Qiskit.",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3"
},
"title": "Transpile with pass managers"
},
"nbformat": 4,
"nbformat_minor": 4
}