Searching

Quantum Searching

Published on September 11, 2026

Quantum Searching

Imagine a shuffled deck of 52 cards, and you are asked to find the ace of spades. The most natural thing to do is to take the top most card, see if it's the ace of spades. If not, put it aside, take the top most and repeat the process until you find the ace of spades. If you are lucky, the ace of spades is the top most then we're done. If you're not so lucky, the ace of spades is at the bottom and it will take you 52 peeks before you find the card.

If we scatter the cards face down on the floor and randomly pick a card, then on the average, we will need 52/252/2 peeks before we can find the ace of spades.

With quantum computing we can do even better!

For the sake of demonstration, let's say we have 8 cards as shown in the figure below. We want to find the ace of spades.

Here are the steps:

  1. Label each card from 0 to 7 in random order. The positions of the cards will represent the states of cards. The state 6\left\vert 6\right\rangle will therefore represent the ace of spades.

  2. Let ϕ\left\vert\phi\right\rangle be the superposition of states:

ϕ=12n/2k=02n1x=12n/2(0++7)=18[11111111]\displaystyle \left|\phi\right\rangle = \frac{1}{2^{n/2}} \sum_{k=0}^{2^n-1} \left|x\right\rangle = \frac{1}{2^{n/2}} \Big( \left|0\right\rangle + \ldots + \left|7\right\rangle \Big) = \frac{1}{\sqrt{8}} \left[ \begin{array}{c} 1\\ 1\\ 1\\ 1\\ 1\\ 1\\ 1\\ 1 \end{array} \right]
  1. Let ff be a function such that
