Period finding: from a QFT peak to the period

After the QFT, a measurement lands close to a multiple of N/r. Continued fractions turn that reading into the period r. This is the classical post-processing in Shor’s algorithm, shown with exact numbers.

10 min read · reviewed December 2026

The setting

Shor’s algorithm for factoring reduces the problem to finding the period r of the function f(x) = a^x mod M, for some number a with no factor in common with M. A register of qubits holds a superposition of the inputs x, and a second register holds f(x). Measuring the second register leaves the first in an equal superposition of the x values that give the same f(x). Since f repeats every r steps, those x values are a, a + r, a + 2r, and so on, a periodic state with an unknown offset.

The lab’s Period finding tab starts at exactly this state. You choose the register size, the period r and the offset, and the page computes the exact output distribution of the QFT.

When r divides N

Take N = 2^t. If r divides N, as it does for r = 2, 4 or 8, the output has exactly r peaks at the multiples k·N/r, each with chance 1/r. All other outputs have chance exactly zero. A measurement gives m = k·N/r for a random k from 0 to r − 1, so m/N = k/r. Reduce the fraction k/r to lowest terms. If k and r share no factor, the reduced denominator is r, and the period is found.

The probability that k is coprime to r is φ(r)/r, where φ is Euler’s totient, the count of numbers below r that share no factor with it. For r = 8 that is 4/8 = 50 percent. The table lists the exact chances the lab computes for several values of r on a 6-qubit register, next to φ(r)/r for the cases where r divides 64.

Register of 6 qubits (N = 64), denominator bound 30. Chance that one measurement plus continued fractions returns the full period r. Exact sum over the output distribution.
Period rDivides 64?Chance of the full periodφ(r)/r, this page’s own arithmetic
2yes50.0%50.0%
4yes50.0%50.0%
8yes50.0%50.0%
3no56.1%not exact
5no58.3%not exact
6no0.0%not exact
10no0.0%not exact

When r does not divide N

In real factoring runs r is not a power of two, so N/r is not a whole number and the peaks are blurred. The QFT output puts most of its probability on the integers closest to k·N/r, but spread across two neighbours. The reading m satisfies |m/N − k/r| ≤ 1/(2N) with a good probability, which the lab reports as the mass on the nearest integer to each multiple.

Shor’s analysis chooses N between M² and 2M², where M is the number being factored. Then 1/(2N) is less than 1/(2r²), because r is smaller than M. That inequality is the condition under which the fraction k/r is guaranteed to appear among the continued-fraction approximations of m/N, by a classical theorem of Legendre.

Continued fractions in practice

A continued fraction writes a number as an integer plus 1 over an integer plus 1 over …, and the successive truncations, called convergents, are the best rational approximations with growing denominators. The lab computes them with the Euclidean algorithm. Take N = 256 and the reading m = 171. The ratio 171/256 is about 0.668, and its convergents are 0/1, 1/1, 2/3 and 171/256. With a denominator bound of 15, the last convergent allowed is 2/3, so the algorithm proposes r = 3. That is what a period-3 state on 8 qubits produces: the peak for k = 2 sits at 2·256/3 = 170.67, and 171 is the nearest integer.

The bound matters. Without a limit on the denominator you would always accept m/N itself, whose denominator is a power of two. The bound, chosen from what you know about r, picks the convergent that is plausible. In the lab, use the Largest allowed denominator slider to see that a bound lower than r makes recovery fail.

When a run gives only part of the answer

In practice, one run is often not enough. Collecting denominators from several runs and taking their least common multiple recovers r from partial readings, because each denominator divides r and different runs tend to miss different factors. The probability of a full-period reading per run is the number the lab prints, which is exactly the sum of the outputs that decode to r.

  • If k and r share a factor, the reduced fraction has a smaller denominator that divides r. The lab labels these readings a divisor. They are not wrong, only incomplete.
  • If m = 0, nothing is learned. The lab labels it nothing.
  • A reading can also land on a neighbour that gives the wrong fraction. The lab labels it wrong, and in Shor’s algorithm the candidate period is checked by computing a^r mod M.

The quantum step finds a number close to k/r times N. The rest is ordinary arithmetic on a classical computer.

Where this sits in Shor’s algorithm

After the period r is found, factors of M come from greatest common divisors of a^(r/2) ± 1 with M, provided r is even and a^(r/2) is not −1 mod M. The Shor’s Algorithm Lab on this site walks through that chain on small numbers. This guide has covered only the Fourier part.

Try it

Questions

Why continued fractions and not just rounding?

Rounding m/N gives a fraction with denominator N. Continued fractions find the simplest nearby fraction, which has the small denominator r we want.

What is the chance of success per run?

When r divides N it is φ(r)/r. In other cases the lab sums the exact probabilities of every outcome that decodes to r.

Does the offset affect the answer?

It changes the phases of the peaks but not their positions, so the period can be read without knowing the offset.

Read next

Sources and further reading

Check the original sources for exact citations.