{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Openqasm Tutorials\n", "\n", "An openqasm compiler called `oqasm2circ` that compiles openqasm code into a QLM circuit, then dumps it in a file.\n", "\n", "\n", "A few examples are available, to compile an openqasm `.qasm` file use the following command:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!oqasm2circ --help" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%writefile inverseqft1.qasm\n", "// QFT and measure, version 1\n", "OPENQASM 2.0;\n", "include \"qelib1.inc\";\n", "qreg q[4];\n", "creg c[4];\n", "h q;\n", "barrier q;\n", "h q[0];\n", "measure q[0] -> c[0];\n", "if(c==1) u1(pi/2) q[1];\n", "h q[1];\n", "measure q[1] -> c[1];\n", "if(c==1) u1(pi/4) q[2];\n", "if(c==2) u1(pi/2) q[2];\n", "if(c==3) u1(pi/2+pi/4) q[2];\n", "h q[2];\n", "measure q[2] -> c[2];\n", "if(c==1) u1(pi/8) q[3];\n", "if(c==2) u1(pi/4) q[3];\n", "if(c==3) u1(pi/4+pi/8) q[3];\n", "if(c==4) u1(pi/2) q[3];\n", "if(c==5) u1(pi/2+pi/8) q[3];\n", "if(c==6) u1(pi/2+pi/4) q[3];\n", "if(c==7) u1(pi/2+pi/4+pi/8) q[3];\n", "h q[3];\n", "measure q[3] -> c[3];" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "!oqasm2circ inverseqft1.qasm inverseqft1.circ" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Recovering circuits directly\n", "\n", "It is also possible to use the parser's commands to directly parse a string and recover the QLM circuit object in python for\n", "further use. \n", "\n", "Note: This however doesn't thoroughly process _include_ statements, because those are done with the binary.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.interop.openqasm.qasm_parser import OqasmParser\n", "\n", "# We will use the adder openqasm example from github\n", "\n", "file = open(\"inverseqft1.qasm\")\n", "data = file.read()\n", "\n", "# Building our parser\n", "\n", "oq_parser = OqasmParser()\n", "#oq_parser.build()\n", "\n", "# Parsing\n", "\n", "#oq_parser.parse(data)\n", "\n", "qlm_circuit = oq_parser.compile(data)\n", "\n", "from qat.core.util import get_syntax\n", "\n", "for index, op in enumerate(qlm_circuit.ops):\n", " print(\"Gate {} with params {} on qubits {} and cbits {}\".format(*get_syntax(qlm_circuit, index),\n", " op.qbits, op.cbits))\n" ] } ], "metadata": { "authors": [ "Simon Martiel", "Arnaud Gazda" ], "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.12.11" } }, "nbformat": 4, "nbformat_minor": 2 }