From Period to Factors
Shor's algorithm has two parts: a quantum subroutine that finds the period of the function , and a classical post-processing step that converts that period into a factor of . This lesson is about the classical step, which relies on a beautiful theorem from number theory.
Why the period gives factors
Suppose (otherwise itself or is already a factor). The period is the smallest positive integer satisfying
If is even we can write
so divides the product of the two factors on the right. Unless (a failure case), at least one of those factors shares a non-trivial common divisor with . The Euclidean algorithm extracts it:
If and then is a proper factor of , and we are done. The other factor follows immediately as .
A worked example: factoring 21
Let and . The quantum period-finding circuit would measure the period (since ). Check: .
Post-processing:
- is even, so we proceed.
- . Since , the condition is satisfied.
- .
- The two factors are and .
Indeed .
Euclidean gcd in JavaScript
The Euclidean algorithm is iterative and takes only a few lines:
function gcd(x, y) {
while (y !== 0) {
const t = y;
y = x % y;
x = t;
}
return x;
}
Each step replaces with ; when reaches zero, holds the answer.
Try it
The quantum subroutine has already done the hard work and returned for , . Implement the classical post-processing and return the non-trivial factor.
Sign in on the full site to ask questions and join the discussion.