Skip to content
Algorithms

Quantum Algorithms: Grover, Shor & QFT Explained

A quantum algorithm is a finite sequence of unitary gates and measurements that uses superposition and interference to make a useful outcome likely, often followed by a short classical calculation.

This page is the map of the algorithm section. Five ideas carry a first course: Grover search, the amplitude amplification that generalizes it, the oracle those routines call, the quantum Fourier transform, and Shor’s factoring method. Each block below answers the same three questions. Which problem is the idea for? Which cost falls, and from what figure to what figure? What single picture makes that drop feel inevitable? Gate-level circuits stay on the linked lesson. The paragraphs here are the lines worth quoting, together with the hypotheses those lines depend on.

The speedups are specific. Grover’s quadratic improvement applies to search that has no usable structure. Shor’s polynomial runtime applies to factoring, through the period-finding problem factoring reduces to. A classical method that already uses sorting, a number-theoretic sieve, or dynamic programming is a different baseline, and the section states that baseline when it is a raw scan and when it is something tighter.

Figures on this page are standard asymptotic counts. Constant factors, the gates inside an oracle, and error-correction overhead sit outside the big-O symbol and are named in the section that uses it. When those constants push the crossover past every input that fits on Earth, the method is a galactic algorithm.

Grover’s algorithm

Grover’s algorithm is a quantum search procedure that finds a marked item in an unstructured collection by calling a yes-or-no checker about square-root as many times as a classical scan.

The problem is unstructured search. You are given NN candidate inputs and a function that returns 1 on the marked items and 0 on everything else. The set is not sorted, hashed, or generated by a formula you are allowed to exploit, so the legal move is to evaluate the checker. With one marked item, a classical scan uses on the order of NN evaluations in the worst case and about N/2N/2 evaluations on average. Grover’s algorithm uses on the order of N\sqrt{N} evaluations. For a million candidates and one marked item, that is a drop from about 500,000500{,}000 average checks to about (π/4)×1,000≈785(\pi/4)\times 1{,}000 \approx 785 checks. When MM items are marked, the classical count is on the order of N/MN/M and the quantum count is on the order of N/M\sqrt{N/M}. The improvement is quadratic, and in the black-box query model that gap is optimal.

In one sentence, a phase flip on the marked states followed by a reflection about the average amplitude is a small rotation, and about (π/4)N/M(\pi/4)\sqrt{N/M} of those rotations aligns the state with a marked item.

The diffuser and a two-qubit circuit are on the Grover search page. Extra iterations rotate past the target, and the success probability falls.

Amplitude amplification

Amplitude amplification is the quantum technique that raises the probability of a chosen good subspace by repeated reflections. Grover search is the special case whose starting state is the uniform superposition.

The problem is amplified sampling. A unitary UU prepares a state that already has success probability pp of lying in the good subspace, and a checker recognizes that subspace. Classically you repeat the preparation until a trial lands in the good subspace, which takes on the order of 1/p1/p trials. Amplitude amplification calls UU, its inverse, and the checker on the order of 1/p1/\sqrt{p} times and lifts the success probability to a constant. When the start is uniform and one item out of NN is good, p=1/Np = 1/N, and the drop matches Grover: from on the order of NN trials down to on the order of N\sqrt{N} trials. If a heuristic quantum routine already succeeds with probability 1/10,0001/10{,}000, amplification cuts the expected repetitions from about 10,00010{,}000 to about 100100. The useful iteration count is about π/(4θ)\pi/(4\theta) where p=sin⁡2θp = \sin^2\theta.

In one sentence, two reflections compose into one rotation in the plane spanned by the good state and the state orthogonal to it, so each iteration walks toward the answer.

When pp is unknown, the basic count can overshoot. Estimating the angle, or switching to a fixed-point variant that keeps the probability climbing as iterations are added, is covered with the reflection formulas on the amplitude amplification page.

Quantum oracles

A quantum oracle is a reversible circuit that recognizes whether an input is a solution and marks that fact on the quantum state, usually by a phase flip.

