{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a2f48f2b",
   "metadata": {},
   "source": [
    "Import the Q# module.\n",
    "\n",
    "This enables the `%%qsharp` magic and initializes a Q# interpreter singleton."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed1b75bf",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e8e4faa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import qsharp\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a536d53",
   "metadata": {},
   "source": [
    "Run Q# using the `%%qsharp` magic.\n",
    "\n",
    "`DumpMachine()` and `Message()` output get formatted as HTML. Return value is shown as cell output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df62352",
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation Main() : Result {\n",
    "    use q = Qubit();\n",
    "    X(q);\n",
    "    Std.Diagnostics.DumpMachine();\n",
    "    let r = M(q);\n",
    "    Message($\"The result of the measurement is {r}\");\n",
    "    Reset(q);\n",
    "    r\n",
    "}\n",
    "\n",
    "Main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4584c494",
   "metadata": {},
   "source": [
    "`qsharp.eval()` does the same thing as the `%%qsharp` magic.\n",
    "\n",
    "`DumpMachine()` and `Message()` print to stdout and get displayed in the notebook as plain text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d0995bf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "qsharp.eval(\"Main()\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3bde193",
   "metadata": {},
   "source": [
    "Assign a result to a Python variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50383f8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = qsharp.eval(\"1 + 2\")\n",
    "\n",
    "print(f\"Result: {result} (type: {type(result).__name__})\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b06b7857",
   "metadata": {},
   "source": [
    "Errors are exceptions. \n",
    "\n",
    "Catch and handle compilation errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33fd3c4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qsharp import QSharpError\n",
    "\n",
    "try:\n",
    "    qsharp.eval(\n",
    "        \"\"\"\n",
    "operation Foo() : Unit {\n",
    "    Bar();\n",
    "    Baz();\n",
    "}\n",
    "\"\"\"\n",
    "    )\n",
    "except QSharpError as ex:\n",
    "    print(ex)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00f82a16",
   "metadata": {},
   "source": [
    "Catch and handle runtime errors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e70a95d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    qsharp.eval(\"operation Foo() : Unit { use q = Qubit(); X(q) } Foo()\")\n",
    "except QSharpError as ex:\n",
    "    print(ex)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e294471",
   "metadata": {},
   "source": [
    "A runtime error that's not caught gets reported as a Python exception."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d40d86cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "qsharp.eval(\"operation Foo() : Unit { use q = Qubit(); X(q) } Foo()\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba2f98ad",
   "metadata": {},
   "source": [
    "In `%%qsharp` cells, exceptions are handled and displayed as error text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b55e53c",
   "metadata": {
    "scrolled": false,
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation Bar() : Unit {\n",
    "    use q = Qubit();\n",
    "    Std.Diagnostics.DumpMachine();\n",
    "    X(q);\n",
    "}\n",
    "\n",
    "Bar()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98247ac2",
   "metadata": {},
   "source": [
    "Streaming output for long running operations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd25ae87",
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "import Std.Diagnostics.*;\n",
    "\n",
    "operation Main() : Unit {\n",
    "    Message(\"Generating random bit... \");\n",
    "    for i in 0..400000 {\n",
    "        use q = Qubit();\n",
    "        H(q);\n",
    "        let r = M(q);\n",
    "        if (i % 100000) == 0 {\n",
    "            DumpMachine();\n",
    "            Message($\"Result: {r}\");\n",
    "        }\n",
    "        Reset(q);\n",
    "    }\n",
    "}\n",
    "\n",
    "Main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a2d9e7d",
   "metadata": {},
   "source": [
    "Running multiple shots for an expression. Each shot uses an independent instance of the simulator. A list of results (or runtime errors) is returned."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb3cd29f",
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation RandomBit() : Result {\n",
    "    use q = Qubit();\n",
    "    H(q);\n",
    "    let res = M(q);\n",
    "    Reset(q);\n",
    "    return res;\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a9f5335",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = qsharp.run(\"RandomBit()\", 10)\n",
    "\n",
    "results\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a476e6d",
   "metadata": {},
   "source": [
    "The results can then be processed, e.g. plotted in a histogram using popular Python libraries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bd77379",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install matplotlib\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83cee4e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from collections import Counter\n",
    "\n",
    "# Sort the results so that the histogram labels appear in the correct order\n",
    "results.sort()\n",
    "# Count the number of times each result appears\n",
    "counts = Counter(results)\n",
    "\n",
    "(values, counts) = counts.keys(), counts.values()\n",
    "xlabels = np.arange(len(counts))\n",
    "plt.bar(xlabels, counts)\n",
    "plt.xticks(xlabels, values)\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaf766f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install pandas\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8095640a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas\n",
    "from collections import Counter\n",
    "\n",
    "# Sort the results so that the histogram labels appear in the correct order\n",
    "results.sort()\n",
    "pandas.Series(results).value_counts(sort=False).plot(kind='bar')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "990642e4",
   "metadata": {},
   "source": [
    "A compiler error in the entry expression:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f045ad9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "qsharp.run(\"\"\"RandomBit(\"a\")\"\"\", 10)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ba56462",
   "metadata": {},
   "source": [
    "Some shots throw runtime errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b85eb2d",
   "metadata": {
    "vscode": {
     "languageId": "qsharp"
    }
   },
   "outputs": [],
   "source": [
    "%%qsharp\n",
    "\n",
    "operation Bad() : Unit {\n",
    "    use q = Qubit();\n",
    "    H(q);\n",
    "    let res = M(q);\n",
    "    if (res == One) {\n",
    "        // Do something bad, sometimes\n",
    "        use q2 = Qubit();\n",
    "        X(q2);\n",
    "    }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2445df05",
   "metadata": {},
   "outputs": [],
   "source": [
    "qsharp.run(\"Bad()\", 10)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
