microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/python_interop/cirq_submission_to_azure.ipynb
184lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "fae381bf", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "# Submitting Cirq Circuits to Azure Quantum with the QDK\n", |
| 9 | "\n", |
| 10 | "This notebook shows how to take a Cirq `Circuit`, export it to OpenQASM 3, compile that OpenQASM 3 source to QIR using the Quantum Development Kit (QDK) Python APIs, and submit it as a job to an Azure Quantum target." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "markdown", |
| 15 | "id": "0d696a7c", |
| 16 | "metadata": {}, |
| 17 | "source": [ |
| 18 | "The workflow demonstrated here:\n", |
| 19 | "\n", |
| 20 | "1. Build (or load) a Cirq `Circuit`.\n", |
| 21 | "2. Convert it to OpenQASM 3 text via `circuit.to_qasm(version=\"3.0\")`.\n", |
| 22 | "3. Compile the OpenQASM 3 source to QIR with `qdk.openqasm.compile`.\n", |
| 23 | "4. Connect to (or create) an Azure Quantum workspace using `qdk.azure.Workspace`.\n", |
| 24 | "5. Pick a target (e.g., a simulator like `rigetti.sim.qvm`).\n", |
| 25 | "6. Submit the QIR payload and retrieve measurement results." |
| 26 | ] |
| 27 | }, |
| 28 | { |
| 29 | "cell_type": "markdown", |
| 30 | "id": "22d78680", |
| 31 | "metadata": {}, |
| 32 | "source": [ |
| 33 | "## Prerequisites\n", |
| 34 | "\n", |
| 35 | "Ensure the `qdk` package is installed with `azure` and `cirq` extras. If not, install dependencies below." |
| 36 | ] |
| 37 | }, |
| 38 | { |
| 39 | "cell_type": "code", |
| 40 | "execution_count": null, |
| 41 | "id": "eb556730", |
| 42 | "metadata": {}, |
| 43 | "outputs": [], |
| 44 | "source": [ |
| 45 | "%pip install \"qdk[azure,cirq]\"" |
| 46 | ] |
| 47 | }, |
| 48 | { |
| 49 | "cell_type": "markdown", |
| 50 | "id": "20b9ed32", |
| 51 | "metadata": {}, |
| 52 | "source": [ |
| 53 | "After installing, restart the kernel if necessary. Verify imports:" |
| 54 | ] |
| 55 | }, |
| 56 | { |
| 57 | "cell_type": "code", |
| 58 | "execution_count": null, |
| 59 | "id": "7531d5a6", |
| 60 | "metadata": {}, |
| 61 | "outputs": [], |
| 62 | "source": [ |
| 63 | "import cirq, qdk, qdk.azure # should import without errors" |
| 64 | ] |
| 65 | }, |
| 66 | { |
| 67 | "cell_type": "markdown", |
| 68 | "id": "e2b111db", |
| 69 | "metadata": {}, |
| 70 | "source": [ |
| 71 | "## Submitting a simple Cirq circuit\n", |
| 72 | "We'll build a small circuit creating a superposition on one qubit and flipping another, then measuring both. Afterwards we submit it to an Azure Quantum target." |
| 73 | ] |
| 74 | }, |
| 75 | { |
| 76 | "cell_type": "code", |
| 77 | "execution_count": null, |
| 78 | "id": "db4e96e3", |
| 79 | "metadata": {}, |
| 80 | "outputs": [], |
| 81 | "source": [ |
| 82 | "# Build a simple circuit\n", |
| 83 | "q0, q1 = cirq.LineQubit.range(2)\n", |
| 84 | "simple_circuit = cirq.Circuit(\n", |
| 85 | " cirq.H(q0),\n", |
| 86 | " cirq.measure(q0, key='m0'),\n", |
| 87 | " cirq.X(q1),\n", |
| 88 | " cirq.measure(q1, key='m1'),\n", |
| 89 | ")\n", |
| 90 | "print(simple_circuit)" |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | "cell_type": "markdown", |
| 95 | "id": "f582bc13", |
| 96 | "metadata": {}, |
| 97 | "source": [ |
| 98 | "## Configure Azure Quantum workspace connection\n", |
| 99 | "Replace the placeholder values below with your own subscription, resource group, workspace name, and location." |
| 100 | ] |
| 101 | }, |
| 102 | { |
| 103 | "cell_type": "code", |
| 104 | "execution_count": null, |
| 105 | "id": "0344527e", |
| 106 | "metadata": {}, |
| 107 | "outputs": [], |
| 108 | "source": [ |
| 109 | "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n", |
| 110 | "resource_group = 'myresourcegroup'\n", |
| 111 | "workspace_name = 'myworkspace'\n", |
| 112 | "location = 'westus'" |
| 113 | ] |
| 114 | }, |
| 115 | { |
| 116 | "cell_type": "code", |
| 117 | "execution_count": null, |
| 118 | "id": "9f336702", |
| 119 | "metadata": {}, |
| 120 | "outputs": [], |
| 121 | "source": [ |
| 122 | "from qdk.openqasm import compile\n", |
| 123 | "from qdk.azure import Workspace\n", |
| 124 | "from qdk import TargetProfile\n", |
| 125 | "\n", |
| 126 | "def submit_cirq_circuit_to_azure(circuit: cirq.Circuit, target_name: str, name: str, shots: int = 100):\n", |
| 127 | " # 1. Export to OpenQASM 3\n", |
| 128 | " qasm3_str = circuit.to_qasm(version='3.0')\n", |
| 129 | " # 2. Compile to QIR with a base target profile\n", |
| 130 | " qir = compile(qasm3_str, target_profile=TargetProfile.Base)\n", |
| 131 | " # 3. Connect workspace\n", |
| 132 | " workspace = Workspace(\n", |
| 133 | " subscription_id=subscription_id,\n", |
| 134 | " resource_group=resource_group,\n", |
| 135 | " name=workspace_name,\n", |
| 136 | " location=location,\n", |
| 137 | " )\n", |
| 138 | " # 4. Select target (string e.g. 'rigetti.sim.qvm' or other available target)\n", |
| 139 | " target = workspace.get_targets(target_name)\n", |
| 140 | " # 5. Submit QIR payload\n", |
| 141 | " job = target.submit(qir, name, shots=shots)\n", |
| 142 | " return job.get_results()" |
| 143 | ] |
| 144 | }, |
| 145 | { |
| 146 | "cell_type": "markdown", |
| 147 | "id": "a0436202", |
| 148 | "metadata": {}, |
| 149 | "source": [ |
| 150 | "### Submit the simple circuit\n", |
| 151 | "(Make sure you changed the workspace credentials above.)" |
| 152 | ] |
| 153 | }, |
| 154 | { |
| 155 | "cell_type": "code", |
| 156 | "execution_count": null, |
| 157 | "id": "417d7818", |
| 158 | "metadata": {}, |
| 159 | "outputs": [], |
| 160 | "source": [ |
| 161 | "# Uncomment after setting workspace credentials\n", |
| 162 | "# results = submit_cirq_circuit_to_azure(simple_circuit, 'rigetti.sim.qvm', 'cirq-simple-job')\n", |
| 163 | "# print(results)" |
| 164 | ] |
| 165 | }, |
| 166 | { |
| 167 | "cell_type": "markdown", |
| 168 | "id": "1478e88a", |
| 169 | "metadata": {}, |
| 170 | "source": [ |
| 171 | "## Notes\n", |
| 172 | "- Ensure all measurement keys appear before any classical condition usage if you introduce classical controls.\n", |
| 173 | "- Multi-target or custom gates may need full decomposition before submission if they produce unsupported classical constructs." |
| 174 | ] |
| 175 | } |
| 176 | ], |
| 177 | "metadata": { |
| 178 | "language_info": { |
| 179 | "name": "python" |
| 180 | } |
| 181 | }, |
| 182 | "nbformat": 4, |
| 183 | "nbformat_minor": 5 |
| 184 | } |
| 185 | |