f(x)={1x corresponds to ace of spades0 otherwisef(x) = \begin{cases} 1 & x \text{ corresponds to ace of spades}\\ 0 & \text{ otherwise} \end{cases}

Define the operator V\mathbf{V}

V=I2aa\mathbf{V} = \mathbf{I} - 2\left|a\right\rangle \left\langle a\right|

where a is the unique value where

f(a)=1f(a) = 1

In our example, the value of a is 6. Therefore,

V=[1.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.000.000.000.000.000.000.000.000.001.00]\mathbf{V} = \begin{bmatrix} 1.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 0.00 & 1.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 0.00 & 0.00 & 1.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 0.00 & 0.00 & 0.00 & 1.00 & 0.00 & 0.00 & 0.00 & 0.00 \\ 0.00 & 0.00 & 0.00 & 0.00 & 1.00 & 0.00 & 0.00 & 0.00 \\ 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 1.00 & 0.00 & 0.00 \\ 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & -1.00 & 0.00 \\ 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 0.00 & 1.00 \\ \end{bmatrix}

Observe that the matrix element V6,6=1\mathbf{V}_{6,6} = -1

  1. Define the operator W:
W=2ϕϕI=[0.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.75]\mathbf{W} = 2\left|\phi\right\rangle \left\langle\phi\right| - \mathbf{I} = \begin{bmatrix} -0.75 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 \\ 0.25 & -0.75 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 \\ 0.25 & 0.25 & -0.75 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & -0.75 & 0.25 & 0.25 & 0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & -0.75 & 0.25 & 0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.75 & 0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.75 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.75 \\ \end{bmatrix}
  1. Compute the operator WV\mathbf{WV}:
WV=[0.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.750.250.250.250.250.250.250.250.250.75]\mathbf{WV} = \begin{bmatrix} -0.75 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.25 & 0.25 \\ 0.25 & -0.75 & 0.25 & 0.25 & 0.25 & 0.25 & -0.25 & 0.25 \\ 0.25 & 0.25 & -0.75 & 0.25 & 0.25 & 0.25 & -0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & -0.75 & 0.25 & 0.25 & -0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & -0.75 & 0.25 & -0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.75 & -0.25 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.75 & 0.25 \\ 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & 0.25 & -0.25 & -0.75 \\ \end{bmatrix}
  1. Multiply WV\mathbf{WV} to ϕ\left\vert\phi\right\rangle a number of times, about π/42n/2=π/48=2.22\pi/4 \cdot 2^{n/2} = \pi/4 \cdot \sqrt{8} = 2.2 \approx 2:
WVϕ=[0.180.180.180.180.180.180.880.18],(WV)2ϕ=[0.090.090.090.090.090.090.970.09]\mathbf{WV}\left|\phi\right\rangle = \begin{bmatrix} 0.18 \\ 0.18 \\ 0.18 \\ 0.18 \\ 0.18 \\ 0.18 \\ 0.88 \\ 0.18 \\ \end{bmatrix}, \mathbf{(WV)^2}\left|\phi\right\rangle = \begin{bmatrix} -0.09 \\ -0.09 \\ -0.09 \\ -0.09 \\ -0.09 \\ -0.09 \\ 0.97 \\ -0.09 \\ \end{bmatrix}

As you can see, the probability of getting the state 6\left\vert 6\right\rangle becomes very close to 1.

Making a measurement of the input register at this point will give us the state 6\left\vert 6\right\rangle with a probability very close to 1.

We have just demonstrated the quantum search algorithm!

Why it works

Let ff be a function such that

f(x)={1x the item we are looking for0 otherwisef(x) = \begin{cases} 1 & x \text{ the item we are looking for}\\ 0 & \text{ otherwise} \end{cases}

Define the operator Uf\mathbf{U}_f whose action on an n qubit register and 1 qubit output register is

Uf(xy)=xyf(x)\mathbf{U}_f(\left|x\right\rangle \otimes \left|y\right\rangle) = \left|x\right\rangle\otimes\left|y\oplus f(x)\right\rangle

Prepare the n qubit input register and 1 qubit output register in the following state:

00n times1\underbrace{\left|0\right\rangle\ldots \left|0\right\rangle}_{\text{n times}} \otimes \left|1\right\rangle

Applying the Hadamard operator on the input and output qubits gives us a superposition of N=2nN=2^n states:

HnH(001)=Hn00H1=12n/2x=02n1x12(01)\begin{array}{rl} \displaystyle \mathbf{H}^{\otimes n}\otimes\mathbf{H}(\left|0\ldots 0\right\rangle\otimes \left|1\right\rangle )&= \mathbf{H}^{\otimes n}\left|0\ldots 0\right\rangle\otimes \mathbf{H}\left|1\right\rangle\\ &= \displaystyle \frac{1}{2^{n/2}}\sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \frac{1}{\sqrt{2}} \left(\left|0\right\rangle - \left|1\right\rangle\right) \end{array}

Next apply the operator Uf\mathbf{U}_f to get

Uf[12n/2x=02n1x12(01)]=1212n/2[x=02n1Ufx0x=02n1Ufx1]=1212n/2[x=02n1x0f(x)first underbracex=02n1x1f(x)second underbrace]\begin{array}{rl} \displaystyle \mathbf{U}_f \left[\frac{1}{2^{n/2}}\sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \frac{1}{\sqrt{2}} \left(\left|0\right\rangle - \left|1\right\rangle\right)\right] &= \displaystyle \frac{1}{\sqrt{2}} \frac{1}{2^{n/2}}\left[\sum_{x=0}^{2^n-1} \mathbf{U}_f \left|x\right\rangle \otimes \left|0\right\rangle - \sum_{x=0}^{2^n-1} \mathbf{U}_f \left|x\right\rangle \otimes \left|1\right\rangle\right] \\ &= \displaystyle \frac{1}{\sqrt{2}} \frac{1}{2^{n/2}}\left[\underbrace{\sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \left|0\oplus f(x) \right\rangle }_{\text{first underbrace}} - \underbrace{\sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \left|1\oplus f(x) \right\rangle}_{\text{second underbrace}}\right] \end{array}

When f(x)=1f(x) = 1 for x=a, the first underbrace will expand to

x=02n1x0f(x)=00++a1++2n10\displaystyle \sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \left|0\oplus f(x) \right\rangle = \left|0\right\rangle\otimes\left|0\right\rangle + \ldots + \underbrace{\left|a\right\rangle\otimes\left|1\right\rangle} + \ldots + \left|2^n-1\right\rangle \otimes \left|0\right\rangle

The second underbrace will expand to

x=02n1x1f(x)=01++a0++2n11\displaystyle \sum_{x=0}^{2^n-1} \left|x\right\rangle \otimes \left|1\oplus f(x) \right\rangle = \left|0\right\rangle\otimes\left|1\right\rangle + \ldots + \underbrace{\left|a\right\rangle\otimes\left|0\right\rangle} + \ldots + \left|2^n-1\right\rangle \otimes \left|1\right\rangle\\

The underbraces in the above summations can be swapped so that we get

1212n/2[x=02n1(1)f(x)x0x=02n1(1)f(x)x1]\displaystyle \frac{1}{\sqrt{2}} \frac{1}{2^{n/2}} \left[ \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle \otimes \left|0\right\rangle - \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle \otimes \left|1\right\rangle\right] =12n/2x=02n1(1)f(x)x12(01)=12n/2x=02n1(1)f(x)xH1\begin{array}{rl} &= \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle \otimes \frac{1}{\sqrt{2}} \left( \left|0\right\rangle - \left|1\right\rangle\right)\\ &= \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle \otimes \mathbf{H}\left|1\right\rangle \end{array}

This means that the action of Uf\mathbf{U}_f leaves the output qubit unentangled with the input qubit. We can just ignore this moving forward and focus our attention to the input qubits.

We view the current state of the input qubit as the result of some operator V\mathbf{V} defined by

Vϕ=12n/2x=02n1(1)f(x)x\mathbf{V} \left|\phi\right\rangle = \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle

where

ϕ=12n/2x=02n1x\left|\phi\right\rangle = \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} \left|x\right\rangle

We can derive the expression of V\mathbf{V} by expanding Vϕ\mathbf{V} \left\vert\phi\right\rangle, noting that at x=a, f(x)=1f(x) = 1:

Vϕ=12n/2x=02n1(1)f(x)x=12n/2[0+a++2n1]=12n/2[0++a++2n12a]=12n/2x=02n1x22n/2a=ϕ2aϕa=[I2aa]ϕ\begin{array}{rl} \mathbf{V} \left|\phi\right\rangle &= \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} (-1)^{f(x)} \left|x\right\rangle\\ &= \displaystyle \frac{1}{2^{n/2}} \left[\left|0\right\rangle + \ldots - \left|a\right\rangle +\ldots + \left|2^n-1\right\rangle\right]\\ &= \displaystyle \frac{1}{2^{n/2}} \left[\left|0\right\rangle + \ldots + \left|a\right\rangle +\ldots + \left|2^n-1\right\rangle - 2\left|a\right\rangle\right]\\ &= \displaystyle \frac{1}{2^{n/2}} \sum_{x=0}^{2^n-1} \left|x\right\rangle - \frac{2}{2^{n/2}} \left|a\right\rangle\\ &= \left|\phi\right\rangle - 2\left\langle a|\phi\right\rangle \left|a\right\rangle\\ &= \Big[\mathbf{I} - 2\left|a\right\rangle \left\langle a\right|\Big] \left|\phi\right\rangle\\ \end{array}