The problem it solves is representation. Grover’s algorithm and amplitude amplification both assume a checker f(x)f(x) that equals 1 on solutions and 0 elsewhere, and a quantum circuit can apply only unitaries. The phase oracle is the unitary UfU_f defined by Uf∣x⟩=(−1)f(x)∣x⟩U_f\lvert x\rangle = (-1)^{f(x)}\lvert x\rangle: marked basis states receive a minus sign, and every other basis state stays unchanged. The bit oracle writes the bit into an ancilla, ∣x⟩∣y⟩↦∣x⟩∣y⊕f(x)⟩\lvert x\rangle\lvert y\rangle \mapsto \lvert x\rangle\lvert y \oplus f(x)\rangle. Prepare that ancilla in ∣−⟩\lvert -\rangle and phase kickback converts the bit oracle into the phase oracle. The complexity drop is in the number of calls, because one call still costs one evaluation of ff, the same order as one classical test. Unstructured search then falls from on the order of NN evaluations to on the order of N\sqrt{N} evaluations, once each evaluation is an efficient reversible circuit and every scratch qubit is returned to ∣0⟩\lvert 0\rangle. Scratch qubits left entangled with the input erase the interference, so the measured distribution never shows the saving.

In one sentence, the oracle writes a minus sign on solutions, and later interference turns that sign into a large measurement probability.

A controlled-Z that marks ∣11⟩\lvert 11\rangle, and a small Boolean checker built the same way, are on What is an oracle?.

The quantum Fourier transform

The quantum Fourier transform (QFT) is the unitary change of basis that rewrites a computational-basis integer as a superposition of phase-weighted basis states, the same change of basis as the discrete Fourier transform.

The problem it solves is moving information from an index into a frequency so that phase estimation and period finding can read it. On nn qubits, with N=2nN = 2^n,

QFT ∣x⟩=1N∑k=0N−1e2πixk/N ∣k⟩. \mathrm{QFT}\,\lvert x\rangle = \frac{1}{\sqrt{N}}\sum_{k=0}^{N-1} e^{2\pi i x k / N}\,\lvert k\rangle.

A classical discrete Fourier transform writes down all NN complex outputs. The direct product costs on the order of N2N^2 arithmetic operations, and the fast Fourier transform brings that down to on the order of Nlog⁡NN \log N. The QFT circuit, a ladder of Hadamard gates and controlled phase rotations, uses on the order of n2n^2 elementary gates, which is (log⁡N)2(\log N)^2. On 20 qubits that is a few hundred gates, against about 20×22020 \times 2^{20} operations for an FFT of the same length. The circuit prepares an nn-qubit state, and a measurement draws one index from the frequency distribution. Phase estimation needs that single draw. A printed table of every coefficient is a separate task, still performed by the classical FFT at Θ(Nlog⁡N)\Theta(N \log N) cost.

In one sentence, Hadamards and controlled phases copy the input integer into relative phases, which is the shape a hidden period or an eigenphase already has.

A three-qubit gate list is on the quantum Fourier transform page. Shor’s algorithm applies the inverse, the same ladder with the rotation angles negated.

Shor’s algorithm

Shor’s algorithm is a hybrid method that factors an integer by finding the period of ax mod Na^x \bmod N on a quantum computer and converting that period into factors with classical gcd arithmetic.

The problem is integer factoring, reduced to order finding. Choose an integer aa coprime to NN and find the smallest positive integer rr such that ar≡1(modN)a^r \equiv 1 \pmod{N}. When rr is even and ar/2≢−1(modN)a^{r/2} \not\equiv -1 \pmod{N}, the two gcd values gcd⁡(ar/2±1,N)\gcd(a^{r/2}\pm 1, N) are nontrivial factors. Classical trial division up to N\sqrt{N} takes time exponential in the bit length n=log⁡2Nn = \log_2 N: doubling the number of bits squares the number of candidates you must try. The number field sieve, the general classical method used for large integers, improves that to a subexponential cost that is still superpolynomial in nn. Shor’s period finding is polynomial in nn. Modular exponentiation dominates the gate count and is commonly placed on the order of n3n^3 gates with schoolbook reversible arithmetic; the inverse QFT after it is the smaller n2n^2 term. Continued fractions recover a candidate period from the measured phase, and a few gcd checks either return the factors or ask for a new base aa.

In one sentence, the powers ax mod Na^x \bmod N hide their period in a phase, and the Fourier transform turns that phase into an integer a measurement can return.

The classical–quantum–classical workflow, and the scale of fault-tolerant hardware cryptographic sizes require, are on Shor’s algorithm.

How the five ideas connect

Search by amplification and period finding by the Fourier transform are two families that share interference and a reversible encoding of the answer.

