microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billt/revert-mimalloc

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/notebooks/azure_submission.ipynb

252lines · 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\"}",
45 "text/plain": [
46 "Q# initialized with configuration: {'targetProfile': 'base'}"
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 "data": {
78 "text/plain": [
79 "\u001b[31mQsc.BaseProfCk.ResultComparison\u001b[0m\n",
80 "\n",
81 " \u001b[31m×\u001b[0m cannot compare measurement results\n",
82 " ╭─[\u001b[36;1;4mline_0\u001b[0m:4:1]\n",
83 " \u001b[2m4\u001b[0m │ let result = M(q);\n",
84 " \u001b[2m5\u001b[0m │ if (result == Zero) {\n",
85 " · \u001b[35;1m ──────────────\u001b[0m\n",
86 " \u001b[2m6\u001b[0m │ Message(\"The result is Zero\");\n",
87 " ╰────\n",
88 "\u001b[36m help: \u001b[0mcomparing measurement results is not supported when performing base\n",
89 " profile QIR generation\n",
90 "\u001b[31mQsc.BaseProfCk.ResultLiteral\u001b[0m\n",
91 "\n",
92 " \u001b[31m×\u001b[0m result literals are not supported\n",
93 " ╭─[\u001b[36;1;4mline_0\u001b[0m:4:1]\n",
94 " \u001b[2m4\u001b[0m │ let result = M(q);\n",
95 " \u001b[2m5\u001b[0m │ if (result == Zero) {\n",
96 " · \u001b[35;1m ────\u001b[0m\n",
97 " \u001b[2m6\u001b[0m │ Message(\"The result is Zero\");\n",
98 " ╰────\n",
99 "\u001b[36m help: \u001b[0mresult literals `One` and `Zero` are not supported when performing\n",
100 " base profile QIR generation\n"
101 ]
102 },
103 "metadata": {},
104 "output_type": "display_data"
105 }
106 ],
107 "source": [
108 "%%qsharp\n",
109 "\n",
110 "operation NotAllowed() : Unit {\n",
111 " use q = Qubit();\n",
112 " let result = M(q);\n",
113 " if (result == Zero) {\n",
114 " Message(\"The result is Zero\");\n",
115 " }\n",
116 " Reset(q);\n",
117 "}\n"
118 ]
119 },
120 {
121 "cell_type": "markdown",
122 "metadata": {},
123 "source": [
124 "Define some more Q# operations. This should compile without error."
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": 3,
130 "metadata": {
131 "vscode": {
132 "languageId": "qsharp"
133 }
134 },
135 "outputs": [],
136 "source": [
137 "%%qsharp\n",
138 "\n",
139 "operation Random() : Result {\n",
140 " use q = Qubit();\n",
141 " H(q);\n",
142 " let result = M(q);\n",
143 " Reset(q);\n",
144 " return result\n",
145 "}\n",
146 "\n",
147 "operation RandomNBits(N: Int): Result[] {\n",
148 " mutable results = [];\n",
149 " for i in 0 .. N - 1 {\n",
150 " let r = Random();\n",
151 " set results += [r];\n",
152 " }\n",
153 " return results\n",
154 "}\n"
155 ]
156 },
157 {
158 "cell_type": "markdown",
159 "metadata": {},
160 "source": [
161 "Submit to Azure Quantum with a custom entry expression.\n",
162 "\n",
163 "Make sure the `azure-quantum` package is installed."
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": null,
169 "metadata": {},
170 "outputs": [],
171 "source": [
172 "%pip install azure-quantum\n"
173 ]
174 },
175 {
176 "cell_type": "code",
177 "execution_count": 4,
178 "metadata": {},
179 "outputs": [
180 {
181 "name": "stdout",
182 "output_type": "stream",
183 "text": [
184 "........."
185 ]
186 },
187 {
188 "data": {
189 "text/plain": [
190 "{'[0, 1, 1, 1]': 0.08,\n",
191 " '[1, 1, 0, 0]': 0.1,\n",
192 " '[0, 0, 1, 0]': 0.04,\n",
193 " '[0, 1, 0, 0]': 0.05,\n",
194 " '[1, 0, 1, 0]': 0.05,\n",
195 " '[1, 0, 0, 0]': 0.07,\n",
196 " '[0, 1, 0, 1]': 0.07,\n",
197 " '[1, 0, 1, 1]': 0.07,\n",
198 " '[0, 0, 0, 0]': 0.08,\n",
199 " '[1, 1, 1, 0]': 0.05,\n",
200 " '[0, 0, 0, 1]': 0.1,\n",
201 " '[0, 0, 1, 1]': 0.04,\n",
202 " '[0, 1, 1, 0]': 0.09,\n",
203 " '[1, 0, 0, 1]': 0.04,\n",
204 " '[1, 1, 1, 1]': 0.05,\n",
205 " '[1, 1, 0, 1]': 0.02}"
206 ]
207 },
208 "execution_count": 4,
209 "metadata": {},
210 "output_type": "execute_result"
211 }
212 ],
213 "source": [
214 "import azure.quantum\n",
215 "\n",
216 "operation = qsharp.compile(\"RandomNBits(4)\")\n",
217 "\n",
218 "workspace = azure.quantum.Workspace(\n",
219 " subscription_id=subscription_id,\n",
220 " resource_group=resource_group,\n",
221 " name=workspace_name,\n",
222 " location=location,\n",
223 ")\n",
224 "target = workspace.get_targets(\"rigetti.sim.qvm\")\n",
225 "job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n",
226 "job.get_results()\n"
227 ]
228 }
229 ],
230 "metadata": {
231 "kernelspec": {
232 "display_name": "qsharp",
233 "language": "python",
234 "name": "python3"
235 },
236 "language_info": {
237 "codemirror_mode": {
238 "name": "ipython",
239 "version": 3
240 },
241 "file_extension": ".py",
242 "mimetype": "text/x-python",
243 "name": "python",
244 "nbconvert_exporter": "python",
245 "pygments_lexer": "ipython3",
246 "version": "3.11.7"
247 },
248 "orig_nbformat": 4
249 },
250 "nbformat": 4,
251 "nbformat_minor": 2
252}
253