Algorithms
Quantum algorithms are not faster because they "try all answers" and then reveal the right one. They are faster when a circuit arranges amplitudes so that measurement samples information about hidden structure: a period, an eigenphase, a marked subspace, or a useful expectation value. This page gives the foundational circuit-model algorithms and uses Nielsen and Chuang's notation for oracles, quantum Fourier transforms, phase estimation, Shor's order-finding reduction, and Grover search.
This page synthesizes the wiki's earlier algorithm overview with Chapters 1, 5, and 6 of Nielsen and Chuang. The N&C emphasis is that QFT and search are the two canonical algorithmic engines: QFT exposes algebraic periodicity, while Grover's iterate performs an optimal two-dimensional amplitude rotation.
Definitions
A quantum algorithm is a uniform family of circuits followed by classical post-processing. Inputs may be classical bit strings, quantum states, or oracle access to a function. Costs may count gates, circuit depth, qubits, oracle calls, precision, or fault-tolerant logical resources. A speedup claim is meaningful only after the input model and output model are fixed.
A common bit oracle for is
If the second register is prepared as
then phase kickback gives
This phase-oracle form underlies Deutsch-Jozsa, Bernstein-Vazirani, Simon's algorithm, and Grover search.
The quantum Fourier transform over basis states is the unitary
N&C stress that this is a Fourier transform of amplitudes, not a direct replacement for the classical fast Fourier transform on explicitly stored data. The efficient circuit comes from writing in binary and using the product representation
up to the final reversal of qubit order.
Phase estimation estimates when
It uses a phase register, controlled powers , the inverse QFT, and computational-basis measurement. If the input is a superposition of eigenvectors, measurement samples one eigenphase with probability equal to the squared overlap with its eigenvector.
For a positive integer with , the order of modulo is the least such that
Shor's algorithm reduces factoring to finding this order.
Amplitude amplification starts with
and uses a phase oracle plus a reflection about to rotate amplitude toward the good subspace.
Key results
Deutsch-Jozsa gives an exact oracle separation for a promise problem. Given promised constant or balanced, the amplitude of after the final Hadamards is
It is for constant functions and for balanced functions. N&C introduce this early because it demonstrates phase kickback and interference before the more substantial algorithms.
Phase estimation is the core QFT subroutine. With phase qubits, Hadamards prepare . Controlled powers transform this into
The inverse QFT maps the phase pattern to a computational-basis estimate. If has an exact -bit binary expansion, the estimate is exact. Otherwise, extra phase bits raise the success probability of obtaining a close approximation.
Shor's factoring algorithm proceeds as follows.
- Choose a random with . If , a factor has already been found.
- Define the modular multiplication unitary
on the subspace , with a reversible convention on unused basis states. 3. Use phase estimation on . The eigenstates associated with the cyclic orbit of are
with eigenvalue . Preparing gives a uniform superposition of these eigenstates, so phase estimation samples an approximation to . 4. Use continued fractions to recover a candidate denominator from the measured rational approximation. 5. Check whether . If not, repeat or refine. 6. If is even and , compute
These are nontrivial factors. The success probability is constant after accounting for random , useful Fourier samples, and the coprimality of and , so repetition boosts success.
Grover search solves an unstructured marked-item problem with a quadratic query improvement. Let be the number of items and the number of marked items. Define
The initial uniform state is
Set . The Grover iterate
is the product of two reflections and rotates the state by in the plane spanned by and . After iterations, the success probability is
A near-optimal choice is
which gives oracle calls. N&C also emphasize two limits: quantum counting combines Grover with phase estimation to estimate , and Grover search is optimal in the black-box model, so unstructured search cannot be improved to logarithmic query complexity.
Visual
This diagram replaces the old taxonomy with circuit-level views of the core algorithms. The kickback circuits show the query register, ancilla preparation, oracle action, final Hadamards, and measurement contract; QFT and QPE expose the controlled-phase and controlled-power ladders used by Shor. Grover's loop shows the oracle and diffusion sub-operations explicitly, while HHL shows the phase-estimation, controlled-rotation, uncomputation, and postselection structure that makes its output a quantum state rather than a full classical vector.
| Algorithmic pattern | N&C representative | Quantum operation | Classical post-processing | Main caveat |
|---|---|---|---|---|
| Phase kickback | Deutsch-Jozsa, Bernstein-Vazirani | Convert function values into phases | Interpret final bit string | Promise or hidden linearity is special |
| Fourier sampling | Phase estimation, Shor | Inverse QFT turns phases into estimates | Continued fractions, gcd checks | Needs coherent controlled powers |
| Order finding | Shor | Modular exponentiation plus phase estimation | Recover , then factors | Fault-tolerant depth dominates practical factoring |
| Amplitude amplification | Grover | Product of two reflections | Verify sampled solution | Oracle construction can dominate |
| Quantum counting | Grover plus phase estimation | Estimate eigenphase of Grover iterate | Convert angle to | Still needs oracle access |
| State-output linear algebra | HHL-style later algorithms | Prepare amplitude-encoded answer state | Estimate observables | Data loading and readout can erase speedup |
Worked example 1: Shor post-processing for factoring 15
Problem. Factor using the order-finding branch of Shor's algorithm with . Show the order and the classical gcd step.
Method.
- Check that is usable:
So no factor is found immediately, and order finding is needed.
- Compute powers of modulo :
No earlier positive exponent gives , so the order is
- Check the factoring conditions. The order is even. Also
because .
- Compute the gcds:
- Connect this to the quantum measurement. If a phase register of size measured , the rational estimate is
Continued fractions recover denominator , matching the order. A measurement of would give , also revealing denominator .
Answer. The order of modulo is , and the classical post-processing returns factors and . The checked point is that the quantum subroutine is not "factoring directly"; it estimates a phase whose denominator is the period needed by the number-theoretic reduction.
Worked example 2: Grover iteration count and success check
Problem. A search space has items and marked item. Estimate the number of Grover iterations and check the success probability formula.
Method.
- Compute the initial good amplitude:
- Find the angle:
- Use the near-optimal iteration estimate:
So choose iterations.
- Check the success probability:
- Multiply the angle:
This is close to , so the sine squared is close to .
Answer. Use about Grover iterations. The state has rotated very near the marked subspace, so measurement returns the marked item with high probability. The check also shows why over-iteration is a mistake: after passing the marked subspace, further rotations decrease success probability.
Code
This snippet implements the classical post-processing part of Shor's algorithm for small examples. It assumes the quantum phase-estimation stage has supplied a candidate rational approximation.
from fractions import Fraction
from math import gcd
def recover_order_from_phase(measured, q, n):
"""Recover a candidate order r from measured/q using continued fractions."""
frac = Fraction(measured, q).limit_denominator(n)
return frac.denominator
def shor_postprocess(n, a, measured, q):
g = gcd(a, n)
if g > 1:
return g, n // g
r = recover_order_from_phase(measured, q, n)
if pow(a, r, n) != 1:
return None
if r % 2 != 0:
return None
half = pow(a, r // 2, n)
if half == n - 1:
return None
p = gcd(half - 1, n)
q_factor = gcd(half + 1, n)
if 1 < p < n and 1 < q_factor < n:
return tuple(sorted((p, q_factor)))
return None
print(shor_postprocess(n=15, a=2, measured=64, q=256))
print(shor_postprocess(n=15, a=2, measured=192, q=256))
Common pitfalls
- Saying "quantum parallelism" without explaining interference. Superposition alone does not make all branch results readable.
- Treating QFT as a faster classical FFT. It transforms amplitudes of a quantum state; extracting all transformed amplitudes would require many measurements.
- Hiding modular exponentiation in Shor's algorithm. It is the large reversible arithmetic block and dominates resource estimates.
- Forgetting continued fractions and gcd checks. The quantum measurement gives a rational approximation, not factors by itself.
- Assuming every choice of works in Shor. Random choices can fail if the order is odd or .
- Over-iterating Grover search. The good amplitude rotates up and then back down.
- Ignoring oracle construction. Query complexity can hide a real circuit whose cost is comparable to the classical verifier.
- Concluding that Grover solves NP-complete problems efficiently. It gives a quadratic speedup for unstructured search, and N&C discuss optimality bounds against better black-box scaling.
- Claiming NISQ devices can run useful Shor factoring at cryptographic sizes. Shor requires deep, fault-tolerant modular arithmetic.
- Forgetting input and output models in later algorithms such as HHL. Preparing and reading useful observables are part of the algorithm.
Connections
- Quantum hardware determines whether controlled powers, modular arithmetic, or Grover oracles can be run before noise dominates.
- Quantum error correction is required for deep algorithms such as Shor and high-precision phase estimation.
- Quantum machine learning borrows amplitude estimation, phase estimation, kernels, and variational circuits, but often changes the input-output model.
- Cryptography connects directly to factoring, discrete logarithms, and post-quantum security.
- Linear algebra supplies eigenvectors, unitaries, tensor products, projections, and Fourier transforms.
- Quantum mechanics supplies measurement, unitary evolution, phase, and interference.
- Quantum communication uses many of the same primitive circuits, including teleportation and phase-sensitive measurements.
Further reading
- Michael A. Nielsen and Isaac L. Chuang, Quantum Computation and Quantum Information, Chapters 1, 5, and 6.
- Peter Shor, polynomial-time algorithms for prime factorization and discrete logarithms.
- Lov Grover, a fast quantum mechanical algorithm for database search.
- Daniel Simon, on the power of quantum computation.
- Ethan Bernstein and Umesh Vazirani, quantum complexity theory.
- Aram Harrow, Avinatan Hassidim, and Seth Lloyd, quantum algorithm for linear systems.