{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd995976",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A good physical qubit gets an operation wrong about once in a thousand\n",
    "# tries, and a useful algorithm needs billions right in a row. Error\n",
    "# correction closes that gap by encoding one reliable logical qubit into a\n",
    "# patch of many physical ones -- and once patches are split across\n",
    "# QPUs, the boundary between them needs a Bell pair delivered over the\n",
    "# network every single round.\n",
    "\n",
    "from qndk.circuit.qiskit_circuits import QiskitCircuit\n",
    "from qndk.quantum_network import build_bcube, QPUSpec\n",
    "from qndk.simulator.dqc import DQCScheduler\n",
    "from qndk.quantum_error_correction import (\n",
    "    fault_tolerant_encode, tler, plot_comparison,\n",
    "    SurfaceCode, FixedIdleEpr, PoissonEpr)\n",
    "\n",
    "circuit = QiskitCircuit(\"tfim\", num_qubits=4, num_trotter_steps=1, clifford_t_synthesis=True)\n",
    "network = build_bcube(\n",
    "    switch_layers=0, num_qpus=2,\n",
    "    qpu_spec=QPUSpec(comp_qubits=4, connectivity=\"all2all\")\n",
    ")\n",
    "_ = network.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11a62189",
   "metadata": {},
   "outputs": [],
   "source": [
    "code = SurfaceCode(d=3)\n",
    "try:\n",
    "    pr = fault_tolerant_encode(circuit, network, code=code, num_ancilla=1)\n",
    "except Exception as e:\n",
    "    print('Exception:', e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3e22aea",
   "metadata": {},
   "outputs": [],
   "source": [
    "network = build_bcube(\n",
    "    switch_layers=0, num_qpus=2,\n",
    "    qpu_spec=QPUSpec(comp_qubits=4, connectivity=\"all2all\"),\n",
    "    num_magic_factories=1 # necessary for QEC\n",
    ")\n",
    "\n",
    "code = SurfaceCode(d=3)\n",
    "pr = fault_tolerant_encode(circuit, network, code=code, num_ancilla=1)\n",
    "_ = pr.plot_encoded_network()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a10eea8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = DQCScheduler(pr).run_simulation()\n",
    "\n",
    "# k_idle == a factor describing idle noise while a patch waits for EPRs\n",
    "# lam_seam == a factor describing how noisy operations across seams are than local operations\n",
    "# distill_rounds == how many time the magic-state factory repeats its purification step\n",
    "ler = tler(result, code=SurfaceCode(d=15), epr=FixedIdleEpr(lam_seam=4, k_idle=4), distill_rounds=2)\n",
    "\n",
    "# total logical error rate is an additive regime where all contributions from different\n",
    "# error sources are summed up\n",
    "_ = ler.plot(show_validity=False, show_thresholds=False,\n",
    "             show_distill_rounds=False, show_uncorrected=True,\n",
    "             show_full_range=True)\n",
    "\n",
    "# the contribution of k_idle makes the remote gate_ler higher. even though the number of local\n",
    "# gates is higher than the number of remote gates, the error contribution is approximately the\n",
    "# same"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c037803",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A bigger code does not always win. Race a big patch on a slow network\n",
    "# (d=17, k_idle=16) against a smaller patch on a fast one (d=13, k_idle=4).\n",
    "# Above the crossing, the big code is *worse* -- its extra seam rounds cost\n",
    "# more entanglement than its distance buys back -- and only below it does\n",
    "# distance take over.\n",
    "big_slow = tler(result, code=SurfaceCode(d=17), epr=FixedIdleEpr(k_idle=16))\n",
    "small_fast = tler(result, code=SurfaceCode(d=13), epr=FixedIdleEpr(k_idle=4))\n",
    "_ = plot_comparison([big_slow, small_fast],\n",
    "                label_fn=lambda r: f\"d={r.d}, k_idle={r.epr.k_idle}\")\n",
    "\n",
    "# k_idle is associated with the latency introduced by epr generation rate. when\n",
    "# distance `d` is higher, the load on the network related to the number of epr\n",
    "# pairs required increases which means syndrome cycles are stretched, increasing\n",
    "# the overall error contribution from the network. there is a crossover to\n",
    "# a single syndrome round is made up of `d` measurements of the ancilla seam\n",
    "# syndrome measurements. and then you do `d` rounds of syndrome measurements "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env (3.13.5)",
   "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.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
