{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Connect to system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py4lexis.session import LexisSession\n",
    "from qaas.client import QProvider, QBackend, QJob\n",
    "\n",
    "\n",
    "# 1. Authentication\n",
    "lexis_session = LexisSession()\n",
    "token = lexis_session.get_access_token()\n",
    "\n",
    "# 2. Configure resources\n",
    "PROJECT = \"\"\n",
    "RESOURCE = \"VLQ-XX\"\n",
    "\n",
    "# 3. Initialize QaaS\n",
    "provider = QProvider(PROJECT, token)\n",
    "backend:QBackend = provider.get_backend(RESOURCE)\n",
    "pulla = provider.get_pulla(RESOURCE)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Prepare circuit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit\n",
    "\n",
    "# Define quantum circuit\n",
    "num_qb = 5\n",
    "qc = QuantumCircuit(num_qb)\n",
    "qc.h(0)\n",
    "for qb in range(1, num_qb):\n",
    "    qc.cx(0, qb)\n",
    "\n",
    "qc.barrier()\n",
    "qc.measure_all()\n",
    "\n",
    "qc.draw(output='mpl')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from iqm.qiskit_iqm import  transpile_to_IQM\n",
    "\n",
    "# Transpile circuit\n",
    "qc_transpiled = transpile_to_IQM(qc, backend)\n",
    "qc_transpiled.draw(output=\"mpl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert to pulse schedule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qaas import qiskit_to_pulla, sweep_job_to_qiskit\n",
    "from iqm.pulse.playlist.visualisation.base import inspect_playlist\n",
    "from IPython.core.display import HTML\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\", category=UserWarning, module=\"IPython.core.display\")\n",
    "\n",
    "circuits, compiler = qiskit_to_pulla(pulla, backend, qc_transpiled)\n",
    "\n",
    "SHOTS = 5000\n",
    "settings = compiler.get_settings(circuits=circuits)\n",
    "settings.set_shots(SHOTS)\n",
    "\n",
    "job_definition, context = compiler.compile(circuits, settings=settings)\n",
    "\n",
    "HTML(inspect_playlist(job_definition.sweep_definition.playlist, [0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run pulse schedule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit.visualization import plot_histogram\n",
    "\n",
    "job = pulla.submit_playlist(job_definition, context=context)\n",
    "job.wait_for_completion()\n",
    "\n",
    "results = sweep_job_to_qiskit(job,shots=SHOTS).get_counts()\n",
    "\n",
    "# Plot result\n",
    "print(results)\n",
    "\n",
    "result_clean = {key: count for key, count in results.items() if count > 15}\n",
    "display(plot_histogram(result_clean))"
   ]
  }
 ],
 "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",
   "version": "3.12.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
