{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "37d2b600",
      "metadata": {},
      "source": [
        "---\n",
        "title: Analysis\n",
        "description: A free IBM course on quantum information and computation\n",
        "---\n",
        "\n",
        "# Analysis\n",
        "\n",
        "Now we'll analyze Grover's algorithm to understand how it works.\n",
        "We'll start with what could be described as a *symbolic* analysis, where we calculate how the Grover operation $G$ acts on certain states, and then we'll tie this symbolic analysis to a *geometric* picture that's helpful for visualizing how the algorithm works.\n",
        "\n",
        "## Solutions and non-solutions\n",
        "\n",
        "Let's start by defining two sets of strings.\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  A_0 &= \\bigl\\{ x\\in\\Sigma^n : f(x) = 0\\bigr\\} \\\\\n",
        "  A_1 &= \\bigl\\{ x\\in\\Sigma^n : f(x) = 1\\bigr\\}\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "The set $A_1$ contains all of the solutions to our search problem while $A_0$ contains the strings that aren't solutions (which we can refer to as *non-solutions* when it's convenient).\n",
        "These two sets satisfy $A_0 \\cap A_1 = \\varnothing$ and $A_0 \\cup A_1 = \\Sigma^n,$ which is to say that this is a *bipartition* of $\\Sigma^n.$\n",
        "\n",
        "Next we'll define two unit vectors representing uniform superpositions over the sets of solutions and non-solutions.\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  \\vert A_0\\rangle &= \\frac{1}{\\sqrt{\\vert A_0\\vert}} \\sum_{x\\in A_0} \\vert x\\rangle \\\\\n",
        "  \\vert A_1\\rangle &= \\frac{1}{\\sqrt{\\vert A_1\\vert}} \\sum_{x\\in A_1} \\vert x\\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "Formally speaking, each of these vectors is only defined when its corresponding set is nonempty, but hereafter we're going to focus on the case that neither $A_0$ nor $A_1$ is empty.\n",
        "The cases that $A_0 = \\varnothing$ and $A_1 = \\varnothing$ are easily handled separately, and we'll do that later.\n",
        "\n",
        "As an aside, the notation being used here is common: any time we have a finite and nonempty set $S,$ we can write $\\vert S\\rangle$ to denote the quantum state vector that's uniform over the elements of $S.$\n",
        "\n",
        "Let's also define $\\vert u \\rangle$ to be a *uniform* quantum state over all $n$-bit strings:\n",
        "\n",
        "$$\n",
        "\\vert u\\rangle = \\frac{1}{\\sqrt{N}} \\sum_{x\\in\\Sigma^n} \\vert x\\rangle.\n",
        "$$\n",
        "\n",
        "Notice that\n",
        "\n",
        "$$\n",
        "\\vert u\\rangle\n",
        "= \\sqrt{\\frac{\\vert A_0 \\vert}{N}} \\vert A_0\\rangle\n",
        "+ \\sqrt{\\frac{\\vert A_1 \\vert}{N}} \\vert A_1\\rangle.\n",
        "$$\n",
        "\n",
        "We also have that $\\vert u\\rangle = H^{\\otimes n} \\vert 0^n \\rangle,$ so $\\vert u\\rangle$ represents the state of the register $\\mathsf{Q}$ after the initialization in step 1 of Grover's algorithm.\n",
        "\n",
        "This implies that just before the iterations of $G$ happen in step 2, the state of $\\mathsf{Q}$ is contained in the two-dimensional vector space spanned by $\\vert A_0\\rangle$ and $\\vert A_1\\rangle,$ and moreover the coefficients of these vectors are real numbers.\n",
        "As we will see, the state of $\\mathsf{Q}$ will always have these properties — meaning that the state is a real linear combination of $\\vert A_0\\rangle$ and $\\vert A_1\\rangle$ — after any number of iterations of the operation $G$ in step 2.\n",
        "\n",
        "## An observation about the Grover operation\n",
        "\n",
        "We'll now turn our attention to the Grover operation\n",
        "\n",
        "$$\n",
        "G = H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n} Z_f,\n",
        "$$\n",
        "\n",
        "beginning with an interesting observation about it.\n",
        "\n",
        "Imagine for a moment that we replaced the function $f$ by the composition of $f$ with the NOT function — or, in other words, the function we get by flipping the output bit of $f.$\n",
        "We'll call this new function $g,$ and we can express it using symbols in a few alternative ways.\n",
        "\n",
        "$$\n",
        "g(x) = \\neg f(x) = 1 \\oplus f(x) = 1 - f(x) =\n",
        "\\begin{cases}\n",
        "1 & f(x) = 0\\\\[1mm]\n",
        "0 & f(x) = 1\n",
        "\\end{cases}\n",
        "$$\n",
        "\n",
        "Notice that\n",
        "\n",
        "$$\n",
        "(-1)^{g(x)} = (-1)^{1 \\oplus f(x)} = - (-1)^{f(x)}\n",
        "$$\n",
        "\n",
        "for every string $x\\in\\Sigma^n,$ and therefore\n",
        "\n",
        "$$\n",
        "Z_g = - Z_f.\n",
        "$$\n",
        "\n",
        "This means that if we were to substitute the function $f$ with the function $g,$ Grover's algorithm wouldn't function any differently — because the states we obtain from the algorithm in the two cases are necessarily equivalent up to a global phase.\n",
        "\n",
        "This isn't a problem!\n",
        "Intuitively speaking, the algorithm doesn't care which strings are solutions and which are non-solutions — it only needs to be able to *distinguish* solutions and non-solutions to operate correctly.\n",
        "\n",
        "## Action of the Grover operation\n",
        "\n",
        "Now let's consider the action of $G$ on the quantum state vectors $\\vert A_0\\rangle$ and $\\vert A_1\\rangle.$\n",
        "\n",
        "First, let's observe that the operation $Z_f$ has a very simple action on $\\vert A_0\\rangle$ and $\\vert A_1\\rangle.$\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "Z_f \\vert A_0\\rangle & = \\vert A_0\\rangle \\\\[1mm]\n",
        "Z_f \\vert A_1\\rangle & = -\\vert A_1\\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "Second, we have the operation $H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n}.$\n",
        "The operation $Z_{\\mathrm{OR}}$ is defined as\n",
        "\n",
        "$$\n",
        "Z_{\\mathrm{OR}} \\vert x\\rangle\n",
        "= \\begin{cases}\n",
        "\\vert x\\rangle & x = 0^n \\\\[2mm]\n",
        "-\\vert x\\rangle & x \\neq 0^n,\n",
        "\\end{cases}\n",
        "$$\n",
        "\n",
        "again for every string $x\\in\\Sigma^n,$ and a convenient alternative way to express this operation is like this:\n",
        "\n",
        "$$\n",
        "Z_{\\mathrm{OR}} = 2 \\vert 0^n \\rangle \\langle 0^n \\vert - \\mathbb{I}.\n",
        "$$\n",
        "\n",
        "A simple way to verify that this expression agrees with the definition of $Z_{\\mathrm{OR}}$ is to evaluate its action on standard basis states.\n",
        "\n",
        "The operation $H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n}$ can therefore be written like this:\n",
        "\n",
        "$$\n",
        "H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n} = 2 H^{\\otimes n} \\vert 0^n \\rangle \\langle 0^n \\vert H^{\\otimes n} - \\mathbb{I} = 2 \\vert u \\rangle \\langle u \\vert - \\mathbb{I},\n",
        "$$\n",
        "\n",
        "using the same notation, $\\vert u \\rangle,$ that we used above for the uniform superposition over all $n$-bit strings.\n",
        "\n",
        "And now we have what we need to compute the action of $G$ on $\\vert A_0\\rangle$ and $\\vert A_1\\rangle.$\n",
        "First let's compute the action of $G$ on $\\vert A_0\\rangle.$\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  G \\vert A_0 \\rangle\n",
        "  & = \\bigl( 2 \\vert u\\rangle \\langle u \\vert - \\mathbb{I}\\bigr) Z_f \\vert A_0\\rangle \\\\\n",
        "  & = \\bigl( 2 \\vert u\\rangle \\langle u \\vert - \\mathbb{I}\\bigr) \\vert A_0\\rangle \\\\\n",
        "  & = 2 \\sqrt{\\frac{\\vert A_0\\vert}{N}} \\vert u\\rangle -\\vert A_0 \\rangle\\\\\n",
        "  & = 2 \\sqrt{\\frac{\\vert A_0\\vert}{N}} \\biggl(\n",
        "  \\sqrt{\\frac{\\vert A_0\\vert}{N}} \\vert A_0\\rangle + \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\vert A_1\\rangle\\biggr)\n",
        "  -\\vert A_0 \\rangle \\\\\n",
        "  & = \\biggl( \\frac{2\\vert A_0\\vert}{N} - 1\\biggr) \\vert A_0 \\rangle\n",
        "  + \\frac{2 \\sqrt{\\vert A_0\\vert \\cdot \\vert A_1\\vert}}{N} \\vert A_1 \\rangle \\\\\n",
        "  & = \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} \\vert A_0 \\rangle\n",
        "  + \\frac{2 \\sqrt{\\vert A_0\\vert \\cdot \\vert A_1\\vert}}{N} \\vert A_1 \\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "And second, let's compute the action of $G$ on $\\vert A_1\\rangle.$\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  G \\vert A_1 \\rangle\n",
        "  & = \\bigl( 2 \\vert u\\rangle \\langle u \\vert - \\mathbb{I} \\bigr) Z_f \\vert A_1\\rangle \\\\\n",
        "  & = - \\bigl( 2 \\vert u\\rangle \\langle u \\vert - \\mathbb{I} \\bigr) \\vert A_1\\rangle \\\\\n",
        "  & = - 2 \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\vert u\\rangle + \\vert A_1 \\rangle \\\\\n",
        "  & = - 2 \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\biggl(\\sqrt{\\frac{\\vert A_0\\vert}{N}} \\vert A_0\\rangle\n",
        "      + \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\vert A_1\\rangle\\biggr) + \\vert A_1 \\rangle \\\\\n",
        "  & = - \\frac{2 \\sqrt{\\vert A_1\\vert \\cdot \\vert A_0\\vert}}{N} \\vert A_0 \\rangle\n",
        "      + \\biggl( 1 - \\frac{2\\vert A_1\\vert}{N} \\biggr) \\vert A_1 \\rangle \\\\\n",
        "  & = - \\frac{2 \\sqrt{\\vert A_1\\vert \\cdot \\vert A_0\\vert}}{N} \\vert A_0 \\rangle\n",
        "      + \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} \\vert A_1 \\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "In both cases we're using the equation\n",
        "\n",
        "$$\n",
        "\\vert u\\rangle\n",
        "= \\sqrt{\\frac{\\vert A_0 \\vert}{N}} \\vert A_0\\rangle\n",
        "+ \\sqrt{\\frac{\\vert A_1 \\vert}{N}} \\vert A_1\\rangle\n",
        "$$\n",
        "\n",
        "along with the expressions\n",
        "\n",
        "$$\n",
        "\\langle u \\vert A_0\\rangle = \\sqrt{\\frac{\\vert A_0 \\vert}{N}}\n",
        "\\qquad\\text{and}\\qquad\n",
        "\\langle u \\vert A_1\\rangle = \\sqrt{\\frac{\\vert A_1 \\vert}{N}}\n",
        "$$\n",
        "\n",
        "that follow.\n",
        "\n",
        "In summary, we have\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "  G \\vert A_0 \\rangle\n",
        "  & = \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} \\vert A_0 \\rangle\n",
        "  + \\frac{2 \\sqrt{\\vert A_0\\vert \\cdot \\vert A_1\\vert}}{N} \\vert A_1 \\rangle\\\\[2mm]\n",
        "  G \\vert A_1 \\rangle\n",
        "  & = - \\frac{2 \\sqrt{\\vert A_1\\vert \\cdot \\vert A_0\\vert}}{N} \\vert A_0 \\rangle\n",
        "      + \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} \\vert A_1 \\rangle.\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "As we already noted, the state of $\\mathsf{Q}$ just prior to step 2 is contained in the two-dimensional space spanned by $\\vert A_0\\rangle$ and $\\vert A_1\\rangle,$ and we have just established that $G$ maps any vector in this space to another vector in the same space.\n",
        "This means that, for the sake of the analysis, we can focus our attention exclusively on this subspace.\n",
        "\n",
        "To better understand what's happening within this two-dimensional space, let's express the action of $G$ on this space as a matrix,\n",
        "\n",
        "$$\n",
        "M = \\begin{pmatrix}\n",
        "  \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} & -\\frac{2 \\sqrt{\\vert A_1\\vert \\cdot \\vert A_0\\vert}}{N} \\\\[2mm]\n",
        "  \\frac{2 \\sqrt{\\vert A_0\\vert \\cdot \\vert A_1\\vert}}{N} & \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N}\n",
        "\\end{pmatrix},\n",
        "$$\n",
        "\n",
        "whose first and second rows/columns correspond to $\\vert A_0\\rangle$ and $\\vert A_1\\rangle,$ respectively.\n",
        "So far in this series, we've always connected the rows and columns of matrices with the classical states of a system, but matrices can also be used to describe the actions of linear mappings on different bases like we have here.\n",
        "\n",
        "While it isn't at all obvious at first glance, the matrix $M$ is what we obtain by *squaring* a simpler-looking matrix.\n",
        "\n",
        "$$\n",
        "\\begin{pmatrix}\n",
        "  \\sqrt{\\frac{\\vert A_0\\vert}{N}} & - \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\\\[2mm]\n",
        "  \\sqrt{\\frac{\\vert A_1\\vert}{N}} & \\sqrt{\\frac{\\vert A_0\\vert}{N}}\n",
        "\\end{pmatrix}^2\n",
        "=\n",
        "\\begin{pmatrix}\n",
        "  \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N} & -\\frac{2 \\sqrt{\\vert A_1\\vert \\cdot \\vert A_0\\vert}}{N} \\\\[2mm]\n",
        "  \\frac{2 \\sqrt{\\vert A_0\\vert \\cdot \\vert A_1\\vert}}{N} & \\frac{\\vert A_0\\vert - \\vert A_1\\vert}{N}\n",
        "\\end{pmatrix} = M\n",
        "$$\n",
        "\n",
        "The matrix\n",
        "\n",
        "$$\n",
        "\\begin{pmatrix}\n",
        "  \\sqrt{\\frac{\\vert A_0\\vert}{N}} & - \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\\\[2mm]\n",
        "  \\sqrt{\\frac{\\vert A_1\\vert}{N}} & \\sqrt{\\frac{\\vert A_0\\vert}{N}}\n",
        "\\end{pmatrix}\n",
        "$$\n",
        "\n",
        "is a *rotation matrix*, which we can alternatively express as\n",
        "\n",
        "$$\n",
        "\\begin{pmatrix}\n",
        "  \\sqrt{\\frac{\\vert A_0\\vert}{N}} & - \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\\\[2mm]\n",
        "  \\sqrt{\\frac{\\vert A_1\\vert}{N}} & \\sqrt{\\frac{\\vert A_0\\vert}{N}}\n",
        "\\end{pmatrix}\n",
        "=\n",
        "\\begin{pmatrix}\n",
        "  \\cos(\\theta) & -\\sin(\\theta) \\\\[2mm]\n",
        "  \\sin(\\theta) & \\cos(\\theta)\n",
        "\\end{pmatrix}\n",
        "$$\n",
        "\n",
        "for\n",
        "\n",
        "$$\n",
        "\\theta = \\sin^{-1}\\biggl(\\sqrt{\\frac{\\vert A_1\\vert}{N}}\\biggr).\n",
        "$$\n",
        "\n",
        "This angle $\\theta$ is going to play a very important role in the analysis that follows, so it's worth stressing its importance here as we see it for the first time.\n",
        "\n",
        "In light of this expression of this matrix, we observe that\n",
        "\n",
        "$$\n",
        "M = \\begin{pmatrix}\n",
        "  \\cos(\\theta) & -\\sin(\\theta) \\\\[2mm]\n",
        "  \\sin(\\theta) & \\cos(\\theta)\n",
        "\\end{pmatrix}^2\n",
        "= \\begin{pmatrix}\n",
        "  \\cos(2\\theta) & -\\sin(2\\theta) \\\\[2mm]\n",
        "  \\sin(2\\theta) & \\cos(2\\theta)\n",
        "\\end{pmatrix}.\n",
        "$$\n",
        "\n",
        "This is because rotating by the angle $\\theta$ two times is equivalent to rotating by the angle $2\\theta.$\n",
        "Another way to see this is to make use of the alternative expression\n",
        "\n",
        "$$\n",
        "\\theta\n",
        "= \\cos^{-1}\\biggl(\\sqrt{\\frac{\\vert A_0\\vert}{N}}\\biggr),\n",
        "$$\n",
        "\n",
        "together with the *double angle* formulas from trigonometry:\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "\\cos(2\\theta) & = \\cos^2(\\theta) - \\sin^2(\\theta)\\\\[1mm]\n",
        "\\sin(2\\theta) & = 2 \\sin(\\theta)\\cos(\\theta).\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "In summary, the state of the register $\\mathsf{Q}$ at the start of step 2 is\n",
        "\n",
        "$$\n",
        "\\vert u\\rangle\n",
        "= \\sqrt{\\frac{\\vert A_0\\vert}{N}} \\vert A_0\\rangle\n",
        "+ \\sqrt{\\frac{\\vert A_1\\vert}{N}} \\vert A_1\\rangle\n",
        "= \\cos(\\theta) \\vert A_0\\rangle + \\sin(\\theta) \\vert A_1\\rangle,\n",
        "$$\n",
        "\n",
        "and the effect of applying $G$ to this state is to rotate it by an angle $2\\theta$ within the space spanned by $\\vert A_0\\rangle$ and $\\vert A_1\\rangle.$\n",
        "So, for example, we have\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "G \\vert u \\rangle &= \\cos(3\\theta) \\vert A_0\\rangle + \\sin(3\\theta) \\vert A_1\\rangle\\\\[1mm]\n",
        "G^2 \\vert u \\rangle &= \\cos(5\\theta) \\vert A_0\\rangle + \\sin(5\\theta) \\vert A_1\\rangle\\\\[1mm]\n",
        "G^3 \\vert u \\rangle &= \\cos(7\\theta) \\vert A_0\\rangle + \\sin(7\\theta) \\vert A_1\\rangle\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "and in general\n",
        "\n",
        "$$\n",
        "G^t \\vert u \\rangle\n",
        "= \\cos\\bigl((2t + 1)\\theta\\bigr) \\vert A_0\\rangle\n",
        "+ \\sin\\bigl((2t + 1)\\theta\\bigr) \\vert A_1\\rangle.\n",
        "$$\n",
        "\n",
        "## Geometric picture\n",
        "\n",
        "Now let's connect the analysis we just went through to a geometric picture.\n",
        "The idea is that the operation $G$ is the product of two *reflections*,\n",
        "$Z_f$ and $H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n}.$\n",
        "And the net effect of performing two reflections is to perform a *rotation*.\n",
        "\n",
        "Let's start with $Z_f.$\n",
        "As we already observed previously, we have\n",
        "\n",
        "$$\n",
        "\\begin{aligned}\n",
        "Z_f \\vert A_0\\rangle & = \\vert A_0\\rangle \\\\[1mm]\n",
        "Z_f \\vert A_1\\rangle & = -\\vert A_1\\rangle.\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "Within the two-dimensional vector space spanned by $\\vert A_0\\rangle$ and $\\vert A_1\\rangle,$\n",
        "this is a *reflection* about the line parallel to $\\vert A_0\\rangle,$ which we'll call $L_1.$\n",
        "Here's a figure illustrating the action of this reflection on a hypothetical unit vector $\\vert\\psi\\rangle,$\n",
        "which we're assuming is a real linear combination of $\\vert A_0\\rangle$ and $\\vert A_1\\rangle.$\n",
        "\n",
        "![A figure depicting the action of a reflection on a vector.](https://quantum.cloud.ibm.com/learning/images/courses/fundamentals-of-quantum-algorithms/grover-algorithm/reflection1.svg)\n",
        "\n",
        "Second we have the operation $H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n},$ which we've already seen can be written as\n",
        "\n",
        "$$\n",
        "H^{\\otimes n} Z_{\\mathrm{OR}} H^{\\otimes n} = 2 \\vert u \\rangle \\langle u \\vert - \\mathbb{I}.\n",
        "$$\n",
        "\n",
        "This is also a reflection, this time about the line $L_2$ parallel to the vector $\\vert u\\rangle.$\n",
        "Here's a figure depicting the action of this reflection on a unit vector $\\vert\\psi\\rangle.$\n",
        "\n",
        "![A figure depicting the action of a second reflection on a vector.](https://quantum.cloud.ibm.com/learning/images/courses/fundamentals-of-quantum-algorithms/grover-algorithm/reflection2.svg)\n",
        "\n",
        "When we compose these two reflections, we obtain a rotation — by twice the angle between the lines of reflection — as this figure illustrates.\n",
        "\n",
        "![A figure depicting the action of the Grover operation on a vector.](https://quantum.cloud.ibm.com/learning/images/courses/fundamentals-of-quantum-algorithms/grover-algorithm/Grover_rotation.svg)\n",
        "\n",
        "This explains, in geometric terms, why the effect of the Grover operation is to rotate linear combinations of $\\vert A_0\\rangle$ and $\\vert A_1\\rangle$ by an angle of $2\\theta.$\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
}