{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To connect to an Azure workspace replace the following variables with your own values."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "subscription_id = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'\n",
        "resource_group = 'myresourcegroup'\n",
        "workspace_name = 'myworkspace'\n",
        "location = 'westus'\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import the Q# package. Set the target profile to be the base profile."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "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\":\"base\"}",
            "text/plain": [
              "Q# initialized with configuration: {'targetProfile': 'base'}"
            ]
          },
          "execution_count": 1,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import qsharp\n",
        "\n",
        "qsharp.init(target_profile=qsharp.TargetProfile.Base)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The following will generate a compiler error because it uses a feature unsupported by the base profile."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "vscode": {
          "languageId": "qsharp"
        }
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "\u001b[31mQsc.BaseProfCk.ResultComparison\u001b[0m\n",
              "\n",
              "  \u001b[31m×\u001b[0m cannot compare measurement results\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[0mcomparing measurement results is not supported when performing base\n",
              "        profile QIR generation\n",
              "\u001b[31mQsc.BaseProfCk.ResultLiteral\u001b[0m\n",
              "\n",
              "  \u001b[31m×\u001b[0m result literals are not supported\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[0mresult literals `One` and `Zero` are not supported when performing\n",
              "        base profile QIR generation\n"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "%%qsharp\n",
        "\n",
        "operation NotAllowed() : Unit {\n",
        "    use q = Qubit();\n",
        "    let result = M(q);\n",
        "    if (result == Zero) {\n",
        "        Message(\"The result is Zero\");\n",
        "    }\n",
        "    Reset(q);\n",
        "}\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define some more Q# operations. This should compile without error."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "vscode": {
          "languageId": "qsharp"
        }
      },
      "outputs": [],
      "source": [
        "%%qsharp\n",
        "\n",
        "operation Random() : Result {\n",
        "    use q = Qubit();\n",
        "    H(q);\n",
        "    let result = M(q);\n",
        "    Reset(q);\n",
        "    return result\n",
        "}\n",
        "\n",
        "operation RandomNBits(N: Int): Result[] {\n",
        "    mutable results = [];\n",
        "    for i in 0 .. N - 1 {\n",
        "        let r = Random();\n",
        "        set results += [r];\n",
        "    }\n",
        "    return results\n",
        "}\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Submit to Azure Quantum with a custom entry expression.\n",
        "\n",
        "Make sure the `azure-quantum` package is installed."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install azure-quantum\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "........."
          ]
        },
        {
          "data": {
            "text/plain": [
              "{'[0, 1, 1, 1]': 0.08,\n",
              " '[1, 1, 0, 0]': 0.1,\n",
              " '[0, 0, 1, 0]': 0.04,\n",
              " '[0, 1, 0, 0]': 0.05,\n",
              " '[1, 0, 1, 0]': 0.05,\n",
              " '[1, 0, 0, 0]': 0.07,\n",
              " '[0, 1, 0, 1]': 0.07,\n",
              " '[1, 0, 1, 1]': 0.07,\n",
              " '[0, 0, 0, 0]': 0.08,\n",
              " '[1, 1, 1, 0]': 0.05,\n",
              " '[0, 0, 0, 1]': 0.1,\n",
              " '[0, 0, 1, 1]': 0.04,\n",
              " '[0, 1, 1, 0]': 0.09,\n",
              " '[1, 0, 0, 1]': 0.04,\n",
              " '[1, 1, 1, 1]': 0.05,\n",
              " '[1, 1, 0, 1]': 0.02}"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import azure.quantum\n",
        "\n",
        "operation = qsharp.compile(\"RandomNBits(4)\")\n",
        "\n",
        "workspace = azure.quantum.Workspace(\n",
        "    subscription_id=subscription_id,\n",
        "    resource_group=resource_group,\n",
        "    name=workspace_name,\n",
        "    location=location,\n",
        ")\n",
        "target = workspace.get_targets(\"rigetti.sim.qvm\")\n",
        "job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n",
        "job.get_results()\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "qsharp",
      "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.7"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
