{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "d60a276e", "metadata": { "collapsed": false }, "source": [ "# IBMQ via Nexus\n", "\n", "Here you can find some examples and explanations on using IBMQ via Nexus. These will require that you have set IBMQ credentials via Settings->Linked Accounts on the Nexus website.\n", "\n", "There are two available configurations:\n", "\n", "- `IBMQConfig` - for submitting projects to the IBMQ system\n", "- `IBMQEmulatorConfig` - for submitting to an emulation of an IBMQ device (running on an `AerBackend` based off sampled device characteristics), but running in a Nexus server." ] }, { "cell_type": "code", "execution_count": 10, "id": "e85549e3", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from pytket import Circuit\n", "\n", "import qnexus as qnx" ] }, { "cell_type": "code", "execution_count": null, "id": "750f8bfe", "metadata": {}, "outputs": [], "source": [ "# See the names of the IBMQ devices available to you by default\n", "qnx.devices.get_all(\n", " issuers=[qnx.devices.IssuerEnum.IBMQ]\n", ").df()" ] }, { "cell_type": "code", "execution_count": null, "id": "57fbc895", "metadata": {}, "outputs": [], "source": [ "# Alternatively if you would like to list devices available to you through a particular IBMQ plan, you can specify the hub, group and project\n", "qnx.devices.get_all(\n", " issuers=[qnx.devices.IssuerEnum.IBMQ],\n", " ibmq_hub=\"my-hub\", \n", " ibmq_group=\"my-group\", \n", " ibmq_project=\"my-project\"\n", ").df()" ] }, { "cell_type": "markdown", "id": "ece09f03", "metadata": {}, "source": [ "## IBMQConfig\n", "\n", "To use Nexus to run projects on IBMQ, you'll need to specify some configuration in a `IBMQConfig`.\n", "\n", "The arguments you can provide to this are:\n", "\n", "- `backend_name` (see Available Devices below)\n", "- `hub` - The IBMQ hub you are running under\n", "- `group` - The IBMQ group\n", "- `project` - The IBMQ project\n", "- `monitor` - Boolean specifying if you want to use the IBM job monitor. Defaults to True." ] }, { "cell_type": "code", "execution_count": 5, "id": "50a48df2", "metadata": {}, "outputs": [], "source": [ "# An example configuration specifying the 127 qubit ibm_kyiv machine under the open plan\n", "ibm_kyiv_configuration = qnx.IBMQConfig(\n", " backend_name=\"ibm_kyiv\", hub=\"ibm-q\", group=\"open\", project=\"main\"\n", ")\n", "\n", "# Use this configuration when submitting compile or execute jobs" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ac538cc4", "metadata": {}, "source": [ "## The IBMQ Emulator\n", "\n", "To use Nexus to run projects on the IBMQ emulator, you'll need to specify some configuration in a `IBMQEmulatorConfig`.\n", "\n", "The arguments you can provide to this are:\n", "\n", "- `backend_name` - You can specify any available IBMQ backend\n", "- `hub` - The IBMQ hub you are running under\n", "- `group` - The IBMQ group\n", "- `project` - The IBMQ project" ] }, { "cell_type": "code", "execution_count": 6, "id": "19750eac", "metadata": {}, "outputs": [], "source": [ "# An example configuration specifying the 5 qubit ibm_nairobi machine (no longer available)\n", "ibm_nairobi_emulator_configuration = qnx.IBMQEmulatorConfig(\n", " backend_name=\"ibm_nairobi\", hub=\"ibm-q\", group=\"open\", project=\"main\"\n", ")\n", "\n", "# Use this configuration when submitting compile or execute jobs" ] }, { "cell_type": "markdown", "id": "ddec4c84", "metadata": {}, "source": [ "## Example of executing a circuit on ibm_kyiv" ] }, { "cell_type": "code", "execution_count": null, "id": "c7228f69", "metadata": {}, "outputs": [], "source": [ "my_project_ref = qnx.projects.get_or_create(name=\"My IBM Kyiv Project\")\n", "\n", "circuit = Circuit(2).H(0).CX(0,1).measure_all()\n", "\n", "my_circuit_ref = qnx.circuits.upload(\n", " name=f\"My IBM Kyiv Circuit from {datetime.now()}\",\n", " circuit = circuit,\n", " project = my_project_ref,\n", ")\n", "\n", "execute_job_ref = qnx.start_execute_job(\n", " circuits=[my_circuit_ref],\n", " name=f\"My Execute Job from {datetime.now()}\",\n", " n_shots=[100],\n", " backend_config=ibm_kyiv_configuration,\n", " project=my_project_ref,\n", ")\n", "\n", "qnx.jobs.wait_for(execute_job_ref)\n", "\n", "\n", "# Retrieve a ExecutionResultRef for every Circuit that was executed\n", "execute_job_result_refs = qnx.jobs.results(execute_job_ref)\n", "\n", "# Get a pytket BackendResult for the execution\n", "result = execute_job_result_refs[0].download_result()\n", "\n", "result.get_counts()" ] } ], "metadata": { "kernelspec": { "display_name": "qnexus-Rou6F43i-py3.12", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }