{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# QAOA\n\nHere we generate and optimize pattern for QAOA circuit.\nYou can run this code on your browser with [mybinder.org](https://mybinder.org/) - click the badge below.\n\n<img src=\"https://mybinder.org/badge_logo.svg\" target=\"https://mybinder.org/v2/gh/TeamGraphix/graphix-examples/HEAD?labpath=qaoa.ipynb\">\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import networkx as nx\nimport numpy as np\nfrom graphix import Circuit\n\nn = 4\nxi = np.random.rand(6)\ntheta = np.random.rand(4)\ng = nx.complete_graph(n)\ncircuit = Circuit(n)\nfor i, (u, v) in enumerate(g.edges):\n    circuit.cnot(u, v)\n    circuit.rz(v, xi[i])\n    circuit.cnot(u, v)\nfor v in g.nodes:\n    circuit.rx(v, theta[v])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "transpile and get the graph state\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pattern = circuit.transpile().pattern\npattern.standardize()\npattern.shift_signals()\npattern.draw_graph(flow_from_pattern=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "perform Pauli measurements and plot the new (minimal) graph to perform the same quantum computation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pattern.perform_pauli_measurements()\npattern.draw_graph(flow_from_pattern=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "finally, simulate the QAOA circuit\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "out_state = pattern.simulate_pattern()\nstate = circuit.simulate_statevector().statevec\nprint(\"overlap of states: \", np.abs(np.dot(state.psi.flatten().conjugate(), out_state.psi.flatten())))\n# sphinx_gallery_thumbnail_number = 2"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.13"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}