{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Qiskit QPUs on the QLM\n", "Here is an exemple of how the Qiskit binders could be used to run a QLM circuit by using the IBM Quantum Experience." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing a circuit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from qat.lang.AQASM import Program, H\n", "\n", "prog = Program()\n", "reg = prog.qalloc(1)\n", "prog.apply(H, reg[0])\n", "\n", "job = prog.to_circ().to_job(nbshots=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Selecting an IBM QPU\n", "An IBMQ token needs to be provided in order to run any circuit on the IBMQ backends. [Creating an account](https://quantum-computing.ibm.com/) is very straightforward. This will allow you to copy your token and check the available backends." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "MY_IBM_TOKEN = \"...\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from qat.interop.qiskit import BackendToQPU\n", "from qiskit import IBMQ\n", "\n", "# we can select a backend from available IBMQ backends\n", "# here we use the backend ibmq_armonk, it should not be too busy since it only has 1 qubit\n", "qpu = BackendToQPU(token=MY_IBM_TOKEN, ibmq_backend=\"ibmq_armonk\")\n", "print(\"Selected backend:\", qpu.backend)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running the job synchronously" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "results = qpu.submit(job) # may take time as it is waiting for the backend to get the results\n", "print(results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running the job asynchronously\n", "For running the job asynchronously, AsyncBackendToQPU shall be used." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time\n", "from qat.interop.qiskit import AsyncBackendToQPU\n", "\n", "async_qpu = AsyncBackendToQPU(token=MY_IBM_TOKEN, ibmq_backend=\"ibmq_armonk\")\n", "\n", "# here we get a QiskitJob object when submitting our QLM Job\n", "qiskit_job = async_qpu.submit(job)\n", "print(qiskit_job.status())\n", "\n", "while not qiskit_job.result():\n", " # waiting for the job to be finished\n", " time.sleep(1)\n", "\n", "print(qiskit_job.status())\n", "results = qiskit_job.result()\n", "print(results)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The perk of using an asynchronous job is to be able to store it and check its results later, this is accomplished by using the functions `QiskitJob.dump()` and `AsyncBackendToQPU.retrieve_job()`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "qiskit_job = async_qpu.submit(job)\n", "\n", "# job dumped\n", "qiskit_job.dump('my_file')\n", "\n", "# in order to retrieve the job, an AsyncBackendToQPU instance similar to the one used for submitting\n", "# the QLM Job in the first place is needed\n", "my_new_async_qpu = AsyncBackendToQPU(token=MY_IBM_TOKEN, ibmq_backend=\"ibmq_armonk\")\n", "retrieved_job = my_new_async_qpu.retrieve_job('my_file')\n", "\n", "print(\"Original job's ID: \", qiskit_job.job_id(), \"\\nRetrieved job's ID:\", retrieved_job.job_id())" ] } ], "metadata": { "authors": [ "Arnaud Gazda", "Thomas Ayral" ], "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 }