Next Article in Journal
A Note on the Appearance of the Simplest Antilinear ODE in Several Physical Contexts
Next Article in Special Issue
On One Problem of the Nonlinear Convex Optimization
Previous Article in Journal
Solution of Inhomogeneous Differential Equations with Polynomial Coefficients in Terms of the Green’s Function, in Nonstandard Analysis
Previous Article in Special Issue
Structure of Triangular Numbers Modulo m
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

Continued Fractions and Probability Estimations in Shor’s Algorithm: A Detailed and Self-Contained Treatise

Institute of Architecture of Application Systems, University of Stuttgart, Universitätsstr. 38, 70569 Stuttgart, Germany
*
Author to whom correspondence should be addressed.
AppliedMath 2022, 2(3), 393-432; https://doi.org/10.3390/appliedmath2030023
Submission received: 21 May 2022 / Revised: 1 July 2022 / Accepted: 12 July 2022 / Published: 18 July 2022
(This article belongs to the Special Issue Applications of Number Theory to the Sciences and Mathematics)

Abstract

:
Shor’s algorithm for prime factorization is a hybrid algorithm consisting of a quantum part and a classical part. The main focus of the classical part is a continued fraction analysis. The presentation of this is often short, pointing to text books on number theory. In this contribution, we present the relevant results and proofs from the theory of continued fractions in detail (even in more detail than in text books), filling the gap to allow a complete comprehension of Shor’s algorithm. Similarly, we provide a detailed computation of the estimation of the probability that convergents will provide the period required for determining a prime factor.

1. Introduction

Shor’s algorithm [1] for prime factorization is generally considered as a major milestone and a breakthrough in quantum computing: it solves a practically very relevant problem (which is, e.g., an underpinning of cryptography) with an exponential speedup compared to classical methods.
The algorithm is based on the fact that determining a divisor and finally a prime factor of a natural number n can be reduced to finding the period p of the modular exponentiation function f x = a x m o d n for an a with 0 < a < n (see Section 3.2.1).
The overall algorithm is hybrid, consisting of classical computations and a quantum computation. The classical computations are computing greatest common divisors with the Euclidian algorithm, and perform a continuous fraction analysis. A detailed discussion of the latter is one of the two foci of this contribution (see Section 2).
The quantum part mainly consists of: (i) creating an entangled state based on an oracle computing the modular exponentiation function f above, (ii) performing a quantum Fourier transform (QFT) on this state, and (iii) measuring it. The oracle produces the following state:
a b = 1 N x = 0 N 1 x f x
After applying the quantum Fourier transform and a measurement, the first part (i.e., the |a⟩-part) of the quantum register is in state
1 N A j = 0 A 1 ω N j p y | y
In this state, the searched period p already appears in its amplitude. The measured value y can then be used with high probability (see Section 3.4, Theorem 16) to compute the period p of the modular exponentiation function f by analyzing the convergents of a continued fraction (see Section 3.4.1) and finally, based on the period, a prime factor (see Section 3.2.1). A detailed discussion on how this is achieved is the second focus of this contribution (see Section 3).

Structure of the Article

The article is structured as follows: in Section 2 we cover all details about continued fractions that are required to comprehend the corresponding aspect of Shor’s algorithm.
Section 2.1 defines the notion of a continued fraction, gives examples of how to compute the continued fraction representation of a rational number, and demonstrates how to compute the number that a continued fraction (and thus convergents) represents.
Convergents as the fundamental tool in the theory of continued fractions are detailed in Section 2.2: after defining the term, basic theorems about convergents such as the recursion theorem, two sign theorems, monotony properties, convergent comparison, nesting of a number by its convergents, and several distance estimations are proven.
Next, the brief Section 2.3 presents infinite regular continued fractions to represent non-rational numbers. A corresponding algorithm is provided to compute such continued fractions.
Section 2.4 gives several upper bounds and lower bounds for the difference between a number and its convergents. Exploiting one of these bounds, the convergence of the convergents of an infinite regular continued fraction of a number to this number is proven. Semiconvergents are defined and corresponding monotony properties are given.
Best approximations of a real number are introduced in Section 2.5. It is proven that best approximations of the second kind are convergents and vice versa (Lagrange’s theorem). Best approximations of the first kind are proven to be convergents or semiconvergents (another theorem by Lagrange). Finally, Legendre’s theorem is presented, which is the main result about continued fractions required by Shor’s algorithm: it allows the implication that a given fraction is a convergent of another number.
Section 3 is devoted to estimating the probability that convergents can be used to compute periods, i.e., that Legendre’s theorem can be applied.
At the beginning of Section 3, Section 3.1 proves a lower bound and an upper bound for the secant lengths of the unit circle. This estimation is central for estimating the aforementioned probability.
Section 3.2 contains many different estimations of parameters that appear in the measurement result of Shor’s algorithm. In Section 3.2.1, we recall the very basics of modular arithmetic, relate this to group theory, and use Lagrange’s theorem from group theory to prove that the period of the modular exponentiation function in Shor’s algorithm is less than the number to be factorized (Lemma 8). Intervals of consecutive multiples of the period are studied in Section 3.2.2: it is shown that multiples of N are sparsely scattered across these intervals (Note 12). This implies that measurement results are somehow centered around multiples of N / p (Corollary 9). The cardinality of arguments in the superposition that build the pre-image of a certain f x is estimated in Section 3.2.3. Section 3.2.4 proves bounds of phases of amplitudes relevant for computing the probability of measurement results as a geometric sum.
Finally, Section 3.3 computes this probability: it is proven that a measurement result is close to a multiple of N / p with probability of approximately 4 / π 2 (Lemma 10).
Section 3.4 shows that this measurement result fulfills the assumption of Legendre’s theorem (Theorem 15). Thus, by computing convergents, the period can be determined (Theorem 16 and Section 3.4.1).
Section 3.5 sketches how the main results contribute to the proof of Shor’s algorithm. Its purpose is to avoid getting lost in the huge amount of low-level details.
A brief conclusion and discussion of related work ends this contribution with Section 4.

2. Continued Fractions

2.1. Definition of Continued Fractions and Their Computation

We define the notion of continued fractions and give an example of how to compute them.
Definition 1.
An expression of the form
a 0 + b 1 a 1 + b 2 a 2 + b 3
with a i , b i is called an infinite continued fraction.
If, in this expression, it is b i = 1 for all i, a 0 , and a i for i≥1, the expression is called a regular continued fraction.
A finite regular continued fraction (simply called a continued fraction) satisfies, in addition, the condition N k : a N + k = 0 (convention: “1/0 = 0”).
A continued fraction is, thus, the following expression:
a 0 ; a 1 , , a N = d e f a 0 + 1 a 1 + 1 + 1 a N 1 + 1 a N
A continued fraction of a rational number a/b is computed as follows: the integer part a / b becomes a 0 , leaving the non-negative rational remainder x 1 / y 1 . The latter is now written as 1 / y 1 / x 1 , resulting in
a 0 + 1 y 1 x 1
Next, the integer part y 1 / x 1 becomes a 1 , leaving a rational remainder that is treated as before. This processing stops until the rational remainder is zero. Figure 1 gives an example of the processing.
Beside this straightforward proceeding to compute continued fractions, the well-known Euclidian algorithm can be used for this purpose too. Figure 2 gives a corresponding example; it should be self-descriptive.
Formally, a continued fraction can always be reduced such that its last element is greater than or equal to 2.
Note 1.
Let a 0 ; a 1 , , a N be a continued fraction. Then:
a 0 ; a 1 , , a N = a 0 ; a 1 , , a N 1 + 1 a N
Especially, it can always be achieved that a continued fraction a 0 ; a 1 , , a N satisfies a N 2 .
Proof. 
The following simple computation proves the first claim:
a 0 ; a 1 , , a N = a 0 + 1 a 1 + 1 + 1 a N 1 + 1 a N = a 0 + 1 a 1 + 1 + 1 a N 1 + 1 a N = a 0 ; a 1 , , a N 1 + 1 a N
Furthermore, if a N = 1 in a 0 ; a 1 , , a N then a N 1 + 1 / a N ≥ 2. This is because, by definition, a k 1 for 1 ≤ k ≤ N. □
Equation (5) implies a straightforward way to compute the value represented by a continued fraction a 0 ; a 1 , , a N : see Figure 3.

2.2. Convergents

Next, we define the “workhorses” of the theory of continued fractions.
Definition 2.
a 0 ; a 1 , , a m is called m-th convergent of the continued fraction a 0 ; a 1 , , a N for 0 ≤ m ≤ N, or m-th convergent of the infinite regular continued fraction a 0 ; a 1 , .
Convergents can be computed recursively based on the following theorem:
Theorem 1.
(Recursion Theorem)
Define:
  • p 0 = a 0 ;
  • p 1 = a 1 a 0 + 1 ;
  • p n = a n p n 1 + p n 2 for n ≥ 2;
and define:
  • q 0 = 1 ;
  • q 1 = a 1 ;
  • q n = a n q n 1 + q n 2 for n ≥ 2.
