{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Writing a basic quantum program\n", "\n", "In this notebook, you will learn how to write a quantum program in Python on the Quantum Learning Machine.\n", "\n", "\n", "In the QLM python framework, a quantum program is contained in a ``Program`` class. It comes with methods to allocate quantum and classical registers, apply gates, measures and resets. A given instance of a ``Program`` can then be converted to a quantum ``Circuit``, which is the object that can be fed to a quantum processor.\n", "\n", "### Initializaton of a Quantum Program\n", "\n", "In the following snippet, we instantiate a quantum program object called ``prog``:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:42:32.056309Z", "iopub.status.busy": "2022-11-05T19:42:32.055883Z", "iopub.status.idle": "2022-11-05T19:42:32.191421Z", "shell.execute_reply": "2022-11-05T19:42:32.190775Z" } }, "outputs": [], "source": [ "from qat.lang.AQASM import Program\n", "\n", "prog = Program()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Quantum bit allocation\n", "\n", "The allocation of quantum bits is done via the ``qalloc`` method. It returns a register, which will then be used to refer to the qubits.\n", "\n", "In the following snippet, we allocated an 8-qubit register:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:42:32.195176Z", "iopub.status.busy": "2022-11-05T19:42:32.194830Z", "iopub.status.idle": "2022-11-05T19:42:32.198049Z", "shell.execute_reply": "2022-11-05T19:42:32.197546Z" } }, "outputs": [], "source": [ "qbits = prog.qalloc(8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Applying quantum gates\n", "\n", "The application of quantum gates is carried out through the ``apply`` method.\n", "\n", "Here, we apply standard Hadamard gate on qbit 0, a Pauli X gate on qubit 1, a CNOT gate on qubits 1 and 5, and a phase gate with angle $\\pi/6$ to qubit 5." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:42:32.200897Z", "iopub.status.busy": "2022-11-05T19:42:32.200517Z", "iopub.status.idle": "2022-11-05T19:42:32.204087Z", "shell.execute_reply": "2022-11-05T19:42:32.203628Z" } }, "outputs": [], "source": [ "from qat.lang.AQASM import H, X, CNOT, PH\n", "from math import pi\n", "\n", "prog.apply(H, qbits[0])\n", "prog.apply(X, qbits[1])\n", "prog.apply(CNOT, qbits[1], qbits[5])\n", "prog.apply(PH(pi/6), qbits[5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generation of a quantum circuit\n", "\n", "Before we describe other common operations such as measurements, let us introduce the final step that allows to generate a quantum-simulation-ready quantum circuit out of the quantum program. \n", "\n", "This generation is done via the ``to_circ`` method: " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:42:32.206616Z", "iopub.status.busy": "2022-11-05T19:42:32.206375Z", "iopub.status.idle": "2022-11-05T19:42:32.213696Z", "shell.execute_reply": "2022-11-05T19:42:32.213217Z" } }, "outputs": [], "source": [ "circ = prog.to_circ()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Circuit display\n", "The ``.display`` method outputs a graphical representation of the quantum circuit:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:42:32.216298Z", "iopub.status.busy": "2022-11-05T19:42:32.216051Z", "iopub.status.idle": "2022-11-05T19:42:32.583511Z", "shell.execute_reply": "2022-11-05T19:42:32.582882Z" } }, "outputs": [], "source": [ "circ.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a more comprehensive reference on the Python AQASM library, including **measures, classical control, custom gates, etc.**, check out [this tutorial](py_aqasm.ipynb).\n", "You will find a list of all available gates [here](available_gates.ipynb)." ] }, { "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.10.4" } }, "nbformat": 4, "nbformat_minor": 1 }