{ "cells": [ { "cell_type": "markdown", "metadata": { "qat-category": "1-Circuit generation in Python", "qat-summary": "1-Creation of an EPR pair using two qubits" }, "source": [ "# Creation of an EPR pair using two qubits\n", "\n", "\n", " \n", "Let us start with a simple circuit: the creation of an EPR pair using two qubits.\n", " \n", " \n", "First, we need to import relevant objects from the python AQASM module:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:48.632336Z", "iopub.status.busy": "2022-11-05T19:41:48.632040Z", "iopub.status.idle": "2022-11-05T19:41:48.763181Z", "shell.execute_reply": "2022-11-05T19:41:48.762470Z" } }, "outputs": [], "source": [ "from qat.lang.AQASM import Program, H, CNOT " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creation of the quantum program\n", "\n", "Then, we can declare a new object ``Program``. Let us give it an explicit name:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:48.767071Z", "iopub.status.busy": "2022-11-05T19:41:48.766669Z", "iopub.status.idle": "2022-11-05T19:41:48.769921Z", "shell.execute_reply": "2022-11-05T19:41:48.769313Z" } }, "outputs": [], "source": [ "epr_prog = Program() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create our EPR pair, we need to manipulate two qbits. \n", "Qbits are manipulated through qbit registers only (to keep things structured).\n", "Registers are allocated as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:48.772798Z", "iopub.status.busy": "2022-11-05T19:41:48.772534Z", "iopub.status.idle": "2022-11-05T19:41:48.775649Z", "shell.execute_reply": "2022-11-05T19:41:48.775081Z" } }, "outputs": [], "source": [ "qbits = epr_prog.qalloc(2) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can access our qbits using the register \"qbits\".\n", "\n", "Registers behave like python list/arrays.\n", "\n", "Here our qbits will be refered to using qbits[0] and qbits[1].\n", "\n", "To create our EPR pair, we simply implement the appropriate 2-qbit rotation using a Hadamard gate (H) on the first qbit, followed by a controlled NOT gate (CNOT) on both qbits:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:48.778271Z", "iopub.status.busy": "2022-11-05T19:41:48.777950Z", "iopub.status.idle": "2022-11-05T19:41:48.780919Z", "shell.execute_reply": "2022-11-05T19:41:48.780351Z" } }, "outputs": [], "source": [ "epr_prog.apply(H, qbits[0]) \n", "epr_prog.apply(CNOT, qbits)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that since the CNOT is applied on both qbits (it is a 2-qbit gate), we can pass the whole register as argument to the ``.apply`` method." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The corresponding circuit object can be extracted directly from the Program object as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:48.783686Z", "iopub.status.busy": "2022-11-05T19:41:48.783442Z", "iopub.status.idle": "2022-11-05T19:41:49.085477Z", "shell.execute_reply": "2022-11-05T19:41:49.084721Z" } }, "outputs": [], "source": [ "circuit = epr_prog.to_circ()\n", "circuit.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulation of the execution of the circuit\n", "Now that we have a proper circuit, we can try and simulate it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:49.124025Z", "iopub.status.busy": "2022-11-05T19:41:49.123578Z", "iopub.status.idle": "2022-11-05T19:41:49.140931Z", "shell.execute_reply": "2022-11-05T19:41:49.140242Z" } }, "outputs": [], "source": [ "#Let us import some qpu connected to a classical linear algebra simulator\n", "from qat.qpus import PyLinalg\n", "qpu = PyLinalg()\n", "\n", "job = circuit.to_job()\n", "\n", "result = qpu.submit(job)\n", "for sample in result:\n", " print(\"State\", sample.state, \"with amplitude\", sample.amplitude)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export to Atos Quantum Assembly Language (AQASM) format\n", "We can also export our circuit in the AQASM format:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:49.143796Z", "iopub.status.busy": "2022-11-05T19:41:49.143544Z", "iopub.status.idle": "2022-11-05T19:41:49.147902Z", "shell.execute_reply": "2022-11-05T19:41:49.147312Z" } }, "outputs": [], "source": [ "epr_prog.export(\"helloworld.aqasm\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The generated file *helloworld.aqasm* should look like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:49.150529Z", "iopub.status.busy": "2022-11-05T19:41:49.150282Z", "iopub.status.idle": "2022-11-05T19:41:49.423359Z", "shell.execute_reply": "2022-11-05T19:41:49.422530Z" } }, "outputs": [], "source": [ "!cat helloworld.aqasm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and can be compiled to circ format as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2022-11-05T19:41:49.426835Z", "iopub.status.busy": "2022-11-05T19:41:49.426523Z", "iopub.status.idle": "2022-11-05T19:41:49.921905Z", "shell.execute_reply": "2022-11-05T19:41:49.921010Z" } }, "outputs": [], "source": [ "!aqasm2circ helloworld.aqasm" ] }, { "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 }