{ "cells": [ { "cell_type": "markdown", "id": "f268e562-82fd-4197-b77f-4cb66cd49716", "metadata": {}, "source": [ "# pytket-azure example usage\n", "\n", "This notebook shows how to use the pytket-azure extension to submit pytket circuits to quantinuum devices using the azure credentials.\n", "\n", "First you need to install the extension. The best way to do this is ```pip install pytket-azure```\n", "This also installs pytket if not installed already.\n", "\n" ] }, { "cell_type": "markdown", "id": "f66ab4cd-16dc-4809-9cae-9ffa4b0141f1", "metadata": {}, "source": [ "First import the Backend, it offers the needed functionality to login, send circuits and get results." ] }, { "cell_type": "code", "execution_count": 1, "id": "16f85bb6-e665-4fc0-a908-3eaf5f819591", "metadata": {}, "outputs": [], "source": [ "from pytket.extensions.azure import AzureBackend" ] }, { "cell_type": "markdown", "id": "f9af7004-a97e-48d4-bcab-ae5cf98048e1", "metadata": {}, "source": [ "The next steps is to select the device you want to use. You can find all supported devices [here](https://learn.microsoft.com/en-us/azure/quantum/provider-quantinuum?tabs=tabid-mcmr-with-q-provider%2Ctabid-arbitrary-angle-zz-gates-with-q-provider%2Ctabid-su4-with-q-provider%2Ctabid-emulator-noise-parameters-with-q-provider%2Ctabid-tket-compilation-with-q-provider)\n", "There are three groups: Syntax Checker, Emulator and devices. " ] }, { "cell_type": "markdown", "id": "a069ed6b-54c4-4394-b8a8-87aedf83f0d4", "metadata": {}, "source": [ "## Syntax Checker h1-1sc" ] }, { "cell_type": "code", "execution_count": 2, "id": "33629c9f-4b2e-4b94-b2d4-ebe0d4ac0424", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "backend_sc = AzureBackend(\"quantinuum.sim.h1-1sc\")\n", "print(backend_sc.is_available())" ] }, { "cell_type": "markdown", "id": "446ce811-12c1-4f7b-a88f-fb3110c34573", "metadata": {}, "source": [ "You can use the backend to compile your pytket circuit to the right gateset and send it to the device." ] }, { "cell_type": "code", "execution_count": 3, "id": "55f9870c-9cad-4df2-887f-edddf0b43d05", "metadata": {}, "outputs": [], "source": [ "from pytket import Circuit\n", "\n", "circ = Circuit(2, 2)\n", "circ.H(0)\n", "circ.CX(0,1)\n", "circ.Measure(0, 0)\n", "circ.Measure(1, 1)\n", "compiled_circuit = backend_sc.get_compiled_circuit(circ, 3)\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "7d5b65ea-13a7-48d5-92bd-6c6e0c190b85", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from pytket.circuit.display import render_circuit_jupyter\n", "\n", "render_circuit_jupyter(compiled_circuit)" ] }, { "cell_type": "code", "execution_count": 7, "id": "14f491b6-fdb0-4a83-85fe-408a04b748bf", "metadata": {}, "outputs": [], "source": [ "handle = backend_sc.process_circuit(compiled_circuit, n_shots=1000)\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "462532bf-a831-4747-b52d-313ae35edb6a", "metadata": {}, "outputs": [], "source": [ "result = backend_sc.get_result(handle, timeout=120)" ] }, { "cell_type": "markdown", "id": "1399280b-d9c6-437c-b2de-b18a985c2711", "metadata": {}, "source": [ "The result of the Syntax checker will always be 0 for all measured bits. If there are any issues with the circuit it will return an error." ] }, { "cell_type": "code", "execution_count": 9, "id": "8d1b535e-cb6b-4245-9b97-4126f8d6a31f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({(0, 0): 1000})\n" ] } ], "source": [ "print(result.get_counts())" ] }, { "cell_type": "markdown", "id": "16ab261d-9b3d-4dad-99bd-5fccdf22d0ea", "metadata": {}, "source": [ "Submitting the circuit to the emulator or device works in the same way, which will also give the results of the run:\n" ] }, { "cell_type": "markdown", "id": "45115bb8-0cb1-427e-b566-2160d601920a", "metadata": {}, "source": [ "## Emulator h1-1e" ] }, { "cell_type": "code", "execution_count": 10, "id": "70ea4e0f-924a-4b7d-b310-c8b948c22578", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "backend_e = AzureBackend(\"quantinuum.sim.h1-1e\")\n", "print(backend_e.is_available())" ] }, { "cell_type": "code", "execution_count": 11, "id": "62566b2e-beac-42df-883e-48f5e86a628c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "............" ] } ], "source": [ "from pytket import Circuit\n", "\n", "circ = Circuit(2, 2)\n", "circ.H(0)\n", "circ.CX(0,1)\n", "circ.Measure(0, 0)\n", "circ.Measure(1, 1)\n", "compiled_circuit = backend_e.get_compiled_circuit(circ, 0)\n", "handle = backend_e.process_circuit(compiled_circuit, n_shots=1000)\n", "result = backend_e.get_result(handle, timeout=120)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "a94bc65d-bd22-429b-a87c-64b495767009", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({(0, 0): 497, (1, 1): 497, (1, 0): 4, (0, 1): 2})\n" ] } ], "source": [ "print(result.get_counts())" ] }, { "cell_type": "code", "execution_count": null, "id": "a9c24e60-ad23-4df1-90c6-f90d813283d9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "240fbf49-f7b9-4d1b-ac9d-67c634e47ae4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ae329524-b69e-41a8-8d7d-80cd4245c76a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }