{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# CS 198-120 HW 4\n",
    "\n",
    "This homework is the jupyter notebook companion to HW4. For submission, please just export the notebook to a pdf and attach it to your written work for this homework.\n",
    "\n",
    "This notebook will walk us through how to make your own quantum circuits in Qiskit using python. There are many ways to build circuits in qiskit sort of like how there are many notations for qubit states. I will be going over just one of these circuit notations which I think is the most clear and hides the least \"under-the-hood\". Here is a [reference for Qiskit's QuantumCircuit Module](https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.html) but the first part of this lab is how to read the circuit code! We'll be using the same skeletal structure for circuits throughout this notebook (and possibly future homeworks... if I can write them in time oops :>)\n",
    "\n",
    "I'd like to note that most of Quantum Computing research (that I have been introduced to) doesn't deal with quantum computing in quite this way. Often there are layers of abstractions built upon the circuits to hide the messy circuit underneath. Like how in an electric circuit diagram you only see the Op-Amp, not all the components that build the Op-Amp. Of course, the reason for these exercises is to understand what is going on underneath those abstractions!"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Author: Riley Peterlinz\n",
    "\n",
    "Run this cell to import all the important goodies we'll be playing with.\n",
    "\"\"\"\n",
    "\n",
    "# always gotta have numpy\n",
    "import numpy as np\n",
    "\n",
    "# Various Qiskit Packages\n",
    "from qiskit import QuantumCircuit, assemble, Aer\n",
    "from qiskit.visualization import plot_histogram\n",
    "from qiskit.tools.visualization import circuit_drawer, array_to_latex\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit\n",
    "import qiskit.quantum_info as qi\n",
    "from qiskit.quantum_info import Statevector\n",
    "\n",
    "\"\"\"\n",
    "'sim' is the backend we'll be using to run the circuit. The aer_simulator is just doing standard matrix operations on your classical computer. You can also change this to one of the many quantum computers IBM has, but these have limited qubits, fidelity, and process time unfortunately. We're not quite at the point of making quantum computers a reality (yet!)\n",
    "\"\"\"\n",
    "sim = Aer.get_backend('aer_simulator')\n",
    "\n",
    "def get_attributes(qc):\n",
    "    \"\"\"\n",
    "    Helper Function!\n",
    "\n",
    "    Given a quantum circuit, output the statevector and unitary of the circuit\n",
    "    \"\"\"\n",
    "    qc_state = qc.remove_final_measurements(inplace=False).copy()\n",
    "    qc_unitary = qc.remove_final_measurements(inplace=False).copy()\n",
    "\n",
    "    qc_state.save_statevector()\n",
    "    qvector = assemble(qc_state)\n",
    "    state_vector = sim.run(qvector).result().get_statevector()\n",
    "\n",
    "    qc_unitary.save_unitary()\n",
    "    qunitary = assemble(qc_unitary)\n",
    "    unitary = sim.run(qunitary).result().get_unitary()\n",
    "\n",
    "    return state_vector, unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# How to make a Quantum Circuit in Qiskit\n",
    "\n",
    "Here, we go through\n",
    "1. How to initialize a quantum circuit in qiskit\n",
    "2. How to add gates to a quantum circuit\n",
    "3. How to do measurements on that circuit"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1. Initializing a Quantum Circuit\n",
    "\n",
    "Quantum Circuits can contain two types of registers: Quantum Registers and Classical Registers\n",
    "\n",
    "The Quantum Registers are the registers (data lines) that hold qubits and we operate on with quantum gates\n",
    "\n",
    "The Classical Registers are the standard binary bits we read the measurement of a quantum circuit to\n",
    "\n",
    "Play around with the parameters of the QR and CR!"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=1, name='q') #QR takes two parameters (size, name) name is optional!\n",
    "cr = ClassicalRegister(size=1 , name='cr') #CR takes two parameters (size, name) name is optional!\n",
    "qc = QuantumCircuit(qr, cr) # Our quantum circuit takes the registers as inputs to create the circuit object\n",
    "\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2) # the standard notation for drawing a circuit, scale gives higher resolutions"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2. Adding Gates to the Circuit\n",
    "\n",
    "Here, we will be showing how to make all the gates you need for this homework\n",
    "\n",
    "The anatomy of adding a gate is that given your quantum circuit ```qc``` you can use dot notation to introduce a gate given some position in the circuit. For example, we can make the X gate (quantum version of the not gate) using\n",
    "```python\n",
    "qc.x(position)\n",
    "```\n",
    "\n",
    "There are two ways to specify the position:\n",
    "1. You simply put in the number of the qubit lane you want the gate on. We index from 0 with the highest qubit lane being the 0th. i.e\n",
    "```python\n",
    "qc.x(0)\n",
    "```\n",
    "2. You can specify which quantum register you want to put it in and at what point in the register i.e.\n",
    "```python\n",
    "qc.x(qr[0])\n",
    "```\n",
    "places an x gate at the first position of the qr register. The code below shows how to implement the X, Y, Z, and CNOT gates as well as a variety of ways to specify position. The choice in position is up to you!\n",
    "\n",
    "The basic dot notation for each gate is\n",
    "- qc.x X-Gate\n",
    "- qc.y Y-Gate\n",
    "- qc.h Hadamard Gate\n",
    "- qc.cnot CNOT Gate\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qx = QuantumRegister(size=1 , name='x')\n",
    "qy = QuantumRegister(size=1 , name='y')\n",
    "qz = QuantumRegister(size=1 , name='z')\n",
    "qh = QuantumRegister(size=1 , name='h')\n",
    "qcx = QuantumRegister(size=2 , name='cx')\n",
    "qc = QuantumCircuit(qx, qy, qz, qh, qcx) # yes, you can have multiple registers in a single circuit!\n",
    "\n",
    "\"\"\"\n",
    "Gates\n",
    "\"\"\"\n",
    "qc.x(0) # X-Gate\n",
    "qc.y(qy[0]) # Y-Gate\n",
    "qc.z(qz[0]) # Z-Gate\n",
    "qc.h(3) # Hadamard Gate\n",
    "qc.cx(qcx[0], qcx[1]) # CNOT gate, the first parameter is the location of control, the second parameter is the location of the action\n",
    "\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2) # the standard notation for drawing a circuit, scale gives higher resolutions"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3. Taking Measurements\n",
    "\n",
    "Measuring the quantum circuit means taking a certain amount of \"shots\" (aka measurements) from the circuit and observing their average. Let's walk through an example circuit to illustrate this.\n",
    "\n",
    "The main difference is that we now apply qc.measure(qr[position], cr[position]) to the circuit\n",
    "\n",
    "Running the code below should show these new meaurement blocks."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=2, name='q')\n",
    "cr = ClassicalRegister(size=2 , name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "qc.x(qr[0])\n",
    "qc.h(qr[1])\n",
    "qc.x(qr[1])\n",
    "\n",
    "\"\"\"\n",
    "In order to measure a quantum register we must use the following method to apply the result of a quantum register to a classical register\n",
    "\"\"\"\n",
    "qc.measure(qr[0], cr[0]) # (QuantumRegister, ClassicalRegister)\n",
    "qc.measure(qr[1], cr[1])\n",
    "\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "For this state, the first qubit computes to\n",
    "$$X\\vert{0}\\rangle = \\vert{1}\\rangle$$\n",
    "and the second qubit computes to\n",
    "$$HX\\vert{0}\\rangle = H\\vert{1}\\rangle = \\vert{-}\\rangle$$\n",
    "So the combined state is\n",
    "$$\\vert{-}\\rangle \\otimes \\vert{1}\\rangle = \\vert{-1}\\rangle$$\n",
    "Which, in terms of the $\\vert{0}\\rangle$, $\\vert{1}\\rangle$ basis\n",
    "$$\\vert{-}\\rangle \\otimes \\vert{1}\\rangle =\n",
    "\\frac{1}{\\sqrt{2}}(\\vert{0}\\rangle - \\vert{1}\\rangle) \\otimes \\vert{1}\\rangle =\n",
    "\\frac{1}{\\sqrt{2}}(\\vert{01}\\rangle - \\vert{11}\\rangle)\n",
    "$$\n",
    "and vector representation is\n",
    "$$\\frac{1}{\\sqrt{2}}(\\vert{01}\\rangle - \\vert{11}\\rangle) =\n",
    "\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 0 \\\\ 1 \\\\ 0 \\\\ 1 \\end{bmatrix}$$\n",
    "\n",
    "We can verify this with the code below!\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now, we can see the amplitudes for each state. Now remembering what we learned in problem 1, we can see that we measure the two states we consider in the analytical result and obtain their relative fractions, which is about equal to the complex square of the amplitude."
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Problem 2\n",
    "\n",
    "There are 6 circuits to build and verify with your analytic calculations in this lab. I will provide the skeleton, but you must decide the number of registers needed, build the circuit, and run it. The answer for your statevector output should match the analytical output in your calculations.\n",
    "\n",
    "Basically, edit the first cell of each problem and use the other two cells to evaluate your result!"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## a) $\\vert{01}\\rangle$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## b) $\\vert{10}\\rangle$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## c) $\\vert{+0}\\rangle$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## d) $\\vert{-+}\\rangle$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## e) $\\frac{1}{\\sqrt{2}}(\\vert{00}\\rangle + \\vert{01}\\rangle)$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## f) $ \\frac{1}{\\sqrt{2}}(\\vert{+10}\\rangle + \\vert{+11}\\rangle)$"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qr = QuantumRegister(size=, name='q')\n",
    "cr = ClassicalRegister(size=, name='cr')\n",
    "qc = QuantumCircuit(qr, cr)\n",
    "\n",
    "# defining gates\n",
    "\n",
    "# define measurements\n",
    "qc.measure\n",
    "\n",
    "# helper methods, no need to edit!\n",
    "state_vector, unitary = get_attributes(qc)\n",
    "qc.draw(output = \"mpl\", scale = 2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "display(array_to_latex(state_vector, prefix=\"\\\\text{Statevector} = \", max_size=30)) # display vector statevector\n",
    "display(Statevector(state_vector).draw('latex')) # display 0,1 ket statevector\n",
    "display(array_to_latex(unitary, prefix=\"\\\\text{Circuit = } \", max_size=30)) # display unitary"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# We run the simulator with sim.run(QUANTUM CIRCUIT) and we get the resulting values with .result()\n",
    "result = sim.run(qc, shots=10000).result()\n",
    "\n",
    "# We then collect the results using .get_counts()\n",
    "counts = result.get_counts()\n",
    "\n",
    "# Visualization\n",
    "plot_histogram(counts)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}