Created
January 5, 2023 14:25
-
-
Save HuangJunye/02bdcd927de4aab0cfc214428fa1fdac to your computer and use it in GitHub Desktop.
Save circuit as qasm string or file and reload later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "b1335116-5c30-4e6f-8c8b-3693508d8596", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: "Courier New",Courier,monospace\"> ┌────┐┌────────┐┌───────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(p) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└───────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ───────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ───────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ───────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ══════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ═════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ═══════════════════════════════════════════╩═\n", | |
" </pre>" | |
], | |
"text/plain": [ | |
" ┌────┐┌────────┐┌───────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(p) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└───────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ───────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ───────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ───────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ══════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ═════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ═══════════════════════════════════════════╩═\n", | |
" " | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# create a circuit\n", | |
"from qiskit import QuantumCircuit\n", | |
"from qiskit.circuit import Parameter\n", | |
"import numpy as np\n", | |
"\n", | |
"p = Parameter('p')\n", | |
"qc = QuantumCircuit(4)\n", | |
"qc.sx(0)\n", | |
"qc.rz(3*np.pi, 0)\n", | |
"qc.rz(p,0)\n", | |
"qc.x(0)\n", | |
"qc.measure_all()\n", | |
"qc.draw('text')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "3080415b-f9ef-406b-a44f-db2d27642058", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Traceback \u001b[1;36m(most recent call last)\u001b[0m:\n", | |
" Input \u001b[0;32mIn [2]\u001b[0m in \u001b[0;35m<cell line: 2>\u001b[0m\n", | |
" qc.qasm()\n", | |
"\u001b[1;36m File \u001b[1;32m/opt/conda/lib/python3.8/site-packages/qiskit/circuit/quantumcircuit.py:1673\u001b[1;36m in \u001b[1;35mqasm\u001b[1;36m\u001b[0m\n", | |
"\u001b[1;33m raise QasmError(\"Cannot represent circuits with unbound parameters in OpenQASM 2.\")\u001b[0m\n", | |
"\u001b[1;31mQasmError\u001b[0m\u001b[1;31m:\u001b[0m Cannot represent circuits with unbound parameters in OpenQASM 2.\n", | |
"\n", | |
"Use %tb to get the full traceback.\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
"<style>\n", | |
".button {\n", | |
" border: none;\n", | |
" color: white;\n", | |
" padding: 4px 8px;\n", | |
" text-align: center;\n", | |
" text-decoration: none;\n", | |
" display: inline-block;\n", | |
" font-size: 12px;\n", | |
" margin: 4px 2px;\n", | |
" transition-duration: 0.2s;\n", | |
" cursor: pointer;\n", | |
"}\n", | |
".iqx-button {\n", | |
" background-color: #0f62fe; \n", | |
" color: white; \n", | |
"}\n", | |
".iqx-button:hover {\n", | |
" background-color: #0043ce;\n", | |
" color: white;\n", | |
"}\n", | |
"</style>\n", | |
"<a href=\"https://stackoverflow.com/search?q=QasmError: Cannot represent circuits with unbound parameters in OpenQASM 2.\" target='_blank'><button class='button iqx-button'>Search for solution online</button></a>\n" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"# OpenQASM 2 does not support unbound parameters\n", | |
"qc.qasm()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "5f375d01-bfbf-4578-94ca-09d992ff409f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: "Courier New",Courier,monospace\"> ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" </pre>" | |
], | |
"text/plain": [ | |
" ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" " | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# assign parameter values\n", | |
"qc.assign_parameters({p: 3.14}, inplace=True)\n", | |
"qc.draw('text')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "1c0c057e-2c8f-44c8-b621-c199303b3891", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"OPENQASM 2.0;\n", | |
"include \"qelib1.inc\";\n", | |
"qreg q[4];\n", | |
"creg meas[4];\n", | |
"sx q[0];\n", | |
"rz(3*pi) q[0];\n", | |
"rz(3.14) q[0];\n", | |
"x q[0];\n", | |
"barrier q[0],q[1],q[2],q[3];\n", | |
"measure q[0] -> meas[0];\n", | |
"measure q[1] -> meas[1];\n", | |
"measure q[2] -> meas[2];\n", | |
"measure q[3] -> meas[3];\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"# export qasm string to a variable\n", | |
"qasm_string = qc.qasm()\n", | |
"print(qasm_string)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "f7b3283e-c7cf-4027-b692-d1f5de854d69", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: "Courier New",Courier,monospace\"> ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" </pre>" | |
], | |
"text/plain": [ | |
" ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" " | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# load a quantum circuit from qasm string\n", | |
"qc_from_string = QuantumCircuit.from_qasm_str(qasm_string)\n", | |
"qc_from_string.draw('text')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "b458a058-a0d6-4dca-a37e-b42318ef2703", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# verify that the loaded circuit is the same as the original circuit\n", | |
"qc == qc_from_string" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "0487d1fe-0b5b-4b5c-aafc-f60dd4e2ef0d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# store qasm string into a file\n", | |
"with open('qasm_file', 'w') as output_file:\n", | |
" output_file.write(qasm_string)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "3aff4122-52c5-4dea-86b8-94c900fbccea", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<pre style=\"word-wrap: normal;white-space: pre;background: #fff0;line-height: 1.1;font-family: "Courier New",Courier,monospace\"> ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" </pre>" | |
], | |
"text/plain": [ | |
" ┌────┐┌────────┐┌──────────┐┌───┐ ░ ┌─┐ \n", | |
" q_0: ┤ √X ├┤ Rz(3π) ├┤ Rz(3.14) ├┤ X ├─░─┤M├─────────\n", | |
" └────┘└────────┘└──────────┘└───┘ ░ └╥┘┌─┐ \n", | |
" q_1: ──────────────────────────────────░──╫─┤M├──────\n", | |
" ░ ║ └╥┘┌─┐ \n", | |
" q_2: ──────────────────────────────────░──╫──╫─┤M├───\n", | |
" ░ ║ ║ └╥┘┌─┐\n", | |
" q_3: ──────────────────────────────────░──╫──╫──╫─┤M├\n", | |
" ░ ║ ║ ║ └╥┘\n", | |
"meas_0: ═════════════════════════════════════╩══╬══╬══╬═\n", | |
" ║ ║ ║ \n", | |
"meas_1: ════════════════════════════════════════╩══╬══╬═\n", | |
" ║ ║ \n", | |
"meas_2: ═══════════════════════════════════════════╩══╬═\n", | |
" ║ \n", | |
"meas_3: ══════════════════════════════════════════════╩═\n", | |
" " | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# load a quantum circuit from qasm file\n", | |
"qc_from_file = QuantumCircuit.from_qasm_file('qasm_file')\n", | |
"qc_from_file.draw('text')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "f4a09905-c95d-480e-a181-a1e8b9318148", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# verify that the loaded circuit is the same as the original circuit\n", | |
"qc == qc_from_file" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.8.13" | |
}, | |
"widgets": { | |
"application/vnd.jupyter.widget-state+json": { | |
"state": {}, | |
"version_major": 2, | |
"version_minor": 0 | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment