{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Programmation Quantique\n",
    "\n",
    "### Nicolas Ollinger et Ioan Todinca\n",
    "<img style=\"width:15%; height: auto;\" src=\"img/lifo.png\">\n",
    "\n",
    "#### M2 info, S9 2024/2025"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Descriptif du cours\n",
    "\n",
    "Ce cours propose de découvrir l'informatique quantique à\n",
    "travers la <b class=\"cool\">programmation de circuits quantiques</b>. \n",
    "\n",
    "Les étudiants sont\n",
    "invités à <b class=\"cool\">construire des circuits</b> et à <b class=\"cool\">simuler leur exécution</b> pour\n",
    "comprendre ce que propose la technologie quantique actuelle et comment\n",
    "elle se distingue des modèles classiques de <b class=\"cool\">calcul booléen</b>. Cette\n",
    "approche expérimentale permettra ensuite de présenter plus précisemment\n",
    "les promesses algorithmiques et les enjeux actuels de cette technologie.\n",
    "\n",
    "L'évaluation prend la forme d'un contrôle terminal et d'<b class=\"cool\">éléments à réaliser sur machine</b> et à\n",
    "rendre à l'issu des TPs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Plan de bataille\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\textbf{ARIAS}\\quad 10\\mathrm{h~} &\\textbf{CM} + 10\\mathrm{h~} \\textbf{TD} + 10\\mathrm{h~} \\textbf{TP}\\\\\n",
    "\\textbf{GPEX}\\quad 10\\mathrm{h~} &\\textbf{CM} + \\phantom{1}6\\mathrm{h~} \\textbf{TD}\\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "| Séance | Intervenant | Durée | Programme |\n",
    "| :-- | :-- | :-: | :-- |\n",
    "| CM1 | N. Ollinger | __2h__ | *Circuits quantiques et calcul réversible* |\n",
    "| CM2 | N. Ollinger | __2h__ | *Circuits et calcul quantique, l'algorithme de Bernstein-Vazirani* |\n",
    "| CM3 | I. Todinca | __2h__ | *Informatique quantique, algorithmes importants* |\n",
    "| CM4 | I. Todinca | __2h__ | *L'algorithme de recherche quantique de Grover* |\n",
    "| CM5 | I. Todinca | __2h__ | *Informatique quantique pour informaticien !* |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Évaluation\n",
    "\n",
    "$$\n",
    "\\textbf{UE} = \\frac13 \\textbf{CC} + \\frac23 \\textbf{CT}\n",
    "$$\n",
    "\n",
    "$\\textbf{CC}$ : Des notebook Jupyter à compléter et à rendre sur [Celene](https://celene.univ-orleans.fr/course/view.php?id=12816).\n",
    "\n",
    "Exercices à réaliser à l'aide de Python et de la bibliothèque [Qiskit](https://qiskit.org) (v1.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Circuits quantiques ?\n",
    "\n",
    "Les ordinateurs quantiques d'aujourd'hui (ou plutôt de demain ?) s'utilisent comme des _coprocesseurs_ : on y fait subir une <b class=\"cool\">transformation unitaire</b> à un <b class=\"cool\">registre de qubits</b> initialement tous à zéro avant d'effectuer des <b class=\"cool\">mesures</b> sur son état.\n",
    "\n",
    "Les <b class=\"cool\">transformations unitaires</b> sont codées sous forme de circuits et la programmation quantique ressemble à de la <b class=\"cool\">programmation assembleur</b>.\n",
    "\n",
    "La programmation quantique propose une <b class=\"cool\">nouvelle forme de parallélisme</b>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"width:100%; height: auto;\" src=\"img/quito1.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"width:100%; height: auto;\" src=\"img/quito2.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"width:100%; height: auto;\" src=\"img/quito3.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Illustré avec Qiskit\n",
    "\n",
    "Ce cours est aussi un notebook jupyter téléchargeable et exécutable dans le Cloud dont les exemples sont construits à l'aide de la bibliothèque __[Qiskit](https://qiskit.org)__ qui permet de manipuler des circuits quantiques en Python :\n",
    "  1. <b class=\"cool\">construire</b> des circuits ;\n",
    "  2. <b class=\"cool\">exécuter</b> ces circuits localement dans un  simulateur ;\n",
    "  3. <b class=\"cool\">exécuter</b> ces circuits sur des processeurs quantique (par exemple d'IBM) ;\n",
    "  4. <b class=\"cool\">analyser</b> les résultats des exécutions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "### Importons le nécessaire"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit import QuantumCircuit, transpile\n",
    "from qiskit.providers.basic_provider import BasicProvider\n",
    "from qiskit_aer import UnitarySimulator\n",
    "from qiskit.visualization import plot_histogram\n",
    "import qiskit.quantum_info as qi\n",
    "from math import sqrt\n",
    "import numpy as np\n",
    "\n",
    "# Un simulateur de circuit complet\n",
    "sim = BasicProvider().get_backend('basic_simulator')\n",
    "\n",
    "# Un simulateur calculant la matrice de transformation\n",
    "usim = UnitarySimulator()\n",
    "\n",
    "# Afficher un circuit\n",
    "def draw_circ(circ): display(circ.draw(output='mpl'))\n",
    "    \n",
    "# Calculer la matrice d'un circuit\n",
    "def mat_circ(circ): return usim.run(circ).result().get_unitary()\n",
    "\n",
    "def show(circ):\n",
    "    draw_circ(circ)\n",
    "    display(mat_circ(circ).draw('latex'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Mais au fait... <b class=\"cool\">C'est quoi un circuit quantique ?</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Un zeste de théorie..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Circuits booléens classiques\n",
    "\n",
    "Depuis les travaux de Shannon à la fin des années 30, l'<b class=\"cool\">algèbre booléenne</b> et le système binaire fondent l'électronique numérique.\n",
    "\n",
    "Un registre de <b class=\"cool\">mémoire classique</b> stocke un vecteur de <b class=\"cool\">bits</b> qui prennent chacun une valeur parmi deux : $0$ ou $1$. Les bits sont indépendants, $n$ bits stockent un valeur parmi $2^n$.\n",
    "\n",
    "Des <b class=\"cool\">portes logiques</b> réalisant des fonctions logiques ($\\operatorname{ET}$, $\\operatorname{OU}$, $\\operatorname{NON}$) permettent de <b class=\"cool\">calculer efficacement</b> toute fonction booléenne $f : \\left\\{0,1\\right\\}^n \\longrightarrow \\left\\{0,1\\right\\}^m$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Un qubit\n",
    "\n",
    "Un registre de <b class=\"cool\">mémoire quantique</b> est un <b class=\"cool\">système quantique discret fini</b> constitué de <b class=\"cool\">qubits</b>.\n",
    "\n",
    "Un <b class=\"cool\">bit quantique</b> est une superposition de $0$ et de $1$ :\n",
    "$$\n",
    "\\alpha \\left|0\\right> + \\beta \\left|1\\right> \\quad\\mbox{avec}\\quad \\alpha, \\beta \\in \\mathbb{C}\n",
    "\\quad\\mbox{tels que}\\quad |\\alpha|^2+|\\beta|^2 = 1\n",
    "$$\n",
    "\n",
    "Par exemple $\\left|0\\right>$ ou encore $\\frac{1}{\\sqrt{2}}\\left(\\left|0\\right>+\\left|1\\right>\\right)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Des qubits\n",
    "\n",
    "Un registre de <b class=\"cool\">$n$ qubits</b> est une superposition de chacun des vecteurs de bits possibles, chacun muni d'une amplitude.\n",
    "\n",
    "$$\n",
    "\\left|\\varphi\\right> = \\sum_{x\\in\\{0,1\\}^n} \\alpha_x\\left|x\\right> \\quad\\mbox{tels que}\\quad \\sqrt{\\sum_{x\\in\\{0,1\\}^n}  |\\alpha_x|^2} = 1\n",
    "$$\n",
    "\n",
    "Par exemple $\\frac12\\left|1000\\right>+\\frac12\\left|0100\\right>+\\frac12\\left|0010\\right>+\\frac12\\left|0001\\right>$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Produit tensoriel\n",
    "\n",
    "La <b class=\"cool\">composition</b> de deux registres quantiques s'obtient par l'opérateur bilinéaire de produit tensoriel $\\otimes$ qui satisfait $\\left|x\\right> \\otimes \\left|y\\right> = \\left|xy\\right>$.\n",
    "\n",
    "$$\n",
    "\\left|1\\right> \\otimes \\frac{1}{\\sqrt{2}}\\left(\\left|0\\right>+\\left|1\\right>\\right) =\n",
    "\\frac{1}{\\sqrt{2}}\\left(\\left|10\\right>+\\left|11\\right>\\right)\n",
    "$$\n",
    "\n",
    "Contrairement au cas classique, tout registre n'est pas décomposable en sous-registres ! \n",
    "\n",
    "Exemple d'état <b class=\"cool\">intriqué</b> : $\\frac{1}{\\sqrt{2}}\\left(\\left|00\\right>+\\left|11\\right>\\right)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Mesure quantique (observation)\n",
    "\n",
    "<img style=\"width=100%; height=auto; margin: auto\" src=\"img/mesure.png\" />"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Mesure quantique d'un qubit particulier\n",
    "\n",
    "Partant d'un état $\\left|\\varphi\\right> = \\sum_x \\alpha_x\\left|x\\right>$, si on mesure le qubit numéro $i$, on observe la valeur $r$ avec la probabilité $p_r$ et le système change d'état :\n",
    "\n",
    "$$\n",
    "\\sum_{x\\in\\left\\{0,1\\right\\}^n} \\alpha_x\\left|x\\right> \\underset{\\phantom{blop}p_r\\phantom{blop}}{\\longrightarrow}\n",
    "\\frac{1}{\\sqrt{p_r}} \\sum_{x\\in\\left\\{0,1\\right\\}^n | x_i = r} \\alpha_x\\left|x\\right>\n",
    "$$\n",
    " \n",
    "$$\n",
    "\\mbox{avec}\\quad p_r = \\sum_{x\\in\\left\\{0,1\\right\\}^n | x_i = r}|\\alpha_x|^2\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Évolution unitaire\n",
    "\n",
    "En l'absence de mesure, lorsqu'il est isolé, l'état d'un système quantique évolue de manière <b class=\"cool\">unitaire</b>.\n",
    "\n",
    "$$\n",
    "U : \\mathbb{C}^{\\{0,1\\}^n} \\rightarrow \\mathbb{C}^{\\{0,1\\}^n}\n",
    "$$\n",
    "\n",
    "1. $U$ est linéaire : $$\n",
    "U\\left(\\alpha\\left|\\varphi\\right>+\\beta\\left|\\psi\\right>\\right) = \\alpha U\\left|\\varphi\\right>+\\beta U\\left|\\psi\\right>\\quad;\n",
    "$$\n",
    "2. $U$ préserve la norme : $\\left\\| U\\left|\\varphi\\right> \\right\\| = \\left\\| \\left|\\varphi\\right> \\right\\|$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Porte quantique\n",
    "\n",
    "Un circuit quantique code une <b class=\"cool\">transformation unitaire</b> obtenue par composition de <b class=\"cool\">portes quantiques</b> elles aussi unitaires, donc réversibles, avec autant de qubits en entrée qu'en sortie.\n",
    "- composition séquentielle : produit de matrices ;\n",
    "- composition parallèle : produit tensoriel.\n",
    "\n",
    "Quelle <b class=\"cool\">famille de portes universelles ?</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Portes sur 1 qubit\n",
    "\n",
    "$$\n",
    "\\begin{array}{clclcl}\n",
    "X :& \\left|0\\right> \\mapsto \\left|1\\right> & H :& \\left|0\\right> \\mapsto \\frac{\\left|0\\right>+\\left|1\\right>}{\\sqrt2}  & R_\\theta :& \\left|0\\right> \\mapsto \\left|0\\right>\\\\\n",
    "   & \\left|1\\right> \\mapsto \\left|0\\right> &    & \\left|1\\right> \\mapsto \\frac{\\left|0\\right>-\\left|1\\right>}{\\sqrt2} &  & \\left|1\\right> \\mapsto e^{i\\theta}\\left|1\\right> \\\\\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAATgAAADTCAYAAAAViw+fAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAB2HAAAdhwGP5fFlAAAIgklEQVR4nO3dQWxUdR7A8d/QasAodAu9EKpIIzFIpaVZXDexuro1Mbp4MFlDIjFxMcYD0ZMJJ+PGq4mJF6KBg1yQEE1cSAxZ2ARwD4u2FYoclLAgxpjado0KBEZmD8bKc+rSrjBv+uPzuf3fmzfv30zyzf/N68xUarVaLQASmlP2BACuFoED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2BA9ISOCAtgQPSEjggLYED0hI4IC2B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QlsABaQkckJbAAWkJHJCWwAFpCRyQlsABaQkckJbAAWkJHJCWwAFpCRyQlsABaQkckJbAAWkJHJCWwAFpVWp+gw5IygoOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLQEDkhL4IC0BA5IS+CAtAQOSEvggLT+C5sSt2tiCovBAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 178.035x112.373 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 105,
       "width": 156
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\n",
       "\\begin{bmatrix}\n",
       "\\frac{\\sqrt{2}}{2} & \\frac{\\sqrt{2}}{2}  \\\\\n",
       " \\frac{\\sqrt{2}}{2} & - \\frac{\\sqrt{2}}{2}  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "circuit = QuantumCircuit(1)\n",
    "# remplacer ici le x par h ou t (=r(pi/4)) ou sx (sqrt(x))\n",
    "circuit.h(0)\n",
    "show(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Portes contrôlées\n",
    "\n",
    "$$\n",
    "\\begin{array}{cl}\n",
    "CX :& \\left|00\\right> \\mapsto \\left|00\\right> \\\\\n",
    "   & \\left|01\\right> \\mapsto \\left|01\\right> \\\\\n",
    "   & \\left|10\\right> \\mapsto \\left|11\\right> \\\\\n",
    "   & \\left|11\\right> \\mapsto \\left|10\\right> \\\\\n",
    "\\end{array}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9160 characters]",
      "text/plain": [
       "<Figure size 195.536x192.64 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 167,
       "width": 169
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\n",
       "\\begin{bmatrix}\n",
       "1 & 0 & 0 & 0  \\\\\n",
       " 0 & 1 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 1  \\\\\n",
       " 0 & 0 & 1 & 0  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "circuit = QuantumCircuit(2)\n",
    "circuit.cx(1,0)\n",
    "show(circuit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13112 characters]",
      "text/plain": [
       "<Figure size 195.73x272.907 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 229,
       "width": 169
      }
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/latex": [
       "$$\n",
       "\n",
       "\\begin{bmatrix}\n",
       "1 & 0 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1  \\\\\n",
       " 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0  \\\\\n",
       " 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0  \\\\\n",
       " 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0  \\\\\n",
       " \\end{bmatrix}\n",
       "$$"
      ],
      "text/plain": [
       "<IPython.core.display.Latex object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "circuit = QuantumCircuit(3)  \n",
    "circuit.ccx(0,1,2)\n",
    "show(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Universalité\n",
    "\n",
    "La famille de portes $\\left\\{H, CX, R_\\theta\\right\\}_{\\theta\\in[0,2\\pi[}$ est <b class=\"cool\">universelle</b> pour les transformations unitaires.\n",
    "\n",
    "La famille de portes $\\left\\{H, CX, R_{\\frac{\\Pi}4}\\right\\}$ est <b class=\"cool\">approximativement universelle</b> pour les transformations unitaires : elle les réalise avec une erreur $\\varepsilon$ souhaitée.\n",
    "\n",
    "Le <b class=\"cool\">théorème de Solovay-Kitaev</b> nous assure que toutes les familles de portes approximativement universelles le sont <b class=\"cool\">efficacement</b>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Compatibilité\n",
    "\n",
    "Toute fonction classique $f : \\{0,1\\}^n \\rightarrow \\{0,1\\}^m$ est simulée par un circuit quantique de taille similaire.\n",
    "\n",
    "$$\n",
    "\\begin{array}{ccccc}\n",
    "U_f &:& \\mathbb{C}^{\\{0,1\\}^{m+n}} & \\rightarrow & \\mathbb{C}^{\\{0,1\\}^{m+n}}\\\\\n",
    "    & & \\left|x,y\\right> & \\mapsto & \\left|x, f(x)\\oplus y \\right>\n",
    "\\end{array}\n",
    "$$\n",
    "\n",
    "C'est un résultat de <b class=\"cool\">calcul réversible</b>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Algorithme quantique\n",
    "\n",
    "La plupart des <b class=\"cool\">algorithmes quantiques</b> fonctionnent sur le principe suivant :\n",
    "1. créer une superposition pertinente de qubits à partir de $\\left|00\\cdots 0\\right>$ ;\n",
    "2. effectuer <b class=\"cool\">en parallèle</b> un calcul classique sur la superposition des entrées ;\n",
    "3. effectuer une <b class=\"cool\">transformation quantique pertinente</b> ;\n",
    "4. observer le résultat grâce à des <b class=\"cool\">mesures</b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37376 characters]",
      "text/plain": [
       "<Figure size 757.597x353.173 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 291,
       "width": 602
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "circ = QuantumCircuit(3,3)\n",
    "for i in range(2): circ.h(i)\n",
    "circ.barrier()\n",
    "circ.cx(0,2)\n",
    "circ.ccx(1,2,0)\n",
    "circ.barrier()\n",
    "for i in range(3): circ.measure(i,i)\n",
    "draw_circ(circ)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44108 characters]",
      "text/plain": [
       "<Figure size 672x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 426,
       "width": 598
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "res = sim.run(circ, shots=1024).result()\n",
    "display(plot_histogram(res.get_counts()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 82576 characters]",
      "text/plain": [
       "<Figure size 2012.17x353.173 with 1 Axes>"
      ]
     },
     "metadata": {
      "image/png": {
       "height": 301,
       "width": 1568
      }
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "res=transpile(circ,basis_gates=['h','cx', 'rz'], coupling_map = [[0,1],[1,2],[2,0]], optimization_level=1)\n",
    "draw_circ(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Pour aller plus loin\n",
    "\n",
    "P. Arrighi et S. Perdrix. _Modèles de calcul quantique_. dans __[Informatique Mathématique, une photographie en 2016](https://www.gdr-ifm.fr/wp-content/uploads/2020/04/recueilEJCIM16-1.pdf)__. CNRS Éditions.\n",
    "\n",
    "M.A. Nielsen et I.L. Chuang. _Quantum Computation and Quantum Information_. Cambridge Univ. Press, 2000.\n",
    "\n",
    "__[IBM Q Experience.](https://quantum-computing.ibm.com)__\n",
    "\n",
    "__[Introduction to Coding Quantum Algorithms: A Tutorial Series Using Qiskit](https://arxiv.org/abs/1903.04359)__"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Diaporama",
  "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.9.19"
  },
  "rise": {
   "autolaunch": true,
   "chalkboard": {
    "readOnly": false
   },
   "enable_chalkboard": true,
   "reveal_shortcuts": {
    "chalkboard": {
     "colorNext": "s",
     "colorPrev": "q",
     "toggleChalkboard": "a",
     "toggleNotesCanvas": "z"
    }
   },
   "scroll": true,
   "theme": "white"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
