{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d493d7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "from pathlib import Path\n",
    "\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "from mqt.bench.evaluation import evaluation\n",
    "\n",
    "font = {\"size\": 16}\n",
    "mpl.rc(\"font\", **font)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03f145b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluation.create_statistics()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "062813f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "with Path(\"evaluation_data.pkl\").open(\"rb\") as f:\n",
    "    eval_data = pickle.load(f)\n",
    "df = pd.DataFrame(eval_data)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b5d8948",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.join(pd.json_normalize(df[\"supermarq_features\"])).drop(\"supermarq_features\", axis=\"columns\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46bcd90a",
   "metadata": {},
   "outputs": [],
   "source": [
    "benchmarks = df[\"filename\"].values\n",
    "num_qubits = df[\"num_qubits\"].values\n",
    "depth = df[\"depth\"].values\n",
    "num_gates = df[\"num_gates\"].values\n",
    "num_multiple_qubit_gates = df[\"num_multiple_qubit_gates\"].values\n",
    "program_communication = df[\"program_communication\"].values\n",
    "critical_depth = df[\"critical_depth\"].values\n",
    "entanglement_ratio = df[\"entanglement_ratio\"].values\n",
    "parallelism = df[\"parallelism\"].values\n",
    "liveness = df[\"liveness\"].values\n",
    "perc_multi = num_multiple_qubit_gates / num_gates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0cde4a8b",
   "metadata": {},
   "source": [
    "# Device Distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "588b8449",
   "metadata": {},
   "outputs": [],
   "source": [
    "overall_count_mapped = evaluation.count_occurrences(benchmarks, \"mapped\")\n",
    "mapped_ibm_montreal_count = evaluation.count_occurrences(benchmarks, \"mapped_ibm_montreal\")\n",
    "mapped_ibm_washington_count = evaluation.count_occurrences(benchmarks, \"mapped_ibm_washington\")\n",
    "mapped_oqc_lucy_count = evaluation.count_occurrences(benchmarks, \"mapped_oqc_lucy\")\n",
    "mapped_rigetti_count = evaluation.count_occurrences(benchmarks, \"mapped_rigetti\")\n",
    "mapped_quantinuum_count = evaluation.count_occurrences(benchmarks, \"mapped_quantinuum\")\n",
    "mapped_ionq_aria1_count = evaluation.count_occurrences(benchmarks, \"mapped_ionq_aria1\")\n",
    "mapped_ionq_harmony_count = evaluation.count_occurrences(benchmarks, \"mapped_ionq_harmony\")\n",
    "\n",
    "qiskit_qubit_numbers = evaluation.count_qubit_numbers_per_compiler(benchmarks, \"qiskit\")\n",
    "tket_qubit_numbers = evaluation.count_qubit_numbers_per_compiler(benchmarks, \"tket\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bfc2767",
   "metadata": {},
   "outputs": [],
   "source": [
    "perc_montreal = mapped_ibm_montreal_count / overall_count_mapped\n",
    "perc_washington = mapped_ibm_washington_count / overall_count_mapped\n",
    "perc_oqc = mapped_oqc_lucy_count / overall_count_mapped\n",
    "perc_rigetti = mapped_rigetti_count / overall_count_mapped\n",
    "perc_quantinuum = mapped_quantinuum_count / overall_count_mapped\n",
    "perc_ionq_aria1 = mapped_ionq_aria1_count / overall_count_mapped\n",
    "perc_ionq_harmony = mapped_ionq_harmony_count / overall_count_mapped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "089acc25",
   "metadata": {},
   "outputs": [],
   "source": [
    "font = {\"size\": 12}\n",
    "mpl.rc(\"font\", **font)\n",
    "data = [\n",
    "    perc_washington,\n",
    "    perc_rigetti,\n",
    "    perc_quantinuum,\n",
    "    perc_montreal,\n",
    "    perc_ionq_aria1,\n",
    "    perc_ionq_harmony,\n",
    "    perc_oqc,\n",
    "]\n",
    "colors = [\"#B7E6A5\", \"#7CCBA2\", \"#46AEA0\", \"#089099\", \"#02818B\", \"#026688\", \"#025582\"]\n",
    "labels = [\n",
    "    \"IBMQ Washington (127)\",\n",
    "    \"Rigetti Aspen M3 (79)\",\n",
    "    \"Quantinuum H2 (32)\",\n",
    "    \"IBMQ Montreal (27)\",\n",
    "    \"IonQ Aria 1 (25)\",\n",
    "    \"IonQ Harmony (11)\",\n",
    "    \"OQC Lucy (8)\",\n",
    "]\n",
    "pies = plt.pie(data, labels=labels, autopct=\"%1.1f%%\", colors=colors)\n",
    "for pie in pies[0]:\n",
    "    pie.set_alpha(0.7)\n",
    "plt.savefig(\"results/pie.pdf\", format=\"pdf\", bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fc0ca44",
   "metadata": {},
   "source": [
    "# Num Qubits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd1cc85d",
   "metadata": {},
   "outputs": [],
   "source": [
    "font = {\"size\": 16}\n",
    "mpl.rc(\"font\", **font)\n",
    "qiskit_qubit_numbers = evaluation.count_qubit_numbers_per_compiler(benchmarks, \"qiskit\")\n",
    "tket_qubit_numbers = evaluation.count_qubit_numbers_per_compiler(benchmarks, \"tket\")\n",
    "\n",
    "# colors = ['#7CCBA2', '#46AEA0', '#089099', \"#00718B\", \"#045275\", \"#003147\"]\n",
    "# colors = ['#089099', '#003147']\n",
    "plt.figure(figsize=(8, 5))\n",
    "plot = sns.histplot(\n",
    "    data=[tket_qubit_numbers, qiskit_qubit_numbers],\n",
    "    bins=129,\n",
    "    stat=\"density\",\n",
    "    alpha=0.3,\n",
    "    kde=True,\n",
    "    edgecolor=\"white\",\n",
    "    linewidth=0.5,\n",
    "    palette=sns.color_palette(\"viridis\", 2)[::-1],\n",
    ")\n",
    "plt.legend([\"Qiskit\", \"TKET\"])\n",
    "plt.ylabel(\"Relative Frequency\")\n",
    "plt.xlabel(\"Number of Qubits\")\n",
    "plt.savefig(\"results/qubit_dist.pdf\", format=\"pdf\", bbox_inches=\"tight\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed8581be",
   "metadata": {},
   "source": [
    "# Further Characteristics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f71d39c",
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets = [\n",
    "    program_communication.astype(float),\n",
    "    critical_depth.astype(float),\n",
    "    entanglement_ratio.astype(float),\n",
    "    liveness.astype(float),\n",
    "    parallelism.astype(float),\n",
    "    perc_multi.astype(float),\n",
    "]\n",
    "labels = [\n",
    "    \"Program Communication\",\n",
    "    \"Critical Depth\",\n",
    "    \"Entanglement Ratio\",\n",
    "    \"Liveness\",\n",
    "    \"Parallelism\",\n",
    "    \"Percentage \\nMulti-Qubit Gates\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54ac24f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(19, 4))\n",
    "ax = fig.add_subplot(111)\n",
    "colors = [\"#7CCBA2\", \"#46AEA0\", \"#089099\", \"#00718B\", \"#045275\", \"#003147\"]\n",
    "pos = range(len(datasets))\n",
    "plots = plt.violinplot(datasets, pos)\n",
    "for pc, color in zip(plots[\"bodies\"], colors, strict=False):\n",
    "    pc.set_facecolor(color)\n",
    "\n",
    "ax.set_xticks(pos)\n",
    "plt.xticks(rotation=0)\n",
    "ax.set_xticklabels(labels)\n",
    "plt.savefig(\"results/violins.pdf\", format=\"pdf\", bbox_inches=\"tight\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
