{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "746adee2",
   "metadata": {},
   "source": [
    "# Basic OpenQASM code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5f1355d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.openqasm import run, DepolarizingNoise, compile\n",
    "from qdk.widgets import Histogram\n",
    "\n",
    "source = \"\"\"\n",
    "    include \"stdgates.inc\";\n",
    "    bit[2] c;\n",
    "    qubit[2] q;\n",
    "    h q[0];\n",
    "    cx q[0], q[1];\n",
    "    c = measure q;\n",
    "\"\"\"\n",
    "\n",
    "Histogram(run(source, noise=DepolarizingNoise(0.01), as_bitstring=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c20b9ab1",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(compile(source, as_bitstring=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bee6b70",
   "metadata": {},
   "source": [
    "## QASM and code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "253f3b44",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.openqasm import import_openqasm, code\n",
    "\n",
    "source = \"\"\"\n",
    "    include \"stdgates.inc\";\n",
    "    bit[2] c;\n",
    "    qubit[2] q;\n",
    "    h q[0];\n",
    "    cx q[0], q[1];\n",
    "    c = measure q;\n",
    "\"\"\"\n",
    "\n",
    "import_openqasm(source, name=\"bell\")\n",
    "\n",
    "code.bell()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae62fc8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.openqasm import circuit\n",
    "from qdk.widgets import Circuit\n",
    "\n",
    "display(Circuit(circuit(code.bell)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad272e34",
   "metadata": {},
   "source": [
    "## Qiskit integration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce82c8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit\n",
    "import numpy as np\n",
    "\n",
    "circuit = QuantumCircuit(2, 2)\n",
    "circuit.name = \"state_prep\"\n",
    "\n",
    "# State vector to initialize: |ψ⟩ = (|0⟩ - |1⟩) / √2\n",
    "circuit.initialize([1 / np.sqrt(2), -1 / np.sqrt(2)], 0)\n",
    "circuit.h(0)\n",
    "circuit.measure(0, 0)\n",
    "\n",
    "circuit.prepare_state([1 / np.sqrt(2), -1 / np.sqrt(2)], 1)\n",
    "circuit.h(1)\n",
    "circuit.measure(1, 1)\n",
    "\n",
    "circuit.draw(output=\"text\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b54a8930",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.qiskit import QSharpBackend, TargetProfile\n",
    "\n",
    "backend = QSharpBackend(target_profile=TargetProfile.Adaptive_RI)\n",
    "job = backend.run(circuit)\n",
    "counts = job.result().get_counts()\n",
    "print(counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a728e279",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(backend.qir(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b843134d",
   "metadata": {},
   "source": [
    "## Resource estimation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57c6fd03",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.qsharp import init, eval, estimate\n",
    "from qdk.widgets import EstimatesOverview\n",
    "\n",
    "init()\n",
    "with open(\"../estimation/EkeraHastadFactoring.qs\", \"r\") as f:\n",
    "    eval(f.read())\n",
    "\n",
    "rsa_number = 1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139\n",
    "\n",
    "estimates = estimate(f\"EkeraHastad({rsa_number.bit_length()}, {rsa_number}L, 7L)\", [\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_ns_e3\"}},\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_ns_e4\"}},\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_us_e3\"}},\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_gate_us_e4\"}},\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_maj_ns_e4\"}, \"qecScheme\": {\"name\": \"floquet_code\"}},\n",
    "    {\"errorBudget\": 0.333, \"qubitParams\": {\"name\": \"qubit_maj_ns_e6\"}, \"qecScheme\": {\"name\": \"floquet_code\"}}\n",
    "])\n",
    "\n",
    "EstimatesOverview(\n",
    "    estimates,\n",
    "    colors=[\"#1f77b4\", \"#ff7f0e\", \"blue\", \"red\", \"green\", \"yellow\"],\n",
    "    runNames=[\"Gate ns e3, surface\", \"Gate ns e4, surface\", \"Gate us e3, surface\", \"Gate us e4, surface\", \"Majorana ns e4, floquet\", \"Majorana ns e6, floquet\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "241dab0c",
   "metadata": {},
   "source": [
    "## Azure submission"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "822a19b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qdk.qsharp import init, TargetProfile, eval, compile\n",
    "from qdk.azure import Workspace\n",
    "\n",
    "init(target_profile=TargetProfile.Base)\n",
    "\n",
    "eval(\"operation Main() : Result { use q = Qubit(); H(q); return M(q);}\")\n",
    "operation = compile(\"Main()\")\n",
    "\n",
    "workspace = Workspace(\n",
    "    subscription_id = \"916dfd6d-030c-4bd9-b579-7bb6d1926e97\",\n",
    "    resource_group = \"billti-team\",\n",
    "    name = \"billti-quantum01\",\n",
    "    location = \"westus2\",\n",
    ")\n",
    "\n",
    "target = workspace.get_targets(\"quantinuum.sim.h2-2e\")\n",
    "print(target)\n",
    "# job = target.submit(operation, \"my-azure-quantum-job\", shots=100)\n",
    "# print(job.get_results())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
