{
 "cells": [
  {
   "id": "cell-1",
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Compile 30-qubit MaxCut circuits for IBM Fez\n",
    "\n",
    "One connected, unweighted 3-regular graph; 45 edges. The edge list fixes the graph across NetworkX versions. The positive RZZ angles implement the ZZ term of the negative MaxCut objective, up to a global phase. The angles are fixed so the comparison isolates compilation. No hardware job is submitted by this notebook."
   ]
  },
  {
   "id": "cell-2",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "edges = [(0, 18), (0, 25), (0, 26), (1, 4), (1, 6), (1, 17), (2, 5), (2, 8), (2, 19), (3, 10), (3, 13), (3, 21), (4, 11), (4, 13), (5, 9), (5, 15), (6, 17), (6, 24), (7, 20), (7, 24), (7, 27), (8, 18), (8, 29), (9, 14), (9, 23), (10, 20), (10, 23), (11, 21), (11, 24), (12, 19), (12, 25), (12, 26), (13, 16), (14, 21), (14, 22), (15, 22), (15, 27), (16, 20), (16, 25), (17, 27), (18, 23), (19, 28), (22, 28), (26, 29), (28, 29)]\n",
    "assert len(edges) == 45"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "cell-3",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from qiskit import QuantumCircuit\n",
    "\n",
    "gammas = [0.25, 0.35, 0.45, 0.55, 0.65]\n",
    "betas = [0.18, 0.20, 0.22, 0.24, 0.26]\n",
    "\n",
    "def maxcut_circuit(p):\n",
    "    circuit = QuantumCircuit(30)\n",
    "    circuit.h(range(30))\n",
    "    for gamma, beta in zip(gammas[:p], betas[:p]):\n",
    "        for a, b in edges:\n",
    "            circuit.rzz(gamma, a, b)\n",
    "        circuit.rx(2 * beta, range(30))\n",
    "    return circuit\n",
    "\n",
    "circuits = {p: maxcut_circuit(p) for p in (1, 3, 5)}"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "audit-heading",
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check the archived measurements locally\n",
    "\n",
    "Download the compiled circuits ZIP from the solution page and place it next to this notebook. This optional cell reproduces the table from the six saved QPY circuits."
   ]
  },
  {
   "id": "audit-cell",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from pathlib import Path\n",
    "from zipfile import ZipFile\n",
    "from io import BytesIO\n",
    "from qiskit import qpy\n",
    "\n",
    "def metrics(qc):\n",
    "    return {\n",
    "        \"depth\": qc.depth(),\n",
    "        \"two_qubit_depth\": qc.depth(lambda op: len(op.qubits) == 2),\n",
    "        \"two_qubit_gates\": sum(len(op.qubits) == 2 for op in qc.data),\n",
    "    }\n",
    "\n",
    "archive_path = Path(\"compiled-circuits.zip\")\n",
    "if archive_path.is_file():\n",
    "    with ZipFile(archive_path) as archive:\n",
    "        for p in (1, 3, 5):\n",
    "            for name in (\"weylattice\", \"qiskit\"):\n",
    "                qc = qpy.load(BytesIO(archive.read(f\"p{p}-{name}.qpy\")))[0]\n",
    "                print(p, name, metrics(qc))\n",
    "else:\n",
    "    print(\"Download compiled-circuits.zip to inspect the recorded runs.\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "cell-4",
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compile with your Spinorio connection\n",
    "\n",
    "Run these cells in an authenticated Spinorio notebook. Select your IBM Fez target ID. This submits six compilation jobs against that target. The published comparison used the 9 November 2026 Fez target snapshot. Compiler versions, routing choices and target updates can affect subsequent results."
   ]
  },
  {
   "id": "cell-5",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "from spinorio import Spinorio\n",
    "\n",
    "client = Spinorio()\n",
    "target_id = input(\"Spinorio target ID for IBM Fez: \").strip()\n",
    "if not target_id:\n",
    "    raise ValueError(\"Select an IBM Fez target first.\")\n",
    "\n",
    "rows = []\n",
    "for p, circuit in circuits.items():\n",
    "    primary = client.compile.run(circuit, target_id=target_id, optimization_level=2)\n",
    "    compiled = primary.result(wait=True, timeout=900)\n",
    "    rows.append({\"p\": p, \"engine\": \"Weylattice\", **metrics(compiled)})\n",
    "    version_id = primary.status()[\"circuit_version_id\"]\n",
    "    baseline = client.compile.run(circuit_version_id=version_id,\n",
    "                                  target_id=target_id, optimization_level=2,\n",
    "                                  baseline=True, baseline_of_job_id=primary.job_id)\n",
    "    reference = baseline.result(wait=True, timeout=900)\n",
    "    rows.append({\"p\": p, \"engine\": \"Qiskit\", **metrics(reference)})\n",
    "rows"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "cell-6",
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Inspect the candidate partition\n",
    "\n",
    "The listed partition was obtained with Hilvarian\u2019s classical tabu-search routine. Count the crossing edges to check its cut weight independently."
   ]
  },
  {
   "id": "cell-7",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "left = set([0, 2, 3, 4, 6, 7, 11, 12, 14, 15, 16, 17, 23, 28, 29])\n",
    "right = set(range(30)) - left\n",
    "cut_weight = sum((u in left) != (v in left) for u, v in edges)\n",
    "print(sorted(left), sorted(right), cut_weight)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "cell-8",
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare a Hilvarian solver plan for the same graph\n",
    "\n",
    "Inspect the plan before running the final cell. That cell submits a solver run using your Spinorio allowance."
   ]
  },
  {
   "id": "cell-9",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "problem = {\n",
    "    \"schema\": \"spinorio.solver.model.v1\",\n",
    "    \"problem_type\": \"maxcut\",\n",
    "    \"variables\": [str(v) for v in range(30)],\n",
    "    \"edges\": [[str(a), str(b), 1.0] for a, b in edges],\n",
    "}\n",
    "plan = client.solver.plan(problem=problem, executor=\"local_aer\",\n",
    "                          strategy=\"auto\", budget={\"max_total_shots\": 100000})\n",
    "plan.result(wait=True)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "id": "cell-10",
   "cell_type": "code",
   "metadata": {},
   "source": [
    "run = plan.run()\n",
    "run.result(wait=True, timeout=1800)"
   ],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
