{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gpt as g\n",
    "import numpy as np\n",
    "import gpt.qis.backends.dynamic.state as state\n",
    "from gpt.qis.gate import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lecture 4: A first look at the QIS module.  This performs a simple Bell-type experiment with 3 qubits.\n",
    "\n",
    "We first setup a random number generator, used for the measurement process and then create a 3 qubit state and inspect it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPT :       0.392495 s : Initializing gpt.random(test,vectorized_ranlux24_389_64) took 0.000437975 s\n",
      "GPT :       0.399611 s : map_init: timing: unprofiled           = 6.723404e-05 s (=   1.32 %)\n",
      "                       : map_init: timing: masks                = 2.255678e-03 s (=  44.24 %)\n",
      "                       : map_init: timing: coordinates          = 2.776384e-03 s (=  54.45 %)\n",
      "                       : map_init: timing: total                = 5.099297e-03 s (= 100.00 %)\n",
      "GPT :       0.404877 s :  + (1+0j) |000>\n"
     ]
    }
   ],
   "source": [
    "r = g.random(\"test\")\n",
    "state_initial = state(r, 3)\n",
    "g.message(state_initial)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we create the Bell state using a Hadamard and two CNOT gates.  This illustrates how to create a combined circuit and apply it to a state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPT :       0.422044 s :  + (0.7071067811865475+0j) |000>\n",
      "                       :  + (0.7071067811865475+0j) |111>\n"
     ]
    }
   ],
   "source": [
    "state_bell = (H(0) | CNOT(0,1) | CNOT(0,2)) * state_initial\n",
    "g.message(state_bell)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, let us measure qubit 0 and print the classical result and the resulting collapsed state."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPT :       0.434542 s : 0\n",
      "GPT :       0.440541 s :  + (0.9999999999999998+0j) |000>\n"
     ]
    }
   ],
   "source": [
    "state_collapsed = M(0) * state_bell\n",
    "g.message(state_collapsed.classical_bit[0])\n",
    "g.message(state_collapsed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also measure all qubits at the same time by omitting the argument.  Let us do this 1000 times and gather statistics."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPT :       1.084628 s : {'111': 505, '000': 495}\n"
     ]
    }
   ],
   "source": [
    "histogram = {}\n",
    "for i in range(1000):\n",
    "    state_collapsed = M() * state_bell\n",
    "    result = \"\".join(str(x) for x in state_collapsed.classical_bit)\n",
    "    if result not in histogram:\n",
    "        histogram[result] = 1\n",
    "    else:\n",
    "        histogram[result] += 1\n",
    "g.message(histogram)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