Grover, the oracle, and amplitude amplification proceed through the whole search family. The oracle marks a subspace, two reflections rotate toward it, and Grover is that rotation started from the uniform superposition. Substitute another preparation UU and the same geometry applies at a different angle θ\theta. Shor’s family runs along the other path. Reversible modular exponentiation writes a periodic function into a register, phase estimation stores the period as an eigenphase on a control register, and the inverse QFT converts that phase into a basis state. Classical gcd arithmetic then finishes the factoring. Both families hide the answer in a phase and let interference raise its measurement probability. The phase pattern differs: a single minus sign on each marked basis state in the search family, and a progression of roots of unity in the period-finding family. The shared prerequisite for seeing where either phase comes from is phase kickback. The circuits after that point stay distinct, so the Grover lesson and the Shor lesson each still need their own pass.

A useful test of the split is which resource you would add if the algorithm slowed down. A slower Grover run wants a cheaper checker or fewer marked-item guesses. A slower Shor run wants a cheaper modular multiplication; shrinking the QFT changes the total far less, because the transform is the smaller term.

Query cost and gate cost

Query cost counts calls to a checker or a state preparation. Gate cost counts every elementary operation, including the work hidden inside each call.

The Grover and amplitude-amplification drops above are query drops: N\sqrt{N} checker calls, or 1/p1/\sqrt{p} uses of a preparation. They become statements about runtime once each call is an efficient circuit whose scratch space returns to zero. An oracle that itself scans a list of size NN would spend linear work per call and would hand the quadratic saving back. The QFT drop is a gate count from the start, from Θ(Nlog⁡N)\Theta(N \log N) operations that emit every Fourier coefficient to Θ(n2)\Theta(n^2) gates that prepare an nn-qubit state. Shor’s polynomial bound is a gate count too, and modular multiplication supplies most of those gates. Error correction multiplies logical gate counts by a physical overhead that depends on the code and the physical error rate. The same quadratic query improvement can be genuine in a fault-tolerant cost model and still sit beyond present noisy devices, because the depth exceeds the coherence time. When you compare an algorithm paper with a hardware estimate, take the query figure and the gate figure from the same ledger, and say which one you are quoting.

Complexity at a glance

Unstructured search drops quadratically, from on the order of NN checks to on the order of N\sqrt{N} checks, and factoring drops from a superpolynomial classical cost to a cost polynomial in the number of bits.

IdeaProblemClassical costQuantum cost
GroverOne marked item among NNΘ(N)\Theta(N) checksΘ(N)\Theta(\sqrt{N}) checks
Amplitude amplificationSuccess probability ppΘ(1/p)\Theta(1/p) trialsΘ(1/p)\Theta(1/\sqrt{p}) trials
OracleRecognize a solutionOne evaluation per callOne evaluation per call; search then uses Θ(N)\Theta(\sqrt{N}) calls
QFTFourier change of basis, N=2nN = 2^nΘ(Nlog⁡N)\Theta(N \log N) to emit all coefficientsΘ(n2)\Theta(n^2) gates to prepare the state
ShorFactor an nn-bit integerSuperpolynomial (number field sieve); trial division is exponential in nnPolynomial in nn

Read each row beside its section. The oracle row is a unit price. The saving appears after Grover or amplitude amplification decides how many times to pay it. The QFT row compares two outputs: a full table of complex numbers, and an nn-qubit state you measure once. Shor’s row is the scaling of the whole factoring pipeline with the bit length. Taking trial division as the classical column makes the gap look like exponential versus polynomial; taking the number field sieve makes it superpolynomial versus polynomial. Both comparisons are used in the literature, and they answer different questions. Trial division is the cost of the obvious method. The sieve is the cost of the best general method now in use.

Where to read next

Read Grover first, then the oracle and amplitude amplification, then the quantum Fourier transform, and Shor last.

Grover is the shortest complete argument: one checker, one diffuser, a stated iteration count, and a quadratic gap visible on four basis states. The oracle lesson shows the checker as a circuit you compile, including the uncomputation that clears scratch qubits. Amplitude amplification separates the uniform start, which belongs to Grover, from the pair of reflections, which is the general engine. The QFT lesson turns Shor’s last register into a concrete gate list. Shor’s lesson then stacks phase estimation, the inverse transform, and the classical conditions on the period. Stay on this page when you need a definition and the complexity change. Open the linked lesson when you need to edit a circuit or check an iteration count.

  1. Grover search
  2. What is an oracle?
  3. Amplitude amplification
  4. Quantum Fourier Transform (QFT)
  5. Shor’s algorithm