159 lines
13 KiB
Plaintext
159 lines
13 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "539f98fa-9ccc-472a-99da-ebe6382243dc",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Save circuits to disk"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "2cd24003-6d72-432a-8573-7a4809e3411c",
|
|
"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": "3aeaa5b8-c8f5-4448-9d72-1a2452d79f74",
|
|
"metadata": {},
|
|
"source": [
|
|
"Use [QPY serialization](/api/qiskit/qpy) to save your circuit to file. QPY files store the full Qiskit circuit object and will be compatible with newer versions of Qiskit (although not necessarily with older versions of Qiskit).\n",
|
|
"\n",
|
|
"To demonstrate, the following cell creates a simple quantum circuit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "fa58c6e1-1102-4409-80b8-01b02e4e6ee5",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit import QuantumCircuit\n",
|
|
"\n",
|
|
"qc = QuantumCircuit(2)\n",
|
|
"qc.h(0)\n",
|
|
"qc.cx(0, 1)\n",
|
|
"qc.measure_all()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d208d7a6-d465-4f08-909e-fd6803577700",
|
|
"metadata": {},
|
|
"source": [
|
|
"To save this file to disk, use the `qpy.dump` function. You can also save a list of circuits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "e02f1f2a-2705-4406-bb1c-c80d436b5110",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from qiskit import qpy\n",
|
|
"\n",
|
|
"with open(\"test.qpy\", \"wb\") as file:\n",
|
|
" qpy.dump(qc, file)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3af68772-b488-411d-9084-14129b9a9f1d",
|
|
"metadata": {},
|
|
"source": [
|
|
"This circuit is now saved to the file `test.qpy`. If you restart your Python kernel, you can re-load the circuit using the `qpy.load` function. Note that this always returns a list of circuits, even if you only serialized one circuit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "210d05c8-c9eb-4135-bb3f-94c281708f4b",
|
|
"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=\"336.17pt\" height=\"172pt\" version=\"1.1\" viewBox=\"0 0 336.17 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 172h336.17v-172h-336.17z\" fill=\"#ffffff\"/><path d=\"m87.926 44.283h236.41\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m87.926 90.637h236.41\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m97.197 141.63 4.6354-9.2708\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\"/><path d=\"m87.926 135.48h236.41\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m87.926 138.5h236.41\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m245.99 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#f0908935d6b)\" fill=\"#778899\"/><path d=\"m292.35 126.45h12.052l-6.026 8.6914z\" clip-path=\"url(#f0908935d6b)\" fill=\"#778899\"/><path d=\"m83.29 21.106h-1158.8v185.42h1158.8z\" clip-path=\"url(#f0908935d6b)\" fill=\"#ffffff\" stroke=\"#ffffff\" stroke-width=\"1.5\"/><path d=\"m159.31 90.637v-46.354\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#002d9c\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m253.53 44.283v82.162\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m250.51 44.283v82.162\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m299.88 90.637v35.808\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m296.87 90.637v35.808\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#778899\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m196.63 67.46h18.078v-46.354h-18.078z\" clip-path=\"url(#f0908935d6b)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m196.63 113.81h18.078v-46.354h-18.078z\" clip-path=\"url(#f0908935d6b)\" fill=\"#a8a8a8\" opacity=\".6\"/><path d=\"m97.892 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f0908935d6b)\" fill=\"#fa4d56\" stroke=\"#fa4d56\" stroke-width=\"1.5\"/><path d=\"m159.31 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(#f0908935d6b)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"1.5\"/><path d=\"m159.31 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(#f0908935d6b)\" fill=\"#002d9c\" stroke=\"#002d9c\" stroke-width=\"2\"/><path d=\"m236.95 59.348h30.13v-30.13h-30.13z\" clip-path=\"url(#f0908935d6b)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m262.56 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(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m252.02 48.803 10.546-10.546\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m283.31 105.7h30.13v-30.13h-30.13z\" clip-path=\"url(#f0908935d6b)\" fill=\"#a8a8a8\" stroke=\"#a8a8a8\" stroke-width=\"1.5\"/><path d=\"m308.92 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(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-width=\"2\"/><path d=\"m298.37 95.157 10.546-10.546\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m159.31 96.663v-12.052\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><path d=\"m153.28 90.637h12.052\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#ffffff\" stroke-linecap=\"square\" stroke-width=\"2\"/><g clip-path=\"url(#f0908935d6b)\"><g transform=\"translate(60.617 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(#f0908935d6b)\"><g transform=\"translate(60.617 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(#f0908935d6b)\"><g transform=\"translate(92.561 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(#f0908935d6b)\"><g transform=\"translate(34.404 141.48) scale(.1625 -.1625)\"><defs><path id=\"DejaVuSans-6d\" transform=\"scale(.015625)\" d=\"m3328 2828q216 388 516 572t706 184q547 0 844-383 297-382 297-1088v-2113h-578v2094q0 503-179 746-178 244-543 244-447 0-707-297-259-296-259-809v-1978h-578v2094q0 506-178 748t-550 242q-441 0-701-298-259-298-259-808v-1978h-578v3500h578v-544q197 322 472 475t653 153q382 0 649-194 267-193 395-562z\"/><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-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\"/></defs><use xlink:href=\"#DejaVuSans-6d\"/><use x=\"97.412109\" xlink:href=\"#DejaVuSans-65\"/><use x=\"158.935547\" xlink:href=\"#DejaVuSans-61\"/><use x=\"220.214844\" xlink:href=\"#DejaVuSans-73\"/></g></g><g clip-path=\"url(#f0908935d6b)\"><g transform=\"translate(108.07 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><path d=\"m205.66 21.106v46.354\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><path d=\"m205.66 67.46v46.354\" clip-path=\"url(#f0908935d6b)\" fill=\"none\" stroke=\"#000000\" stroke-dasharray=\"3.7,1.6\"/><g clip-path=\"url(#f0908935d6b)\"><g transform=\"translate(263.61 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-30\"/></g></g><g clip-path=\"url(#f0908935d6b)\"><g transform=\"translate(309.96 130.19) scale(.104 -.104)\"><use xlink:href=\"#DejaVuSans-31\"/></g></g><defs><clipPath id=\"f0908935d6b\"><rect x=\"7.2\" y=\"7.2\" width=\"321.77\" height=\"157.6\"/></clipPath></defs></svg>"
|
|
],
|
|
"text/plain": [
|
|
"<Figure size 580.387x284.278 with 1 Axes>"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"with open(\"test.qpy\", \"rb\") as handle:\n",
|
|
" qc = qpy.load(handle)\n",
|
|
"\n",
|
|
"qc[0].draw(\"mpl\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "55d34ab2-1031-49e1-8aea-9794adabd0fc",
|
|
"metadata": {
|
|
"tags": [
|
|
"remove-cell"
|
|
]
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Cleanup the file we created (this cell should be hidden from the user)\n",
|
|
"import pathlib\n",
|
|
"\n",
|
|
"pathlib.Path(\"test.qpy\").unlink()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"description": "Store Qiskit objects on disk or in the cloud so you can continue where you left off",
|
|
"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": "Save and retrieve Qiskit objects"
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|