{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, we will learn how to use the graphix-perceval library to convert graphix.Pattern objects into perceval.Circuit objects.\n",
    "\n",
    "We first generate a MBQC pattern using graphix library. We create GHZ state as an example.\n",
    "\n",
    "First, let us import relevant modules and define function we will use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graphix import Circuit\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx\n",
    "\n",
    "\n",
    "# define the functions required for GHZ state generation\n",
    "def ghz(circuit: Circuit):\n",
    "    \"\"\"generate GHZ circuit\"\"\"\n",
    "    circuit.h(1)\n",
    "    circuit.h(2)\n",
    "    circuit.cnot(0, 1)\n",
    "    circuit.cnot(0, 2)\n",
    "\n",
    "# generate the GHZ state generation pattern\n",
    "circuit = Circuit(3)\n",
    "ghz(circuit)\n",
    "pattern = circuit.transpile().pattern\n",
    "\n",
    "# plot the pattern\n",
    "pattern.draw_graph()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pattern-to-circuit conversion\n",
    "\n",
    "Now let us convert the pattern into a circuit using the *graphix-perceval* library:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graphix_perceval import to_perceval\n",
    "from perceval import pdisplay\n",
    "\n",
    "exp = to_perceval(pattern)\n",
    "pdisplay(exp.circ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By running the Perceval’s computing backends, We can obtain the probability distribution of the measurement outcomes, or sampling distribution with a given number of samples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# distribution\n",
    "exp.set_local_processor(\"SLOS\")\n",
    "dist = exp.get_probability_distribution()\n",
    "dist.draw()\n",
    "\n",
    "# sampling\n",
    "exp.set_local_processor(\"SLOS\")\n",
    "dist = exp.sample(num_samples=1000)\n",
    "dist.draw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the current implementation only supports SLOS and Naive as local Perceval processors. See Perceval documentation for more details."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gp-test",
   "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.10.14"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}