which means

V=I2aa\mathbf{V} = \mathbf{I} - 2\left|a\right\rangle \left\langle a\right|

The two vectors ϕ\left\vert\phi\right\rangle and a\left\vert a\right\rangle determine a plane P. Let a\left\vert a_{\perp}\right\rangle be the vector in the plane perpendicular to a\left\vert a\right\rangle. We can write ϕ\left\vert \phi\right\rangle as

ϕ=ϕ1a+ϕ2a\left|\phi\right\rangle = \phi_1 \left|a_\perp\right\rangle + \phi_2\left|a\right\rangle

Applying V\mathbf{V} to ϕ\left\vert \phi\right\rangle gives us:

Vϕ=(I2aa)(ϕ1a+ϕ2a)=ϕ1a+ϕ2a2ϕ2a=ϕ1aϕ2a\begin{array}{rl} \mathbf{V}\left|\phi\right\rangle &= \left(\mathbf{I} - 2\left|a\right\rangle \left\langle a\right| \right) \left( \phi_1 \left|a_\perp\right\rangle + \phi_2\left|a\right\rangle \right)\\ &= \phi_1 \left|a_\perp\right\rangle + \phi_2\left|a\right\rangle - 2\phi_2\left|a\right\rangle\\ &= \phi_1 \left|a_\perp\right\rangle - \phi_2\left|a\right\rangle \end{array}

