{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "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."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "1e8e4faa",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "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",
            "text/plain": []
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/x.qsharp-config": "{\"targetProfile\":\"unrestricted\"}",
            "text/plain": [
              "Q# initialized with configuration: {'targetProfile': 'unrestricted'}"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import qsharp\n",
        "\n",
        "qsharp.init(project_root='./test_project')\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we can reference the compiled sources."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "9df62352",
      "metadata": {
        "vscode": {
          "languageId": "qsharp"
        }
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "<table>\n",
              "  <thead>\n",
              "    <tr>\n",
              "      <th style=\"text-align: left\">Basis State<br />(|𝜓₁…𝜓ₙ⟩)</th>\n",
              "      <th style=\"text-align: left\">Amplitude</th>\n",
              "      <th style=\"text-align: left\">Measurement Probability</th>\n",
              "      <th style=\"text-align: left\" colspan=\"2\">Phase</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "  <td style=\"text-align: left\">\n",
              "    <span style=\"display: inline-block\">|1⟩</span>\n",
              "  </td>\n",
              "  <td style=\"text-align: left\">\n",
              "    <span style=\"display: inline-block\">1.0000+0.0000𝑖</span>\n",
              "  </td>\n",
              "  <td style=\"text-align: left\">\n",
              "    <progress max=\"100\" value=\"100\"></progress>\n",
              "    <span style=\"display: inline-block\">100.0000%</span>\n",
              "  </td>\n",
              "  <td style=\"text-align: left; transform: rotate(0.0000rad)\">↑</td>\n",
              "  <td style=\"text-align: left\">\n",
              "    <span style=\"display: inline-block\">0.0000</span>\n",
              "  </td>\n",
              "</tr>\n",
              "\n",
              "  </tbody>\n",
              "</table>\n"
            ],
            "text/plain": [
              "STATE:\n",
              "|1⟩: 1.0000+0.0000𝑖"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<p>The result of the measurement is One</p>"
            ],
            "text/plain": [
              "The result of the measurement is One"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "One"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "%%qsharp\n",
        "\n",
        "Sample.Main()\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
