{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "1eb71376",
      "metadata": {},
      "source": [
        "---\n",
        "title: Quantum information\n",
        "description: A free IBM course on quantum information and computation\n",
        "---\n",
        "\n",
        "{/* cspell:ignore biggr operatorname */}\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f3ac1882",
      "metadata": {},
      "source": [
        "# Quantum information\n",
        "\n",
        "Now we're ready to move on to quantum information, where we make a different choice for the type of vector that represents a state — in this case a *quantum state* — of the system being considered.\n",
        "Like in the previous discussion of classical information, we'll be concerned with systems having finite and nonempty sets of classical states, and we'll make use of much of the same notation.\n",
        "\n",
        "## Quantum state vectors\n",
        "\n",
        "A *quantum state* of a system is represented by a column vector, similar to a probabilistic state. As before, the indices of the vector label the classical states of the system.\n",
        "Vectors representing quantum states are characterized by these two properties:\n",
        "\n",
        "1. The entries of a quantum state vector are *complex numbers*.\n",
        "2. The sum of the *absolute values squared* of the entries of a quantum state vector is $1.$\n",
        "\n",
        "Thus, in contrast to probabilistic states, vectors representing quantum states need not have nonnegative real number entries, and it is the sum of the absolute values squared of the entries (as opposed to the sum of the entries) that must equal $1.$ Simple as these changes are, they give rise to the differences between quantum and classical information; any speedup from a quantum computer, or improvement from a quantum communication protocol, is ultimately derived from these simple mathematical changes.\n",
        "\n",
        "The *Euclidean norm* of a column vector\n",
        "\n",
        "$$\n",
        "  v = \\begin{pmatrix}\n",
        "    \\alpha_1\\\\\n",
        "    \\vdots\\\\\n",
        "    \\alpha_n\n",
        "  \\end{pmatrix}\n",
        "$$\n",
        "\n",
        "is denoted and defined as follows:\n",
        "\n",
        "$$\n",
        "  \\| v \\| = \\sqrt{\\sum_{k=1}^n |\\alpha_k|^2}.\n",
        "$$\n",
        "\n",
        "The condition that the sum of the absolute values squared of a quantum state vector equals $1$ is therefore equivalent to that vector having Euclidean norm equal to $1.$\n",
        "That is, quantum state vectors are *unit vectors* with respect to the Euclidean norm.\n",
        "\n",
        "### Examples of qubit states\n",
        "\n",
        "The term *qubit* refers to a quantum system whose classical state set is $\\{0,1\\}.$\n",
        "That is, a qubit is really just a bit — but by using this name we explicitly recognize that this bit can be in a quantum state.\n",
        "\n",
        "These are examples of quantum states of a qubit:\n",
        "\n",
        "$$\n",
        "  \\begin{pmatrix}\n",
        "    1\\\\[2mm]\n",
        "    0\n",
        "  \\end{pmatrix}\n",
        "  = \\vert 0\\rangle\n",
        "  \\quad\\text{and}\\quad\n",
        "  \\begin{pmatrix}\n",
        "    0\\\\[2mm]\n",
        "    1\n",
        "  \\end{pmatrix}\n",
        "  = \\vert 1\\rangle,\n",
        "$$\n",
        "\n",
        "$$\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}}\\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\frac{1}{\\sqrt{2}}\\,\\vert 0\\rangle + \\frac{1}{\\sqrt{2}}\\,\\vert 1\\rangle,\n",
        "  \\tag{1}\n",
        "$$\n",
        "\n",
        "and\n",
        "\n",
        "$$\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1+2i}{3}\\\\[2mm]\n",
        "    -\\frac{2}{3}\n",
        "  \\end{pmatrix}\n",
        "  = \\frac{1+2i}{3}\\,\\vert 0\\rangle - \\frac{2}{3}\\,\\vert 1\\rangle.\n",
        "$$\n",
        "\n",
        "The first two examples, $\\vert 0\\rangle$ and $\\vert 1\\rangle,$ illustrate that standard basis elements are valid quantum state vectors: their entries are complex numbers, where the imaginary part of these numbers all happen to be $0,$ and computing the sum of the absolute values squared of the entries yields\n",
        "\n",
        "$$\n",
        "  \\vert 1\\vert^2 + \\vert 0\\vert^2 = 1\n",
        "  \\quad\\text{and}\\quad\n",
        "  \\vert 0\\vert^2 + \\vert 1\\vert^2 = 1,\n",
        "$$\n",
        "\n",
        "as required.\n",
        "Similar to the classical setting, we associate the quantum state vectors $\\vert 0\\rangle$ and $\\vert 1\\rangle$ with a qubit being in the classical state $0$ and $1,$ respectively.\n",
        "\n",
        "For the other two examples, we again have complex number entries, and computing the sum of the absolute value squared of the entries yields\n",
        "\n",
        "$$\n",
        "  \\biggl\\vert\\frac{1}{\\sqrt{2}}\\biggr\\vert^2 +\n",
        "  \\biggl\\vert\\frac{1}{\\sqrt{2}}\\biggr\\vert^2 = \\frac{1}{2} + \\frac{1}{2} = 1\n",
        "$$\n",
        "\n",
        "and\n",
        "\n",
        "$$\n",
        "  \\biggl\\vert \\frac{1+2i}{3} \\biggr\\vert^2 +\n",
        "  \\biggl\\vert -\\frac{2}{3} \\biggr\\vert^2 = \\frac{5}{9} + \\frac{4}{9} = 1.\n",
        "$$\n",
        "\n",
        "These are therefore valid quantum state vectors. Note that they are linear combinations of the standard basis states $\\vert 0 \\rangle$ and $\\vert 1 \\rangle,$ and for this reason we often say that they're *superpositions* of the states $0$ and $1.$\n",
        "Within the context of quantum states, *superposition* and *linear combination* are essentially synonymous.\n",
        "\n",
        "The example $(1)$ of a qubit state vector above is very commonly encountered — it is called the *plus state* and is denoted as follows:\n",
        "\n",
        "$$\n",
        "  \\vert {+} \\rangle = \\frac{1}{\\sqrt{2}} \\vert 0\\rangle + \\frac{1}{\\sqrt{2}} \\vert 1\\rangle.\n",
        "$$\n",
        "\n",
        "We also use the notation\n",
        "\n",
        "$$\n",
        "  \\vert {-} \\rangle = \\frac{1}{\\sqrt{2}} \\vert 0\\rangle - \\frac{1}{\\sqrt{2}} \\vert 1\\rangle\n",
        "$$\n",
        "\n",
        "to refer to a related quantum state vector where the second entry is negative rather than positive, and we call this state the *minus state*.\n",
        "\n",
        "This sort of notation, where some symbol other than one referring to a classical state appears inside of a ket, is common — we can use whatever name we wish inside of a ket to name a vector.\n",
        "It is quite common to use the notation $\\vert\\psi\\rangle,$ or a different name in place of $\\psi,$ to refer to an arbitrary vector that may not necessarily be a standard basis vector.\n",
        "\n",
        "Notice that, if we have a vector $\\vert \\psi \\rangle$ whose indices correspond to some classical state set $\\Sigma,$ and if $a\\in\\Sigma$ is an element of this classical state set, then the matrix product $\\langle a\\vert \\vert \\psi\\rangle$ is equal to the entry of the vector $\\vert \\psi \\rangle$ whose index corresponds to $a.$\n",
        "As we did when $\\vert \\psi \\rangle$ was a standard basis vector, we write $\\langle a \\vert \\psi \\rangle$ rather than\n",
        "$\\langle a\\vert \\vert \\psi\\rangle$ for the sake of readability.\n",
        "\n",
        "For example, if $\\Sigma = \\{0,1\\}$ and\n",
        "\n",
        "$$\n",
        "\\vert \\psi \\rangle =\n",
        "\\frac{1+2i}{3} \\vert 0\\rangle - \\frac{2}{3} \\vert 1\\rangle\n",
        "= \\begin{pmatrix}\n",
        "    \\frac{1+2i}{3}\\\\[2mm]\n",
        "    -\\frac{2}{3}\n",
        "  \\end{pmatrix},\n",
        "  \\tag{2}\n",
        "$$\n",
        "\n",
        "then\n",
        "\n",
        "$$\n",
        "  \\langle 0 \\vert \\psi \\rangle = \\frac{1+2i}{3}\n",
        "  \\quad\\text{and}\\quad\n",
        "  \\langle 1 \\vert \\psi \\rangle = -\\frac{2}{3}.\n",
        "$$\n",
        "\n",
        "In general, when using the Dirac notation for arbitrary vectors, the notation $\\langle \\psi \\vert$ refers to the row vector obtained by taking the *conjugate-transpose* of the column vector $\\vert\\psi\\rangle,$ where the vector is transposed from a column vector to a row vector and each entry is replaced by its complex conjugate.\n",
        "For example, if $\\vert\\psi\\rangle$ is the vector defined in $(2),$ then\n",
        "\n",
        "$$\n",
        "\\langle\\psi\\vert = \\frac{1-2i}{3} \\langle 0\\vert - \\frac{2}{3} \\langle 1\\vert\n",
        "= \\begin{pmatrix}\n",
        "    \\frac{1-2i}{3} &\n",
        "    -\\frac{2}{3}\n",
        "  \\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "The reason we take the complex conjugate, in addition to the transpose, will be made more clear later on we discuss inner products.\n",
        "\n",
        "### Quantum states of other systems\n",
        "\n",
        "We can consider quantum states of systems having arbitrary classical state sets.\n",
        "For example, here is a quantum state vector for an electrical fan switch:\n",
        "\n",
        "$$\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{2}\\\\[1mm]\n",
        "    0 \\\\[1mm]\n",
        "    -\\frac{i}{2}\\\\[1mm]\n",
        "    \\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\frac{1}{2} \\vert\\mathrm{high}\\rangle\n",
        "  - \\frac{i}{2} \\vert\\mathrm{low}\\rangle\n",
        "  + \\frac{1}{\\sqrt{2}} \\vert\\mathrm{off}\\rangle.\n",
        "$$\n",
        "\n",
        "The assumption in place here is that the classical states are ordered as *high*, *medium*, *low*, *off*.\n",
        "There may be no particular reason why one would want to consider a quantum state of an electrical fan switch, but it is possible in principle.\n",
        "\n",
        "Here's another example, this time of a quantum decimal digit whose classical states are $0, 1, \\ldots, 9:$\n",
        "\n",
        "$$\n",
        "  \\frac{1}{\\sqrt{385}}\n",
        "  \\begin{pmatrix}\n",
        "    1\\\\\n",
        "    2\\\\\n",
        "    3\\\\\n",
        "    4\\\\\n",
        "    5\\\\\n",
        "    6\\\\\n",
        "    7\\\\\n",
        "    8\\\\\n",
        "    9\\\\\n",
        "    10\n",
        "  \\end{pmatrix}\n",
        "  =\n",
        "  \\frac{1}{\\sqrt{385}}\\sum_{k = 0}^9 (k+1) \\vert k \\rangle.\n",
        "$$\n",
        "\n",
        "This example illustrates the convenience of writing state vectors using the Dirac notation.\n",
        "For this particular example, the column vector representation is merely cumbersome — but if there were significantly more classical states it would become unusable.\n",
        "The Dirac notation, in contrast, supports precise descriptions of large and complicated vectors in a compact form.\n",
        "\n",
        "The Dirac notation also allows for the expression of vectors where different aspects of the vectors are *indeterminate,* meaning that they are unknown or not yet established.\n",
        "For example, for an arbitrary classical state set $\\Sigma,$ we can consider the quantum state vector\n",
        "\n",
        "$$\n",
        "  \\frac{1}{\\sqrt{|\\Sigma|}} \\sum_{a\\in\\Sigma} \\vert a \\rangle,\n",
        "$$\n",
        "\n",
        "where the notation $\\sqrt{|\\Sigma|}$ refers to the Euclidean norm of $\\Sigma,$ and $\\vert\\Sigma\\vert$ in this case is simply the number of elements in $\\Sigma.$\n",
        "In words, this is a *uniform superposition* over the classical states in $\\Sigma.$\n",
        "\n",
        "We'll encounter much more complicated expressions of quantum state vectors in later lessons, where the use of column vectors would be impractical or impossible.\n",
        "In fact, we'll mostly abandon the column vector representation of state vectors, except for vectors having a small number of entries (often in the context of examples), where it may be helpful to display and examine the entries explicitly.\n",
        "\n",
        "Here's one more reason why expressing state vectors using the Dirac notation is convenient: it alleviates the need to explicitly specify an ordering of the classical states (or, equivalently, the correspondence between classical states and vector indices).\n",
        "\n",
        "For example, a quantum state vector for a system having classical state set\n",
        "$\\{\\clubsuit,\\diamondsuit,\\heartsuit,\\spadesuit\\},$ such as\n",
        "\n",
        "$$\n",
        "    \\frac{1}{2} \\vert\\clubsuit\\rangle\n",
        "  + \\frac{i}{2} \\vert\\diamondsuit\\rangle\n",
        "  - \\frac{1}{2} \\vert\\heartsuit\\rangle\n",
        "  - \\frac{i}{2} \\vert\\spadesuit\\rangle,\n",
        "$$\n",
        "\n",
        "is unambiguously described by this expression, and there's really no need to choose or specify an ordering of this classical state set to make sense of the expression.\n",
        "In this case, it's not difficult to specify an ordering of the standard card suits — for instance, we might choose to order them like this: $\\clubsuit,$ $\\diamondsuit,$ $\\heartsuit,$ $\\spadesuit.$\n",
        "If we choose this particular ordering, the quantum state vector above would be represented by the column vector\n",
        "\n",
        "$$\n",
        "\\begin{pmatrix}\n",
        " \\frac{1}{2}\\\\[2mm]\n",
        " \\frac{i}{2}\\\\[2mm]\n",
        " -\\frac{1}{2}\\\\[2mm]\n",
        " -\\frac{i}{2}\n",
        "\\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "In general, however, it is convenient to be able to simply ignore the issue of how classical state sets are ordered.\n",
        "\n",
        "## Measuring quantum states\n",
        "\n",
        "Next let us consider what happens when a quantum state is *measured*, focusing on a simple type of measurement known as a *standard basis measurement*.\n",
        "(There are more general notions of measurement that we'll discuss later on.)\n",
        "\n",
        "Similar to the probabilistic setting, when a system in a quantum state is measured, the hypothetical observer performing the measurement won't see a quantum state vector, but rather will see some classical state.\n",
        "In this sense, measurements act as an interface between quantum and classical information, through which classical information is extracted from quantum states.\n",
        "\n",
        "The rule is simple: if a quantum state is measured, each classical state of the system appears with probability equal to the *absolute value squared* of the entry in the quantum state vector corresponding to that classical state.\n",
        "This is known as the *Born rule* in quantum mechanics.\n",
        "Notice that this rule is consistent with the requirement that the absolute values squared of the entries in a quantum state vector sum to $1,$ as it implies that the probabilities of different classical state measurement outcomes sum to $1.$\n",
        "\n",
        "For example, measuring the plus state\n",
        "\n",
        "$$\n",
        "  \\vert {+} \\rangle =\n",
        "  \\frac{1}{\\sqrt{2}} \\vert 0 \\rangle\n",
        "  + \\frac{1}{\\sqrt{2}} \\vert 1 \\rangle\n",
        "$$\n",
        "\n",
        "results in the two possible outcomes, $0$ and $1,$ with probabilities as follows.\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 0})\n",
        "  = \\bigl\\vert \\langle 0 \\vert {+} \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert \\frac{1}{\\sqrt{2}} \\biggr\\vert^2\n",
        "  = \\frac{1}{2}\n",
        "$$\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 1})\n",
        "  = \\bigl\\vert \\langle 1 \\vert {+} \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert \\frac{1}{\\sqrt{2}} \\biggr\\vert^2\n",
        "  = \\frac{1}{2}\n",
        "$$\n",
        "\n",
        "Interestingly, measuring the minus state\n",
        "\n",
        "$$\n",
        "  \\vert {-} \\rangle =\n",
        "  \\frac{1}{\\sqrt{2}} \\vert 0 \\rangle\n",
        "  - \\frac{1}{\\sqrt{2}} \\vert 1 \\rangle\n",
        "$$\n",
        "\n",
        "results in exactly the same probabilities for the two outcomes.\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 0})\n",
        "  = \\bigl\\vert \\langle 0 \\vert {-} \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert \\frac{1}{\\sqrt{2}} \\biggr\\vert^2\n",
        "  = \\frac{1}{2}\n",
        "$$\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 1})\n",
        "  = \\bigl\\vert \\langle 1 \\vert {-} \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert -\\frac{1}{\\sqrt{2}} \\biggr\\vert^2\n",
        "  = \\frac{1}{2}\n",
        "$$\n",
        "\n",
        "This suggests that, as far as standard basis measurements are concerned, the plus and minus states are no different.\n",
        "Why, then, would we care to make a distinction between them?\n",
        "The answer is that these two states behave differently when operations are performed on them, as we will discuss in the next subsection below.\n",
        "\n",
        "Of course, measuring the quantum state $\\vert 0\\rangle$ results in the classical state $0$ with certainty, and likewise measuring the quantum state $\\vert 1\\rangle$ results in the classical state $1$ with certainty.\n",
        "This is consistent with the identification of these quantum states with the system *being* in the corresponding classical state, as was suggested previously.\n",
        "\n",
        "As a final example, measuring the state\n",
        "\n",
        "$$\n",
        "  \\vert \\psi \\rangle = \\frac{1+2i}{3} \\vert 0\\rangle - \\frac{2}{3} \\vert 1\\rangle\n",
        "$$\n",
        "\n",
        "causes the two possible outcomes to appear with probabilities as follows:\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 0})\n",
        "  = \\bigl\\vert \\langle 0 \\vert \\psi \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert \\frac{1+2i}{3} \\biggr\\vert^2\n",
        "  = \\frac{5}{9},\n",
        "$$\n",
        "\n",
        "and\n",
        "\n",
        "$$\n",
        "  \\operatorname{Pr}(\\text{outcome is 1})\n",
        "  = \\bigl\\vert \\langle 1 \\vert \\psi \\rangle \\bigr\\vert^2\n",
        "  = \\biggl\\vert -\\frac{2}{3} \\biggr\\vert^2\n",
        "  = \\frac{4}{9}.\n",
        "$$\n",
        "\n",
        "## Unitary operations\n",
        "\n",
        "Thus far, it may not be evident why quantum information is fundamentally different from classical information.\n",
        "That is, when a quantum state is measured, the probability to obtain each classical state is given by the absolute value squared of the corresponding vector entry — so why not simply record these probabilities in a probability\n",
        "vector?\n",
        "\n",
        "The answer, at least in part, is that the set of allowable *operations* that can be performed on a quantum state is different than it is for classical information.\n",
        "Similar to the probabilistic setting, operations on quantum states are linear mappings — but rather than being represented by stochastic matrices, like in the classical case, operations on quantum state vectors are represented by *unitary* matrices.\n",
        "\n",
        "A square matrix $U$ having complex number entries is *unitary* if it satisfies the equations\n",
        "\n",
        "$$\n",
        "  \\begin{aligned}\n",
        "    U U^{\\dagger} &= \\mathbb{I} \\\\\n",
        "    U^{\\dagger} U &= \\mathbb{I}.\n",
        "  \\end{aligned}\n",
        "  \\tag{3}\n",
        "$$\n",
        "\n",
        "Here, $\\mathbb{I}$ is the identity matrix, and $U^{\\dagger}$ is the *conjugate transpose* of $U,$ meaning the matrix obtained by transposing $U$ and taking the complex conjugate of each entry.\n",
        "\n",
        "$$\n",
        "  U^{\\dagger} = \\overline{U^T}\n",
        "$$\n",
        "\n",
        "If either of the two equalities numbered $(3)$ above is true, then the other must also be true.\n",
        "Both equalities are equivalent to $U^{\\dagger}$ being the inverse of $U:$\n",
        "\n",
        "$$\n",
        "  U^{-1} = U^{\\dagger}.\n",
        "$$\n",
        "\n",
        "(Warning: if $M$ is not a square matrix, then it could be that $M^{\\dagger} M = \\mathbb{I}$ and $M M^{\\dagger} \\neq \\mathbb{I},$ for instance.\n",
        "The equivalence of the two equalities in the first equation above is only true for square matrices.)\n",
        "\n",
        "The condition that $U$ is unitary is equivalent to the condition that multiplication by $U$ does not change the Euclidean norm of any vector.\n",
        "That is, an $n\\times n$ matrix $U$ is unitary if and only if\n",
        "$\\| U \\vert \\psi \\rangle \\| = \\|\\vert \\psi \\rangle \\|$\n",
        "for every $n$-dimensional column vector $\\vert \\psi \\rangle$ with complex number entries.\n",
        "Thus, because the set of all quantum state vectors is the same as the set of vectors having Euclidean norm equal to $1,$ multiplying a unitary matrix to a quantum state vector results in another quantum state vector.\n",
        "\n",
        "Indeed, unitary matrices are exactly the set of linear mappings that always transform quantum state vectors to other quantum state vectors. Notice here a resemblance to the classical probabilistic case where operations are associated with stochastic matrices, which are the ones that always transform probability vectors into probability vectors.\n",
        "\n",
        "### Examples of unitary operations on qubits\n",
        "\n",
        "The following list describes some commonly encountered unitary operations on qubits.\n",
        "\n",
        "1. *Pauli operations.* The four Pauli matrices are as follows:\n",
        "\n",
        "   $$\n",
        "     \\mathbb{I} =\n",
        "     \\begin{pmatrix}\n",
        "       1 & 0\\\\\n",
        "       0 & 1\n",
        "     \\end{pmatrix},\n",
        "     \\quad\n",
        "     \\sigma_x =\n",
        "     \\begin{pmatrix}\n",
        "       0 & 1\\\\\n",
        "       1 & 0\n",
        "     \\end{pmatrix},\n",
        "     \\quad\n",
        "     \\sigma_y =\n",
        "     \\begin{pmatrix}\n",
        "       0 & -i\\\\\n",
        "       i & 0\n",
        "     \\end{pmatrix},\n",
        "     \\quad\n",
        "     \\sigma_z =\n",
        "     \\begin{pmatrix}\n",
        "       1 & 0\\\\\n",
        "       0 & -1\n",
        "     \\end{pmatrix}.\n",
        "   $$\n",
        "\n",
        "   A common alternative notation is $X = \\sigma_x,$ $Y = \\sigma_y,$ and $Z = \\sigma_z$ (but be aware that the letters $X,$ $Y,$ and $Z$ are also commonly used for other purposes). The $X$ operation is also called a *bit flip* or a *NOT operation* because it induces this action on bits:\n",
        "\n",
        "   $$\n",
        "     X \\vert 0\\rangle = \\vert 1\\rangle\n",
        "     \\quad \\text{and} \\quad\n",
        "     X \\vert 1\\rangle = \\vert 0\\rangle.\n",
        "   $$\n",
        "\n",
        "   The $Z$ operation is also called a *phase flip,* and it has this action:\n",
        "\n",
        "   $$\n",
        "     Z \\vert 0\\rangle = \\vert 0\\rangle\n",
        "     \\quad \\text{and} \\quad\n",
        "     Z \\vert 1\\rangle = - \\vert 1\\rangle.\n",
        "   $$\n",
        "\n",
        "2. *Hadamard operation*. The Hadamard operation is described by this matrix:\n",
        "\n",
        "   $$\n",
        "     H = \\begin{pmatrix}\n",
        "       \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "       \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "     \\end{pmatrix}.\n",
        "   $$\n",
        "\n",
        "3. *Phase operations.*  A phase operation is one described by the matrix\n",
        "\n",
        "   $$\n",
        "     P_{\\theta} =\n",
        "     \\begin{pmatrix}\n",
        "       1 & 0\\\\\n",
        "       0 & e^{i\\theta}\n",
        "     \\end{pmatrix}\n",
        "   $$\n",
        "\n",
        "   for any choice of a real number $\\theta.$\n",
        "   The operations\n",
        "\n",
        "   $$\n",
        "     S = P_{\\pi/2} =\n",
        "     \\begin{pmatrix}\n",
        "       1 & 0\\\\\n",
        "       0 & i\n",
        "     \\end{pmatrix}\n",
        "     \\quad \\text{and} \\quad\n",
        "     T = P_{\\pi/4} =\n",
        "     \\begin{pmatrix}\n",
        "       1 & 0\\\\\n",
        "       0 & \\frac{1 + i}{\\sqrt{2}}\n",
        "     \\end{pmatrix}\n",
        "   $$\n",
        "\n",
        "   are particularly important examples. Other examples include $\\mathbb{I} = P_0$ and $Z = P_{\\pi}.$\n",
        "\n",
        "All of the matrices just defined are unitary, and therefore represent quantum operations on a single qubit.\n",
        "For example, here is a calculation that verifies that $H$ is unitary:\n",
        "\n",
        "$$\n",
        "\\begin{pmatrix}\n",
        "  \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "  \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "\\end{pmatrix}^{\\dagger}\n",
        "\\begin{pmatrix}\n",
        "  \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "  \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "\\end{pmatrix}\n",
        "= \\begin{pmatrix}\n",
        "  \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "  \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "\\end{pmatrix}\n",
        "\\begin{pmatrix}\n",
        "  \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "  \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "\\end{pmatrix}\n",
        "= \\begin{pmatrix}\n",
        "  \\frac{1}{2} + \\frac{1}{2} & \\frac{1}{2} - \\frac{1}{2}\\\\[2mm]\n",
        "  \\frac{1}{2} - \\frac{1}{2} & \\frac{1}{2} + \\frac{1}{2}\n",
        "\\end{pmatrix}\n",
        "= \\begin{pmatrix}\n",
        "  1 & 0\\\\\n",
        "  0 & 1\n",
        "\\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "And here's the action of the Hadamard operation on a few commonly encountered qubit state vectors.\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  H \\vert 0 \\rangle & =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    1\\\\[2mm]\n",
        "    0\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}}\\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\vert + \\rangle\\\\[6mm]\n",
        "  H \\vert 1 \\rangle\n",
        "  & =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    0\\\\[2mm]\n",
        "    1\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}}\\\\[2mm]\n",
        "    -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\vert - \\rangle\\\\[6mm]\n",
        "  H \\vert + \\rangle\n",
        "  & =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}}\\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    1\\\\[2mm]\n",
        "    0\n",
        "  \\end{pmatrix}\n",
        "  = \\vert 0 \\rangle\\\\[6mm]\n",
        "  H \\vert - \\rangle\n",
        "  & =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}}\\\\[2mm]\n",
        "    -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    0\\\\[2mm]\n",
        "    1\n",
        "  \\end{pmatrix}\n",
        "  = \\vert 1 \\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "More succinctly, we obtain these four equations.\n",
        "\n",
        "$$\n",
        "  \\begin{aligned}\n",
        "    H \\vert 0 \\rangle = \\vert {+} \\rangle & \\qquad H \\vert {+} \\rangle = \\vert 0 \\rangle \\\\[1mm]\n",
        "    H \\vert 1 \\rangle = \\vert {-} \\rangle & \\qquad H \\vert {-} \\rangle = \\vert 1 \\rangle\n",
        "  \\end{aligned}\n",
        "$$\n",
        "\n",
        "It's worth pausing to consider the fact that $H\\vert {+} \\rangle = \\vert 0\\rangle$ and\n",
        "$H\\vert {-} \\rangle = \\vert 1\\rangle,$ in light of the question suggested in the previous section concerning the distinction between the states $\\vert {+} \\rangle$ and $\\vert {-} \\rangle.$\n",
        "\n",
        "Imagine a situation in which a qubit is prepared in one of the two quantum states $\\vert {+} \\rangle$ and\n",
        "$\\vert {-} \\rangle,$ but where it is not known to us which one it is.\n",
        "Measuring either state produces the same output distribution as the other, as we already observed:\n",
        "$0$ and $1$ both appear with equal probability $1/2,$ which provides no information whatsoever about which of the two states was prepared.\n",
        "\n",
        "However, if we first apply a Hadamard operation and then measure, we obtain the outcome $0$ with certainty if the original state was $\\vert {+} \\rangle,$ and we obtain the outcome $1,$ again with certainty, if the original state was $\\vert {-} \\rangle.$\n",
        "The quantum states $\\vert {+} \\rangle$ and $\\vert {-} \\rangle$ can therefore be discriminated *perfectly*.\n",
        "This reveals that sign changes, or more generally changes to the *phases* (which are also traditionally called *arguments*) of the complex number entries of a quantum state vector, can significantly change that state.\n",
        "\n",
        "Here's another example, showing how a Hadamard operation acts on a state vector that was mentioned previously.\n",
        "\n",
        "$$\n",
        "  H \\biggl(\\frac{1+2i}{3} \\vert 0\\rangle - \\frac{2}{3} \\vert 1\\rangle\\biggr)\n",
        "  = \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1+2i}{3}\\\\[2mm]\n",
        "    -\\frac{2}{3}\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    \\frac{-1+2i}{3\\sqrt{2}}\\\\[2mm]\n",
        "    \\frac{3+2i}{3\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\frac{-1+2i}{3\\sqrt{2}} | 0 \\rangle\n",
        "  + \\frac{3+2i}{3\\sqrt{2}} | 1 \\rangle\n",
        "$$\n",
        "\n",
        "Next, let's consider the action of a $T$ operation on a plus state.\n",
        "\n",
        "$$\n",
        "  T \\vert {+} \\rangle\n",
        "  = T \\biggl(\\frac{1}{\\sqrt{2}} \\vert 0\\rangle + \\frac{1}{\\sqrt{2}} \\vert 1\\rangle\\biggr)\n",
        "  = \\frac{1}{\\sqrt{2}} T\\vert 0\\rangle + \\frac{1}{\\sqrt{2}} T\\vert 1\\rangle\n",
        "  = \\frac{1}{\\sqrt{2}} \\vert 0\\rangle + \\frac{1+i}{2} \\vert 1\\rangle\n",
        "$$\n",
        "\n",
        "Notice here that we did not bother to convert to the equivalent matrix/vector forms, and instead used the linearity of matrix multiplication together with the formulas\n",
        "\n",
        "$$\n",
        "T \\vert 0\\rangle = \\vert 0\\rangle\n",
        "\\quad\\text{and}\\quad\n",
        "T \\vert 1\\rangle = \\frac{1 + i}{\\sqrt{2}} \\vert 1\\rangle.\n",
        "$$\n",
        "\n",
        "Along similar lines, we may compute the result of applying a Hadamard operation to the quantum state vector just obtained:\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "H\\, \\biggl(\\frac{1}{\\sqrt{2}} \\vert 0\\rangle + \\frac{1+i}{2} \\vert 1\\rangle\\biggr)\n",
        "& = \\frac{1}{\\sqrt{2}} H \\vert 0\\rangle + \\frac{1+i}{2} H \\vert 1\\rangle\\\\\n",
        "& = \\frac{1}{\\sqrt{2}} \\vert +\\rangle + \\frac{1+i}{2} \\vert -\\rangle \\\\\n",
        "& = \\biggl(\\frac{1}{2} \\vert 0\\rangle + \\frac{1}{2} \\vert 1\\rangle\\biggr)\n",
        "+ \\biggl(\\frac{1+i}{2\\sqrt{2}} \\vert 0\\rangle - \\frac{1+i}{2\\sqrt{2}} \\vert 1\\rangle\\biggr)\\\\\n",
        "& = \\biggl(\\frac{1}{2} + \\frac{1+i}{2\\sqrt{2}}\\biggr) \\vert 0\\rangle\n",
        "+ \\biggl(\\frac{1}{2} - \\frac{1+i}{2\\sqrt{2}}\\biggr) \\vert 1\\rangle.\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "The two approaches — one where we explicitly convert to matrix representations and the other where we use linearity and plug in the actions of an operation on standard basis states — are equivalent.\n",
        "We can use whichever one is more convenient in the case at hand.\n",
        "\n",
        "### Compositions of qubit unitary operations\n",
        "\n",
        "Compositions of unitary operations are represented by matrix multiplication, just like we had in the probabilistic setting.\n",
        "\n",
        "For example, suppose we first apply a Hadamard operation, followed by an $S$ operation, followed by another Hadamard operation.\n",
        "The resulting operation, which we shall name $R$ for the sake of this example, is as follows:\n",
        "\n",
        "$$\n",
        "  R = H S H =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    1 & 0\\\\\n",
        "    0 & i\n",
        "  \\end{pmatrix}\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} \\\\[2mm]\n",
        "    \\frac{1}{\\sqrt{2}} & -\\frac{1}{\\sqrt{2}}\n",
        "  \\end{pmatrix}\n",
        "  = \\begin{pmatrix}\n",
        "    \\frac{1+i}{2} & \\frac{1-i}{2} \\\\[2mm]\n",
        "    \\frac{1-i}{2} & \\frac{1+i}{2}\n",
        "  \\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "This unitary operation $R$ is an interesting example.\n",
        "By applying this operation twice, which is equivalent to squaring its matrix representation, we obtain a NOT operation:\n",
        "\n",
        "$$\n",
        "  R^2 =\n",
        "  \\begin{pmatrix}\n",
        "    \\frac{1+i}{2} & \\frac{1-i}{2} \\\\[2mm]\n",
        "    \\frac{1-i}{2} & \\frac{1+i}{2}\n",
        "  \\end{pmatrix}^2\n",
        "  = \\begin{pmatrix}\n",
        "    0 & 1 \\\\[2mm]\n",
        "    1 & 0\n",
        "  \\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "That is, $R$ is a *square root of NOT* operation.\n",
        "Such a behavior, where the same operation is applied twice to yield a NOT operation, is not possible for a classical operation on a single bit.\n",
        "\n",
        "### Unitary operations on larger systems\n",
        "\n",
        "In subsequent lessons, we will see many examples of unitary operations on systems having more than two classical states.\n",
        "An example of a unitary operation on a system having three classical states is given by the following matrix.\n",
        "\n",
        "$$\n",
        "  A =\n",
        "  \\begin{pmatrix}\n",
        "    {0} & {0} & {1} \\\\\n",
        "    {1} & {0} & {0} \\\\\n",
        "    {0} & {1} & {0}\n",
        "  \\end{pmatrix}\n",
        "$$\n",
        "\n",
        "Assuming that the classical states of the system are $0,$ $1,$ and $2,$ we can describe this operation as addition modulo $3.$\n",
        "\n",
        "$$\n",
        "  A \\vert 0\\rangle = \\vert 1\\rangle,\n",
        "  \\quad\n",
        "  A \\vert 1\\rangle = \\vert 2\\rangle,\n",
        "  \\quad\\text{and}\\quad\n",
        "  A \\vert 2\\rangle = \\vert 0\\rangle\n",
        "$$\n",
        "\n",
        "The matrix $A$ is an example of a *permutation matrix*, which is a matrix in which every row and column has exactly one $1.$\n",
        "Such matrices merely rearrange, or permute, the entries of the vectors they act upon.\n",
        "The identity matrix is perhaps the simplest example of a permutation matrix, and another example is the NOT operation on a bit or qubit.\n",
        "Every permutation matrix, in any positive integer dimension, is unitary.\n",
        "These are the only examples of matrices that represent both classical and quantum operations: a matrix is both stochastic and unitary if and only if it is a permutation matrix.\n",
        "\n",
        "Another example of a unitary matrix, this time being a $4\\times 4$ matrix, is this one:\n",
        "\n",
        "$$\n",
        "  U =\n",
        "  \\frac{1}{2}\n",
        "  \\begin{pmatrix}\n",
        "    1 & 1 & 1 & 1 \\\\[1mm]\n",
        "    1 & i & -1 & -i \\\\[1mm]\n",
        "    1 & -1 & 1 & -1 \\\\[1mm]\n",
        "    1 & -i & -1 & i\n",
        "  \\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "This matrix describes an operation known as the *quantum Fourier transform*, specifically in the $4\\times 4$ case.\n",
        "The quantum Fourier transform can be defined more generally, for any positive integer dimension $n,$ and plays a key role in quantum algorithms.\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "id": "a1b8767d",
      "source": "© IBM Corp., 2017-2026"
    }
  ],
  "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"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}