Then, for every convergent a 0 ; a 1 , , a n , it is:
a 0 ; a 1 , , a n = p n q n
Proof (by induction).
Let n = 0, 1: Then, a 0 = a 0 = p 0 q 0 and a 0 ; a 1 = a 0 + 1 a 1 = a 0 a 1 + 1 a 1 = p 1 q 1 .
Induction hypothesis: a 0 ; a 1 , , a n = p n q n = a n p n 1 + p n 2 a n q n 1 + q n 2 .
Induction step n → n + 1: According to Note 1, it is
a 0 ; a 1 , , a n , a n + 1 = a 0 ; a 1 , , a n + 1 a n + 1
and the last continued fraction has n elements, i.e., the induction hypothesis applies:
a 0 ; a 1 , , a n + 1 a n + 1 ] = a n + 1 a n + 1 p n 1 + p n 2 a n + 1 a n + 1 q n 1 + q n 2 = a n a n + 1 + 1 a n + 1 p n 1 + p n 2 a n a n + 1 + 1 a n + 1 q n 1 + q n 2 = a n a n + 1 + 1 p n 1 + a n + 1 p n 2 a n a n + 1 + 1 q n 1 + a n + 1 q n 2 = a n + 1 a n p n 1 + p n 2 + p n 1 a n + 1 a n q n 1 + q n 2 + q n 1 = A a n + 1 p n + p n 1 a n + 1 q n + q n 1 = B p n + 1 q n + 1
Here, (A) is valid because of the induction hypothesis, and (B) is the definition of p n + 1 and q n + 1 . □
The recursion theorem implies the often used.
Corollary 1.
Numerators and denominators of convergents of a continued fraction a 0 ; a 1 , , a N with a 0 0 are strictly monotonically increasing:
p n > p n 1   and   q n > q n 1   for   all   n .
Proof (by induction).
Let n = 1: By definition, p 0 = a 0 , p 1 = a 1 a 0 + 1 . Because a i 1 for i ≥ 1, and a 0 0 , it is p 1 > p 0 0 . Similarly, q 1 > q 0 > 0
Now, p n = a n p n 1 + p n 2 and q n = a n q n 1 + q n 2 for n ≥ 2. With a n 1 by definition, and p n 1 > p n 2 (≥1) as well as q n 1 > q n 2 (≥1) by induction hypothesis, the claim follows. □
The next theorem is about the sign of a combination of the numerators and denominators of consecutive convergents of a continued fraction.
Theorem 2.
(Sign Theorem)
For  a 0 ; a 1 , , a n = p n q n , the following holds:
p n q n 1 p n 1 q n = 1 n 1
Proof (by induction).
For n = 1, it is
p 1 q 0 p 0 q 1 = a 1 a 0 + 1 1 a 0 a 1 = 1 = ( 1 ) 0
Induction step n → n + 1:
p n + 1 q n p n q n + 1 = a n + 1 p n + p n 1 q n p n a n + 1 q n + q n 1 = a n + 1 p n q n + p n 1 q n p n a n + 1 q n p n q n 1 = p n 1 q n p n q n 1 = p n q n 1 p n 1 q n = A 1 n 1 = 1 n
(A) uses the induction hypothesis. □
In case the numerators and denominators stem from the n-th convergent and the (n − 2)-nd convergent, the last n-th element of the convergent becomes part of the equation.
Theorem 3.
(Second Sign Theorem)
For  a 0 ; a 1 , , a n = p n q n , the following holds:
p n q n 2 p n 2 q n = 1 n a n
Proof. 
It is p n = a n p n 1 + p n 2 and q n = a n q n 1 + q n 2 .
Multiplying the first equation by q n 2 and the second equation by p n 2 results in q n 2 p n = q n 2 a n p n 1 + q n 2 p n 2 and p n 2 q n = p n 2 a n q n 1 + p n 2 q n 2 . Next, both equations are subtracted:
p n q n 2 p n 2 q n = q n 2 a n p n 1 + q n 2 p n 2 p n 2 a n q n 1 p n 2 q n 2 = q n 2 a n p n 1 p n 2 a n q n 1 = a n p n 1 q n 2 p n 2 q n 1 = A 1 n a n
where (A) is implied by the sign theorem (Theorem 2) and considering ( 1 ) n 2 = ( 1 ) n . □
The sign theorem immediately yields the important.
Corollary 2.
Numerator and denominator of a convergent are co-prime.
Proof. 
Let t be a divisor of p n and q n , i.e., t | p n and t | q n . Then, t | p n q n 1 p n 1 q n , but p n q n 1 p n 1 q n = 1 n 1 according to the sign theorem. Thus, t = ±1. □
Convergents can be represented as a sum of fractions with alternating sign and whose denominators consist of products of two consecutive denominators from the recursion theorem.
Theorem 4.
(Representation as a Sum)
Each convergent can be represented as a sum:
a 0 ; a 1 , , a n = a 0 + 1 q 1 q 0 1 q 2 q 1 + + 1 n 1 1 q n q n 1
Proof. 
Let a 0 ; a 1 , , a n = p n q n . Since p i q i + p i q i = 0 , we can write
a 0 ; a 1 , , a n = p n q n p n 1 q n 1 + p n 1 q n 1 p n 2 q n 2 + p n 2 q n 2 + p 1 q 1 p 0 q 0 + p 0 q 0
Computing the differences results in
a 0 ; a 1 , , a n = p n q n 1 q n p n 1 q n q n 1 + p n 1 q n 2 q n 1 p n 2 q n 1 q n 2 + + p 1 q 0 q 1 p 0 q 1 q 0 + p 0 q 0 = A 1 n 1 q n q n 1 + 1 n 2 q n 1 q n 2 + + 1 0 q 1 q 0 + a 0
where the sign theorem is applied in (A) and the last term a 0 = p 0 / q 0 is the recursion theorem. □
The next theorem is key for many estimations in the domain of continued fractions.
Theorem 5.
(Monotony Theorem)
Let  x n = d e f p n q n = a 0 ; a 1 , , a n  denote the n-th convergent. Then:
x 2 n < x 2 n + 2
and
x 2 n + 1 > x 2 n + 3
I.e., even convergents are strictly monotonically increasing, and odd convergents are strictly monotonically decreasing.
Proof. 
We compute the following difference, where (A) again uses p i q i + p i q i = 0 :
x n x n 2 = p n q n p n 2 q n 2 = A p n q n p n 1 q n 1 + p n 1 q n 1 p n 2 q n 2 = p n q n 1 q n p n 1 q n q n 1 + p n 1 q n 2 q n 1 p n 2 q n 1 q n 1 = B ( 1 ) n 1 q n q n 1 + ( 1 ) n 2 q n 1 q n 2 = ( 1 ) n 1 q n 2 + ( 1 ) n 2 q n q n q n 1 q n 2 = ( 1 ) n 2 q n ( 1 ) n 2 q n 2 q n q n 1 q n 2 = ( 1 ) n 2 q n q n 2 q n q n 1 q n 2 = ( 1 ) n q n q n 2 q n q n 1 q n 2 = C ( 1 ) n a n q n 1 q n q n 1 q n 2 = ( 1 ) n a n q n q n 2
(B) is because of the sign theorem, and (C) follows from q n = a n q n 1 + q n 2 , i.e., the recursion theorem.
Now, because of a n , q n , q n 2 > 0 , it is a n q n q n 2 > 0 . Thus, ( 1 ) n a n q n q n 2 > 0 for n even and ( 1 ) n a n q n q n 2 < 0 for n odd. This implies x n = ( 1 ) n a n q n q n 2 + x n 2 > x n 2 for n even as well as x n = ( 1 ) n a n q n q n 2 + x n 2 < x n 2 for n odd. □
While even convergents are increasing and odd convergence are decreasing, all even convergents are smaller than all odd convergents. This is the content of the next very important theorem.
Theorem 6.
(Convergents Comparison Theorem)
For  0 2 n , 2 m + 1 N , it is  x 2 n < x 2 m + 1
Proof. 
As before, using the sign theorem in (A), we obtain
x n x n 1 = p n q n p n 1 q n 1 = p n q n 1 q n p n 1 q n q n 1 = A ( 1 ) n 1 q n q n 1 = ( 1 ) n 1 β n
with β n : = q n q n 1 . Because q n , q n 1 > 0 , it is βn > 0, i.e., the sign of ( 1 ) n 1 β n is in fact ( 1 ) n 1 .
Thus, x 2 n + 1 x 2 n = ( 1 ) 2 n β 2 n + 1 > 0 , and we get x 2 n + 1 = ( 1 ) 2 n β 2 n + 1 + x 2 n > x 2 n . This shows that an even convergent x 2 n is strictly smaller than its immediate succeeding odd convergent x 2 n + 1 .
But what about an arbitrary odd convergent x 2 m + 1 ? For n < m, the monotony theorem (Theorem 6) yields x 2 n < x 2 m and we showed before that x 2 m < x 2 m + 1 ; thus, x 2 n < x 2 m + 1 .
For n > m, the monotony theorem yields x 2 m + 1 > x 2 n + 1 and with x 2 n + 1 > x 2 n we see x 2 n < x 2 m + 1 . □
The following often-used corollary computes the difference of two immediately succeeding convergents by mean of the denominators of the convergents, while the difference of the n-th convergent and the (n − 2)-nd convergent adds the n-th element of the n-th convergent as a factor.
Corollary 3.
p n q n p n 1 q n 1 = ( 1 ) n 1 q n q n 1
and
p n q n p n 2 q n 2 = 1 n a n q n q n 2
Proof. 
Equation (10) is the first equation from the proof of Theorem 6. The second equation follows because of
p n q n p n 2 q n 2 = p n q n 2 p n 2 q n q n q n 2 = A 1 n a n q n q n 2
where (A) is because of the second sign theorem (Theorem 3). □
We already saw that the even convergents are strictly monotonically increasing, that the odd convergents are strictly monotonically decreasing, and that each even convergent is less than all odd convergents. According to the next theorem, the value of a continued fraction lies between the even convergents and the odd convergents, i.e., this value is larger than all even convergents and smaller than all odd convergents. The situation is depicted in Figure 4.
Note that the notion of the value of a continued fraction is defined for finite continued fractions. In Section 4, this notion will also be defined for regular infinite continued fractions.
Theorem 7.
(Nesting Theorem)
Let x be the value of the continued fraction a 0 ; a 1 , , a N and let  x k  be its convergents. Then:
m , n < N : x 2 m < x < x 2 n + 1
Proof. 
The value of x is the convergent with the highest index N, i.e., x = x N = a 0 ; a 1 , , a N .
Let N = 2k be even. Since even convergents are strictly monotonically increasing, we know that 2 m < N : x 2 m < x 2 k = x N = x , and according to the convergent comparison theorem (Theorem 6), we know 2 n + 1 : x = x N = x 2 k < x 2 n + 1 .
Let N = 2k + 1 be odd. Since odd convergents are strictly monotonically decreasing, we know that 2 n + 1 < N : x 2 n + 1 > x 2 k + 1 = x N = x , and according to the convergent comparison theorem (Theorem 6), we know 2 m : x = x N = x 2 k + 1 > x 2 m . □
Because the value of a continued fraction is nested within its even convergents and odd convergents, the distance of this value from any of its convergents can be estimated by the distance of two consecutive convergents:
Theorem 8.
(Distance Theorem)
Let  x = a 0 ; a 1 , , a N  and let  x k  be its convergents. Then:
n : x x n < x n 1 x n
and
n : x x n < x n + 1 x n
Proof. 
Let n be even. Then, x n < x < x n 1 , i.e., x x n < x n 1 x n . Additionally, it is x x n > 0 and x n 1 x n > 0 . Thus, x x n < x n 1 x n for n even.
Now, let n be odd. It is x n 1 < x < x n , which implies x x n > x n 1 x n x n x > x n x n 1   x n x < x n x n 1 . Because of x n x > 0 and x n x n 1 > 0 , it is x n x < x n x n 1 x x n < x n 1 x n for n odd.
Together, this proves Equation (13). Equation (14) is proven similarly. □
Figure 5 shows the corresponding geometric situation for an even n.
Similarly, the difference between any two arbitrary convergents can be estimated by the difference of the convergent with the smaller index and its immediate predecessor:
Theorem 9.
(Difference Theorem)
Let  x = a 0 ; a 1 , , a N  and let  x k  be its convergents. Then:
m > n : x m x n < x n 1 x n
Proof. 
Let n be even, e.g., n = 2k.
Let m = 2t be even. By Theorem 6, even convergents are smaller than all odd convergents, i.e., x 2 t < x 2 k 1 for any t . Thus, x m x n = x 2 t x 2 k < < x 2 k 1 x 2 k = x n 1 x n .
Let m = 2t − 1 be odd. By the monotony theorem (Theorem 5), odd convergents are strictly monotonically decreasing, i.e., x 2 t 1 < x 2 k 1 for each t > k. Thus, x m x n = x 2 t 1 x 2 k < x 2 k 1 x 2 k = x n 1 x n .
For n odd, the proof is analogous. □
The geometry of the last theorem is depicted in Figure 6.
In several calculations, the size of the denominator of a convergent must be estimated:
Lemma 1.
(Size of Denominators)
For the denominator q n of a convergent p n q n = a 0 ; a 1 , , a n , the following holds:
n : q n n
and
n > 3 : q n > n
Proof. 
By definition, q 0 = 1 > 0 , and q 1 = a 1 1 because a i , and finally,
q 2 = A a 2 q 1 + q 0 = B a 2 q 1 + 1 C q 1 + 1 D 2
(A) holds because of the recursion theorem (Theorem 1), (B) is by definition of q 0 , (C) is because a 2 , and (D) has been seen just before (i.e., q 1 1 ). This proves the lemma for n 2 .
The proof for n ≥ 3 is by induction. It is
q n = A a n q n 1 + q n 2 B q n 1 + q n 2 C q n 1 + n 2 D q n 1 + 1 E n
where (A) is the recursion theorem, (B) is because of a n , (C) is by induction hypothesis applied to q n 2 , (D) is because n ≥ 3, and (E) is by induction hypothesis applied to q n 1 . This proves Equation (16).
Equation (17) is proven by induction again. Let n > 3. The argumentation is exactly as before, with the exception of (D):
q n = A a n q n 1 + q n 2 B q n 1 + q n 2 C q n 1 + n 2 > D q n 1 + 1 E n
(D) holds because n > 3, i.e., n 2 > 1 . □
In fact, denominators of a convergent grow much faster than the inequation q n > n may indicate:
Lemma 2.
(Geometric Growth of Denominators)
Let  q n ( n 2 ) be the denominator of the convergent  p n q n = a 0 ; a 1 , , a n . Then:
q n 2 n 1 2
Proof. 
It is q k = a k q k 1 + q k 2 > q k 1 + q k 2 > A 2 q k 2 , with (A) because, according to corollary 1, denominators are strictly monotonically increasing, i.e., q k 1 > q k 2 .
By induction, it is q 2 k 2 k q 0 , and then 2 k q 0 = A 2 k B 2 2 k 1 2 with (A) because q 0 = 1 , and (B) follows from
2 k = 2 2 k 2 1 2 2 2 k 2 = 2 2 k 2 1 2 = 2 2 k 1 2
Similarly, by induction, it is q 2 k + 1 2 k q 1 and then 2 k q 1 A 2 k = 2 2 k + 1 1 2 with (A) because of q 1 .
With n = 2 k and n = 2 k + 1 , respectively, Equation (18) is implied. □

