{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The circuit inliner plugin\n", "\n", "By default, circuits constructed by the pyAQASM library have a somewhat compressed structure.\n", "\n", "Circuits consists in a main body, and each gate might have a subcircuit implementation that recursively describes the flow of the circuit.\n", "\n", "Lets consider, for instance, a QFT adder:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we define a simple function that prints the instructions in the main body of a circuit:\n", "def print_main(circuit):\n", " from qat.core.util import extract_syntax\n", " for op in circuit.ops:\n", " print(extract_syntax(circuit.gateDic[op.gate], circuit.gateDic), op.qbits)\n", " \n", "from qat.lang.AQASM import *\n", "from qat.lang.AQASM.qftarith import add\n", "\n", "prog = Program()\n", "qbits = prog.qalloc(4)\n", "prog.apply(add(2, 2), qbits)\n", "circuit = prog.to_circ()\n", "\n", "print_main(circuit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is not very explicit.\n", "In fact, if one iterates over the circuit, we can see that the circuit contains many more instructions:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "for op in circuit.iterate_simple():\n", " print(op)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happened here is that the iterator `iterate_simple` emulated an execution stack when iterating over the circuit and unfolded the full circuit.\n", "\n", "Equivalently, one could simply ask the `to_circ` method to unfold all the circuit:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circuit_full = prog.to_circ(inline=True)\n", "print_main(circuit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`CircuitInliner` is a plugin that performs this inlining process in the stack. It should be used in case some other plugins requires an inlined circuit to run properly." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from qat.plugins import CircuitInliner\n", "from qat.core import Batch\n", "plugin = CircuitInliner()\n", "print(\"==== Before 'compile'\")\n", "print_main(circuit)\n", "batch = Batch(jobs=[circuit.to_job()])\n", "batch = plugin.compile(batch, None)\n", "print(\"==== After 'compile'\")\n", "print_main(circuit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the circuit was modified in place. This behavior can be changed via the 'inplace' parameter:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "circuit = prog.to_circ()\n", "plugin = CircuitInliner(inplace=False)\n", "print(\"==== Before 'compile'\")\n", "print_main(circuit)\n", "batch = Batch(jobs=[circuit.to_job()])\n", "batch = plugin.compile(batch, None)\n", "print(\"==== After 'compile'\")\n", "print_main(circuit)\n", "print(\"==== output:\")\n", "print_main(batch.jobs[0].circuit)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "authors": [ "Simon Martiel" ], "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 }