microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
pip/samples/sample.ipynb
292lines · modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "id": "a2f48f2b", |
| 6 | "metadata": {}, |
| 7 | "source": [ |
| 8 | "Import the Q# module.\n", |
| 9 | "\n", |
| 10 | "This enables the `%%qsharp` magic and initializes a Q# interpreter singleton." |
| 11 | ] |
| 12 | }, |
| 13 | { |
| 14 | "cell_type": "code", |
| 15 | "execution_count": 1, |
| 16 | "id": "1e8e4faa", |
| 17 | "metadata": {}, |
| 18 | "outputs": [], |
| 19 | "source": [ |
| 20 | "import qsharp" |
| 21 | ] |
| 22 | }, |
| 23 | { |
| 24 | "attachments": {}, |
| 25 | "cell_type": "markdown", |
| 26 | "id": "3a536d53", |
| 27 | "metadata": {}, |
| 28 | "source": [ |
| 29 | "Run Q# using the `%%qsharp` magic.\n", |
| 30 | "\n", |
| 31 | "`DumpMachine()` and `Message()` output get formatted as HTML. Return value is shown as cell output." |
| 32 | ] |
| 33 | }, |
| 34 | { |
| 35 | "cell_type": "code", |
| 36 | "execution_count": 2, |
| 37 | "id": "9df62352", |
| 38 | "metadata": {}, |
| 39 | "outputs": [ |
| 40 | { |
| 41 | "data": { |
| 42 | "text/html": [ |
| 43 | "<table>\n", |
| 44 | " <thead><tr>\n", |
| 45 | " <th style=\"text-align: left;\">Basis State<br/>(|𝜓ₙ…𝜓₁⟩)</th>\n", |
| 46 | " <th style=\"text-align: left;\">Amplitude</th>\n", |
| 47 | " <th style=\"text-align: left;\">Measurement Probability</th>\n", |
| 48 | " <th style=\"text-align: left;\" colspan=\"2\">Phase</th>\n", |
| 49 | " </tr></thead>\n", |
| 50 | " <tbody><tr>\n", |
| 51 | " <td style=\"text-align: left;\"><span style=\"display: inline-block;\">|1⟩</span></td>\n", |
| 52 | " <td style=\"text-align: left;\"><span style=\"display: inline-block;\">1.0000+0.0000𝑖</span></td>\n", |
| 53 | " <td style=\"text-align: left;\">\n", |
| 54 | " <progress max=\"100\" value=\"100\"></progress>\n", |
| 55 | " <span style=\"display: inline-block;\">100.0000%</span>\n", |
| 56 | " </td>\n", |
| 57 | " <td style=\"text-align: left; transform: rotate(0.0000rad)\">↑</td>\n", |
| 58 | " <td style=\"text-align: left;\">\n", |
| 59 | " <span style=\"display: inline-block;\">0.0000</span>\n", |
| 60 | " </td>\n", |
| 61 | "</tr></tbody>\n", |
| 62 | " </table>The result of the measurement is One<p>Result.One</p>" |
| 63 | ], |
| 64 | "text/plain": [ |
| 65 | "<qsharp._ipython.DisplayableOutput at 0x7f8f2f002310>" |
| 66 | ] |
| 67 | }, |
| 68 | "execution_count": 2, |
| 69 | "metadata": {}, |
| 70 | "output_type": "execute_result" |
| 71 | } |
| 72 | ], |
| 73 | "source": [ |
| 74 | "%%qsharp\n", |
| 75 | "\n", |
| 76 | "operation Main() : Result {\n", |
| 77 | " use q = Qubit();\n", |
| 78 | " X(q);\n", |
| 79 | " Microsoft.Quantum.Diagnostics.DumpMachine();\n", |
| 80 | " let r = M(q);\n", |
| 81 | " Message(\"The result of the measurement is \" + AsString(r));\n", |
| 82 | " Reset(q);\n", |
| 83 | " r\n", |
| 84 | "}\n", |
| 85 | "\n", |
| 86 | "Main()" |
| 87 | ] |
| 88 | }, |
| 89 | { |
| 90 | "attachments": {}, |
| 91 | "cell_type": "markdown", |
| 92 | "id": "4584c494", |
| 93 | "metadata": {}, |
| 94 | "source": [ |
| 95 | "`qsharp.interpret()` does the same thing as the `%%qsharp` magic.\n", |
| 96 | "\n", |
| 97 | "`DumpMachine()` and `Message()` print to stdout and get displayed in the notebook as plain text" |
| 98 | ] |
| 99 | }, |
| 100 | { |
| 101 | "cell_type": "code", |
| 102 | "execution_count": 3, |
| 103 | "id": "7d0995bf", |
| 104 | "metadata": { |
| 105 | "scrolled": true |
| 106 | }, |
| 107 | "outputs": [ |
| 108 | { |
| 109 | "name": "stdout", |
| 110 | "output_type": "stream", |
| 111 | "text": [ |
| 112 | "STATE:\n", |
| 113 | "|1⟩: 1.0000+0.0000i\n", |
| 114 | "The result of the measurement is One\n" |
| 115 | ] |
| 116 | }, |
| 117 | { |
| 118 | "data": { |
| 119 | "text/plain": [ |
| 120 | "Result.One" |
| 121 | ] |
| 122 | }, |
| 123 | "execution_count": 3, |
| 124 | "metadata": {}, |
| 125 | "output_type": "execute_result" |
| 126 | } |
| 127 | ], |
| 128 | "source": [ |
| 129 | "qsharp.interpret(\"Main()\")" |
| 130 | ] |
| 131 | }, |
| 132 | { |
| 133 | "cell_type": "markdown", |
| 134 | "id": "a3bde193", |
| 135 | "metadata": {}, |
| 136 | "source": [ |
| 137 | "Assign a result to a Python variable." |
| 138 | ] |
| 139 | }, |
| 140 | { |
| 141 | "cell_type": "code", |
| 142 | "execution_count": 4, |
| 143 | "id": "50383f8a", |
| 144 | "metadata": {}, |
| 145 | "outputs": [ |
| 146 | { |
| 147 | "name": "stdout", |
| 148 | "output_type": "stream", |
| 149 | "text": [ |
| 150 | "Result: 3 (type: int)\n" |
| 151 | ] |
| 152 | } |
| 153 | ], |
| 154 | "source": [ |
| 155 | "result = qsharp.interpret(\"1 + 2\")\n", |
| 156 | "\n", |
| 157 | "print(f\"Result: {result} (type: {type(result).__name__})\") " |
| 158 | ] |
| 159 | }, |
| 160 | { |
| 161 | "cell_type": "markdown", |
| 162 | "id": "b06b7857", |
| 163 | "metadata": {}, |
| 164 | "source": [ |
| 165 | "Errors are exceptions. \n", |
| 166 | "\n", |
| 167 | "Catch and handle compilation errors." |
| 168 | ] |
| 169 | }, |
| 170 | { |
| 171 | "cell_type": "code", |
| 172 | "execution_count": 5, |
| 173 | "id": "33fd3c4d", |
| 174 | "metadata": {}, |
| 175 | "outputs": [ |
| 176 | { |
| 177 | "name": "stdout", |
| 178 | "output_type": "stream", |
| 179 | "text": [ |
| 180 | "\u001b[33mname error\u001b[0m\n", |
| 181 | "\u001b[33mname error\u001b[0m\n" |
| 182 | ] |
| 183 | } |
| 184 | ], |
| 185 | "source": [ |
| 186 | "from qsharp import ( CompilationException, RuntimeException )\n", |
| 187 | "\n", |
| 188 | "try:\n", |
| 189 | " qsharp.interpret(\"\"\"\n", |
| 190 | "operation Foo() : Unit {\n", |
| 191 | " Bar();\n", |
| 192 | " Baz();\n", |
| 193 | "}\n", |
| 194 | "\"\"\")\n", |
| 195 | "except CompilationException as ex:\n", |
| 196 | " for diagnostic in ex.diagnostics:\n", |
| 197 | " print(\"\\x1b[33m\" + diagnostic.message + \"\\x1b[0m\")" |
| 198 | ] |
| 199 | }, |
| 200 | { |
| 201 | "cell_type": "markdown", |
| 202 | "id": "00f82a16", |
| 203 | "metadata": {}, |
| 204 | "source": [ |
| 205 | "Catch and handle runtime errors." |
| 206 | ] |
| 207 | }, |
| 208 | { |
| 209 | "cell_type": "code", |
| 210 | "execution_count": 6, |
| 211 | "id": "e70a95d9", |
| 212 | "metadata": {}, |
| 213 | "outputs": [ |
| 214 | { |
| 215 | "name": "stdout", |
| 216 | "output_type": "stream", |
| 217 | "text": [ |
| 218 | "\u001b[31mQubit0 released while not in |0⟩ state\u001b[0m\n" |
| 219 | ] |
| 220 | } |
| 221 | ], |
| 222 | "source": [ |
| 223 | "try:\n", |
| 224 | " qsharp.interpret(\"\"\"\n", |
| 225 | " operation Main(): Unit {\n", |
| 226 | " use q = Qubit();\n", |
| 227 | " X(q);\n", |
| 228 | " }\n", |
| 229 | " Main()\n", |
| 230 | " \"\"\")\n", |
| 231 | "except RuntimeException as ex:\n", |
| 232 | " for diagnostic in ex.diagnostics:\n", |
| 233 | " print(\"\\x1b[31m\" + diagnostic.message + \"\\x1b[0m\")" |
| 234 | ] |
| 235 | }, |
| 236 | { |
| 237 | "cell_type": "markdown", |
| 238 | "id": "ba2f98ad", |
| 239 | "metadata": {}, |
| 240 | "source": [ |
| 241 | "In `%%qsharp` cells, exceptions are handled and displayed as error text." |
| 242 | ] |
| 243 | }, |
| 244 | { |
| 245 | "cell_type": "code", |
| 246 | "execution_count": 7, |
| 247 | "id": "1b55e53c", |
| 248 | "metadata": { |
| 249 | "scrolled": true |
| 250 | }, |
| 251 | "outputs": [ |
| 252 | { |
| 253 | "name": "stdout", |
| 254 | "output_type": "stream", |
| 255 | "text": [ |
| 256 | "\u001b[31mQubit1 released while not in |0⟩ state\u001b[0m\n" |
| 257 | ] |
| 258 | } |
| 259 | ], |
| 260 | "source": [ |
| 261 | "%%qsharp\n", |
| 262 | "\n", |
| 263 | "operation Main(): Unit {\n", |
| 264 | " use q = Qubit();\n", |
| 265 | " X(q);\n", |
| 266 | "}\n", |
| 267 | "Main()" |
| 268 | ] |
| 269 | } |
| 270 | ], |
| 271 | "metadata": { |
| 272 | "kernelspec": { |
| 273 | "display_name": "Python 3 (ipykernel)", |
| 274 | "language": "python", |
| 275 | "name": "python3" |
| 276 | }, |
| 277 | "language_info": { |
| 278 | "codemirror_mode": { |
| 279 | "name": "ipython", |
| 280 | "version": 3 |
| 281 | }, |
| 282 | "file_extension": ".py", |
| 283 | "mimetype": "text/x-python", |
| 284 | "name": "python", |
| 285 | "nbconvert_exporter": "python", |
| 286 | "pygments_lexer": "ipython3", |
| 287 | "version": "3.11.3" |
| 288 | } |
| 289 | }, |
| 290 | "nbformat": 4, |
| 291 | "nbformat_minor": 5 |
| 292 | } |