The effect of the operator V\mathbf{V} is therefore to reflect the vector ϕ\left\vert \phi\right\rangle with respect to the vector a\left\vert a_\perp\right\rangle.

Now, we want to reflect this vector Vϕ\mathbf{V}\left\vert \phi\right\rangle with respect to ϕ\left\vert \phi\right\rangle. To accomplish this, we define the operator W\mathbf{W} by

W=2ϕϕI\mathbf{W} = 2\left|\phi\right\rangle \left\langle\phi\right| - \mathbf{I}

Apply this operator to Vϕ\mathbf{V}\left\vert\phi\right\rangle:

WVϕ=[2ϕϕI]Vϕ=2ϕϕVϕVϕ\begin{array}{rl} \mathbf{WV}\left|\phi\right\rangle &= \Big[ 2\left|\phi\right\rangle \left\langle\phi\right| - \mathbf{I} \Big]\mathbf{V}\left|\phi\right\rangle\\ &= 2\left|\phi\right\rangle \left\langle\phi\right|\mathbf{V}\left|\phi\right\rangle -\mathbf{V}\left|\phi\right\rangle \end{array}

If we express Vϕ\mathbf{V}\left\vert \phi\right\rangle as a linear combination of ϕ\left\vert\phi\right\rangle and a vector ϕ\left\vert\phi_\perp\right\rangle in the plane P,

Vϕ=αϕ+βϕ\mathbf{V}\left|\phi\right\rangle = \alpha \left|\phi\right\rangle + \beta \left|\phi_\perp\right\rangle

We have,

WVϕ=[2ϕϕI]Vϕ=2ϕϕVϕVϕ=2αϕαϕβϕ=αϕβϕ\begin{array}{rl} \mathbf{WV}\left|\phi\right\rangle &= \Big[ 2\left|\phi\right\rangle \left\langle\phi\right| - \mathbf{I} \Big]\mathbf{V}\left|\phi\right\rangle\\ &= 2\left|\phi\right\rangle \left\langle\phi\right|\mathbf{V}\left|\phi\right\rangle -\mathbf{V}\left|\phi\right\rangle\\ &= 2\alpha\left|\phi\right\rangle - \alpha \left|\phi\right\rangle - \beta \left|\phi_\perp\right\rangle\\ &= \alpha\left|\phi\right\rangle - \beta \left|\phi_\perp\right\rangle \end{array}

which demonstrates that W\mathbf{W} reflects Vϕ\mathbf{V}\left\vert \phi\right\rangle with respect to ϕ\left\vert \phi\right\rangle.

Therefore, the effect of WV\mathbf{WV} on ϕ\left\vert \phi\right\rangle is to rotate it by an angle γ\gamma counter-clockwise.

We can compute this γ\gamma by getting the inner product of WVϕ\mathbf{WV}\left\vert\phi\right\rangle and ϕ\left\vert\phi\right\rangle. First, let's find the expression of WVϕ\mathbf{WV}\left\vert \phi\right\rangle:

WVϕ=W(I2aa)ϕ=W(ϕ2aϕa)\begin{array}{rl} \mathbf{WV}\left|\phi\right\rangle &= \mathbf{W} \left( \mathbf{I} - 2\left|a\right\rangle \langle a |\right) \left|\phi\right\rangle\\ &= \mathbf{W} \left(\left|\phi\right\rangle - 2 \underbrace{\langle a| \phi \rangle} \left|a\right\rangle \right) \end{array}

The quantity aϕ\langle a\vert \phi \rangle is

