{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Large-scale simulations with tensor network simulator\n\nIn this example, we demonstrate simulation of MBQC involving 10k+ nodes.\n\nYou can also 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=qft_with_tn.ipynb\">\n\nFirstly, let us import relevant modules and define the circuit:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nfrom graphix import Circuit\n\n\ndef cp(circuit, theta, control, target):\n    circuit.rz(control, theta / 2)\n    circuit.rz(target, theta / 2)\n    circuit.cnot(control, target)\n    circuit.rz(target, -1 * theta / 2)\n    circuit.cnot(control, target)\n\n\ndef qft_rotations(circuit, n):\n    circuit.h(n)\n    for qubit in range(n + 1, circuit.width):\n        cp(circuit, np.pi / 2 ** (qubit - n), qubit, n)\n\n\ndef swap_registers(circuit, n):\n    for qubit in range(n // 2):\n        circuit.swap(qubit, n - qubit - 1)\n    return circuit\n\n\ndef qft(circuit, n):\n    for i in range(n):\n        qft_rotations(circuit, i)\n    swap_registers(circuit, n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We will simulate 55-qubit QFT, which requires graph states with more than 10000 nodes.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "n = 55\nprint(\"{}-qubit QFT\".format(n))\ncircuit = Circuit(n)\n\nfor i in range(n):\n    circuit.h(i)\nqft(circuit, n)\n\n# standardize pattern\npattern = circuit.transpile(opt=True).pattern\npattern.standardize()\npattern.shift_signals()\nnodes, edges = pattern.get_graph()\nprint(f\"Number of nodes: {len(nodes)}\")\nprint(f\"Number of edges: {len(edges)}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Using efficient graph state simulator `graphix.GraphSim`, we can classically preprocess Pauli measurements.\nWe are currently improving the speed of this process by using rust-based graph manipulation backend.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pattern.perform_pauli_measurements(use_rustworkx=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To specify TN backend of the simulation, simply provide as a keyword argument.\nhere we do a very basic check that one of the statevector amplitudes is what it is expected to be:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import time  # noqa: E402\n\nt1 = time.time()\ntn = pattern.simulate_pattern(backend=\"tensornetwork\")\nvalue = tn.get_basis_amplitude(0)\nt2 = time.time()\nprint(\"amplitude of |00...0> is \", value)\nprint(\"1/2^n (true answer) is\", 1 / 2**n)\nprint(\"approximate execution time in seconds: \", t2 - t1)"
      ]
    }
  ],
  "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
}