2.3. Convergence of Infinite Regular Continuous Fractions

In Section 2.1, we presented an algorithm to compute the continued fraction representation of a rational number. Next, we show how to compute such a representation for a non-rational number (Algorithm 1).
Algorithm 1 Continued Fraction Representation of Non-Rational Number
1.
Let   α . Define:
  • α 0 : = α   and   b 0 : = α 0 ;
  • α i : = 1 α i 1 b i 1   and   b i : = α i for i ≥ 1.
2.
Then, b 0 ; b 1 , b 2 , is the continued fraction representation of   α .   Each   α i is called the i-th complete quotient of α.
The above algorithm does not terminate, i.e., the continued fraction representation of a non-rational number is infinite. This is the content of the following note:
Note 2.
 
In Algorithm 1, it is α i .
Proof (by induction).
 
n = 0: Then, by definition, α 0 = α .
Induction hypothesis: α n .
n → n + 1: Assume α n b n α n b n = k α n = k + b n , which is a contradiction to the hypothesis! Thus, α n b n α n + 1 : = 1 α n b n . □
Figure 7 gives the computation of the continued fraction representation of 2 :

2.4. Bounds Expressed by Denominators of Convergents

In the following, we give upper bounds and lower bounds of the approximations of a number by the convergents of its continued fraction representation by means of the denominators of the convergents.
First, we start with estimations of upper bounds:
Lemma 3.
(Upper Bounds)
Let p n / q n be a convergent of the continued fraction representation of x. Then:
x p n q n < 1 q n q n + 1 < 1 q n 2 1 n 2
Proof. 
With x n = p n / q n , it is x x n < x n + 1 x n (see Theorem 8, Equation (14)). According to Corollary 3 (Equation (10)), it is
x n + 1 x n = p n + 1 q n + 1 p n q n = ( 1 ) n q n q n + 1
Thus,
x x n < x n + 1 x n = ( 1 ) n q n q n + 1 = 1 q n q n + 1 < A 1 q n 2 B 1 n 2
where (A) holds because of q n + 1 > q n (Corollary 1), and (B) is true because of q n n (Lemma 1). □
An immediate consequence of this theorem is the convergence of the sequence of the convergents of a continued fraction to the value of the continued fraction. This, by the way, is the origin of the name “convergents”.
Corollary 4.
The series p n / q n of the convergents of the continued fraction representation of x converges to x:
l i m p n q n = x
Proof. 
The claim follows immediately from x p n q n < 1 n 2 . □
Often, two fractions are compared by means of their mediant (“mediant” means “somewhere in between”).
Definition 3.
For a / b , c / d and b, d > 0, the term a + c b + d is called the mediant of the two fractions.
The following simple inequation is often used.
Note 3.
(Mediant Property)
Let a / b , c / d and b, d > 0 and a b < c d .
Then:
a b < a + c b + d < c d
Proof. 
It is a b < c d a d < b c b c a d > 0 and b , d > 0 b b + d > 0 . This implies a + c b + d a b = b a + c a b + d b b + d = b c a d b b + d > 0 and thus a b < a + c b + d . The inequation a + c b + d < c d follows similarly. □
Mediants of convergents that are weighted in a certain way are another important concept for computing bounds:
Definition 4.
The term x n , t = t p n + 1 + p n t q n + 1 + q n with 1 t a n + 2 is called the (n,t)-th semiconvergent.
Semiconvergents of an even n are strictly monotonically increasing, and semiconvergents of an odd n are strictly monotonically decreasing. This is the content of the following lemma.
Lemma 4.
(Monotony of Semiconvergents)
Let n be even. Then, x n , t < x n , t + 1 .
Let n be odd. Then, x n , t > x n , t + 1 .
Proof. 
A simple calculation and the use of the sign theorem (Theorem 2) results in
x n , t + 1 x n , t = t + 1 p n + 1 + p n t + 1 q n + 1 + q n t p n + 1 + p n t q n + 1 + q n   = 1 n t + 1 q n + 1 + q n t q n + 1 + q n
The denominator of the last fraction is always positive. Thus, the last term is positive iff n is even (i.e., x n , t + 1 x n , t > 0 ), and it is negative iff n is odd (i.e., x n , t + 1 x n , t < 0 ). □
In order to simplify proofs in what follows, the following conventions are used:
p 1 = d e f 1   and   q 1 = d e f 0
With this, x 1 , 1 = p 0 + p 1 q 0 + q 1 = a 0 + 1 1 + 0 = a 0 + 1 becomes a semiconvergent. Now, x 1 = p 1 q 1 = A a 1 a 0 + 1 a 1 = a 0 + 1 a 1 B a 0 + 1 = x 1 , 1 where (A) is the recursion theorem and (B) follows because a 1 1 ; thus, x 1 x 1 , 1 .
Furthermore, it is x 1 , t = t p 0 + p 1 t q 0 + q 1 = t a 0 + 1 t 1 + 0 = t a 0 + 1 t = a 0 + 1 t for 1 t a 1 .
Putting things together, it is
x 1 , 1 = a 0 + 1 > a 0 + 1 2 > > a 0 + 1 a 1 = x 1
Based on this, we can refine Figure 4, which depicts the nesting and ordering of convergents by including semiconvergents: Between two succeeding convergents (e.g., x n and x n + 2 in Figure 8, the corresponding semiconvergents ordered according to Lemma 4 are nested (in increasing order as shown for an even n in Figure 8). Furthermore, beyond x 1 = a 0 + 1 a 1 , the semiconvergents x 1 , t are added.
Now, we are prepared to prove a lower bound of the approximation of a number by the convergents of its continued fraction representation by means of the denominators of the convergents.
Lemma 5.
(Lower Bounds)
Let p n / q n be a convergent of the continued fraction representation of x. Then:
x p n q n > 1 q n + q n + 1 q n
Proof. 
The proof is based on the following claims:
Claim 1.
n even ⇒ p n q n < p n + 1 + p n q n + 1 + q n < x < p n + 1 q n + 1 .
Proof. 
p n + 1 + p n q n + 1 + q n is the mediant of p n + 1 q n + 1 and p n q n . Thus, the mediant property (Note 3) shows that p n q n < p n + 1 + p n q n + 1 + q n < p n + 1 q n + 1 . Then:
p n q n < p n + 1 + p n q n + 1 + q n < A 2 p n + 1 + p n 2 q n + 1 + q n < < a n + 2 p n + 1 + p n a n + 2 q n + 1 + q n = B p n + 2 q n + 2
where (A) follows by the monotony of even semiconvergents (Lemma 4), and (B) is the recursion theorem. Because of Theorem 7 (note that n + 2 is even and n + 1 is odd), it is p n + 2 q n + 2 < x < p n + 1 q n + 1 . This proves Claim 1.   c l a i m 1
Claim 2.
n odd ⇒ p n 1 q n 1 < x < p n + 1 + p n q n + 1 + q n < p n q n .
Proof. 
As before, p n + 1 + p n q n + 1 + q n is the mediant of p n + 1 q n + 1 and p n q n . Because n is odd, it is p n + 1 q n + 1 < p n q n (Theorem 7). Thus, p n + 1 q n + 1 < p n + 1 + p n q n + 1 + q n < p n q n because of the mediant property (Note 3). Then:
p n q n > p n + 1 + p n q n + 1 + q n > A 2 p n + 1 + p n 2 q n + 1 + q n > > a n + 2 p n + 1 + p n a n + 2 q n + 1 + q n = B p n + 2 q n + 2
where (A) follows by the monotony of odd semiconvergents (Lemma 4), and (B) is the recursion theorem. Because of Theorem 7 (note that n − 1 is even and n + 2 is odd), it is p n 1 q n 1 < x < p n + 2 q n + 2 , and because n is odd, it is p n + 2 q n + 2 < p n q n . This proves Claim 2. c l a i m 2
With Claim 1, for even n, it is p n q n < p n + 1 + p n q n + 1 + q n < x x p n q n > p n + p n + 1 q n + q n + 1 p n q n .
With Claim 2, for n odd, it is x < p n + 1 + p n q n + 1 + q n < p n q n p n q n x > p n q n p n + p n + 1 q n + q n + 1 x p n q n > p n + p n + 1 q n + q n + 1 p n q n .
Thus, for any k ∈ ℕ:   x p k q k > p k + 1 + p k q k + 1 + q k p k q k . Next, we compute
p k + p k + 1 q k + q k + 1 p k q k = p k + p k + 1 q k q k + q k + 1 p k q k + q k + 1 q k = p k + 1 q k p k q k + 1 q k + q k + 1 q k = A 1 k q k + q k + 1 q k
where (A) is the sign theorem (Theorem 2).
This implies x p k q k > 1 k q k + q k + 1 q k = 1 q k + q k + 1 q k . □
Because of q k + 1 > q k (Corollary 1), it is
q k + q k + 1 < 2 q k + 1 1 2 q k + 1 < 1 q k + q k + 1 1 2 q k q k + 1 < 1 q k + q k + 1 q k
Using the last inequality in Lemma 5 (Lower Bounds) and using Lemma 3 (Upper Bounds), we obtain the concluding theorem of this section:
In summary, we have proved the following:
Theorem 10.
(Bounds of Approximations by Convergents)
Let p k / q k  be a convergent of the continued fraction representation of x. Then:
1 2 q k q k + 1 < 1 q k + q k + 1 q k < x p k q k < 1 q k q k + 1 < 1 q k 2
 □

2.5. Best Approximations

Our goal is to approximate a real number by a rational number as good as possible while keeping the denominator of the rational number “small”. Keeping the denominator small is important because in practice, every real number can only be given up to a certain degree of precision, and this is achieved by means of a huge denominator and corresponding numerator. i.e., approximating a real number by a rational number with a huge denominator is canonical, but finding a small denominator is a problem.
This is captured by the following:
Definition 5.
A fraction p / q is called a best approximation (of the first kind) of α :⇔ c / d : d q α c d > α p q (assuming c/d ≠ p/q).
Often, the addition “of the first kind” is omitted. By definition, a best approximation of a real number can only be improved if the denominator of the given approximation is increased.
If p/q is a best approximation of α, then α p q = 1 q q α p is small and, thus, q α p is small. Measuring the goodness of an approximation this way results in the following:
Definition 6.
A fraction p / q is called a best approximation of the second kind of α :⇔ c / d : d q d α c > q α p (assuming c/d ≠ p/q).
The question is whether every best approximation is also a best approximation of the second kind. Now, 1/3 is a best approximation of 1/5 because the only possible fractions for c/d, with d ≤ 3 = q, are 0, 1/2, 2/3, and 1, and these numbers satisfy 1 5 c d > 1 5 1 3 .
Next, we observe that 1 1 5 0 < 3 1 5 1 with 1 < 3. Thus, with d = 1 and q = 3 (i.e., d < q ) and α = 1 / 5 , we found a fraction c / d = 0 / 1 with d α c < q α p ! As a consequence, although 1/3 is a best approximation of the first kind of 1/5, it is not a best approximation of the second kind.
Thus, not all best approximations of the first kind are best approximations of the second kind. But the reverse holds true:
Lemma 6.
(Every 2nd Kind Best Approximation is a 1st Kind Best Approximation)
If p / q is a best approximation of the second kind of α , then p / q is also a best approximation of the first kind of α.
Proof (by contradiction).
Assume p/q is not a best approximation of the first kind. Then, α c d α p q for a fraction c/d with d < q. Multiplying both inequations results in d α c d q α p q d α c q α p , which is a contradiction because p/q is a best approximation of the second kind. □
The next simple estimation about the distance of two fractions by means of the product of their denominators is often used.
Note 4.
(Distance of Fractions)
Let a b , p q with a b p q . Then:
p q a b 1 q b
Proof. 
With a , p and b , q , it is p b a q . Also, p b a q 0 because otherwise p b = a q p q = a b which contradicts the premise. Thus, p b a q , i.e., p b a q 1 . This implies
p q a b = p b a q q b = p b a q q b 1 q b
where q b = q b because b , q . □
Next, we prove that every best approximation of the second kind is a convergent.
Theorem 11.
(2nd Kind Best Approximations are Convergents)
Let  a / b  be a best approximation of the second kind of  x , and let  x = a 0 ; a 1 ,  be the continued fraction representation of x.
Then  a / b  is a convergent of x.
Proof. 
Being a best approximation of the second kind of x, a / b satisfies, by definition, d x c > b x a for d ≤ b.
Claim 1.
a b a 0 = x 0 .
Proof (by contradiction).
Assume a b < a 0 a 0 < a b x a 0 < x a b ; thus, x a 0 < x a b A b x a b = b x a , where (A) holds because b , i.e., 1 ≤ b. This implies 1 x a 0 b x a , which contradicts d x c > b x a for d ≤ b (with d = 1 b and c = a 0 ). This means that a b a 0 = a 0 1 = B q 0 q 0 = x 0 , (B) is because of the recursion theorem. c l a i m 1
Thus, the geometric situation is as depicted in Figure 9, i.e., a / b is in the grey shaded area being greater than or equal to the convergent x 0 . This will be refined in what follows.
Next, we proceed with a proof by contradiction assuming that a / b is not a convergent of x.
Assumption. 
a b q k q k = x k for k .
According to Claim 1, a b a 0 = x 0 . Thus, one of the following must hold:
( i )   a b p k 1 q k 1 , p k + 1 q k + 1   for   k     1
or
( ii )   a b > p 1 q 1 = x 1
This situation is shown in Figure 10.
Case (1).
If (i) is true, then
a b p k 1 q k 1 < x p k 1 q k 1 < T h 8 p k q k p k 1 q k 1 = C 1 q k q k 1
where (Th8) is Theorem 8, Equation (14), and (C) is from Corollary 3, Equation (10). Furthermore, a b p k 1 q k 1 D 1 b q k 1 , with (D) because of Note 4 (Distance of Fractions).
Together, 1 b q k 1 a b p k 1 q k 1 < 1 q k q k 1 1 b < 1 q k b > q k (iii).
Also, if (i) is true, then x a b p k + 1 q k + 1 a b E 1 b q k + 1 , where (E) is again using Note 4. This implies b x a b 1 q k + 1 b x a 1 q k + 1 (iv).
Lemma 3 (Upper Bounds) tells us that x p k q k < 1 q k q k + 1 which is equivalent to q k x p k q k < 1 q k + 1 q k x p k < 1 q k + 1 q k x p k < b x a (see (iv) just before). Since q k < b (see (iii) above), this is a contradiction to a / b being a best approximation of the second kind of x. Thus, Case (1) does not occur.
Case (2).
This case is shown in Figure 11. Then, x a b > p 1 q 1 a b = F 1 b q 1 , where (F) again uses Note 4. This implies b x a > 1 q 1 = G 1 a 1 (v) with (G) using the recursion theorem.
Now, x a 0 = 1 a 1 + 1 a 2 + 1 a 1 , where the last inequality holds because of 1 a 2 + > 0 ; thus, x a 0 1 a 1 < H b x a , (H) based on (v) before. This means that 1 x a 0 < b x a with 1 b , i.e., a / b is not a best approximation of the second kind of x, which is a contradiction. Thus, Case (2) does not occur either.
Consequently, the assumption is wrong and there is a k with a b = q k q k = x k , i.e., a / b is a convergent. □
So, every best approximation of the second kind is a convergent. The next theorem proves the reverse, i.e., that every convergent is a best approximation of the second kind.
Theorem 12.
(Lagrange, 1798—Convergents are 2nd Kind Best Approximations)
Let  p n / q n  be a convergent of  x = a 0 ; a 1 , , a N , x a 0 + 1 2 , and  n 0 . Then, for  d q n  and  c d p n q n  it is  d x c > q n x p n , i.e., the convergent is a best approximation of the second kind of x.
The cases x = a 0 + 1 2 and n = 0 are excluded because the convergent p 0 q 0 = a 0 1 is not a best approximation of the second kind of x = a 0 + 1 2 : it is 1 x a 0 + 1 = a 0 + 1 2 a 0 1 = 1 2 and 1 x a 0 = a 0 + 1 2 a 0 = 1 2 , which implies 1 x a 0 + 1 = 1 x a 0 . Setting d : = 1 q 0 , c : = a 0 + 1 results in d x c = 1 x a 0 + 1 = 1 x a 0 = q 0 x p 0 . If p 0 q 0 would be a best approximation of the second kind of x, then 1 x a 0 + 1 > 1 x a 0 would hold.
The proof of Lagrange’s theorem is very technical. First, the expression y 0 x z 0 is analyzed to find the smallest integral numbers y 0 and z 0 such that the expression is minimized under the constraint y 0 q 0 , , q k , i.e., y 0 is a denominator of a convergent. It is shown both that z 0 / y 0 is a best approximation of the second kind of x, and that z 0 = p k and y 0 = q k .
Proof. 
Let k and let p k / q k be a convergent. First, we are looking for the smallest numbers y 0 , z 0 with y 0 q 0 , , q k such that y 0 x z 0 is minimal.
Step 1.
Pick an arbitrary z , and based on this we determine y 0 q 0 , , q k .
It is m i n y y x z = 0 y = z x , but in general y . Looking for a solution y 0 q 0 , , q k that minimizes y 0 x z results in the following potential positions of z / x with respect to the denominators q 0 , , q k (see Figure 12):
  • Case 1: z / x > q k . Then, y 0 = q k is the solution;
  • Case 2: z / x < q 0 . Then, y 0 = q 0 is the solution;
    Let q i z / x q i + 1 for 1 ≤ i ≤ k.
  • Case 3: For q i + 1 x z < q i x z (i.e., z / x is closer to q i + 1 than to q i ), y 0 = q i + 1 is the solution, and for q i + 1 x z > q i x z (i.e., z / x is closer to q i than to q i + 1 ), y 0 = q i is the solution;
  • Case 4: For q i + 1 x z = q i x z (i.e., z / x is exactly in the middle between q i and q i + 1 ), y 0 = q i is the solution because q i < q i + 1 , and we are looking for the smallest y 0 , especially y 0 q 0 = 1 . s t e p 1
Step 2.
Based on the y 0 found, we determine z 0 next. It is m i n z y 0 x z = 0   z = y 0 x , but in general, z . In solving the minimization problem within (i.e., z 0 : = a r g m i n z y 0 x z ), the following cases can be distinguished (see Figure 13):
  • Case 0: It may happen that y 0 x . Then, choose z 0 = y 0 x ;
  • Case 1: y 0 x is between two integral numbers s and t, i.e., s < y 0 x < t . For y 0 x s > y 0 x t (i.e., y 0 x is closer to t than to s), z 0 = t is the solution; and for y 0 x s < y 0 x t (i.e., y 0 x is closer to s than to t), z 0 = s is the solution;
  • Case 2: For y 0 x s = y 0 x t (i.e., y 0 x is exactly in the middle between t and s), z 0 = s is the solution because s < t , and we are looking for the smallest z 0 . s t e p 2
Claim 1.
z 0 is uniquely determined.
Proof (by contradiction).
Assume there exists a z ˜ 0 with z ˜ 0 z 0 and x z 0 y 0 = x z ˜ 0 y 0 . This can only happen iff one term is positive and the other is negative, i.e., for example, if x z 0 y 0 > 0 and x z ˜ 0 y 0 < 0 , and then x z 0 y 0 = z ˜ 0 y 0 x , i.e., x = z 0 + z ˜ 0 2 y 0 .
As an intermediate step we prove:
Claim 2.
z 0 + z ˜ 0 and 2 y 0 are co-prime, i.e., g c d z 0 + z ˜ 0 , 2 y 0 = 1
Proof (by contradiction).
Let z ˜ 0 + z 0 = L p and 2 y 0 = L q with L > 1 . Then, x = z 0 + z ˜ 0 2 y 0 = L p L q x = p q and thus
( i )   q x p = q p q p = 0
Assume L > 2 . Then, with 2 y 0 = L q and L / 2 > 1 , it follows:
( ii )   y 0 = L 2 q > q
Now, y 0 has been determined in Step 1 to satisfy y 0 = a r g m i n y   y x z for a given z, especially for z = p , i.e., y 0 = a r g m i n y   y x p . Because 0 = m i n   y y x p and q x p = 0 , it must be q = y 0 . This is a contradiction because q < y 0 according to (ii) before. Thus, 1 < L 2 , i.e., L = 2.
With L = 2 and 2 y 0 = L q , we get y 0 = q , which implies. By definition of z 0 , q x p = y 0 x p > y 0 x z 0 . However, q x p = 0 (see (i) above); thus, 0 > y 0 x z 0 , which is a contraction.   c l a i m 2
We continue the proof of Claim 1: It is z 0 + z ˜ 0 2 y 0 = x and also x = p N q N , i.e., z 0 + z ˜ 0 2 y 0 = p N q N . Because g c d z 0 + z ˜ 0 , 2 y 0 = 1 according to Claim 2, it follows that p N = z 0 + z ˜ 0 and q N = 2 y 0 .
Now, let N 2 . Then, it is 2 y 0 = q N = A a N q N 1 + q N 2 ((A) uses the recursion theorem (Theorem 1)), and with Note 1, it is a N 2 . Thus, 2 y 0 2 q N 1 + q N 2   y 0 q N 1 + q N 2 2   q N 1 y 0 q N 2 2 < B y 0 ((B) is because q N 2 > 0 ). Now:
q N 1 x p N 1 = q N 1 p N q N p N 1 = 1 q N q N 1 p N p N 1 q N = C 1 q N = 1 2 y 0 D 1 2
where (C) holds because of the sign theorem and (D) because y 0 1 (see the end of the proof of Step 1).
Furthermore,
y 0 x z 0 = y 0 z 0 + z ˜ 0 2 y 0 z 0 = z 0 + z ˜ 0 2 z 0 = 1 2 z 0 + z ˜ 0 2 z 0 = 1 2 z ˜ 0 z 0 E 1 2 i i i
where (E) is true because z ˜ 0 z 0 and, thus, z ˜ 0 z 0 1 for integral numbers z ˜ 0 and z 0 . Together, we obtained y 0 x z 0 1 2 q N 1 x p N 1 , which is a contradiction to the choice of y 0 and z 0 ! This proves Claim 1 for N 2 .
Now, let N = 1 and choose a 1 = 2 (based on Note 1, the highest element of a continued fraction is always greater than or equal 2, thus a 1 2 ). Then
x = a 0 ; a 1 = p 1 q 1 = F a 1 a 0 + 1 a 1 = 2 a 0 + 1 2 = a 0 + 1 2
((F) is the recursion theorem) which has been excluded from the theorem.
Thus, let N = 1 and a 1 > 2 . Then
1 x a 0 = G q 0 x p 0 = q 0 p 1 q 1 p 0 = 1 q 1 q 0 p 1 q 1 p 0 = H 1 q 1 = G 1 a 1 < 1 2
where (G) applies the recursion theorem and (H) the sign theorem. Because of (iii), it is y 0 x z 0 1 2 , i.e., together, q 0 x p 0 < y 0 x z 0 which contradicts the definition of y 0 and z 0 ! This proves Claim 1 for N = 1 . c l a i m 1
Next, we observe
Claim 3.
z 0 y 0 is a best approximation of the second kind of x.
Otherwise: b x a y 0 x z 0 for an a b z 0 y 0 with b y 0 , which contradicts the definition of y 0 and z 0 !   c l a i m 3
According to Theorem 11, z 0 y 0 is a convergent of x, i.e., z 0 y 0 = p s q s for an s k . If s = k , the proof is done. Thus, we assume s < k .
Claim 4.
For s < k , it is 1 q s + q s + 1 1 q k + q k 1 .
Proof. 
s < k s k 1 q s q k 1 (Corollary 1: denominators are monotonically increasing). Similarly, s < k s + 1 k q s + 1 q k . Together, this implies q k + q k 1 q s + q s + 1 . c l a i m 4
Next, we get
q s x p s = q s x p s q s > I q s 1 q s + q s + 1 q s = 1 q s + q s + 1 J 1 q k + q k 1
where (I) is Lemma 5 (Lower Bounds) and (J) is Claim 4.
Furthermore, q k x p k = q k x p k q k < K q k 1 q k q k + 1 = 1 q k + 1 , where (K) holds because of Lemma 3 (Upper Bounds).
With z 0 y 0 = p s q s and the definition of y 0 = q s and z 0 = p s (i.e., the minimizing property), it is q s x p s = y 0 x z 0 q k x p k 1 q k + q k 1 1 q k + 1 , which implies q k + 1 < q k + q k 1 . This is a contradiction; because of the recursion theorem, it is q k + 1 = a k + 1 q k + q k 1 L q k + q k 1 , where (L) holds with a k 1 . Thus, s = k which proves the overall theorem. □
Putting the last two theorems together yields:
Corollary 5.
a / b is a best approximation of the second kind of x ⇔ x is a convergent of x. □
According to Theorem 12, every convergent is a best approximation of the second kind, and each best approximation of the second kind is also a best approximation of the first kind (Lemma 6). We keep this observation as:
Note 5.
Every convergent is a best approximation of the first kind. □
But are best approximations of the first kind also always convergents? Not quite: the next theorem proves that a best approximation of the first kind is a convergent or a semiconvergent.
Theorem 13.
(Lagrange, 1798—1st Kind Best Approximations are Convergents or Semiconvergents)
Let  a / b  be a best approximation of the first kind of  x = a 0 ; a 1 , , a N . Then a / b is a convergent or a semiconvergent of x.
Proof. 
By definition, it is x c d > x a b for c c a b and d b .
Claim 1.
a / b > a 0 .
Otherwise: a b a 0 = a 0 1 ; thus, x a 0 x a b . Now, x a 0 = 1 a 1 + > 0 ; thus, 0 < x a 0 x a b x a 0 1 x a b . Because 1 b , we obtained a contradiction since a / b is a best approximation of the first kind.   c l a i m 1
Claim 2.
a / b < a 0 + 1 .
Otherwise: a b a 0 + 1 and based on the geometric situation depicted in Figure 8, it follows that x a 0 + 1 1 x a b with 1 b , which contradicts a / b being a best approximation of the first kind.   c l a i m 2
Consequently, a / b lies between x 0 = a 0 and x 1 , 1 = a 0 + 1 (see Equation (22)), i.e.,
x 0 = a 0 < a b < a 0 + 1 = x 1 , 1
and is, thus, covered by the set of intervals defined by the convergents and semiconvergents of x (see Figure 8).
Assumption. 
a / b is neither a convergent nor a semiconvergent.
This results in the following cases:
  • Case 1: a / b lies between two semiconvergents x k 1 , r and x k 1 , r + 1 ;
  • Case 2: a / b lies between two convergents x k and x k + 2 ;
  • Case 3: a / b lies between a convergent and a semiconvergent.
We will show that all three cases lead to a contradiction, i.e., the assumption must be false; thus, the theorem is proven.
Case 1.
a / b lies between x k 1 , r = r p k + p k 1 r q k + q k 1 and x k 1 , r + 1 = r + 1 p k + p k 1 r + 1 q k + q k 1 .
Then,
a b r p k + p k 1 r q k + q k 1 < r + 1 p k + p k 1 r + 1 q k + q k 1   r p k + p k 1 r q k + q k 1 = A 1 r + 1 q k + q k 1 r q k + q k 1
where (A) results from the same computation performed in the proof of Lemma 4.
Furthermore, it is
( i )   a b r p k + p k 1 r q k + q k 1 = a r q k + q k 1 b r p k + p k 1 b r q k + q k 1 B 1 b r q k + q k 1
where (B) is seen to be valid as follows: a r q k + q k 1 b r p k + p k 1 and, thus, a r q k + q k 1 b r p k + p k 1 0 ; if it would be zero, the first modulus in (i) would be zero, i.e., a / b = x k 1 , r which contradicts the assumption of the claim, which in turn implies a r q k + q k 1 b r p k + p k 1 1 .
Together,
1 b r q k + q k 1 < 1 r + 1 q k + q k 1 r q k + q k 1     1 b < 1 r + 1 q k + q k 1 ,  
thus,
( ii )   b > r + 1 q k + q k 1
Because of the monotony of the sequence of semiconvergents ( x s , t ) t (Lemma 4), it is for an odd k (i.e., k 1 even) x k 1 , r < x k 1 , r + 1 (see the geometric situation in Figure 14);
thus,
x a b > x r + 1 p k + p k 1 r + 1 q k + q k 1
But with (ii), it is r + 1 q k + q k 1 < b ; thus, a / b is not a best approximation of the first kind to x, which is a contradiction. k even leads to a contradiction too, i.e., Case (1) is not possible c a s e 1
Case 2.
a / b lies between x k and x k + 2 .
Then, a b p k q k < p k q k p k + 2 q k + 2 = C a k + 2 q k q k + 2 < 1 q k q k + 2 where (C) is Equation (11) from Corollary 3, and with Note 4, it is a b p k q k 1 b q k .
Together, 1 b q k < 1 q k q k + 2 1 b < 1 q k + 2 b > q k + 2 . Because of the geometric situation shown in Figure 15, it is x a b > x p k + 2 q k + 2 , which is a contradiction to a / b being a best approximation of the first kind to x and b > q k + 2 .   c a s e 2
Case 3.
a / b lies between a convergent and a semiconvergent.
This implies that a / b lies between x k and x k , 1 (see Figure 8), otherwise a / b would lie between two semiconvergents, which has already been covered in Case 1.
Thus, a b p k q k < x k x k , 1 , but
x k x k , 1 = p k q k p k + 1 + p k q k + 1 + q k = p k q k + 1 + q k q k p k + 1 + p k q k q k + 1 + q k = p k q k + 1 q k p k + 1 q k q k + 1 + q k = D 1 q k q k + 1 + q k
where (D) is the sign theorem. I.e., it is a b p k q k < 1 q k q k + 1 + q k . As before, with Note 4, it is a b p k q k 1 b q k 1 b q k < 1 q k q k + 1 + q k b > q k + 1 + q k .
The geometric situation from Figure 16 reveals x a b > x p k + p k 1 q k + q k 1 , which is a contradiction to a / b being a best approximation of the first kind to x and b > q k + 1 + q k .   c a s e 3  □
Finally, we give a simple criterion that allows us to prove that a given fraction is a convergent of another real number. This theorem is a cornerstone of computing a prime factor with Shor’s algorithm.
Theorem 14.
(Legendre, 1798—Convergent Criterion)
Let  x a b < 1 2 b 2 a / b  is a convergent of x.
Proof. 
We show that a / b is a best approximation of the second kind of x. Theorem 11 then proves the claim.
Let d x c b x a for a b c d and d > 0 . We need to prove d > b .
Now, b x a = b x a b < b 1 2 b 2 = 1 2 b . This implies d x c < 1 2 b d x c d < 1 2 b x c d < 1 2 d b . Thus,
c d a b = c d x + x a b c d x + x a b < 1 2 d b + 1 2 b 2 = b + d 2 d b 2
With Note 4 (Distance of Fractions), it is also c d a b 1 d b . Together, it is
1 d b < b + d 2 d b 2     1 < b + d 2 b     2 b < b + d     d > b .

3. Probability of the Occurrence of Convergents

3.1. Estimating Secant Lengths

In this part, we use the main arguments of [2].
In order to estimate the probability of the occurrence of a certain state after having performed the quantum Fourier transform, we need the following estimation of a lower bound and an upper bound of the length of a secant of the unit circle:
Lemma 7.
(Secant Length Estimation)
If  φ π , π  then  2 φ π 1 e i φ φ .
Proof. 
The upper bound follows from elementary geometry, namely that the length of a secant is less than or equal to the length of the corresponding arc of a circle (see Figure 17).
The length of the arc determined by the angle φ on a circle of radius r is r φ , i.e., if the circle is a unit circle, the length of the arc (green in the Figure) is φ .
A secant of the unit circle (red in the Figure 17) can be defined by the two complex numbers on the unit circle (black in the Figure 17) that are the endpoints of the secant. Thus, the length of this secant is the difference of these complex numbers. One of these points can always be 1 because a corresponding rotation is length-preserving; the other point is then e i φ , where φ is the angle of the arc cut by the secant. The length of this secant is then 1 e i φ .
This proves the inequality 1 e i φ φ .   u p p e r b o u n d
Next, we compute
1 e i φ = A 1 c o s φ i s i n φ = B 1 c o s φ 2 + s i n 2 φ = 1 2 c o s φ + c o s 2 φ + s i n 2 φ = 2 2 c o s φ = 2 1 c o s φ = C 2 2 s i n 2 φ 2 = D 2 s i n φ 2  
where (A) uses Euler’s formula, (B) is the definition of the modulus of a complex number with R e = 1 c o s   φ and I m = s i n   φ , (C) is the double-angle formula, and (D) assumes that s i n φ 2 0 .
To estimate a lower bound for s i n φ 2 , we analyze the function f x = s i n   x 2 x π . From elementary calculus, it is known that a function ψ is concave on D if and only if its second derivative is not positive on D, i.e., ψ 0 on D.
(Reminder: ψ is concave on D:⇔ x , y D t 0 , 1 : ψ t x + 1 t y t ψ x + 1 t ψ y ,
I.e., for any two points on the graph of ψ , the secant between these points is below the graph, Figure 18).
With d 2 s i n   x / d x 2 = s i n   x 0 especially for x 0 , π / 2 , i.e., s i n   x is concave on x 0 , π / 2 . Thus, the secant between s i n   0 = 0 and s i n π 2 = 1 is below the graph of s i n   x (orange in Figure 18). However, this secant is given by g x = 2 π x (green in Figure 18). Thus, it is 2 π x s i n x for x 0 , π / 2 , i.e., with φ : = 2 x we get φ π s i n φ 2 for φ 0 , π , and this implies 2 φ π 2 s i n φ 2 for φ 0 , π .
Now, 1 e i φ = 2 s i n φ 2 (see the computation before) implies 1 e i φ 2 φ π for φ 0 , π .
Furthermore, s i n   x is convex on x π / 2 , 0 ; thus, an argument analogous to the above shows that 1 e i φ 2 φ π for φ π , π .   l o w e r b o u n d  □

3.2. Estimating Amplitude Parameters

As stated in the introduction, the quantum part of Shor’s algorithm produces in its final step the following quantum state via a measurement:
1 N A j = 0 A 1 ω N j p y | y
Thus, according to the Born rule, the probability P y of this particular state |y⟩ is the square of the modulus of the amplitude of |y⟩, i.e.,
P y = 1 N A j = 0 A 1 ω N j p y 2 = 1 N A j = 0 A 1 ω N j p y 2
The argument of the modulus is a geometric sum q j with q = ω N p y = e 2 π i N p y ; thus, in case q 1 ,
P y = 1 N A j = 0 A 1 q j 2 = 1 N A 1 q A 1 q 2
With q A = e 2 π i N A p y . In this section, in order to compute a lower bound for P y , we investigate some relations between the following parameters appearing in Equation (28):
  • n: the number to be factorized;
  • N: a power of 2 (e.g., N = 2 m ) with n 2 < N < 2 n 2 ;
    • the choice of N effectively determines the domain of numbers that can be represented in the | a -part of the quantum register (see Equation (1)).
  • p: the period of the modular exponentiation function f x = a x   m o d   n ;
  • A: the number of arguments mapped to a given value of f .
We also estimate bounds of the argument 2 π A p y N of q A = e 2 π i N A p y .

3.2.1. Basics from Number Theory

For convenience, we state the definition of the modulo function.
Definition 7.
The modulo function is the following map:
m o d : 0 × z , n z z n n = d e f z   m o d   n
z   m o d   n is, thus, the residue left when dividing z by n. I.e., if r = z   m o d   n , then there is a number k 0 such that z = k n + r with 0 r < n .
If z   m o d   n = z ˜   m o d   n = r , we find numbers k 1 and k 2 such that z = k 1 n + r and z ˜ = k 2 n + r with 0 r < n . This implies that z z ˜ = k 1 k 2 n = : k n , i.e., n is a divisor of z z ˜ (in symbols: n | z z ˜ ). We also obtain that z   m o d   n = z ˜   m o d   n implies that z ˜ = z + k n .
The equation z   m o d   n = z ˜   m o d   n is abbreviated as z z ˜   m o d   n ; in words, z is congruent  z ˜ modulo n. As shown just before, z z ˜   m o d n   is equivalent to n | z z ˜ and to z ˜ = z + k n . We keep this as
Note 6.
 
z z ˜   m o d   n n | z z ˜ z ˜ = z + k n . □
Furthermore, we state the definition of modular exponentiation which turns out to play a key role in finding factors.
Definition 8.
 
For 0 < a < n , the modular exponentiation function is the following map:
f : 0 0 x a x   m o d   n
The smallest number p that satisfies f x = f x + p for all x is called the period of f. Especially, with x = 0 , we get f 0 = f p which means that a p   m o d   n = a 0   m o d n = 1   m o d   n , i.e., a p 1   m o d   n which in turn is equivalent to n | a p 1 . Thus, we have proven:
Note 7.
 
f x = a x m o d n has period p ⇔ a p 1 m o d n n | a p 1 . □
Finding a factor of n can be achieved by finding the period p of the function f x = a x m o d n . This is seen as follows: Let p be the period of f , then n | a p 1 , i.e., a p 1 = k n . Assume p is even (if p is odd, Shor’s algorithm is repeated with a different a, until an even p is found). With such an even p, it is a p 1 = a p / 2 1 a p / 2 + 1 = k n which implies that a p / 2 1 and a p / 2 + 1 have a common divisor, which in turn means that g c d a p / 2 1 , n or g c d a p / 2 + 1 , n is a divisor of n. Thus, if an even period has been determined, classically efficient calculations can be used to compute a factor of n. If this factor is a prime number, we can finish. Otherwise, we continue determining a factor of the former factor, and so on, until we end up with a prime factor of n.
Next, we determine an upper bound of the period p of the modular exponentiation by using group theory. A simple calculation shows that “ ” is an equivalence relation on . The equivalence class of z is denoted as z and is referred to as the residue class of z modulo n. It is z = z ˜   |   z ˜ z   m o d   n = z + k n   |   k (see Note 6), where the latter set is sometimes written as z + n . The set of all residue classes modulo n is denoted as n , i.e., n = 0 , 1 , , n 1 .
We can multiply two residue classes modulo n as follows: x y = x y . With this multiplication, n * = z n   |   g c d z , n = 1 becomes a group. Because n * n , it is φ n : = c a r d   n * c a r d   n = n . Since every integer is a divisor of itself, it is g c d n , n = n 1 (for n 2 ), i.e., the cardinality of numbers co-prime to n is less than n: n 2 φ n < n .
The well-known Lagrange’s theorem from group theory states that for a group G with c a r d   G = m < and for each x G , it is x m = e (e is the unit element of G)—see Lemma 3.2.5 in [3], for example. Thus, for x n * , it is x φ n = 1 , i.e., x φ n 1   m o d   n . Since the period p is the smallest number with x p 1   m o d   n , it follows that p φ n and, thus, p < n .
Now, the assumption of Shor’s algorithm is that 0 < a < n and that g c d a , n = 1 , which ensures that a n * ; thus, [ a ] p 1   m o d   n and p < n .
Lemma 8.
 
Let p be the period of f x = a x   m o d   n . Then, p < n . □

3.2.2. Intervals of Consecutive Multiples of the Period

The relation between N and p is depicted in Figure 19; multiples of N are always contained in closed intervals defined by consecutive multiples of p, i.e., it may happen that a multiple of N coincides with a multiple of p.
Note 8.
k t : t 1 p k N t p .
Proof. 
Pick an arbitrary k , i.e., k N is also given. Then, we find a t ˜ such that t ˜ p k N (trivial because p > 0 ). Let t be the smallest of such t ˜ , i.e., t = d e f m i n t ˜ | t ˜ p k N . Thus, t 1 p k N because otherwise t 1 p > k N , which is a contradiction because t was chosen minimal.
Together, t 1 p k N t p . The claim follows because k an arbitrary number. □
The situation we just discussed is shown in Figure 20.
Furthermore, two different multiples of N are in different intervals defined by succeeding multiples of p. Otherwise, the situation of Figure 21 would imply that N p , which is a contradiction as shown by the proof of Note 9 below.
We denote a t with t 1 p k N t p by t k . This is justified by the next Note 9 which proves that such a t is uniquely determined by k . Especially, a multiple k N is contained in “its” interval:
k t k : k N t k 1 p , t k p
Note 9.
 
Let k and t k with t k 1 p k N t k p .
Then, r s 0 , , p 1 implies t r t s .
Proof (by contradiction).
Assume r s but t r = t s = d e f t with t 1 p r N t p and t 1 p s N t p (see Figure 20). W.l.o.g. r < s r + 1 s s N r N r + 1 N r N = N . Further, s N r N t p t 1 p = p . Together, it is N s N r N p , i.e., N < p .
According to Lemma 8, we know p < n N < p < n < n 2 . However, by selection of N (see the bullet list at the beginning of Section 8), it is n 2 < N , which is a contradiction. □
The proof of Note 9 has shown especially:
Corollary 6.
r s 0 , , p 1     r N t s 1 p , t s p
By Note 9, for r s 0 , , p 1 , the numbers t r , t s are different, i.e., for each k 0 , , p 1 , such a unique t k exists, i.e., the p numbers t 0 , t 1 , , t p 1 are different. Thus:
Corollary 7.
 
There exist p different numbers t k , 0 k p 1 , such that t k 1 p k N t k p . □
These different numbers are strictly monotonically increasing.
Note 10.
 
Let k and t k with t k 1 p k N t k p . Then, t k < t k + 1 . Thus, t 0 < t 1 < < t p 1 .
Proof (by contradiction).
Assume t k + 1 t k ; thus, t k + 1 1 t k 1 , which implies t k + 1 p t k p and t k + 1 1 p t k 1 p .
Now, k N < k + 1 N , t k 1 p k N t k p , and t k + 1 1 p k + 1 N t k + 1 p . This implies k + 1 N t k + 1 p t k p and t k 1 p k N < k + 1 N , which finally results in t k 1 p < k + 1 N t k p —which is a contradiction to corollary 6 because this would imply that k + 1 N t k 1 p , t k p . □
Each multiple k N of N is “close” to a multiple t p in the sense that k N is at most p / 2 apart from t k 1 p or t k p (see Figure 22).
More precisely:
Note 11.
k t : t 1 p k N p 2 t p k N p 2 .
Proof. 
It is t 1 p k N t p , i.e., by definition k N t 1 p , t p . This implies k N t 1 p p 2 t p k N p 2 (see Figure 22), otherwise:
k N t 1 p > p 2 t p k N > p 2 t 1 p > p 2 k N t p > p 2 + k N
t p t 1 p > p 2 k N + p 2 + k N = p , but t p t 1 p = p , i.e., p > p , which is a contradiction! This proves the claim t 1 p k N p 2 t p k N p 2 . □
As before, from Note 9, it follows that for k 0 , , p 1 , these numbers t are all different. Precisely:
Corollary 8.
 
Let 0 k p 1 and t k such that t k 1 p k N p 2 t k p k N p 2 . If r s , then t r t s . □
The multiples of N are sparsely scattered across the intervals of consecutive multiples of p. More precisely, intervals of consecutive multiples of p, which contain a multiple of N, are not consecutive. This is the content of
Note 12.
 
Let n > 2 , k 0 , , p 1 , and t k with t k 1 p k N t k p .
Then t k + 1 > t k + 1 as well as t k 1 < t k 1 .
Proof. 
Because of Note 10, it is t k + 1 > t k ; thus, t k + 1 t k + 1 .
Assumption. 
t k + 1 = t k + 1 .
By definition, k + 1 N t k + 1 1 p , t k + 1 p , and by assumption, t k + 1 = t k + 1 ; thus, it is k + 1 N t k p , t k + 1 p . Furthermore, by definition, k N t k 1 p , t k p (see Figure 23).
Now, t k 1 p , t k p , t k p , t k + 1 p t k 1 p , t k + 1 p which implies k N , k + 1 N t k 1 p , t k + 1 p .
Thus, N = k + 1 N k N t k + 1 p t k 1 p = 2 p , i.e., N 2 p (see Figure 23).
By Lemma 8, it is p < n 2 p < 2 n . With n > 2 n 2 > 2 n and by definition of N, it is n 2 < N ; thus, N > n 2 > 2 n > 2 p : a contradiction!
Thus, the assumption is false, which implies t k + 1 > t k + 1 . The claim t k 1 < t k 1 is proven similarly. □
The resulting geometric situation is depicted in Figure 24.
If k N t k 1 p , t k p , it is t k p k N p / 2 or k N t k 1 p p / 2 (see Figure 22 or Figure 24). In case k N t k 1 p p / 2 , we define t ^ : = t k 1 and k N t ^ p p / 2 results, and in case of t k p k N p / 2 , we define t ^ : = t k implying t ^ p k N p / 2 . According to Note 9, this t ^ is uniquely defined. This proves
Note 13.
 
k     ! t ^ : t ^ p k N p 2 , i.e., t ^ is uniquely determined by k . □
This is next rewritten into a format more useful for what follows.
Note 14.
 
Let k 0 , , p 1 and t k with t k k N p 1 2 , k N p + 1 2 .
If r s 0 , , p 1 , then t r t s .
Proof. 
It is t k k N p 1 2 , k N p + 1 2   k N p 1 2 t k k N p + 1 2   k N p 2 p t k k N + p 2 p 2 p t k k N p 2 p t k k N p 2 . Note 13 shows that t k is uniquely determined by k . □
Finally, we can prove the following:
Corollary 9.
 
There exist p different numbers t k , 0 k p 1 , such that
t k k N p 1 2 , k N p + 1 2
Proof. 
There exist p different numbers t k , 0 k p 1 , such that t k 1 p k N t k p (Corollary 7). The proof of Note 11 shows that this implies t k 1 p k N p 2 t k p k N p 2 . The proof of Note 14 shows that this implies t k k N p 1 2 , k N p + 1 2 . □

3.2.3. Cardinality of Pre-Images

First, we show that the parameter A is greater than 1, i.e., at least two numbers available in the | a -part of the quantum register are mapped by f to the same value.
Note 15.
A > 1 .
Proof. 
As reminded in the introduction, the quantum Fourier transform of Shor’s algorithm produces the following state:
a b = 1 N x = 0 N 1 x f x
After measurement of the | b -part of the register, the | a -part is in the state
| a = 1 A x + x + p + x + 2 p + + x + A 1 p
i.e., f 1 ( | x ) = x , x + p , x + 2 p , , x + A 1 p .
Choose x < p —such an x exists because otherwise it would be p = 0 , but a period p satisfies p > 0 . With p < n (Lemma 8) and n < n 2 < N (by choice of N), it is x + p < 2 p < N (see the proof of Note 12). Thus, x + p is in the domain of f (in the sense that it is a value in the | a -part of the quantum register available as an argument for f), i.e., f x + p is available in the | b -part of the register.
A = 1 would imply that f 1 ( | x ) = | x and, thus, | x + p f 1 ( | x ) , i.e., f ( | x ) f ( | x + p ) for p 0 . Since p > 0 , the function f would not be periodic. □
Next, we prove tighter bounds for the parameter A.
Note 16.
A 1 p < N < A + 1 p .
Proof. 
As in the proof of Note 15, we choose x < p . With
a = 1 A x + x + p + x + 2 p + + x + A 1 p ,
i.e., x , x + p , x + 2 p , , x + A 1 p are all values in the | a -part of the register being mapped to f x , i.e., A is the largest number satisfying x + A 1 p < N . With x 0 , this implies A 1 p < N —which is the first part of the claim.
Thus, A + 1 p > N . Otherwise, A + 1 p N and with x < p , it would be x + A p < p + A p = A + 1 p N , i.e., | x + A p would also be in the | a -part of the register being mapped to f x , which is a contradiction to the definition of A. This proves the second part of the claim. □
The next estimation gives an approximation of N in terms of the product of A and p .
Note 17.
N A p .
Proof. 
Because of A 1 p < N < A + 1 p , the geometric situation is as depicted in Figure 25, i.e., N A 1 p , A p or N A p , A + 1 p . Thus, N A p p .
Now, p < n (Lemma 8) and n < n 2 < N by choice of N. In practice, n is a large number, i.e., n 2 is huge compared to n: n n 2 < N . Together:
p N
In this sense, p is a small number, i.e., N A p is small too: N A p . □

3.2.4. Estimating Arguments of Amplitudes of Potential Measurement Results

The next Lemma is the main result of this section for what follows.
Lemma 9.
 
Let y k N p 1 2 , k N p + 1 2 and k 0 , , p 1 . Then:
2 π y A 1 p N , 2 π p y N π , + π
Proof. 
It is y k N p 1 2 , k N p + 1 2 k N p 1 2 y k N p + 1 2 ⇔ (multiply with p) k N p 2 p y k N + p 2 p 2 p y k N p 2
( i )   y p k N p 2 k N y p p 2
By Note 16, it is
( ii )   A 1 p < N     A 1 p N < 1
Furthermore:
( iii )   A 1 p N = A p N p N A N N p N = 1 p N B 1
where (A) is because of Note 17 ( N A p ), and (B) is because of Equation (34) ( p N ).
Next, we compute the lower bound for the first fraction of the claim:
2 π y A 1 p N = 2 π A 1 N y p C 2 π A 1 N k N p 2 = 2 π A 1 k π A 1 p N D π A 1 p N E π
where (C) follows from the second inequation of (i) above, (D) is because of 2 π A 1 k 0 , and (E) is implied by (iii) above.
The upper bound for the first fraction of the claim is computed next:
2 π y A 1 p N = 2 π A 1 N y p F 2 π A 1 N k N + p 2 = 2 π A 1 k + π A 1 p N < G 2 π A 1 k + π < H 2 π A 1 p + π < I 2 π N + π J π
where (F) is implied by the first inequation of (i) above, (G) is (ii) above, (H) follows from the prerequisite k I 0 , , p 1 , i.e., k < p , and (I) is the first inequation of (ii) above. Finally, we will estimate e i φ , and because of e i 2 π N = 1 , (J) is justified.
Together, π 2 π y A 1 p N π , which proves the first claim.   f i r s t f r a c t i o n
Next,
2 π p y N = 2 π N p y K 2 π N k N + p 2 = 2 π k + π p N < L 2 π k + π M π
with (K) from the first inequation of (i) before, (L) because p < N , and (M) because we will estimate e i φ . I.e., the upper bound of the second fraction is as claimed.
The correctness of the lower bound is seen as follows:
2 π p y N = 2 π N p y N 2 π N k N p 2 = 2 π k π p N > O π p N > Q π
with the second inequation of (i) before giving (N), (O) is because of 2 π k > 0 , and (Q) is true because 0 < p < N ; thus, 0 < p / N < 1 . s e c o n d f r a c t i o n  □

3.3. Estimating Probabilities

We are now ready to compute the probability P y that the state |y⟩, which is prepared by the quantum part of Shor’s algorithm, is “close” (i.e., within a distance of 1 / 2 ) to a multiple of p / N .
Lemma 10.
 
Assume q = e i 2 π y p N 1 and let P be the probability that y k N p 1 2 , k N p + 1 2 for a k 0 , , p 1 . Then, P 4 π 2 .
Proof. 
According to Equation (29), the probability P y to measure a particular y k N p 1 2 , k N p + 1 2 is
P y = 1 N A 1 q A 1 q 2
In case q 1 (which is the assumption) where q = e i 2 π y p N (the case q = 1 will be treated separately in Note 18). Thus, with q A = e i 2 π y A p N , it is
1 q A 1 q = 1 q A 1 q = 1 e i 2 π y A p N 1 e i 2 π y p N
The structure of the numerator and denominator recommends the estimation of both by means of the Lemma 7 (Secant Length Estimation). However, applying Lemma 7 requires that 2 π y A p N , 2 π y p N π , + π . By Lemma 9, we know that under the prerequisite y k N p 1 2 , k N p + 1 2 , it is 2 π p y N π , + π as well as   2 π y A 1 p N π , + π , but Lemma 9 does not imply 2 π y A p N π , + π .
Now, consider the following calculation:
1 q A 1 q = 1 q A 1 1 q + q A 1 A 1 q A 1 1 q q A 1 = B 1 q A 1 1 q 1
where (A) holds because of a + b a b , and e i φ = 1 implies (B): q t = e i 2 π y p N t = e i 2 π y p t N = 1 .
Equation (37) allows us to apply the secant length estimation (Lemma 7) because in
1 q A 1 1 q = 1 q A 1 1 q = 1 e i 2 π y A 1 p N 1 e i 2 π y p N
it is now 2 π y A 1 p N , 2 π y p N π , + π according to Lemma 9.
First, we use the second inequation of 2 φ π 1 e i φ φ from Lemma 7 with φ = 2 π y p N π , + π and obtain
1 e i 2 π y p N 2 π y p N
Then, we use the first inequation of 2 φ π 1 e i φ φ from Lemma 7 with φ = 2 π y A 1 p N π , + π and obtain
1 e i 2 π y A 1 p N 2 π 2 π y A 1 p N
Using Equations (39) and (40) in Equation (38) results in
1 q A 1 1 q = 1 e i 2 π y A 1 p N 1 e i 2 π y p N 2 π 2 π y A 1 p N N 2 π y p = 2 A 1 π
This result is now used in Equation (37) (step (C) below) and we obtain
1 q A 1 q = 1 q A 1 1 q 1 C 2 A 1 π 1 = 2 A π 2 π 1 = 2 A π 2 π + 1
Using Equation (42) in Equation (35) (step (D) below) results in
P y = 1 N A 1 q A 1 q 2 D 1 N A 2 A π 2 π + 1 2
= 1 N A 4 A 2 π 2 4 A π 2 π + 1 + 2 π + 1 2
= 1 N A 4 A 2 π 2 8 A π 2 4 A π + 4 π 2 + 4 π + 1
= 4 A π 2 N 8 π 2 N 4 π N + 4 π 2 N A + 4 π N A + 1 N A
4 A π 2 N 8 π 2 N 4 π N = 4 A π 2 N 4 π N 1 + 2 π
Thus,
P y 4 A π 2 N 4 π N 1 + 2 π
According to Note 17, we know N A p A N 1 p , i.e.,
4 A π 2 N 4 π 2 p
Furthermore, since N is a “huge” number, we know that the following is “small”:
4 π N 1 + 2 π = d e f ε
Using Equations (44) and (45) in Equation (43) results in
P y 4 π 2 1 p ε
for each y k N p 1 2 , k N p + 1 2 . According to Corollary 9, there exist p different numbers y k with y k k N p 1 2 , k N p + 1 2 and for each of them P y k 4 π 2 1 p ε . Since we are not interested in a particular y k , but in any of them, we need to sum up all probabilities P y k to obtain the overall probability P :
P = i = 0 p 1 P y i 4 π 2 p ε 4 π 2
This proves the claim. □
We still need to estimate the probability for the case q = 1 .
Note 18.
Let q = 1 . Then P y = A N .
Proof. 
In case q = 1 , the probability is
P y = 1 N A j = 0 A 1 q A 2 = 1 N A j = 0 A 1 1 2 = 1 N A A 2 = A N

3.4. Computing the Period

Let y be the result of the measurement produced by Shor’s algorithm. Under the assumption that q 1 , the following holds:
Theorem 15.
 
With probability  P 4 π 2 , there exists a  k 0 , , p 1 , such that
y N k p < 1 2 p 2
Proof. 
According to Lemma 10, the probability that y k N p 1 2 , k N p + 1 2 for a k 0 , , p 1 is 4 / π 2 .
However, y k N p 1 2 , k N p + 1 2 1 2 y k N p + 1 2 . Dividing the latter inequations by N yields: y k N p 1 2 , k N p + 1 2 1 2 N y N k p + 1 2 N .
Thus, y N k p 1 2 N . By choice of N , it is n 2 < N . Furthermore, p < n p 2 < n 2 p 2 < N 1 N < 1 p 2 . This results in y N k p 1 2 N < 1 2 p 2 . □
Legendre’s Theorem (Theorem 14) proves immediately:
Theorem 16.
 
With probability  4 / π 2 , k / p  is a convergent of  y / N . □

3.4.1. Determining the Period by Convergents: q 1

The Algorithm 2 determines with probability of approximately 4 / π 2 the period p we are looking for; is is applicable in the case q 1 :
Algorithm 2 Determining with probability of approximately 4 / π 2 the period p we are looking for
  • Compute y N > 0 ;
    • The result of the measurement is y and N has been chosen ⇒ y N > 0 can be computed.
  • Compute the continued fraction representation a 0 ; a 1 , , a m of y N ;
  • Compute the convergents a 0 ; a 1 , , a u = g u h u , 1 u m ;
  • Determine h ω with h ω h u for 1 u m and h ω < n
    g ω h ω is a very good approximation of k p because 1 2 h ω 2 1 2 h u 2 ;
  • Thus, h ω p is a candidate for the period p;
  • Check whether p is in fact the period.

3.4.2. Determining the Period by Convergents: q = 1

In case q = 1 , the above algorithm is not applicable. However, q = 1 e 2 π i N p y = 1 p y N p = k N y with k . Thus, the Algorithm 3 can be used:
Algorithm 3 q = 1 e 2 π i N p y = 1 p y N p = k N y   with   k
  • Compute N y > 0 . The result of the measurement is y and N has been chosen ⇒ N y > 0 can be computed;
  • Select k ;
  • Compute k N y ;
  • If k N y , go back to step (2);
  • If k N y n , go back to step (2);
  • p = k N y is a candidate for the period p;
  • Check whether p is in fact the period;
  • If p is not the period:
    a.
    If some predefined termination criterion is met: stop;
    b.
    Go back to step (2).
This may yield the period p but does not guarantee it.

3.5. How the Presented Results Relate

The contribution contains several low-level details. In order to avoid getting lost in these details, this section sketches how the main details contribute to the proof of Shor’s algorithm. The Figure 26 at the end of this section is a cartoon of these relations.

3.5.1. Applying the Results about Continued Factions

Determining a divisor and finally a prime factor of a natural number n can be reduced to finding the period p of the modular exponentiation function f x = a x m o d n for an a with 0 < a < n —see Section 3.2.1 and Note 7.
The quantum part of Shor’s algorithm produces the state 1 N A j = 0 A 1 ω N j p y | y from Equation (2). Measuring this state results in a natural number y .
The natural number N in Equation (2) must be chosen in advance based on the number n to be factorized: it is chosen as N = 2 m with n 2 < N < 2 n 2 —see the introduction of Section 8. This ensures that the relevant arguments to compute the f x by the quantum part of Shor’s algorithm can be captured as quantum states.
Theorem 15 guarantees with probability P 4 / π 2 the existence of a k 0 , , p 1 such that y N k p < 1 2 p 2 .
Thus, according to the convergent criterion of Legendre’s Theorem (Theorem 14), k / p is a convergent of y / N .
The proof of Legendre’s convergent criterion (Theorem 14), in turn, is based on the fact that convergents are exactly the best approximations of the second kind (Theorem 11 and Lagrange’s theorem (Theorem 12)).
The proof of Lagrange’s theorem (Theorem 12—each convergent is a best approximation of the second kind) makes use of the recursion theorem (Theorem 1), the sign theorem (Theorem 2), the monotony property of denominators of convergents (Corollary 1), as well as the estimations of the upper bounds of convergents (Lemma 3) and their lower bounds (Lemma 5).
The proof of Theorem 11 (each best approximation of the second kind is a convergent) makes use of the recursion theorem (Theorem 1), the distance theorem (Theorem 8), the computation of the difference of convergents (Corollary 3), the distance of fractions (Note 4), and the estimation of the upper bounds of convergents (Lemma 3).
The estimations of the lower and upper bounds of convergents depend on the distance theorem (Theorem 8), the computation of the difference of convergents (Corollary 3), the monotony property of denominators of convergents (Corollary 1), and on estimations of the size of denominators of convergents (Lemma 1). The estimation of the lower bounds (Lemma 5) makes use of semiconvergents (Definition 4) and their monotony property (Lemma 4) as well as the nesting theorem (Theorem 7).
Remark: Theorem 13, which proves that best approximations of the first kind are convergents or semiconvergents, is not immediately relevant to Shor’s algorithm and may be ignored when focusing on Shor’s algorithm.

3.5.2. Applying Probability Estimations

According to Equation (29) (which is implied by the Born rule), the probability P y to measure a particular y is P y = 1 N A 1 q A 1 q 2 for q = e i 2 π p y N 1 .
Equations (37) and (38) show that this probability can be estimated as 1 q A 1 q 1 q A 1 1 q 1 = 1 e i 2 π y A 1 p N 1 e i 2 π y p N 1 . The latter fraction, in turn, can be estimated by means of Lemma 7 (Secant Length Estimation) in case 2 π y A 1 p N , 2 π p y N π , + π .
Lemma 9 shows that the latter inclusion is satisfied in case of y k N p 1 2 , k N p + 1 2 and k 0 , , p 1 .
Lemma 10 proves that with probability P y 4 / π 2 it is, in fact, y k N p 1 2 , k N p + 1 2 for k 0 , , p 1 . The proof of this lemma is based on a proper estimation of N (Note 17) which in turn relies on Note 16. Further, it makes use of Corollary 9, which is the summary of the various results of Section 3.2.2.
A simple calculation in the proof of Theorem 15 finally shows that y k N p 1 2 , k N p + 1 2 implies y N k p 1 2 N < 1 2 p 2 . Thus, with probability P y 4 / π 2 , the convergent criterion of Legendre’s Theorem (Theorem 14) is satisfied.

4. Conclusions and Related Work

The literature analyzing, discussing, and refining Shor’s algorithm [1] is vast. Of course, most text books on quantaum computing explain the algorithm too (e.g., [4,5]). In doing so, all this literature puts a sharp focus on the quantum part of the algorithm and sketches its classical parts at various depths. However, the mathematical treatment of the classical aspects is sketchy, omitting most of the details and leaving them as an exercise for the reader with references to corresponding text books from mathematics such as [6] or [7]. The lecture notes by Preskill [2] go a bit deeper, especially on the estimation of probabilities, but still omit the low-level details; however, the authors of the contribution at hand benefited a lot by the treatment in [2]. It is noted that the genesis for the authors’ treatment of probability estimations was inspired by unpublished, non-public work to which the authors had access to several years ago.
In doing so, the contribution at hand is very detailed on the probability estimation of being able to use Legendre’s Theorem in Shor’s algorithm. The authors are not aware of any other publication providing these low-level details.
Furthermore, the contribution at hand is a self-contained treatment on continued fractions up to Legendre’s Theorem. All background that is needed to understand this theorem is presented, including all proofs with low-level details step by step.
The authors hope to foster the comprehension of the classical aspects of Shor’s algorithm even at the level of beginners in quantum computing.

Author Contributions

Writing—original draft, F.L.; Writing—review & editing, J.B. All authors have read and agreed to the published version of the manuscript.

Funding

This work was partially funded by the BMWK project PlanQK (01MK20005N).

Institutional Review Board Statement

Not applicable.

Informed Consent Statement

Not applicable.

Data Availability Statement

Not applicable.

Conflicts of Interest

The authors declare no conflict of interest.

References

  1. Shor, P.W. Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer. SIAM J. Sci. Stat. Comput. 1997, 26, 1484–1509. [Google Scholar] [CrossRef] [Green Version]
  2. Preskill, J. Lecture on Quantum Information—Chapter 6. Quantum Algorithms; California Institute of Technology: Pasadena, CA, USA, 2020; Available online: http://theory.caltech.edu/~preskill/ph219/chap6_20_6A.pdf (accessed on 11 July 2022).
  3. Shult, E.; Surowski, D. Algebra; Springer: Berlin/Heidelberg, Germany, 2015. [Google Scholar]
  4. Nielsen, M.A.; Chuang, I.L. Quantum Computation and Quantum Information; Cambridge University Press: Cambridge, UK, 2016. [Google Scholar]
  5. Rieffel, E.; Polak, W. Quantum Computing: A Gentle Introduction; The MIT Press: Cambridge, MA, USA, 2011. [Google Scholar]
  6. Hardy, G.H.; Wright, E.M. An Introduction to the Theory of Numbers, 4th ed.; Oxford University Press: New York, NY, USA, 1975. [Google Scholar]
  7. Khinchin, A.Y. Continued Fractions, 3rd ed.; The University of Chicago Press: Chicago, IL, USA, 1964. [Google Scholar]
Figure 1. Example of a straightforward computation of a continued fraction.
Figure 1. Example of a straightforward computation of a continued fraction.
Appliedmath 02 00023 g001
Figure 2. Using the Euclidian algorithm to compute a continued fraction.
Figure 2. Using the Euclidian algorithm to compute a continued fraction.
Appliedmath 02 00023 g002
Figure 3. Computing the value of a continued fraction based on Equation (5).
Figure 3. Computing the value of a continued fraction based on Equation (5).
Appliedmath 02 00023 g003
Figure 4. Nesting of the value of a continued fraction by its convergents.
Figure 4. Nesting of the value of a continued fraction by its convergents.
Appliedmath 02 00023 g004
Figure 5. The distance between two succeeding convergents is greater than the distance of a convergent and the value of its continued fraction.
Figure 5. The distance between two succeeding convergents is greater than the distance of a convergent and the value of its continued fraction.
Appliedmath 02 00023 g005
Figure 6. The distance between any two convergents is smaller than the distance between the convergent with the smaller index and its immediate predecessor.
Figure 6. The distance between any two convergents is smaller than the distance between the convergent with the smaller index and its immediate predecessor.
Appliedmath 02 00023 g006
Figure 7. Computing the continued fraction of 2 .
Figure 7. Computing the continued fraction of 2 .
Appliedmath 02 00023 g007
Figure 8. Nesting of convergents and semiconvergents (n even).
Figure 8. Nesting of convergents and semiconvergents (n even).
Appliedmath 02 00023 g008
Figure 9. Any best approximation of the second kind is in the grey shaded area, i.e., greater than or equal to the convergent x 0 .
Figure 9. Any best approximation of the second kind is in the grey shaded area, i.e., greater than or equal to the convergent x 0 .
Appliedmath 02 00023 g009
Figure 10. If a best approximation of the second kind is not a convergent, it is within the indicated grey shaded areas.
Figure 10. If a best approximation of the second kind is not a convergent, it is within the indicated grey shaded areas.
Appliedmath 02 00023 g010
Figure 11. Pictorial representation of Case (2).
Figure 11. Pictorial representation of Case (2).
Appliedmath 02 00023 g011
Figure 12. The potential positions of z / x with respect to the denominators q 0 , , q k .
Figure 12. The potential positions of z / x with respect to the denominators q 0 , , q k .
Appliedmath 02 00023 g012
Figure 13. The potential positions of y 0 x .
Figure 13. The potential positions of y 0 x .
Appliedmath 02 00023 g013
Figure 14. Distances within an interval of semiconvergents (k odd).
Figure 14. Distances within an interval of semiconvergents (k odd).
Appliedmath 02 00023 g014
Figure 15. Distances within an interval of convergents (k even).
Figure 15. Distances within an interval of convergents (k even).
Appliedmath 02 00023 g015
Figure 16. Situation in which a / b is between a convergent and its first semiconvergent (k even).
Figure 16. Situation in which a / b is between a convergent and its first semiconvergent (k even).
Appliedmath 02 00023 g016
Figure 17. The length of a secant is smaller than the arc of the corresponding unit circle.
Figure 17. The length of a secant is smaller than the arc of the corresponding unit circle.
Appliedmath 02 00023 g017
Figure 18. The graphs of sin x and 2x/π.
Figure 18. The graphs of sin x and 2x/π.
Appliedmath 02 00023 g018
Figure 19. Multiples of N are enclosed by immediately succeeding multiples of p.
Figure 19. Multiples of N are enclosed by immediately succeeding multiples of p.
Appliedmath 02 00023 g019
Figure 20. Determining the interval of succeeding multiples of p enclosing a multiple of N.
Figure 20. Determining the interval of succeeding multiples of p enclosing a multiple of N.
Appliedmath 02 00023 g020
Figure 21. No two multiples of N can be enclosed by succeeding multiples of p.
Figure 21. No two multiples of N can be enclosed by succeeding multiples of p.
Appliedmath 02 00023 g021
Figure 22. A multiple of N is always “close” to a multiple of p.
Figure 22. A multiple of N is always “close” to a multiple of p.
Appliedmath 02 00023 g022
Figure 23. Situation in case k N and k + 1 N lying within two consecutive intervals of consecutive multiples of p.
Figure 23. Situation in case k N and k + 1 N lying within two consecutive intervals of consecutive multiples of p.
Appliedmath 02 00023 g023
Figure 24. If k N is in an interval defined by two consecutive multiples of p, the preceding and succeeding intervals do not contain a multiple of N.
Figure 24. If k N is in an interval defined by two consecutive multiples of p, the preceding and succeeding intervals do not contain a multiple of N.
Appliedmath 02 00023 g024
Figure 25. N is embraced by A 1 p and A + 1 p .
Figure 25. N is embraced by A 1 p and A + 1 p .
Appliedmath 02 00023 g025
Figure 26. How the main results of the paper relate.
Figure 26. How the main results of the paper relate.
Appliedmath 02 00023 g026
Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations.

Share and Cite

MDPI and ACS Style

Barzen, J.; Leymann, F. Continued Fractions and Probability Estimations in Shor’s Algorithm: A Detailed and Self-Contained Treatise. AppliedMath 2022, 2, 393-432. https://doi.org/10.3390/appliedmath2030023

AMA Style

Barzen J, Leymann F. Continued Fractions and Probability Estimations in Shor’s Algorithm: A Detailed and Self-Contained Treatise. AppliedMath. 2022; 2(3):393-432. https://doi.org/10.3390/appliedmath2030023

Chicago/Turabian Style

Barzen, Johanna, and Frank Leymann. 2022. "Continued Fractions and Probability Estimations in Shor’s Algorithm: A Detailed and Self-Contained Treatise" AppliedMath 2, no. 3: 393-432. https://doi.org/10.3390/appliedmath2030023

APA Style

Barzen, J., & Leymann, F. (2022). Continued Fractions and Probability Estimations in Shor’s Algorithm: A Detailed and Self-Contained Treatise. AppliedMath, 2(3), 393-432. https://doi.org/10.3390/appliedmath2030023

Article Metrics

Back to TopTop