microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/python_interop/submit_qiskit_circuit_to_azure.ipynb
253lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "90f546a9", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# Submitting Qiskit Circuits to Azure Quantum with the QDK\n", |
| 9 | "\n", |
| 10 | "This notebook shows how to take an arbitrary Qiskit `QuantumCircuit`, compile it to QIR using the Microsoft Quantum Development Kit (QDK) Python APIs, and submit it as a job to an Azure Quantum target. It also demonstrates how to handle parameterized circuits by binding parameters before submission." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "markdown", |
| 15 | "id": "2b838c23", |
| 16 | "metadata": {}, |
| 17 | "source": [ |
| 18 | "The workflow demonstrated here:\n", |
| 19 | "\n", |
| 20 | "1. Build (or load) a Qiskit `QuantumCircuit`.\n", |
| 21 | "2. Convert it to OpenQASM 3 text (`qiskit.qasm3.dumps`).\n", |
| 22 | "3. Compile that OpenQASM 3 source to QIR with `qdk.openqasm.compile`.\n", |
| 23 | "4. Create (or attach to) an Azure Quantum workspace with `qdk.azure.Workspace`.\n", |
| 24 | "5. Select a target (e.g. a simulator such as `rigetti.sim.qvm`).\n", |
| 25 | "6. Submit the QIR payload and retrieve measurement results.\n", |
| 26 | "\n", |
| 27 | "Why use the QDK in this flow?\n", |
| 28 | "\n", |
| 29 | "- Local compilation: QIR generation happens locally—no external CLI needed.\n", |
| 30 | "- Consistency: The same compiler stack powers Q#, OpenQASM, and (via translation) Qiskit interoperability.\n", |
| 31 | "\n", |
| 32 | "This notebook focuses strictly on submission. For local resource estimation of Qiskit circuits, see the separate estimation sample." |
| 33 | ] |
| 34 | }, |
| 35 | { |
| 36 | "cell_type": "markdown", |
| 37 | "id": "004e5982", |
| 38 | "metadata": {}, |
| 39 | "source": [ |
| 40 | "## Prerequisites\n", |
| 41 | "\n", |
| 42 | "This notebook assumes the `qdk`, Azure Quantum integration, and Qiskit packages are installed. You can install everything (including the Azure + Qiskit extras) with:" |
| 43 | ] |
| 44 | }, |
| 45 | { |
| 46 | "cell_type": "code", |
| 47 | "execution_count": null, |
| 48 | "id": "dfa160e8", |
| 49 | "metadata": {}, |
| 50 | "outputs": [], |
| 51 | "source": [ |
| 52 | "%pip install qdk[azure,qiskit]" |
| 53 | ] |
| 54 | }, |
| 55 | { |
| 56 | "cell_type": "markdown", |
| 57 | "id": "b7f37bcd", |
| 58 | "metadata": {}, |
| 59 | "source": [ |
| 60 | "This installs:\n", |
| 61 | "- The base qdk package (compiler, OpenQASM/QIR tooling)\n", |
| 62 | "- Azure Quantum client dependencies for submission\n", |
| 63 | "- Qiskit for circuit construction\n", |
| 64 | "\n", |
| 65 | "After installing, restart the notebook kernel if it was already running. You can verify installation with:" |
| 66 | ] |
| 67 | }, |
| 68 | { |
| 69 | "cell_type": "code", |
| 70 | "execution_count": null, |
| 71 | "id": "f7446ed1", |
| 72 | "metadata": {}, |
| 73 | "outputs": [], |
| 74 | "source": [ |
| 75 | "import qiskit, qdk, qdk.azure # should import without errors" |
| 76 | ] |
| 77 | }, |
| 78 | { |
| 79 | "cell_type": "markdown", |
| 80 | "id": "4f761649", |
| 81 | "metadata": {}, |
| 82 | "source": [ |
| 83 | "## Submitting a simple Qiskit circuit\n", |
| 84 | "\n", |
| 85 | "We start with a minimal circuit that prepares single-qubit superpositions on two qubits and measures them. After constructing the circuit we’ll submit it to an Azure Quantum target." |
| 86 | ] |
| 87 | }, |
| 88 | { |
| 89 | "cell_type": "code", |
| 90 | "execution_count": null, |
| 91 | "id": "e91dd2d7", |
| 92 | "metadata": {}, |
| 93 | "outputs": [], |
| 94 | "source": [ |
| 95 | "from qiskit import QuantumCircuit\n", |
| 96 | "\n", |
| 97 | "circuit = QuantumCircuit(2, 2)\n", |
| 98 | "\n", |
| 99 | "circuit.h(0)\n", |
| 100 | "circuit.measure(0, 0)\n", |
| 101 | "\n", |
| 102 | "circuit.h(1)\n", |
| 103 | "circuit.measure(1, 1)\n", |
| 104 | "\n", |
| 105 | "circuit.draw(output=\"text\")" |
| 106 | ] |
| 107 | }, |
| 108 | { |
| 109 | "cell_type": "markdown", |
| 110 | "id": "efd50528", |
| 111 | "metadata": {}, |
| 112 | "source": [ |
| 113 | "## Configure Azure Quantum workspace connection\n", |
| 114 | "\n", |
| 115 | "To connect to an Azure workspace replace the following variables with your own values." |
| 116 | ] |
| 117 | }, |
| 118 | { |
| 119 | "cell_type": "code", |
| 120 | "execution_count": null, |
| 121 | "id": "54206752", |
| 122 | "metadata": {}, |
| 123 | "outputs": [], |
| 124 | "source": [ |
| 125 | "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n", |
| 126 | "resource_group = 'myresourcegroup'\n", |
| 127 | "workspace_name = 'myworkspace'\n", |
| 128 | "location = 'westus'" |
| 129 | ] |
| 130 | }, |
| 131 | { |
| 132 | "cell_type": "code", |
| 133 | "execution_count": null, |
| 134 | "id": "d9a0f59b", |
| 135 | "metadata": {}, |
| 136 | "outputs": [], |
| 137 | "source": [ |
| 138 | "from qiskit import qasm3\n", |
| 139 | "from qdk.openqasm import compile\n", |
| 140 | "from qdk.azure import Workspace\n", |
| 141 | "from qdk import TargetProfile\n", |
| 142 | "\n", |
| 143 | "def submit_qiskit_circuit_to_azure(circuit, target_name, name, shots=100):\n", |
| 144 | " qasm3_str = qasm3.dumps(circuit)\n", |
| 145 | " qir = compile(qasm3_str, target_profile=TargetProfile.Base)\n", |
| 146 | "\n", |
| 147 | " workspace = Workspace(\n", |
| 148 | " subscription_id=subscription_id,\n", |
| 149 | " resource_group=resource_group,\n", |
| 150 | " name=workspace_name,\n", |
| 151 | " location=location,\n", |
| 152 | " )\n", |
| 153 | " target = workspace.get_targets(target_name)\n", |
| 154 | " job = target.submit(qir, name, shots=shots)\n", |
| 155 | " return job.get_results()\n", |
| 156 | "\n", |
| 157 | "results = submit_qiskit_circuit_to_azure(circuit, \"rigetti.sim.qvm\", \"qiskit-job\")\n", |
| 158 | "print(results)" |
| 159 | ] |
| 160 | }, |
| 161 | { |
| 162 | "cell_type": "markdown", |
| 163 | "id": "9c84df75", |
| 164 | "metadata": {}, |
| 165 | "source": [ |
| 166 | "## Parameterized Qiskit circuits\n", |
| 167 | "\n", |
| 168 | "Many algorithms use symbolic parameters. Before compiling to QIR and submitting, all circuit parameters must be bound to numeric values. Below we build a simple entangling ladder, apply a rotation parameterized by θ across all qubits, then uncompute the entanglement and measure the first qubit." |
| 169 | ] |
| 170 | }, |
| 171 | { |
| 172 | "cell_type": "code", |
| 173 | "execution_count": null, |
| 174 | "id": "5d766661", |
| 175 | "metadata": {}, |
| 176 | "outputs": [], |
| 177 | "source": [ |
| 178 | "from qiskit import QuantumCircuit\n", |
| 179 | "from qiskit.circuit import Parameter\n", |
| 180 | "\n", |
| 181 | "\n", |
| 182 | "def get_parameterized_circuit(n = 5) -> QuantumCircuit:\n", |
| 183 | " theta = Parameter(\"θ\")\n", |
| 184 | " qc = QuantumCircuit(n, 1)\n", |
| 185 | " qc.h(0)\n", |
| 186 | " for i in range(n - 1):\n", |
| 187 | " qc.cx(i, i + 1)\n", |
| 188 | " qc.rz(theta, range(n))\n", |
| 189 | "\n", |
| 190 | " for i in reversed(range(n - 1)):\n", |
| 191 | " qc.cx(i, i + 1)\n", |
| 192 | " qc.h(0)\n", |
| 193 | " qc.measure(0, 0)\n", |
| 194 | " return qc\n", |
| 195 | "\n", |
| 196 | "# Build the symbolic (parameterized) circuit\n", |
| 197 | "circuit = get_parameterized_circuit()\n", |
| 198 | "\n", |
| 199 | "# Bind θ to a numeric value, then visualize the bound circuit\n", |
| 200 | "circuit.assign_parameters({\"θ\": 0.5}).draw(output=\"text\")" |
| 201 | ] |
| 202 | }, |
| 203 | { |
| 204 | "cell_type": "markdown", |
| 205 | "id": "ee4d57b4", |
| 206 | "metadata": {}, |
| 207 | "source": [ |
| 208 | "## Submitting a bound parameterized circuit\n", |
| 209 | "\n", |
| 210 | "Below we reuse the parameterized circuit from above, bind θ to 0.5, and submit the resulting (fully concrete) circuit to the Azure Quantum target using the same helper function as before. The printed result shows the measurement counts. Change the value of θ (or loop over several values) to explore how the outcome distribution varies." |
| 211 | ] |
| 212 | }, |
| 213 | { |
| 214 | "cell_type": "code", |
| 215 | "execution_count": null, |
| 216 | "id": "d9bb3eb0", |
| 217 | "metadata": {}, |
| 218 | "outputs": [], |
| 219 | "source": [ |
| 220 | "# Reuse the previously defined parameterized circuit\n", |
| 221 | "# (circuit is symbolic and must have θ bound to a value before submission)\n", |
| 222 | "\n", |
| 223 | "results = submit_qiskit_circuit_to_azure(\n", |
| 224 | " circuit.assign_parameters({\"θ\": 0.5}),\n", |
| 225 | " \"rigetti.sim.qvm\",\n", |
| 226 | " \"qiskit-parameterized-job\"\n", |
| 227 | ")\n", |
| 228 | "print(results)" |
| 229 | ] |
| 230 | } |
| 231 | ], |
| 232 | "metadata": { |
| 233 | "kernelspec": { |
| 234 | "display_name": ".venv", |
| 235 | "language": "python", |
| 236 | "name": "python3" |
| 237 | }, |
| 238 | "language_info": { |
| 239 | "codemirror_mode": { |
| 240 | "name": "ipython", |
| 241 | "version": 3 |
| 242 | }, |
| 243 | "file_extension": ".py", |
| 244 | "mimetype": "text/x-python", |
| 245 | "name": "python", |
| 246 | "nbconvert_exporter": "python", |
| 247 | "pygments_lexer": "ipython3", |
| 248 | "version": "3.13.8" |
| 249 | } |
| 250 | }, |
| 251 | "nbformat": 4, |
| 252 | "nbformat_minor": 5 |
| 253 | } |
| 254 | |