aϕ=a(12n/2k=02n1x)=12n/2=cosθ\langle a| \phi \rangle = \displaystyle \langle a| \left( \frac{1}{2^{n/2}} \sum_{k=0}^{2^n-1} \left|x\right\rangle \right) = \frac{1}{2^{n/2}} = \cos \theta

where θ\theta is the angle between a\left\vert a\right\rangle and ϕ\left\vert \phi\right\rangle.

The complementary angle, ρ=90θ\rho = 90-\theta is the angle between ϕ\left\vert\phi\right\rangle and a\left\vert a_\perp\right\rangle. Using a well-known trigonometric identity, we can compute for the angle of ρ\rho:

cosθ=sinρ=12n/2\cos \theta = \sin \rho = \displaystyle \frac{1}{2^{n/2}}

Since 12n/2\displaystyle \frac{1}{2^{n/2}} is very small if n is large,

sinρ=12n/2ρ\sin \rho = \displaystyle \frac{1}{2^{n/2}} \approx \rho

Continuing,

WVϕ=W(I2aa)ϕ=W(ϕ2cosρa)=(2ϕϕI)(ϕ2cosρa)=2ϕϕ2cosρϕϕa+2cosρa=ϕ4cos2ρϕ+2cosρa=(14cos2ρ)ϕ+2cosρa\begin{array}{rl} \mathbf{WV}\left|\phi\right\rangle &= \mathbf{W} \left( \mathbf{I} - 2\left|a\right\rangle \langle a |\right) \left|\phi\right\rangle\\ &= \mathbf{W} \left(\left|\phi\right\rangle - 2 \cos\rho \left|a\right\rangle \right)\\ &= (2\left|\phi\right\rangle \langle \phi| - \mathbf{I}) \left(\left|\phi\right\rangle - 2 \cos\rho \left|a\right\rangle \right)\\ &= 2\left|\phi\right\rangle - \left|\phi\right\rangle - 2\cos\rho \left|\phi\right\rangle \langle \phi |a\rangle + 2 \cos\rho \left|a\right\rangle\\ &= \left|\phi\right\rangle - 4\cos^2\rho \left|\phi\right\rangle + 2 \cos\rho \left|a\right\rangle\\ &= (1- 4\cos^2\rho) \left|\phi\right\rangle + 2 \cos\rho \left|a\right\rangle \end{array}

The inner product of WVϕ\mathbf{WV}\left\vert\phi\right\rangle and ϕ\left\vert\phi\right\rangle is given by

ϕWVϕ=ϕ[(14cos2ρ)ϕ+2cosρa]=(14cos2ρ)+2cos2ρ=12cos2ρ=cos2ρ\begin{array}{rl} \langle\phi|\mathbf{WV}\left|\phi\right\rangle &= \langle\phi|\left[ (1- 4\cos^2\rho) \left|\phi\right\rangle + 2 \cos\rho \left|a\right\rangle\right]\\ &= (1- 4\cos^2\rho) + 2\cos^2\rho\\ &= 1- 2\cos^2\rho\\ &= \cos 2\rho \end{array}

Therefore, the angle between these two vectors is 2ρ=12n/22\rho = \displaystyle \frac{1}{2^{n/2}}. How many times do we have to apply the operator WV\mathbf{WV} to get to π/2\pi/2?

m2ρ=π2m22n/2=π2m=π2n/24\begin{array}{rl} m\cdot 2\rho &= \displaystyle \frac{\pi}{2}\\ \displaystyle m \frac{2}{2^{n/2}}&= \displaystyle \frac{\pi}{2}\\ m &=\displaystyle \frac{\pi\cdot 2^{n/2}}{4} \end{array}

Therefore, we need to apply the operator WV\mathbf{WV} O(N)O(\sqrt{N}) times to find our value (where N=2nN=2^n).

Image Credit: "Binoculars at Duomo roof /Prismaticos en el tejado del Duomo, Milan" by albertopveiga is marked with CC BY-NC-SA 2.0. To view the terms, visit https://creativecommons.org/licenses/by-nc-sa/2.0/?ref=openverse