microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/python_interop/pennylane_submission_to_azure.ipynb
201lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "90f546a9", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# Submitting PennyLane Circuits to Azure Quantum with the QDK\n", |
| 9 | "\n", |
| 10 | "This notebook demonstrates end‑to‑end submission of a PennyLane `QNode` to Azure Quantum using the QDK compiler pipeline (OpenQASM 2 → QIR → job submission).\n", |
| 11 | "\n", |
| 12 | "**Workflow overview:**\n", |
| 13 | "\n", |
| 14 | "1. Create a `default.qubit` device and define a parameterized circuit (Hadamard + CNOT entanglement, then `RY(θ)` rotation).\n", |
| 15 | "2. Draw the circuit for a quick structural sanity check.\n", |
| 16 | "3. Export the bound PennyLane circuit to OpenQASM 2 using `qml.to_openqasm`.\n", |
| 17 | "4. Compile the OpenQASM source to QIR with `qdk.openqasm.compile` (selecting a target profile).\n", |
| 18 | "5. Submit the QIR payload to a chosen Azure Quantum target (e.g. `rigetti.sim.qvm`) with a shot count.\n", |
| 19 | "6. Retrieve and display measurement results returned by the job." |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "cell_type": "markdown", |
| 24 | "id": "004e5982", |
| 25 | "metadata": {}, |
| 26 | "source": [ |
| 27 | "## 1. Prerequisites\n", |
| 28 | "\n", |
| 29 | "This notebook assumes the `qdk`, Azure Quantum integration, and PennyLane packages are installed. You can install these with:" |
| 30 | ] |
| 31 | }, |
| 32 | { |
| 33 | "cell_type": "code", |
| 34 | "execution_count": null, |
| 35 | "id": "dfa160e8", |
| 36 | "metadata": {}, |
| 37 | "outputs": [], |
| 38 | "source": [ |
| 39 | "%pip install \"qdk[azure]\" pennylane" |
| 40 | ] |
| 41 | }, |
| 42 | { |
| 43 | "cell_type": "markdown", |
| 44 | "id": "b7f37bcd", |
| 45 | "metadata": {}, |
| 46 | "source": [ |
| 47 | "This installs:\n", |
| 48 | "- The base qdk package (compiler, OpenQASM/QIR tooling)\n", |
| 49 | "- Azure Quantum client dependencies for submission\n", |
| 50 | "- The PennyLane package\n", |
| 51 | "\n", |
| 52 | "After installing, restart the notebook kernel if it was already running. The following imports should run successfully:" |
| 53 | ] |
| 54 | }, |
| 55 | { |
| 56 | "cell_type": "code", |
| 57 | "execution_count": null, |
| 58 | "id": "f7446ed1", |
| 59 | "metadata": {}, |
| 60 | "outputs": [], |
| 61 | "source": [ |
| 62 | "import pennylane as qml\n", |
| 63 | "from qdk.openqasm import compile\n", |
| 64 | "from qdk.azure import Workspace\n", |
| 65 | "from qdk import TargetProfile" |
| 66 | ] |
| 67 | }, |
| 68 | { |
| 69 | "cell_type": "markdown", |
| 70 | "id": "4f761649", |
| 71 | "metadata": {}, |
| 72 | "source": [ |
| 73 | "## 2. Define a Simple Circuit\n", |
| 74 | "We'll create a small parameterized circuit destined for export." |
| 75 | ] |
| 76 | }, |
| 77 | { |
| 78 | "cell_type": "code", |
| 79 | "execution_count": null, |
| 80 | "id": "e91dd2d7", |
| 81 | "metadata": {}, |
| 82 | "outputs": [], |
| 83 | "source": [ |
| 84 | "dev = qml.device('default.qubit', wires=2)\n", |
| 85 | "\n", |
| 86 | "@qml.qnode(dev)\n", |
| 87 | "def small_ansatz(theta):\n", |
| 88 | " qml.H(0); qml.CNOT(wires=[0,1])\n", |
| 89 | " qml.RY(theta, wires=1)\n", |
| 90 | " return qml.expval(qml.PauliZ(1))" |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | "cell_type": "markdown", |
| 95 | "id": "f94b7ac3", |
| 96 | "metadata": {}, |
| 97 | "source": [ |
| 98 | "## 3. Draw for Inspection" |
| 99 | ] |
| 100 | }, |
| 101 | { |
| 102 | "cell_type": "code", |
| 103 | "execution_count": null, |
| 104 | "id": "2706bb1d", |
| 105 | "metadata": {}, |
| 106 | "outputs": [], |
| 107 | "source": [ |
| 108 | "print(qml.draw(small_ansatz)(0.3))" |
| 109 | ] |
| 110 | }, |
| 111 | { |
| 112 | "cell_type": "markdown", |
| 113 | "id": "efd50528", |
| 114 | "metadata": {}, |
| 115 | "source": [ |
| 116 | "## 4. Configure Azure Quantum workspace connection\n", |
| 117 | "\n", |
| 118 | "To connect to an Azure workspace replace the following variables with your own values." |
| 119 | ] |
| 120 | }, |
| 121 | { |
| 122 | "cell_type": "code", |
| 123 | "execution_count": null, |
| 124 | "id": "54206752", |
| 125 | "metadata": {}, |
| 126 | "outputs": [], |
| 127 | "source": [ |
| 128 | "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n", |
| 129 | "resource_group = 'myresourcegroup'\n", |
| 130 | "workspace_name = 'myworkspace'\n", |
| 131 | "location = 'westus'" |
| 132 | ] |
| 133 | }, |
| 134 | { |
| 135 | "cell_type": "markdown", |
| 136 | "id": "552ec2b0", |
| 137 | "metadata": {}, |
| 138 | "source": [ |
| 139 | "## 5. Submitting the Circuit" |
| 140 | ] |
| 141 | }, |
| 142 | { |
| 143 | "cell_type": "code", |
| 144 | "execution_count": null, |
| 145 | "id": "d9a0f59b", |
| 146 | "metadata": {}, |
| 147 | "outputs": [], |
| 148 | "source": [ |
| 149 | "# Define parameters for the job submission\n", |
| 150 | "target_name = 'rigetti.sim.qvm'\n", |
| 151 | "target_profile = TargetProfile.Base\n", |
| 152 | "job_name = 'pennylane-job'\n", |
| 153 | "shots = 100\n", |
| 154 | "\n", |
| 155 | "# Define parameter for the circuit\n", |
| 156 | "theta = 0.3\n", |
| 157 | "\n", |
| 158 | "# Specify Azure Quantum workspace\n", |
| 159 | "workspace = Workspace(\n", |
| 160 | " subscription_id=subscription_id,\n", |
| 161 | " resource_group=resource_group,\n", |
| 162 | " name=workspace_name,\n", |
| 163 | " location=location,\n", |
| 164 | ")\n", |
| 165 | "\n", |
| 166 | "# Compile the PennyLane circuit to QIR via OpenQASM\n", |
| 167 | "qasm_str = qml.to_openqasm(small_ansatz)(theta)\n", |
| 168 | "qir = compile(qasm_str, target_profile)\n", |
| 169 | "\n", |
| 170 | "# Submit the job to Azure Quantum\n", |
| 171 | "target = workspace.get_targets(target_name)\n", |
| 172 | "job = target.submit(qir, job_name, shots)\n", |
| 173 | "\n", |
| 174 | "# Print the results\n", |
| 175 | "results = job.get_results()\n", |
| 176 | "print(results)" |
| 177 | ] |
| 178 | } |
| 179 | ], |
| 180 | "metadata": { |
| 181 | "kernelspec": { |
| 182 | "display_name": ".venv", |
| 183 | "language": "python", |
| 184 | "name": "python3" |
| 185 | }, |
| 186 | "language_info": { |
| 187 | "codemirror_mode": { |
| 188 | "name": "ipython", |
| 189 | "version": 3 |
| 190 | }, |
| 191 | "file_extension": ".py", |
| 192 | "mimetype": "text/x-python", |
| 193 | "name": "python", |
| 194 | "nbconvert_exporter": "python", |
| 195 | "pygments_lexer": "ipython3", |
| 196 | "version": "3.13.8" |
| 197 | } |
| 198 | }, |
| 199 | "nbformat": 4, |
| 200 | "nbformat_minor": 5 |
| 201 | } |