{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Selene quickstart\n",
    "\n",
    "Selene is the Quantinuum emulator that runs compiled Guppy programs, with swappable simulators and error models. Every sweep in this repo is validated on Selene before a single paid Nexus shot is bought.\n",
    "\n",
    "Generated from the crawled `selene` corpus (crawl date 2026-08-24) by `python -m quantum.docs_crawler.notebooks`. Do not edit by hand — edit the generator or pin a snippet out via `notebook_manifest.json`.\n",
    "\n",
    "```bash\n",
    "pip install \"guppylang>=1.0\"   # selene-sim ships with guppylang\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Environment check\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import selene_sim\n",
    "from selene_sim import Quest, IdealErrorModel, DepolarizingErrorModel\n",
    "\n",
    "print('selene_sim', getattr(selene_sim, '__version__', 'unknown'))\n",
    "# Only these noise models exist — there is no coherent / T1-T2 model.\n",
    "print([n for n in dir(selene_sim) if n.endswith('ErrorModel')])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Core examples\n",
    "\n",
    "Each cell below is an upstream example, linked to its source page.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Basic Usage](https://docs.quantinuum.com/selene/user_guide/usage.html)** — `user_guide/usage` block 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.quantum import qubit, h, measure\n",
    "from guppylang.std.builtins import result\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q0: qubit = qubit()\n",
    "    h(q0)\n",
    "    c0 = measure(q0)\n",
    "    result(\"c0\", c0)\n",
    "    if c0:\n",
    "        q1: qubit = qubit()\n",
    "        h(q1)\n",
    "        c1 = measure(q1)\n",
    "        result(\"c1\", c1)\n",
    "        if c1:\n",
    "            q2: qubit = qubit()\n",
    "            h(q2)\n",
    "            c2 = measure(q2)\n",
    "            result(\"c2\", c2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Basic Usage](https://docs.quantinuum.com/selene/user_guide/usage.html)** — `user_guide/usage` block 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build\n",
    "\n",
    "hugr = main.compile()\n",
    "runner = build(hugr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Basic Usage](https://docs.quantinuum.com/selene/user_guide/usage.html)** — `user_guide/usage` block 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ok, so let's construct some classical replay data\n",
    "# to run through all classical branches\n",
    "measurements = [[False], [True, False], [True, True, True]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Basic Usage](https://docs.quantinuum.com/selene/user_guide/usage.html)** — `user_guide/usage` block 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from hugr.qsystem.result import QsysResult\n",
    "from selene_sim import ClassicalReplay\n",
    "\n",
    "shots = QsysResult(\n",
    "    runner.run_shots(\n",
    "        simulator=ClassicalReplay(\n",
    "            measurements=measurements\n",
    "        ),\n",
    "        n_qubits=3,\n",
    "        n_shots=3,\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation](https://docs.quantinuum.com/selene/user_guide/simulation.html)** — `user_guide/simulation` block 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.builtins import array, result\n",
    "from guppylang.std.quantum import qubit, measure, h, cx, measure\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    qubit_top = qubit()\n",
    "    h(qubit_top)\n",
    "    qubits = array(qubit() for _ in range(3))\n",
    "    for q in qubits:\n",
    "        cx(qubit_top, q)\n",
    "        m = measure(q)\n",
    "        result(\"result\", m)\n",
    "    m_top = measure(qubit_top)\n",
    "    result(\"result\", m_top)\n",
    "\n",
    "hugr = main.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation](https://docs.quantinuum.com/selene/user_guide/simulation.html)** — `user_guide/simulation` block 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build\n",
    "from selene_sim import Quest\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "runner = build(hugr)\n",
    "\n",
    "simulator = Quest()\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    n_qubits=4,\n",
    "    n_shots=100,\n",
    "))\n",
    "print(qsys_result.collated_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation](https://docs.quantinuum.com/selene/user_guide/simulation.html)** — `user_guide/simulation` block 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import Stim\n",
    "\n",
    "simulator = Stim()\n",
    "\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    n_qubits=4,\n",
    "    n_shots=100,\n",
    "))\n",
    "print(qsys_result.collated_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation](https://docs.quantinuum.com/selene/user_guide/simulation.html)** — `user_guide/simulation` block 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import Coinflip\n",
    "\n",
    "measurement_result = QsysResult(runner.run_shots(\n",
    "  Coinflip(), \n",
    "  random_seed=249, \n",
    "  n_qubits=4, \n",
    "  n_shots=100\n",
    "))\n",
    "print(qsys_result.collated_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation](https://docs.quantinuum.com/selene/user_guide/simulation.html)** — `user_guide/simulation` block 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "  q0: qubit = qubit()\n",
    "  h(q0)\n",
    "  c0: bool = measure(q0)\n",
    "  result(\"c0\", c0)\n",
    "  if c0:\n",
    "    q1: qubit = qubit()\n",
    "    h(q1)\n",
    "    c1: bool = measure(q1)\n",
    "    result(\"c1\", c1)\n",
    "\n",
    "hugr_conditional = main.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Error Model](https://docs.quantinuum.com/selene/user_guide/error_model.html)** — `user_guide/error_model` block 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build, Quest, DepolarizingErrorModel\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "runner = build(hugr)\n",
    "\n",
    "error_model = DepolarizingErrorModel(\n",
    "    random_seed=123141,\n",
    "    p_1q=1e-5,\n",
    "    p_2q=1.4e-4,\n",
    "    p_meas=1e-3,\n",
    "    p_init=1e-5\n",
    ")\n",
    "\n",
    "simulator = Quest()\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    error_model=error_model,\n",
    "    n_qubits=4,\n",
    "    n_shots=100,\n",
    "))\n",
    "print(qsys_result.collated_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Error Model](https://docs.quantinuum.com/selene/user_guide/error_model.html)** — `user_guide/error_model` block 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build, Quest, SimpleLeakageErrorModel\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "runner = build(hugr)\n",
    "\n",
    "error_model = SimpleLeakageErrorModel(\n",
    "    random_seed=123141,\n",
    "    p_leak=0.1,\n",
    "    leak_measurement_bias=0.6\n",
    ")\n",
    "\n",
    "simulator = Quest()\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    error_model=error_model,\n",
    "    n_qubits=4,\n",
    "    n_shots=100,\n",
    "))\n",
    "print(qsys_result.collated_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Runtime](https://docs.quantinuum.com/selene/user_guide/runtime.html)** — `user_guide/runtime` block 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build\n",
    "from selene_sim import Quest\n",
    "from selene_sim import SimpleRuntime\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "runner = build(hugr)\n",
    "\n",
    "simulator = Quest()\n",
    "helios_runtime = SimpleRuntime()\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    n_qubits=4,\n",
    "    runtime=helios_runtime,\n",
    "    n_shots=100,\n",
    "))\n",
    "qsys_result.collated_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Runtime](https://docs.quantinuum.com/selene/user_guide/runtime.html)** — `user_guide/runtime` block 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import build, Quest, SimpleRuntime\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "runner = build(hugr)\n",
    "\n",
    "simple_runtime = SimpleRuntime()\n",
    "\n",
    "simulator = Quest()\n",
    "\n",
    "qsys_result = QsysResult(runner.run_shots(\n",
    "    simulator, \n",
    "    n_qubits=4,\n",
    "    runtime=simple_runtime,\n",
    "    n_shots=100,\n",
    "))\n",
    "\n",
    "qsys_result.collated_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[MPS Simulator](https://docs.quantinuum.com/selene/user_guide/mps.html)** — `user_guide/mps` block 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.builtins import array, result, comptime\n",
    "from guppylang.std.quantum import qubit, h, cx, t, measure_array\n",
    "\n",
    "n_qubits = 100\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    n_layers = 16\n",
    "    qs = array(qubit() for _ in range(100))\n",
    "    for _ in range(n_layers):\n",
    "        for i in range(comptime(100//2)):\n",
    "            q0 = 2*i\n",
    "            q1 = 2*i+1\n",
    "            h(qs[q0])\n",
    "            h(qs[q1])\n",
    "            cx(qs[q0], qs[q1])\n",
    "            t(qs[q1])\n",
    "            cx(qs[q0], qs[q1])\n",
    "        for i in range(comptime(100//2 - 2)):\n",
    "            q0 = 2*i+1\n",
    "            q1 = 2*i+2\n",
    "            h(qs[q0])\n",
    "            h(qs[q1])\n",
    "            cx(qs[q0], qs[q1])\n",
    "            t(qs[q1])\n",
    "            cx(qs[q0], qs[q1])\n",
    "    \n",
    "    result(\"c\", measure_array(qs))\n",
    "\n",
    "hugr_program = main.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[MPS Simulator](https://docs.quantinuum.com/selene/user_guide/mps.html)** — `user_guide/mps` block 6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "digitized_counts = {c[0][1]: r/100 for c, r in counts.items() if r > 0}\n",
    "df = pd.DataFrame(digitized_counts.items(), columns=[\"Outcome\", \"Probability\"]).sort_values(\"Probability\", ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation Logs](https://docs.quantinuum.com/selene/user_guide/simulation_logs.html)** — `user_guide/simulation_logs` block 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.quantum import qubit, h, measure\n",
    "from guppylang.std.builtins import result\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "  q0: qubit = qubit()\n",
    "  h(q0)\n",
    "  c0: bool = measure(q0)\n",
    "  result(\"c0\", c0)\n",
    "  if c0:\n",
    "    q1: qubit = qubit()\n",
    "    h(q1)\n",
    "    c1: bool = measure(q1)\n",
    "    result(\"c1\", c1)\n",
    "    if c1:\n",
    "      q2: qubit = qubit()\n",
    "      h(q2)\n",
    "      c2: bool = measure(q2)\n",
    "      result(\"c2\", c2)\n",
    "\n",
    "hugr = main.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation Logs](https://docs.quantinuum.com/selene/user_guide/simulation_logs.html)** — `user_guide/simulation_logs` block 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim.event_hooks import CircuitExtractor\n",
    "from selene_sim import Coinflip, build\n",
    "\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "event_hook = CircuitExtractor() \n",
    "runner = build(hugr)\n",
    "\n",
    "shots = QsysResult(runner.run_shots(\n",
    "    simulator=Coinflip(),\n",
    "    n_qubits=3,\n",
    "    n_shots=100,\n",
    "    event_hook=event_hook\n",
    "))\n",
    "shots.collated_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Simulation Logs](https://docs.quantinuum.com/selene/user_guide/simulation_logs.html)** — `user_guide/simulation_logs` block 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selene_sim import MetricStore, MultiEventHook, SoftRZRuntime\n",
    "\n",
    "\n",
    "event_hook = MultiEventHook(\n",
    "   event_hooks=[\n",
    "      CircuitExtractor(), \n",
    "      MetricStore()\n",
    "])\n",
    "\n",
    "shots = QsysResult(runner.run_shots(\n",
    "   Coinflip(),\n",
    "   runtime=SoftRZRuntime(),\n",
    "   n_qubits=2,\n",
    "   n_shots=2,\n",
    "   event_hook=event_hook,\n",
    "))\n",
    "\n",
    "shots.collated_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Debugging Runtime Errors](https://docs.quantinuum.com/selene/user_guide/runtime_errors.html)** — `user_guide/runtime_errors` block 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.builtins import array, result, comptime\n",
    "from guppylang.std.quantum import cx, qubit, measure_array\n",
    "from guppylang.std.debug import state_result\n",
    "\n",
    "N_QUBITS = 16\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q = array(qubit() for _ in range(comptime(N_QUBITS)))\n",
    "    for n in range(comptime(N_QUBITS)):\n",
    "        i = n \n",
    "        j = n + 1\n",
    "        cx(q[i], q[j])\n",
    "    c = measure_array(q)\n",
    "    result(\"measurement_result\", c)\n",
    "\n",
    "hugr = main.compile()\n",
    "\n",
    "\n",
    "from selene_sim import Stim, build\n",
    "from selene_sim.result_handling.parse_shot import postprocess_unparsed_stream\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "hugr_binary = main.compile()\n",
    "runner = build(hugr_binary)\n",
    "\n",
    "shots, error = postprocess_unparsed_stream(\n",
    "    runner.run_shots(\n",
    "        simulator=Stim(),\n",
    "        n_qubits=N_QUBITS,\n",
    "        n_shots=10,\n",
    "        parse_results=False\n",
    "    ),\n",
    ")\n",
    "\n",
    "print(error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Debugging Runtime Errors](https://docs.quantinuum.com/selene/user_guide/runtime_errors.html)** — `user_guide/runtime_errors` block 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.builtins import array, result\n",
    "from guppylang.std.quantum import cx, qubit, measure_array\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q = array(qubit() for _ in range(comptime(N_QUBITS)))\n",
    "    for n in range(comptime(N_QUBITS - 1)):\n",
    "        i = n \n",
    "        j = n + 1\n",
    "        cx(q[i], q[j])\n",
    "    c = measure_array(q)\n",
    "    result(\"measurement_result\", c)\n",
    "\n",
    "hugr = main.compile()\n",
    "\n",
    "\n",
    "from selene_sim import Stim, build\n",
    "from selene_sim.result_handling.parse_shot import postprocess_unparsed_stream\n",
    "from hugr.qsystem.result import QsysResult\n",
    "\n",
    "hugr_binary = main.compile()\n",
    "runner = build(hugr_binary)\n",
    "\n",
    "shots, error = postprocess_unparsed_stream(\n",
    "    runner.run_shots(\n",
    "        simulator=Stim(),\n",
    "        n_qubits=N_QUBITS,\n",
    "        n_shots=10,\n",
    "        parse_results=False\n",
    "    ),\n",
    ")\n",
    "\n",
    "print(error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Debugging Runtime Errors](https://docs.quantinuum.com/selene/user_guide/runtime_errors.html)** — `user_guide/runtime_errors` block 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang import guppy\n",
    "from guppylang.std.builtins import array, result, comptime\n",
    "from guppylang.std.quantum import cx, qubit, measure_array\n",
    "\n",
    "from selene_sim import Stim, build\n",
    "from selene_sim.result_handling.parse_shot import postprocess_unparsed_stream\n",
    "\n",
    "N_QUBITS = 16\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q = array(qubit() for _ in range(comptime(N_QUBITS)))\n",
    "    for n in range(comptime(N_QUBITS // 2)):\n",
    "        i = 2 * n \n",
    "        j = 2 * n\n",
    "        cx(q[i], q[j])\n",
    "    c = measure_array(q)\n",
    "    result(\"measurement_result\", c)\n",
    "\n",
    "runner = build(main.compile(), \"no-cloning-error\")\n",
    "\n",
    "shots, error = postprocess_unparsed_stream(\n",
    "    runner.run_shots(\n",
    "        simulator=Stim(),\n",
    "        n_qubits=N_QUBITS,\n",
    "        n_shots=10,\n",
    "        parse_results=False\n",
    "    ),\n",
    ")\n",
    "print(error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Debugging Runtime Errors](https://docs.quantinuum.com/selene/user_guide/runtime_errors.html)** — `user_guide/runtime_errors` block 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "N_QUBITS = 16\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q = array(qubit() for _ in range(comptime(N_QUBITS)))\n",
    "    for n in range(comptime(N_QUBITS // 2)):\n",
    "        i = 2 * n \n",
    "        j = 2 * n + 1\n",
    "        cx(q[i], q[j])\n",
    "    c = measure_array(q)\n",
    "    result(\"measurement_result\", c)\n",
    "\n",
    "runner = build(main.compile(), \"no-cloning-error\")\n",
    "\n",
    "shots, error = postprocess_unparsed_stream(\n",
    "    runner.run_shots(\n",
    "        simulator=Stim(),\n",
    "        n_qubits=N_QUBITS,\n",
    "        n_shots=10,\n",
    "        parse_results=False\n",
    "    ),\n",
    ")\n",
    "print(error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**[Debugging Runtime Errors](https://docs.quantinuum.com/selene/user_guide/runtime_errors.html)** — `user_guide/runtime_errors` block 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from guppylang.decorator import guppy\n",
    "from guppylang.std.quantum import qubit, h, measure\n",
    "from guppylang.std.builtins import result, panic\n",
    "\n",
    "\n",
    "@guppy\n",
    "def main() -> None:\n",
    "    q = qubit()\n",
    "    h(q)\n",
    "    outcome = measure(q)\n",
    "    if outcome:\n",
    "        panic(\"Postselection failed\")\n",
    "    result(\"c\", outcome)\n",
    "\n",
    "runner = build(main.compile(), \"panic\")\n",
    "\n",
    "shots, error = postprocess_unparsed_stream(\n",
    "    runner.run_shots(\n",
    "        simulator=Stim(),\n",
    "        n_qubits=1,\n",
    "        n_shots=10,\n",
    "        parse_results=False\n",
    "    ),\n",
    ")\n",
    "print(error)\n",
    "print(shots)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Bridge: Selene in our sweep runner\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# quantum/emulate.py is the single execution entry point; quantum/backends.py\n",
    "# picks emulator vs cloud and emits one job meter per run.\n",
    "#\n",
    "#     from quantum.emulate import build, Quest, DepolarizingErrorModel\n",
    "#     shots = build(prog).run_shots(\n",
    "#         Quest(), n_qubits=n, n_shots=512,\n",
    "#         error_model=DepolarizingErrorModel(p_1q=1e-4, p_2q=1.29e-3),\n",
    "#         seed=1234,\n",
    "#     )\n",
    "#\n",
    "# Long sweeps go through quantum/sweep.py (SweepRunner), which caches each row\n",
    "# to _cache_<name>/<row-tag>.json so a sandbox timeout costs one row, not the run.\n",
    "print('see quantum/emulate.py, quantum/backends.py, quantum/sweep.py')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Pitfalls\n",
    "\n",
    "1. No coherent / T1-T2 noise model ships with `selene_sim`. Only `IdealErrorModel`, `DepolarizingErrorModel` and `SimpleLeakageErrorModel` exist.\n",
    "2. Vendor-realistic H2 targets: `p_2q = 1.29e-3`, `p_r_01 = 0.9e-3`, `p_r_10 = 1.8e-3`.\n",
    "3. Long sweeps must be resumable via a per-row JSON cache — a single sandbox timeout otherwise wipes the whole run.\n",
    "4. Ship results as committed JSON under `src/data/demos/`, never as a live server function: the Worker runtime stubs `child_process` and blocks arbitrary filesystem reads.\n",
    "5. Compare probabilities against `4*sqrt(0.5/shots)`, not `3σ·sqrt(p(1-p)/n)` — the textbook form produces false FAILs near p = 0 or p = 1.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  },
  "title": "Selene quickstart"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
