microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/azure_submission.ipynb
240lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "To connect to an Azure workspace replace the following variables with your own values." |
| 8 | ] |
| 9 | }, |
| 10 | { |
| 11 | "cell_type": "code", |
| 12 | "execution_count": null, |
| 13 | "metadata": {}, |
| 14 | "outputs": [], |
| 15 | "source": [ |
| 16 | "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n", |
| 17 | "resource_group = 'myresourcegroup'\n", |
| 18 | "workspace_name = 'myworkspace'\n", |
| 19 | "location = 'westus'\n" |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "cell_type": "markdown", |
| 24 | "metadata": {}, |
| 25 | "source": [ |
| 26 | "Import the Q# package. Set the target profile to be the base profile." |
| 27 | ] |
| 28 | }, |
| 29 | { |
| 30 | "cell_type": "code", |
| 31 | "execution_count": 1, |
| 32 | "metadata": {}, |
| 33 | "outputs": [ |
| 34 | { |
| 35 | "data": { |
| 36 | "application/javascript": "// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\n// This file provides CodeMirror syntax highlighting for Q# magic cells\n// in classic Jupyter Notebooks. It does nothing in other (Jupyter Notebook 7,\n// VS Code, Azure Notebooks, etc.) environments.\n\n// Detect the prerequisites and do nothing if they don't exist.\nif (window.require && window.CodeMirror && window.Jupyter) {\n // The simple mode plugin for CodeMirror is not loaded by default, so require it.\n window.require([\"codemirror/addon/mode/simple\"], function defineMode() {\n let rules = [\n {\n token: \"comment\",\n regex: /(\\/\\/).*/,\n beginWord: false,\n },\n {\n token: \"string\",\n regex: String.raw`^\\\"(?:[^\\\"\\\\]|\\\\[\\s\\S])*(?:\\\"|$)`,\n beginWord: false,\n },\n {\n token: \"keyword\",\n regex: String.raw`(namespace|open|as|operation|function|body|adjoint|newtype|controlled|internal)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(if|elif|else|repeat|until|fixup|for|in|return|fail|within|apply)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(Adjoint|Controlled|Adj|Ctl|is|self|auto|distribute|invert|intrinsic)\\b`,\n beginWord: true,\n },\n {\n token: \"keyword\",\n regex: String.raw`(let|set|use|borrow|mutable)\\b`,\n beginWord: true,\n },\n {\n token: \"operatorKeyword\",\n regex: String.raw`(not|and|or)\\b|(w/)`,\n beginWord: true,\n },\n {\n token: \"operatorKeyword\",\n regex: String.raw`(=)|(!)|(<)|(>)|(\\+)|(-)|(\\*)|(/)|(\\^)|(%)|(\\|)|(&&&)|(~~~)|(\\.\\.\\.)|(\\.\\.)|(\\?)`,\n beginWord: false,\n },\n {\n token: \"meta\",\n regex: String.raw`(Int|BigInt|Double|Bool|Qubit|Pauli|Result|Range|String|Unit)\\b`,\n beginWord: true,\n },\n {\n token: \"atom\",\n regex: String.raw`(true|false|Pauli(I|X|Y|Z)|One|Zero)\\b`,\n beginWord: true,\n },\n ];\n let simpleRules = [];\n for (let rule of rules) {\n simpleRules.push({\n token: rule.token,\n regex: new RegExp(rule.regex, \"g\"),\n sol: rule.beginWord,\n });\n if (rule.beginWord) {\n // Need an additional rule due to the fact that CodeMirror simple mode doesn't work with ^ token\n simpleRules.push({\n token: rule.token,\n regex: new RegExp(String.raw`\\W` + rule.regex, \"g\"),\n sol: false,\n });\n }\n }\n\n // Register the mode defined above with CodeMirror\n window.CodeMirror.defineSimpleMode(\"qsharp\", { start: simpleRules });\n window.CodeMirror.defineMIME(\"text/x-qsharp\", \"qsharp\");\n\n // Tell Jupyter to associate %%qsharp magic cells with the qsharp mode\n window.Jupyter.CodeCell.options_default.highlight_modes[\"qsharp\"] = {\n reg: [/^%%qsharp/],\n };\n\n // Force re-highlighting of all cells the first time this code runs\n for (const cell of window.Jupyter.notebook.get_cells()) {\n cell.auto_highlight();\n }\n });\n}\n", |
| 37 | "text/plain": [] |
| 38 | }, |
| 39 | "metadata": {}, |
| 40 | "output_type": "display_data" |
| 41 | }, |
| 42 | { |
| 43 | "data": { |
| 44 | "application/x.qsharp-config": "{\"targetProfile\":\"base\",\"languageFeatures\":null,\"manifest\":null}", |
| 45 | "text/plain": [ |
| 46 | "Q# initialized with configuration: {'targetProfile': 'base', 'languageFeatures': None, 'manifest': None}" |
| 47 | ] |
| 48 | }, |
| 49 | "execution_count": 1, |
| 50 | "metadata": {}, |
| 51 | "output_type": "execute_result" |
| 52 | } |
| 53 | ], |
| 54 | "source": [ |
| 55 | "import qsharp\n", |
| 56 | "\n", |
| 57 | "qsharp.init(target_profile=qsharp.TargetProfile.Base)\n" |
| 58 | ] |
| 59 | }, |
| 60 | { |
| 61 | "cell_type": "markdown", |
| 62 | "metadata": {}, |
| 63 | "source": [ |
| 64 | "The following will generate a compiler error because it uses a feature unsupported by the base profile." |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "cell_type": "code", |
| 69 | "execution_count": 2, |
| 70 | "metadata": { |
| 71 | "vscode": { |
| 72 | "languageId": "qsharp" |
| 73 | } |
| 74 | }, |
| 75 | "outputs": [ |
| 76 | { |
| 77 | "ename": "QSharpCellError", |
| 78 | "evalue": "\u001b[31mQsc.CapabilitiesCk.UseOfDynamicBool\u001b[0m\n\n \u001b[31m×\u001b[0m cannot use a dynamic bool value\n ╭─[\u001b[36;1;4mline_0\u001b[0m:4:1]\n \u001b[2m4\u001b[0m │ let result = M(q);\n \u001b[2m5\u001b[0m │ if (result == Zero) {\n · \u001b[35;1m ──────────────\u001b[0m\n \u001b[2m6\u001b[0m │ Message(\"The result is Zero\");\n ╰────\n\u001b[36m help: \u001b[0musing a bool value that depends on a measurement result is not\n supported by the current target\n", |
| 79 | "output_type": "error", |
| 80 | "traceback": [ |
| 81 | "\u001b[31mQsc.CapabilitiesCk.UseOfDynamicBool\u001b[0m", |
| 82 | "", |
| 83 | " \u001b[31m×\u001b[0m cannot use a dynamic bool value", |
| 84 | " ╭─[\u001b[36;1;4mline_0\u001b[0m:4:1]", |
| 85 | " \u001b[2m4\u001b[0m │ let result = M(q);", |
| 86 | " \u001b[2m5\u001b[0m │ if (result == Zero) {", |
| 87 | " · \u001b[35;1m ──────────────\u001b[0m", |
| 88 | " \u001b[2m6\u001b[0m │ Message(\"The result is Zero\");", |
| 89 | " ╰────", |
| 90 | "\u001b[36m help: \u001b[0musing a bool value that depends on a measurement result is not", |
| 91 | " supported by the current target" |
| 92 | ] |
| 93 | } |
| 94 | ], |
| 95 | "source": [ |
| 96 | "%%qsharp\n", |
| 97 | "\n", |
| 98 | "operation NotAllowed() : Unit {\n", |
| 99 | " use q = Qubit();\n", |
| 100 | " let result = M(q);\n", |
| 101 | " if (result == Zero) {\n", |
| 102 | " Message(\"The result is Zero\");\n", |
| 103 | " }\n", |
| 104 | " Reset(q);\n", |
| 105 | "}\n" |
| 106 | ] |
| 107 | }, |
| 108 | { |
| 109 | "cell_type": "markdown", |
| 110 | "metadata": {}, |
| 111 | "source": [ |
| 112 | "Define some more Q# operations. This should compile without error." |
| 113 | ] |
| 114 | }, |
| 115 | { |
| 116 | "cell_type": "code", |
| 117 | "execution_count": 3, |
| 118 | "metadata": { |
| 119 | "vscode": { |
| 120 | "languageId": "qsharp" |
| 121 | } |
| 122 | }, |
| 123 | "outputs": [], |
| 124 | "source": [ |
| 125 | "%%qsharp\n", |
| 126 | "\n", |
| 127 | "operation Random() : Result {\n", |
| 128 | " use q = Qubit();\n", |
| 129 | " H(q);\n", |
| 130 | " let result = M(q);\n", |
| 131 | " Reset(q);\n", |
| 132 | " return result\n", |
| 133 | "}\n", |
| 134 | "\n", |
| 135 | "operation RandomNBits(N: Int): Result[] {\n", |
| 136 | " mutable results = [];\n", |
| 137 | " for i in 0 .. N - 1 {\n", |
| 138 | " let r = Random();\n", |
| 139 | " set results += [r];\n", |
| 140 | " }\n", |
| 141 | " return results\n", |
| 142 | "}\n" |
| 143 | ] |
| 144 | }, |
| 145 | { |
| 146 | "cell_type": "markdown", |
| 147 | "metadata": {}, |
| 148 | "source": [ |
| 149 | "Submit to Azure Quantum with a custom entry expression.\n", |
| 150 | "\n", |
| 151 | "Make sure the `azure-quantum` package is installed." |
| 152 | ] |
| 153 | }, |
| 154 | { |
| 155 | "cell_type": "code", |
| 156 | "execution_count": null, |
| 157 | "metadata": {}, |
| 158 | "outputs": [], |
| 159 | "source": [ |
| 160 | "%pip install azure-quantum\n" |
| 161 | ] |
| 162 | }, |
| 163 | { |
| 164 | "cell_type": "code", |
| 165 | "execution_count": 4, |
| 166 | "metadata": {}, |
| 167 | "outputs": [ |
| 168 | { |
| 169 | "name": "stdout", |
| 170 | "output_type": "stream", |
| 171 | "text": [ |
| 172 | "........." |
| 173 | ] |
| 174 | }, |
| 175 | { |
| 176 | "data": { |
| 177 | "text/plain": [ |
| 178 | "{'[0, 1, 1, 1]': 0.08,\n", |
| 179 | " '[1, 1, 0, 0]': 0.1,\n", |
| 180 | " '[0, 0, 1, 0]': 0.04,\n", |
| 181 | " '[0, 1, 0, 0]': 0.05,\n", |
| 182 | " '[1, 0, 1, 0]': 0.05,\n", |
| 183 | " '[1, 0, 0, 0]': 0.07,\n", |
| 184 | " '[0, 1, 0, 1]': 0.07,\n", |
| 185 | " '[1, 0, 1, 1]': 0.07,\n", |
| 186 | " '[0, 0, 0, 0]': 0.08,\n", |
| 187 | " '[1, 1, 1, 0]': 0.05,\n", |
| 188 | " '[0, 0, 0, 1]': 0.1,\n", |
| 189 | " '[0, 0, 1, 1]': 0.04,\n", |
| 190 | " '[0, 1, 1, 0]': 0.09,\n", |
| 191 | " '[1, 0, 0, 1]': 0.04,\n", |
| 192 | " '[1, 1, 1, 1]': 0.05,\n", |
| 193 | " '[1, 1, 0, 1]': 0.02}" |
| 194 | ] |
| 195 | }, |
| 196 | "execution_count": 4, |
| 197 | "metadata": {}, |
| 198 | "output_type": "execute_result" |
| 199 | } |
| 200 | ], |
| 201 | "source": [ |
| 202 | "import azure.quantum\n", |
| 203 | "\n", |
| 204 | "operation = qsharp.compile(\"RandomNBits(4)\")\n", |
| 205 | "\n", |
| 206 | "workspace = azure.quantum.Workspace(\n", |
| 207 | " subscription_id=subscription_id,\n", |
| 208 | " resource_group=resource_group,\n", |
| 209 | " name=workspace_name,\n", |
| 210 | " location=location,\n", |
| 211 | ")\n", |
| 212 | "target = workspace.get_targets(\"rigetti.sim.qvm\")\n", |
| 213 | "job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n", |
| 214 | "job.get_results()\n" |
| 215 | ] |
| 216 | } |
| 217 | ], |
| 218 | "metadata": { |
| 219 | "kernelspec": { |
| 220 | "display_name": "qsharp", |
| 221 | "language": "python", |
| 222 | "name": "python3" |
| 223 | }, |
| 224 | "language_info": { |
| 225 | "codemirror_mode": { |
| 226 | "name": "ipython", |
| 227 | "version": 3 |
| 228 | }, |
| 229 | "file_extension": ".py", |
| 230 | "mimetype": "text/x-python", |
| 231 | "name": "python", |
| 232 | "nbconvert_exporter": "python", |
| 233 | "pygments_lexer": "ipython3", |
| 234 | "version": "3.11.7" |
| 235 | }, |
| 236 | "orig_nbformat": 4 |
| 237 | }, |
| 238 | "nbformat": 4, |
| 239 | "nbformat_minor": 2 |
| 240 | } |
| 241 | |