{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister\n", "from qiskit.tools.visualization import plot_histogram, circuit_drawer, matplotlib_circuit_drawer\n", "from qiskit import available_backends, execute, register\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "# define function to draw circuits\n", "# drawer = lambda qc: circuit_drawer(qc, basis='u1,u2,u3,id,cx,h,x,ry')\n", "drawer = lambda qc: matplotlib_circuit_drawer(qc, basis='u1,u2,u3,id,cx,h,x,ry')\n", "\n", "import warnings\n", "warnings.filterwarnings(\"ignore\", category=DeprecationWarning)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Available local backends" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# See a list of available local simulators\n", "print('local backends: ', available_backends({'local': True}))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# select a backend for the tutorial\n", "backend = 'local_qasm_simulator'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setting up Quantum Circuits\n", "\n", "Before we can work with qubits and quantum gates, we first have to initialize our quantum circuit with the corresponding quantum and classical registers.\n", "
\n", "Please note that given $n$ qubits/bits in a quantum/classical register the qubits/bits are read in the following order: $[q_{n-1}, ..., q_1, q_0]$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(2) \n", "c = ClassicalRegister(2)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Some basic gates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## X gate\n", "\n", "The X gate is the quantum equivalent to the classical NOT gate and, thus, flips $|0\\rangle$ to $|1\\rangle$ and vice versa.
\n", "In matrix form the quantum gate reads\n", "$\\left(\\begin{array}{cc} 0 & 1 \\\\ 1 & 0 \\end{array} \\right)$.\n", "
\n", "
\n", "Exercise:
\n", "Try to create a circuit that generates the state |10>." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(2) \n", "c = ClassicalRegister(2)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)\n", "\n", "# add a X gate on qubit, flipping it from |0> to |1>\n", "qc.x(q[0])\n", "# qc.x(q[1])\n", "\n", "# measure qubit\n", "qc.measure(q, c);\n", "\n", "# plot the circuit\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "number_of_shots = 1\n", "job_sim = execute(qc, backend, shots=number_of_shots)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## H gate\n", "\n", "The H gate reads\n", "$\\frac{1}{\\sqrt{2}}\\left(\\begin{array}{cc} 1 & 1 \\\\ 1 & -1 \\end{array} \\right)$.\n", "Thus, it maps $|0\\rangle$ to $\\frac{1}{\\sqrt{2}}(|0\\rangle + |1\\rangle) =: |+\\rangle$, i.e., into the equal superposition state.\n", "
\n", "
\n", "Exercise:
\n", "Repeat the experiment a couple of times.
\n", "How does the result behave?
\n", "What happens if you increase the number of shots?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(1)\n", "c = ClassicalRegister(1)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)\n", "\n", "# add a H gate on qubit 0, flipping the qubit from |0> to |1>\n", "qc.h(q[0])\n", "\n", "# measure qubit\n", "qc.measure(q, c)\n", "\n", "# plot the circuit\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "number_of_shots = 1\n", "job_sim = execute(qc, backend, shots=number_of_shots)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())\n", "\n", "# plot results\n", "plot_histogram(sim_results.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Y rotation\n", "\n", "A Y-rotation of angle $\\theta$, denoted $R_y(\\theta)$, acts like\n", "$e^{-i\\frac{\\theta}{2}Y} = \\left(\\begin{array}{cc} \\cos(\\theta/2) & -\\sin(\\theta/2) \\\\ \\sin(\\theta/2) & \\cos(\\theta/2) \\end{array} \\right)$.
\n", "It rotates a single qubit state around the Y-axis of the Bloch sphere.
\n", "When applied to $|0\\rangle$, the probability of measuring $|1\\rangle$ equals $\\sin^2(\\theta/2)$.\n", "
\n", "
\n", "\n", "\n", "Exercise:
\n", "Find $\\theta$ such that $\\mathbb{P}[|1\\rangle] = 75\\%$ and verify it by executing the circuit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(1)\n", "c = ClassicalRegister(1)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)\n", "\n", "# add a X gate on qubit 0, flipping the qubit from |0> to |1>\n", "theta = np.pi/2\n", "qc.ry(theta, q[0])\n", "\n", "# measure qubit\n", "qc.measure(q, c)\n", "\n", "# plot the circuit\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "number_of_shots = 1024\n", "job_sim = execute(qc, backend, shots=number_of_shots)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())\n", "\n", "# plot results\n", "plot_histogram(sim_results.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## CX gate\n", "\n", "A controlled X (CX) gate acts on two qubits like\n", "$\\left(\\begin{array}{cccc} \n", "1 & 0 & 0 & 0 \\\\ \n", "0 & 1 & 0 & 0 \\\\\n", "0 & 0 & 0 & 1 \\\\\n", "0 & 0 & 1 & 0 \\end{array} \\right)$.\n", "
\n", "Thus, it flips (applies an X gate to) the second qubit if the first qubit is $|1\\rangle$ and otherwise has no effect.\n", "
\n", "
\n", "Exercise:
\n", "See what happens when applying the CX to $|00\\rangle$, $|01\\rangle$, $|10\\rangle$, $|11\\rangle$.\n", "
\n", "The initial qubit states can be prepared via single-qubit X gates." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(2)\n", "c = ClassicalRegister(2)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)\n", "\n", "# set initial state\n", "qc.x(q[0]) # flips q[0] from |0> to |1>\n", "# qc.x(q[1]) # flips q[1] from |0> to |1>\n", "\n", "# apply CX gate with control q[0] and target q[1]\n", "qc.cx(q[0], q[1])\n", "\n", "# measure qubit\n", "qc.measure(q, c)\n", "\n", "# plot the circuit\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "number_of_shots = 1\n", "job_sim = execute(qc, backend, shots=number_of_shots)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())\n", "\n", "# plot results\n", "plot_histogram(sim_results.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The Bell state\n", "\n", "A Bell state is a fully entangled state of two qubits that has no classical counter part.\n", "
\n", "It is given by: $$\\frac{1}{\\sqrt{2}}(|00\\rangle + |11\\rangle)$$\n", "\n", "Given an initial two qubit system $|00\\rangle$, we create a Bell state by applying an H gate to the first qubit and, then, a CX gate where the first qubit acts as control and the second qubit as target state." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create a quantum and classical registers\n", "q = QuantumRegister(2)\n", "c = ClassicalRegister(2)\n", "\n", "# create a quantum circuit\n", "qc = QuantumCircuit(q, c)\n", "\n", "# add a H gate on qubit 0, putting this qubit in superposition\n", "qc.h(q[0])\n", "\n", "# add a CX (CNOT) gate on control qubit 0 and target qubit 1\n", "qc.cx(q[0], q[1])\n", "\n", "# add a measure gate to see the state.\n", "qc.measure(q, c)\n", "\n", "# plot the circuit\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "number_of_shots = 1024\n", "job_sim = execute(qc, backend, shots=number_of_shots)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())\n", "\n", "# plot results\n", "plot_histogram(sim_results.get_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise:
\n", "Prepare the following state: $\\frac{1}{\\sqrt{2}}(|01\\rangle + |10\\rangle)$ by designing a quantum circuit that is similar to the Bell state construction." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "q = QuantumRegister(2)\n", "c = ClassicalRegister(2)\n", "qc = QuantumCircuit(q, c)\n", "\n", "# Write your circuit here:\n", "#\n", "#\n", "#\n", "\n", "qc.measure(q, c)\n", "\n", "drawer(qc)\n", "\n", "# compile and run the quantum circuit on the local simulator\n", "job_sim = execute(qc, backend, shots=1024)\n", "sim_results = job_sim.result()\n", "print(\"simulation:\", sim_results)\n", "print(\"counts: \", sim_results.get_counts())\n", "\n", "# plot results\n", "plot_histogram(sim_results.get_counts())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }