{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [
"mHWrVJUPzYFs",
"IyXkr5MpzbJd",
"rS8hWZHmMDaO",
"4vgFHWWjMQKU",
"nrRWFItmN1Wb",
"5bYirnBUVT-m",
"vcf7RP_pXJjh",
"du4fBz_r8B1Q",
"n-OmBy6v8o-c",
"Njl7VwbWQczf"
]
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# Concepts Review #1: Quantum Gates\n",
"---\n",
"This concept review notebook will go over Quantum Gates. Gates are matrix operations that we utilize in circuit diagrams in order to perform transformations on our qubits. \n",
"\n",
"
\n",
"\n",
"You can find the syntax cheat sheet [here](https://).\n",
"\n",
"
\n",
"Start by importing the standard libraries as discussed in the previous review notebook. "
],
"metadata": {
"id": "ExUXGU-CzEXO"
}
},
{
"cell_type": "code",
"source": [
"# uncomment below if qiskit not already installed \n",
"#!pip install qiskit \n",
"# Importing standard Qiskit libraries\n",
"from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister \n",
"#Importing the QuantumCircuit function from Qiskit. \n",
"#We will use this to create our quantum circuits!\n",
"\n",
"# We will use these functions to run our circuit and visualize its final state\n",
"from qiskit import Aer, execute \n",
"from qiskit.visualization import *\n"
],
"metadata": {
"id": "h4GiblBAQrGA"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"---\n",
"\n",
"#### **Exercise #1:** Can you implement a quantum circuit? \n",
"\n",
"in trivial cases, you may either pass numbers into the `QuantumCircuit` class or, for more later complex cases where a classical register is needed, a `QuantumRegister` class may need to be implemented. Try your hand at both in this exercise. \n",
"\n"
],
"metadata": {
"id": "NdYNfWiTzQic"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "H94JrByizCgt"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"source": [
"**Hint:** Click [here](https://qiskit.org/textbook/ch-appendix/qiskit.html) for syntax.\n",
"\n",
"\n",
"\n"
],
"metadata": {
"id": "CFfXSA9_Kcko"
}
},
{
"cell_type": "markdown",
"source": [
"##### **Solution 1:**"
],
"metadata": {
"id": "I1evfkmzzZWG"
}
},
{
"cell_type": "code",
"source": [
"#q = QuantumRegister(1)\n",
"qc = QuantumCircuit(2) #or numbers\n",
"qc.draw()"
],
"metadata": {
"id": "ELpjbEsrzZ9v",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 94
},
"outputId": "bba13886-d142-4c04-9ba5-f55af7eba59f"
},
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" \n",
"q_0: \n",
" \n",
"q_1: \n",
" "
],
"text/html": [
"
\n", "q_0: \n", " \n", "q_1: \n", "" ] }, "metadata": {}, "execution_count": 8 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "rACZ2OkVQrCj" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "#### **Exercise #2:** Can you implement an X or NOT gate? \n", "This `qc.x` is also known as the **bit flip gate** as it simply flips the bit directly from a 1 or a 0 (when starting out with a 0 or 1 respectively) as one would in a classical bit. " ], "metadata": { "id": "mHWrVJUPzYFs" } }, { "cell_type": "code", "source": [], "metadata": { "id": "ioUQK4pBUoLW" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "##### **Solution 2:**" ], "metadata": { "id": "Hwi4DpuLzca-" } }, { "cell_type": "code", "source": [ "qc = QuantumCircuit(q)\n", "qc.x(q)\n", "qc.draw()" ], "metadata": { "id": "zTm07sADvy8M", "colab": { "base_uri": "https://localhost:8080/", "height": 63 }, "outputId": "914bea06-d85f-4c08-b6d1-75ace77484cc" }, "execution_count": 9, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐\n", "q3: ┤ X ├\n", " └───┘" ], "text/html": [ "
┌───┐\n", "q3: ┤ X ├\n", " └───┘" ] }, "metadata": {}, "execution_count": 9 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #3:** Can you implement the Y Gate? \n", "This `qc.y` also known as the **bit and phase flip gate** as it flips the bit directly from a 1 or a 0 (when starting out with a 0 or 1 respectively) as well as its *phase*." ], "metadata": { "id": "IyXkr5MpzbJd" } }, { "cell_type": "code", "source": [], "metadata": { "id": "u14wW7JnzdA_" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "##### **Solution:**" ], "metadata": { "id": "uNrHhp9yzVUV" } }, { "cell_type": "code", "source": [ "qc = QuantumCircuit(q)\n", "qc.y(q)\n", "qc.draw()" ], "metadata": { "id": "UlGm2XBDzctN", "colab": { "base_uri": "https://localhost:8080/", "height": 63 }, "outputId": "bb1da7e0-95b0-4485-a3bd-064579231e11" }, "execution_count": 10, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐\n", "q3: ┤ Y ├\n", " └───┘" ], "text/html": [ "
┌───┐\n", "q3: ┤ Y ├\n", " └───┘" ] }, "metadata": {}, "execution_count": 10 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #4:** Can you implement the H Gate? \n", "Now we are getting into quantum gates! This `qc.h` is also known as the **Hadamard gate** as it puts our qubits into superposition." ], "metadata": { "id": "rS8hWZHmMDaO" } }, { "cell_type": "code", "source": [ "qc = QuantumCircuit(q)\n", "qc.h(q)\n", "qc.draw()" ], "metadata": { "id": "a3whVGh_MDaO", "colab": { "base_uri": "https://localhost:8080/", "height": 63 }, "outputId": "27ebb463-f509-4dd2-e32e-f2861baa1a3e" }, "execution_count": 11, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐\n", "q3: ┤ H ├\n", " └───┘" ], "text/html": [ "
┌───┐\n", "q3: ┤ H ├\n", " └───┘" ] }, "metadata": {}, "execution_count": 11 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #5:** Can you implement the Z Gate? \n", "This `qc.z` is also known as the **Phase flip gate** as it simply flips the qubit's phase by π around the bloch sphere." ], "metadata": { "id": "4vgFHWWjMQKU" } }, { "cell_type": "code", "source": [ "qc = QuantumCircuit(q)\n", "qc.z(q)\n", "qc.draw()" ], "metadata": { "id": "Se3CetU1MQKV", "colab": { "base_uri": "https://localhost:8080/", "height": 63 }, "outputId": "5c559936-9e6e-41b9-9947-7c038aa8d10e" }, "execution_count": 13, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐\n", "q3: ┤ Z ├\n", " └───┘" ], "text/html": [ "
┌───┐\n", "q3: ┤ Z ├\n", " └───┘" ] }, "metadata": {}, "execution_count": 13 } ] }, { "cell_type": "markdown", "source": [ "**Hint:** Click [here](https://quantum-computing.ibm.com/lab) for a refresher on π rotations.\n" ], "metadata": { "id": "-XxNcdOWNeud" } }, { "cell_type": "markdown", "source": [ "#### **Exercise #6:** Can you implement the CNOT Gate? \n", "This `qc.cx` is also known as the **controlled not gate** which we use to entangle qubits. It acts on 2 qubits to \"control\" one qubit while making the other qubit the \"target\" qubit. " ], "metadata": { "id": "nrRWFItmN1Wb" } }, { "cell_type": "code", "source": [ "q = QuantumRegister(3)\n", "qc = QuantumCircuit(q) #or use numbers\n", "qc.cx(q[0], q[1])\n", "qc.draw()" ], "metadata": { "id": "BabTrvcqN1Wq", "colab": { "base_uri": "https://localhost:8080/", "height": 125 }, "outputId": "d341a6d4-c8f3-4233-ae49-17b4c651daad" }, "execution_count": 22, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " \n", "q8_0: ──■──\n", " ┌─┴─┐\n", "q8_1: ┤ X ├\n", " └───┘\n", "q8_2: ─────\n", " " ], "text/html": [ "
\n", "q8_0: ──■──\n", " ┌─┴─┐\n", "q8_1: ┤ X ├\n", " └───┘\n", "q8_2: ─────\n", "" ] }, "metadata": {}, "execution_count": 22 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #7:** Can you implement the `measure` function? \n", "This `qc.measure` is used to measure a certain qubit to collapse it into the classical register within the circuit. " ], "metadata": { "id": "5bYirnBUVT-m" } }, { "cell_type": "code", "source": [ "q = QuantumRegister(3)\n", "c = ClassicalRegister(3)\n", "qc = QuantumCircuit(q,c) #or use numbers\n", "qc.measure(q,c)\n", "qc.draw()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 156 }, "outputId": "d680e4aa-1884-4647-81fd-6933d9f3ebe4", "id": "f0Eg_G4mVT-4" }, "execution_count": 28, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌─┐ \n", "q14_0: ┤M├──────\n", " └╥┘┌─┐ \n", "q14_1: ─╫─┤M├───\n", " ║ └╥┘┌─┐\n", "q14_2: ─╫──╫─┤M├\n", " ║ ║ └╥┘\n", " c2: 3/═╩══╩══╩═\n", " 0 1 2 " ], "text/html": [ "
┌─┐ \n", "q14_0: ┤M├──────\n", " └╥┘┌─┐ \n", "q14_1: ─╫─┤M├───\n", " ║ └╥┘┌─┐\n", "q14_2: ─╫──╫─┤M├\n", " ║ ║ └╥┘\n", " c2: 3/═╩══╩══╩═\n", " 0 1 2" ] }, "metadata": {}, "execution_count": 28 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #8:** Can you construct a simple Bell State circuit? \n", "Recall the gate operations we used in the course to create a Bell State circuit. The above operations will be all that is necessary to do so!" ], "metadata": { "id": "vcf7RP_pXJjh" } }, { "cell_type": "code", "source": [ "qr = QuantumRegister(2)\n", "cr = ClassicalRegister(2)\n", "qc = QuantumCircuit(qr,cr) #or use numbers\n", "qc.h(qr[0])\n", "qc.cx(qr[0], qr[1])\n", "qc.measure((qr[0], qr[1]), (cr[0], cr[1]))\n", "\n", "qc.draw()\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 125 }, "outputId": "f7e2cfb4-f199-476b-99cf-fdb8577f1ea1", "id": "yVXxmVr8XJju" }, "execution_count": 33, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐ ┌─┐ \n", "q19_0: ┤ H ├──■──┤M├───\n", " └───┘┌─┴─┐└╥┘┌─┐\n", "q19_1: ─────┤ X ├─╫─┤M├\n", " └───┘ ║ └╥┘\n", " c7: 2/═══════════╩══╩═\n", " 0 1 " ], "text/html": [ "
┌───┐ ┌─┐ \n", "q19_0: ┤ H ├──■──┤M├───\n", " └───┘┌─┴─┐└╥┘┌─┐\n", "q19_1: ─────┤ X ├─╫─┤M├\n", " └───┘ ║ └╥┘\n", " c7: 2/═══════════╩══╩═\n", " 0 1" ] }, "metadata": {}, "execution_count": 33 } ] }, { "cell_type": "markdown", "source": [ "\n", "\n", "---\n", "\n" ], "metadata": { "id": "WaHlX89nvtX3" } }, { "cell_type": "markdown", "source": [ "### **Optional - Additional Practice**\n", "If you would like additional practice, try the problems below. The problems are optional gates content for more interesting operations with quantum gates. " ], "metadata": { "id": "du4fBz_r8B1Q" } }, { "cell_type": "markdown", "source": [ "#### **Exercise #1:** \n", "Construct a 4-qubit Bernstein-Vazarani circuit using registers. " ], "metadata": { "id": "n-OmBy6v8o-c" } }, { "cell_type": "code", "source": [], "metadata": { "id": "MkCl10I-Qczr" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "##### **Solution:**" ], "metadata": { "id": "8E1BERSa8-mG" } }, { "cell_type": "code", "source": [ "qr = QuantumRegister(3, 'q')\n", "anc = QuantumRegister(1, 'ancilla')\n", "cr = ClassicalRegister(3, 'c')\n", "qc = QuantumCircuit(qr, anc, cr)\n", "\n", "qc.x(anc[0])\n", "qc.h(anc[0])\n", "qc.h(qr[0:3])\n", "qc.cx(qr[0:3], anc[0])\n", "qc.h(qr[0:3])\n", "qc.barrier(qr)\n", "qc.measure(qr, cr)\n", "\n", "qc.draw()" ], "metadata": { "id": "vAbVa-0S8-Oa", "colab": { "base_uri": "https://localhost:8080/", "height": 186 }, "outputId": "7ce6f16b-fde5-49fa-dd47-dbce2fc161bf" }, "execution_count": 35, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐ ┌───┐ ░ ┌─┐ \n", " q_0: ┤ H ├───────■──┤ H ├───────────░─┤M├──────\n", " ├───┤ │ └───┘┌───┐ ░ └╥┘┌─┐ \n", " q_1: ┤ H ├───────┼────■──┤ H ├──────░──╫─┤M├───\n", " ├───┤ │ │ └───┘┌───┐ ░ ║ └╥┘┌─┐\n", " q_2: ┤ H ├───────┼────┼────■──┤ H ├─░──╫──╫─┤M├\n", " ├───┤┌───┐┌─┴─┐┌─┴─┐┌─┴─┐└───┘ ░ ║ ║ └╥┘\n", "ancilla: ┤ X ├┤ H ├┤ X ├┤ X ├┤ X ├─────────╫──╫──╫─\n", " └───┘└───┘└───┘└───┘└───┘ ║ ║ ║ \n", " c: 3/══════════════════════════════════╩══╩══╩═\n", " 0 1 2 " ], "text/html": [ "
┌───┐ ┌───┐ ░ ┌─┐ \n", " q_0: ┤ H ├───────■──┤ H ├───────────░─┤M├──────\n", " ├───┤ │ └───┘┌───┐ ░ └╥┘┌─┐ \n", " q_1: ┤ H ├───────┼────■──┤ H ├──────░──╫─┤M├───\n", " ├───┤ │ │ └───┘┌───┐ ░ ║ └╥┘┌─┐\n", " q_2: ┤ H ├───────┼────┼────■──┤ H ├─░──╫──╫─┤M├\n", " ├───┤┌───┐┌─┴─┐┌─┴─┐┌─┴─┐└───┘ ░ ║ ║ └╥┘\n", "ancilla: ┤ X ├┤ H ├┤ X ├┤ X ├┤ X ├─────────╫──╫──╫─\n", " └───┘└───┘└───┘└───┘└───┘ ║ ║ ║ \n", " c: 3/══════════════════════════════════╩══╩══╩═\n", " 0 1 2" ] }, "metadata": {}, "execution_count": 35 } ] }, { "cell_type": "markdown", "source": [ "#### **Exercise #2:** \n", "Construct a 5-qubit GHZ circuit. " ], "metadata": { "id": "Njl7VwbWQczf" } }, { "cell_type": "code", "source": [], "metadata": { "id": "Drq5EUuRasi_" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "\n", "##### **Solution:**" ], "metadata": { "id": "04L4_ffhREw2" } }, { "cell_type": "code", "source": [ "qc = QuantumCircuit(5)\n", "qc.h(0)\n", "qc.cx(0, range(1, 5))\n", "qc.measure_all()\n", "qc.draw()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 217 }, "id": "VyDFL6UKaYk5", "outputId": "b86aedab-9c26-472e-e931-f430f5ec94f8" }, "execution_count": 37, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " ┌───┐ ░ ┌─┐ \n", " q_0: ┤ H ├──■────■────■────■───░─┤M├────────────\n", " └───┘┌─┴─┐ │ │ │ ░ └╥┘┌─┐ \n", " q_1: ─────┤ X ├──┼────┼────┼───░──╫─┤M├─────────\n", " └───┘┌─┴─┐ │ │ ░ ║ └╥┘┌─┐ \n", " q_2: ──────────┤ X ├──┼────┼───░──╫──╫─┤M├──────\n", " └───┘┌─┴─┐ │ ░ ║ ║ └╥┘┌─┐ \n", " q_3: ───────────────┤ X ├──┼───░──╫──╫──╫─┤M├───\n", " └───┘┌─┴─┐ ░ ║ ║ ║ └╥┘┌─┐\n", " q_4: ────────────────────┤ X ├─░──╫──╫──╫──╫─┤M├\n", " └───┘ ░ ║ ║ ║ ║ └╥┘\n", "meas: 5/═════════════════════════════╩══╩══╩══╩══╩═\n", " 0 1 2 3 4 " ], "text/html": [ "
┌───┐ ░ ┌─┐ \n", " q_0: ┤ H ├──■────■────■────■───░─┤M├────────────\n", " └───┘┌─┴─┐ │ │ │ ░ └╥┘┌─┐ \n", " q_1: ─────┤ X ├──┼────┼────┼───░──╫─┤M├─────────\n", " └───┘┌─┴─┐ │ │ ░ ║ └╥┘┌─┐ \n", " q_2: ──────────┤ X ├──┼────┼───░──╫──╫─┤M├──────\n", " └───┘┌─┴─┐ │ ░ ║ ║ └╥┘┌─┐ \n", " q_3: ───────────────┤ X ├──┼───░──╫──╫──╫─┤M├───\n", " └───┘┌─┴─┐ ░ ║ ║ ║ └╥┘┌─┐\n", " q_4: ────────────────────┤ X ├─░──╫──╫──╫──╫─┤M├\n", " └───┘ ░ ║ ║ ║ ║ └╥┘\n", "meas: 5/═════════════════════════════╩══╩══╩══╩══╩═\n", " 0 1 2 3 4" ] }, "metadata": {}, "execution_count": 37 } ] }, { "cell_type": "markdown", "source": [ "---\n", "\n", "### **Conclusion:** \n", "\n", "This concludes the Quantum Gates concept review. " ], "metadata": { "id": "biqjpjaGbD5A" } }, { "cell_type": "markdown", "source": [ "---\n", "Author: Shwetha Jayaraj\n", "