microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/notebooks/project.ipynb
151lines ยท modecode
| 1 | { |
| 2 | "cells": [ |
| 3 | { |
| 4 | "cell_type": "markdown", |
| 5 | "metadata": {}, |
| 6 | "source": [ |
| 7 | "Initialize Q# with a project root. This will look for a `qsharp.json` at the specified directory and compile the Q# sources included by the manifest." |
| 8 | ] |
| 9 | }, |
| 10 | { |
| 11 | "cell_type": "code", |
| 12 | "execution_count": 1, |
| 13 | "id": "1e8e4faa", |
| 14 | "metadata": {}, |
| 15 | "outputs": [ |
| 16 | { |
| 17 | "data": { |
| 18 | "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", |
| 19 | "text/plain": [] |
| 20 | }, |
| 21 | "metadata": {}, |
| 22 | "output_type": "display_data" |
| 23 | }, |
| 24 | { |
| 25 | "data": { |
| 26 | "application/x.qsharp-config": "{\"targetProfile\":\"unrestricted\"}", |
| 27 | "text/plain": [ |
| 28 | "Q# initialized with configuration: {'targetProfile': 'unrestricted'}" |
| 29 | ] |
| 30 | }, |
| 31 | "execution_count": 1, |
| 32 | "metadata": {}, |
| 33 | "output_type": "execute_result" |
| 34 | } |
| 35 | ], |
| 36 | "source": [ |
| 37 | "import qsharp\n", |
| 38 | "\n", |
| 39 | "qsharp.init(project_root='./test_project')\n" |
| 40 | ] |
| 41 | }, |
| 42 | { |
| 43 | "cell_type": "markdown", |
| 44 | "metadata": {}, |
| 45 | "source": [ |
| 46 | "Now we can reference the compiled sources." |
| 47 | ] |
| 48 | }, |
| 49 | { |
| 50 | "cell_type": "code", |
| 51 | "execution_count": 2, |
| 52 | "id": "9df62352", |
| 53 | "metadata": { |
| 54 | "vscode": { |
| 55 | "languageId": "qsharp" |
| 56 | } |
| 57 | }, |
| 58 | "outputs": [ |
| 59 | { |
| 60 | "data": { |
| 61 | "text/html": [ |
| 62 | "<table>\n", |
| 63 | " <thead>\n", |
| 64 | " <tr>\n", |
| 65 | " <th style=\"text-align: left\">Basis State<br />(|๐โโฆ๐โโฉ)</th>\n", |
| 66 | " <th style=\"text-align: left\">Amplitude</th>\n", |
| 67 | " <th style=\"text-align: left\">Measurement Probability</th>\n", |
| 68 | " <th style=\"text-align: left\" colspan=\"2\">Phase</th>\n", |
| 69 | " </tr>\n", |
| 70 | " </thead>\n", |
| 71 | " <tbody>\n", |
| 72 | " <tr>\n", |
| 73 | " <td style=\"text-align: left\">\n", |
| 74 | " <span style=\"display: inline-block\">|1โฉ</span>\n", |
| 75 | " </td>\n", |
| 76 | " <td style=\"text-align: left\">\n", |
| 77 | " <span style=\"display: inline-block\">1.0000+0.0000๐</span>\n", |
| 78 | " </td>\n", |
| 79 | " <td style=\"text-align: left\">\n", |
| 80 | " <progress max=\"100\" value=\"100\"></progress>\n", |
| 81 | " <span style=\"display: inline-block\">100.0000%</span>\n", |
| 82 | " </td>\n", |
| 83 | " <td style=\"text-align: left; transform: rotate(0.0000rad)\">โ</td>\n", |
| 84 | " <td style=\"text-align: left\">\n", |
| 85 | " <span style=\"display: inline-block\">0.0000</span>\n", |
| 86 | " </td>\n", |
| 87 | "</tr>\n", |
| 88 | "\n", |
| 89 | " </tbody>\n", |
| 90 | "</table>\n" |
| 91 | ], |
| 92 | "text/plain": [ |
| 93 | "STATE:\n", |
| 94 | "|1โฉ: 1.0000+0.0000๐" |
| 95 | ] |
| 96 | }, |
| 97 | "metadata": {}, |
| 98 | "output_type": "display_data" |
| 99 | }, |
| 100 | { |
| 101 | "data": { |
| 102 | "text/html": [ |
| 103 | "<p>The result of the measurement is One</p>" |
| 104 | ], |
| 105 | "text/plain": [ |
| 106 | "The result of the measurement is One" |
| 107 | ] |
| 108 | }, |
| 109 | "metadata": {}, |
| 110 | "output_type": "display_data" |
| 111 | }, |
| 112 | { |
| 113 | "data": { |
| 114 | "text/plain": [ |
| 115 | "One" |
| 116 | ] |
| 117 | }, |
| 118 | "execution_count": 2, |
| 119 | "metadata": {}, |
| 120 | "output_type": "execute_result" |
| 121 | } |
| 122 | ], |
| 123 | "source": [ |
| 124 | "%%qsharp\n", |
| 125 | "\n", |
| 126 | "Sample.Main()\n" |
| 127 | ] |
| 128 | } |
| 129 | ], |
| 130 | "metadata": { |
| 131 | "kernelspec": { |
| 132 | "display_name": "Python 3 (ipykernel)", |
| 133 | "language": "python", |
| 134 | "name": "python3" |
| 135 | }, |
| 136 | "language_info": { |
| 137 | "codemirror_mode": { |
| 138 | "name": "ipython", |
| 139 | "version": 3 |
| 140 | }, |
| 141 | "file_extension": ".py", |
| 142 | "mimetype": "text/x-python", |
| 143 | "name": "python", |
| 144 | "nbconvert_exporter": "python", |
| 145 | "pygments_lexer": "ipython3", |
| 146 | "version": "3.11.6" |
| 147 | } |
| 148 | }, |
| 149 | "nbformat": 4, |
| 150 | "nbformat_minor": 5 |
| 151 | } |
| 152 | |