{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "5ad2486b", "metadata": { "nbsphinx": { "execute": "never" } }, "source": [ "# Braket via Nexus\n", "\n", "Here you can find some examples and explanations on running quantum circuits on [AWS Braket](https://aws.amazon.com/braket/) via Nexus. \n", "\n", "These will require that you have set Braket credentials via Settings->Linked Accounts on the Nexus website." ] }, { "cell_type": "code", "execution_count": null, "id": "7234def9", "metadata": {}, "outputs": [], "source": [ "from datetime import datetime\n", "from pytket import Circuit\n", "\n", "import qnexus as qnx" ] }, { "attachments": {}, "cell_type": "markdown", "id": "71767db3", "metadata": {}, "source": [ "List available Braket devices, optionally specifying a specific AWS region (by default devices for ALL regions will be returned)." ] }, { "cell_type": "code", "execution_count": null, "id": "4b7f0579", "metadata": {}, "outputs": [], "source": [ "qnx.devices.get_all(\n", " issuers=[qnx.devices.IssuerEnum.BRAKET]\n", ").df()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "63794f17", "metadata": {}, "source": [ "To configure Nexus to target Braket, you'll need to provide the following arguments to the BraketConfig.\n", "\n", "- `local`: `bool`\n", " - if true then the circuit will run as a simulation in the Nexus cloud.\n", "- `device_type`: `str`\n", " - device type from device ARN (e.g. `\"qpu\"`, `\"quantum-simulator\"`)\n", "- `provider`: `str`\n", " - provider name from device ARN (e.g. `\"ionq\"`, `\"rigetti\"`, `\"oqc\"`, `\"amazon\"`, ...)\n", "- `device`: `str`\n", " - device name from device ARN (e.g. `\"ionQdevice\"`, `\"Aspen-8\"`, `\"sv1\"`, ...)\n", "- `s3_bucket`: `str`\n", " - name of S3 bucket to store results\n", "- `s3_folder`: `str`\n", " - name of folder (`\"key\"`) in S3 bucket to store results in" ] }, { "cell_type": "code", "execution_count": null, "id": "aaef7952", "metadata": {}, "outputs": [], "source": [ "braket_config = qnx.BraketConfig(\n", " local=True,\n", " device_type=\"quantum-simulator\",\n", " provider=\"amazon\",\n", " device=\"sv1\",\n", " s3_bucket=\"\",\n", " s3_folder=\"\",\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "068ec022", "metadata": {}, "source": [ "## Example of compiling and executing a circuit on AWS Braket" ] }, { "cell_type": "code", "execution_count": null, "id": "9c6f87a8", "metadata": {}, "outputs": [], "source": [ "my_project_ref = qnx.projects.get_or_create(name=\"My AWS Braket 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 AWS Braket Circuit from {datetime.now()}\",\n", " circuit = circuit,\n", " project = my_project_ref,\n", ")\n", "\n", "compiled_circuits = qnx.compile(\n", " circuits=[my_circuit_ref],\n", " name=f\"My Compile Job from {datetime.now()}\",\n", " optimisation_level=1,\n", " backend_config=braket_config,\n", " project=my_project_ref,\n", ")\n", "\n", "compiled_circuits.df()\n", "\n", "execute_job_ref = qnx.start_execute_job(\n", " circuits=compiled_circuits,\n", " name=f\"My Execute Job from {datetime.now()}\",\n", " n_shots=[100],\n", " backend_config=braket_config,\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": "pytket-myqos-f6eBn_pg-py3.10", "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.10" }, "nbsphinx": { "execute": "never" }, "vscode": { "interpreter": { "hash": "f43baf218a87c34b36d0e78bf5bbda01518be954b242b9492c4c47b42fef4ee3" } } }, "nbformat": 4, "nbformat_minor": 5 }