Reading Week 8 - More on Quantum Algorithms

Ch 6.8: RSA Security

Encryption is the science of converting information into a form that is readable only by the recipient. In modern times, the Data Encryption Standard (DES), meaning the same key is used to encrypt the plaintext and decrypt the cyphertext. DES was replaced later by Advanced Encryption Standard (AES) in 2001. It uses a substitution-permutation network which is a series of linked mathematical operations. Decryption occurs by reversing the order of the network.

We'll focus on the RSA algorithm. It is:

Here the public key is published for all to see, while the private key is only known between the sender and recipient. To use it:

  1. Alice encrypts her message, the plaintext m using the public key
  2. She sends the message, the encoded cyphertext c to Bob.
  3. Bob uses the private key to decipher the message.

It relies on the fact that large-enough prime numbers are difficult to factor, even for a computer. The General Number Field Sieve (GNFS) algorithm is the fastesst well-known classical integer factorization algorithm. It would take the following time to factor these sized-numbers:

6.8.1: Modular Exponentiation

Assume a large composite integer number N with two distinct primes p,q where N=pq. According to number theory, there exists a periodic function F(x) with period r such that:

F(x)=axmodN

where a is an integer, aN, coprime with p or q and coprime with N. The period r=lcm(p1,q1) and is called the Carmichael Totient Function.

Example

For instance, let N=21,p=7,q=3.
Then N=pq=73=21.
Then r=lcm(71,31)=lcm(6,2)=6
Assume a=5. Generating the table:
Pasted image 20240522211534.png
Notice in the table it's periodic every 6-th entry, which agrees with r=6.

Summary

To do the RSA process:

  1. Select two large primes p,q. For example, p=23,q=31.
  2. Calculate N, the composite number, where N=p×q. Here N=23×31=713
  3. Calculate the r period via r=lcm(p1,q1). Here r=lcm(22,30)=330.
  4. Find e public exponent where e is coprime to r (gcd(e,r)=1)while e<r. Here assume e=19 since gcd(19,339)=1
  5. Calculate d, the private exponent using modular inverse, namely d×emodr=1d=e1modr. Here d=191mod330=139.

Now we have the public exponent e=19 and private one d=139. With this, we can try to perform the encryption of the message "A"

6.8.2: Period Estimation

If p,q are known, it is easy to find r. But only if N is known, then it's hard, ensuring the security of RSA. Once r is known, it's easy to calculate d using d=e1modr and RSA fails to be secure. Finding the value of r is called order finding or period estimation. Shor's algorithm is the quantum version of period estimation.

Ch 7.2: Quantum Fourier Transform

7.2.1: Classical Fourier Transformation

The Fourier Transformation (see Lecture Notes#page=113) is a reversible linear transformation. It converts signal g(t) in the time domain into a signal G(f) in the frequency domain. Hence, it has a lot of applications to image and signal processing. The equation (in case you don't use the link above) is:

X(ω)=F(x(t))=x(t)eiωtdt

where ω=2πf, and f is the frequency and t is the time. This equation is the forward transform, but we can do the inverse transform as well:

x(t)=F1(X(ω))=X(ω)eiωtdω
Note

We get, as an output of the Fourier transform, a period function of x(t), namely where x(t)=x(t+T0) where T0 is the period.

We can see that since we have ejωt=cos(ωt)+jsin(ωt) we can conclude that complex sinusoidal functions are the basis functions of the signal. Think of them as unit vectors in a complex plane at a rate of ω radians per second:

Pasted image 20240522213510.png

7.2.2: Discrete Fourier Transform (DFT)

The Discrete Fourier Transform is equivalent to the CFT (Continuous Fourier Transform); however it applies to N samples taken at T time intervals.

Let's assume x(t) is the continuous signal under consideration. We take N samples of this signal x[0], ..., x[N-1]. Each sample x[k] is taken after a time T from the previous sample x[k-1]. the signal is periodic. Thus, we can assume the data in the range x[0], ..., x[N-1] are the same as the data x[N], ..., x[2N-1]. Hence, we can turn:

X(ω)=0(N1)Tx(t)eiωtdt=x[0]ei0+x[1]eiωT++x[k]eiωkT++x[N1]eiω(N1)T

Which becomes the summation:

X(ω)=k=0N1x[k]eiωkT

The sample data is periodic (as we said above), so we should calculate the DFT for the fundamental frequency 2πNTrad/sec and its harmonics:

ω=0,1×2πNT,2×2πNT,...,n×2πNT,1×2πNT,(N1)×2πNT

Applying this equation to the summation above, we get (after normalization):

X[n]=1Nk=0N1x[k]ei2πNnk

where 0nN1. Here X[n] is called the DFT of the sampled data response x[k]. The sampling is done at uniform intervals of time t=0,...,N1. The output X[n] is a vector of complex numbers that encodes the amplitude and phase of a sinusoidal wave of frequency nN:

[X[0]X[1]X[2]X[N1]]=1N[ω0ω0ω0ω0ω0ω1ω2ωN1ω0ω0ω(N1)ω2(N1)ω(N1)(N1)][x[0]x[1]x[2]x[N1]]

where ω=ei2πN. The term 1N is a normalization constant and the DFT is unitary (just like a gate).

The Inverse DFT is given by:

x[k]=1Nk=0N1X[n]ei2πNnk

where 0nN1.

7.2.3: Quantum Fourier Transformation

Recall from the previous sections that the DFT takes in a vector x[N]CN and maps it to another vector X[N]CN. Similarly, the Quantum Fourier Transform acts on a quantum state |x=i=0N1xi|i and maps it into a state i=0N1Xi|i. The transformation is given by:

Xk=1Nn=0N1xnωNkn

where k=0,...,N1 and is called the primitive N-th root of unity. Assuming |x is a basis state, the QFT can be written as the mapping:

|x=1Ny=0N1ωNxy|y

In matrix form it's exactly as what we said the DFT matrix was:

UQFT=1Ny=0N1ωNxy|y,UQFT=1N[ω0ω0ω0ω0ω0ω1ω2ωN1ω0ω0ω(N1)ω2(N1)ω(N1)(N1)]

As an example, assume a qubit in state |ψ=a|0+b|1. For this qubit x0=a and x1=b. We can calculate the QFT as:

X0=12(ae2πi0×02+ae2πi0×12)=12(ae0+be0)=12(a+b)

Similarly, we'd get:

X1==12(ab)

THus then:

UQFT|ψ=12(a+b)|0+12(ab)|1

Which is equivalent to doing an H gate!!!!

Recall that the H gate transforms the Z-basis (the computational basis |0,|1) into the X-basis (the polar basis |+,|). Similarly, we can define the QFT as the transformation from the computational basis to the Fourier basis. With this assumption, we can write our UQFT|ψ for the 2-qubit case as:

UQFT|ψ=a~|0+b~|1

Let's calculate the QFT for large states N=2n. Assume |x=|x1...xn. In this convention, x1 is the most significant bit:

QFTN|x=1Ny=0N1ωNxy|y=12ny=0N1e2πixy/2n|y

We can expand y in the computational basis {0,1}, the range of the sum becomes y1,y2,...,yn{0,1}n:

y=y12n1+y22n2++yn20=i=1nyi2ni

Again, here each yi is some toggle for a bit, either 0 or 1. Using this we get:

QFTN|x=12ny=02n1exp(2πixk=1nyk2nk2n)|y1y2...yn

The exponential of a sum becomes a product of exponentials. With some cancellation:

QFTN|x=12ny=02n1k=1nexp(2πixyk2k)|y1y2...yn

When doing a product of vectors with constants, the constants combine and the vectors tensor up:

i=1nαi|x1...xn=α1α2αn|x1|x2|xn=i=1nαi|xi

Thus:

QFTN|x=12ny=02n1k=1nexp(2πixyk2k)|yk

Then by rearranging the sum and product operators:

QFTN|x=12nk=1n(yk{0,1}exp(2πixyk2k)|yk)

Expanding the sum part:

QFTN|x=12nk=1n(exp(2πx02k)|0+exp(2πix12k)|1)=12nk=1n(|0+exp(2πix2k)|1)

Note that we can expand the exponent part of the previous equation by expanding x as a binary number:

exp(2πix2k)=exp(2πil=1n2nlxl2k)=exp(2πil=1n2nklxl)

Like what we did in expanding the sum above, we can expand this into it's 0,1 parts:

=exp(2πil=1n2nklxl+2πil=nk+1n2nklxl)=exp(2πil=1n2nklxl)×exp(2πil=nk+1n2nklxl)

For the first exponent, notice there loop definition restricts lnk so then nkl0. So then the sum is just some integer, and because exp(2πim)=1 for any mZ+ then the whole expression just becomes 1:

=exp(2πil=nk+1n2nklxl)

Where the internal sum expands as:

l=nk+1n2nklxl=xnk+121+xnk+222++xn2n

Recall that a binary fraction is expanded as:

0.x1x2x3xn=i=1nxi2i

Thus we can put this into our final equation:

QFTN|x=12nk=1n(|0+exp(2πi0.xnk+1xn)|1)

The inverse QFT can be implemented and derived by using UQFT.

Ch 7.11: Quantum Phase Estimation (QPE)

Given an eigenvector |ψ of unitary operator U, such that U|ψ=e2πiθ|ψ , where 0θ1, the goal of this algorithm is to find the eigenvalue e2πiθ for |ψ. This is equivalent to finding the phase θ to some accuracy. This phase estimation algorithm is used in Shor's Algorithm (see the next week for more on that).

Pasted image 20240523134702.png

The quantum phase estimation procedure is similar to the method used to perform arithmetic in the Fourier basis. Each of the successive blocks of the controlled U gates kickbacks a phase θ2n into the upper block, called a counting register. It sums up to a value proportional to the phase factor e2πiθ. An inverse QFT is applied to the counting register to convert this to the computational basis again, which we measure and post-process.

Summary

The steps are:

  1. Start |ψ|0n|ψ.
  2. Apply Hn to the |0n part to put into superpositions. Now 12n(|0+|1)n|ψ.
  3. Apply a cascade of U2i1 gates for each qi qubit.
  4. Apply inverse QFT.

After step 2 we have:

12n(|0+|1)n|ψ

Since U is unitary, then:

U2n|ψ=U2n1e2πiθ|ψ=U2n2e2πiθ21e2πiθ20|ψ

Applying this logic into our previous equation:

12n(|0+|1)e2πiθ2n1|ψ(|0+|1)e2πiθ20|ψ

Rewriting:

12n[(|0+e2πiθ2n1|1)|ψ(|0+e2πiθ20|1)|ψ]12nk=02n1e2πiθk|k|ψ

If we do the QFTN1 as the algorithm describes:

IQFTN|prev12nk=02n1e2πiθk12ny=02n1e2πiky2n|y|ψ12ny=02n1k=02n1e2πiθke2πiky2n|y|ψ12ny=02n1k=02n1exp(2πik2n(y2nθ))|y|ψ

When y=2nθ then the exp makes the whole thing 1, so then:

|2nθ|ψ

Thus if we can measure this vector's probability, then θ can be estimated as we know 2n. The estimation error Δθ is namely 12n since we only get a per-qubit estimation (we can only measure discrete state values). In more mathematical terms, we have the amplitudes αi where:

αi12nk=02n1exp(2πi(θ))

(continue on pg. 348)

Ch 7.9: Shor's Algorithm

I would read Reading Week 8 - More on Quantum Algorithms#Ch 6.8 RSA Security if you haven't already. In there we learned that the period r of a function f(x)axmodN could be used to factorize N into two prime number p,q such that N=p×q. Recall that a was a number that was coprime to N and that:

armodN=1

Peter Shor proposed a novel quantum algorithm to factorize large integers in polynomial time. The largest currently factored integer using this method is 261980999226229. He solved the problem by shifting the problem domain to finding the period of the modular exponentiation function. The problem is as follows:

Problem

Given NZ+ and two prime numbers p,q where p×q=N, let aZN. Let a moldular exponentiation function fa:Z+ZN where fa(x)=axmodN, where fa satisfies the condition where f(x)=f(x0)⇔=x0+kr where r is the period of fa and k is some integer.

Shor's algorithm is trying to determine the period r by querying fa. The block diagram is:

Pasted image 20240530122853.png

An interesting feature of Shor's is the use of inverse QFT to find r. The period r can be used to find the factors for N once known.

The algorithm uses two banks of qubit registers. The upper bank is called the source register while the lower one is the target register. The oracle (shown in green) performs modular exponentiation on the source register and stores the values in the target register. The process is done in one step using quantum parallelism.

The number of qubits s in the source and target must be in the following range:

N22s2N2

Using this range then 2sr>N and it guaruntees that when estimating r that N terms are contributing to the probability amplitude, even if rN2. Using a large s ensures a dense solution space so r can be estimated accurately as possible.

Before starting the algorithm, we need to preprocess some thing. For one, we should check that N is a prime number, using a classical computer (which can be done efficiently). The next thing is to identify a and confirm that N,a are coprime. Lastly, we should determine s (by choosing any suitable value).

Running the Algorithm

We start with both registers in |0 for everything:

|ψ0=|0s|0s

We apply Ns to the source to have an equal superposition:

|ψ1=Hs|0s|0s=12sx=02s1|x|0s

Next we apply modular exponentiation using the oracle. For all states of the source register we calculate yf(x) and store that in the target |y.

7.7 Review

We can calculate axmodN by:

  1. Calculating ax
  2. Dividing by N and giving the remainder

If we have x in its binary form xn1xn2...x0 we can expand the modular exponentiation to:

axmodN=(a20modN)x0(a21modN)x1(a2n1modN)xn1modN

We can do this idea in Quantum Circuits using the modular exponentiation block:

Pasted image 20240608181909.png

The idea here is that each of the controlled multiplier blocks performs a multiplication of the input register 2, by a constant function a2imodN. Each controlled multiplier is controlled by a respective qubit xi in register 1. If we assume |x is the control cubits and |y as the target in register 2, then the operation produces:

CUa2i|x|y=|x(a2i)xymodN

For example, doing 2xmod15 is as follows. Consider the circuit:

Pasted image 20240608182146.png

Here q[0], q[1], q[2] forms |x and the other qubits form |y.

The state after H3 and the X gate is:

|ψ1=123(x=0231|x)|0001

The application of the first block multiplier makes:

|ψ2=CU018(|000|0001+|001|0001+|010|0001+|011|0001+|100|0001+|101|0001+|110|0001+|111|0001=18(|000|0001+|001|0010changed!+|010|0001+|011|0010+|100|0001+|101|0010+|110|0001+|111|0010)

Applying CU1 does something similar:

|ψ3=18(|000|0001+|001|0010+|010|0100changed!+|011|1000+|100|0001+|101|0010+|110|0100+|111|1000)

Applying the last CU2 finishes the process:
Pasted image 20240608183526.png

We see if we try to measure |y that we get only |1000,|0010,|0100,|0001 with equal probability. We can compare these values with the following table and see that the left ket |x makes |y=|2xmod15 as we wanted (keeping in mind the flippded order):

Pasted image 20240608183708.png

Back to the Algorithm

We can used this generalized exponentiation process as follows:

|ψ=12sx=02s1|xf(x)|0s=12sx=02s1|yf(x)=12sx=02s1|f(x)

as we desired. After doing this, we measure the target register. This is because we want to see the point where we get a return back to c=1, if at all. It has the side effect of collapsing our superposition of the source register to the states for which f(x0)=ax0modN. Since f(x) is periodic via r, then the possible values are of the form (x0+kr). Note that x0 is the least x such that f(x) is what was measured in the target register. This ensures k is strictly nonnegative. Thus our new state is:

|ψ=12srk=02sr1|x0+kr|f(x0)

Since the period of f(x) is r, the measurement reduces the range of the source register to 2sr. There are 2s states, so the maximum number of periods we can have is 2sr.

Pasted image 20240608184417.png

But this is why we want to apply the inverse QFT! Because we are guarunteed to get a period for some value k, then we can apply inverse AFT to identify this period r, which is what we are after. This gate does:

|x=1Ny=0N1e2πixyN|y

for x=0,...,N1. Applying the iQFT gives:

|ψ=12s1Qk=0Q1y=02s1(e2πix0y2se2πikry2s)|y

Which (saving you the trouble) reduces down to:

= \frac{1}{\sqrt{2^{s}Q}}\sum\limits_{y=0}^{2^{s}-1} e^{\frac{-2\pi i x_{0} y}{2^{s}}} \sum\limits_{k=0}^{Q-1}\left(\exp\left(-2\pi i \frac{ry}{2^{s}}\right)\right)^{k}\ket{y} $${ #1fdfasdf} The right side summation is a geometric series which is non-zero iff $\exp\frac{-2\pi i ry}{2^{s}}= 1$. This is possible if $\frac{ry}{2^{s}}$ is an integer, so values of $y$ which are multiples of $\frac{2^{s}}{r}$ can have non-zero amplitudes. In other words, if $\frac{2^{s}}{r}$ is an integer, by constructive wave interference, there is a peak values in the value of $y$, which will thus become measured more often. We can try to calculate this probability: { #7dead2}

P(y) = \frac{1}{2^{s}Q}\left| \sum\limits_{k=0}^{Q-1} \exp\left(-2\pi i \frac{kry}{2^{s}}\right)\right|^

When$ry2s$isaninteger,thephasesaddupto1:

P(y) = \frac{r}{2^{s}2^{s}}\left[\frac{2^{s}}{r}\frac{2^{s}}{r}\right] = \frac{1}

We can plug this back into our equation we had for $\ket{\psi'''}$:

\ket{\psi'''} = \frac{1}{\sqrt{r}} \sum\limits_{z=0}^{r-1} \exp\left(-2\pi i \frac{x_{0}z}{r}\right)\left|z \frac{2^{s}}{r}\right\rangle

Thevaluefor$c$,the$axmodN$value,is:

c = z \frac{2^{s}}

Weknow$c$and$2s$thenwecandeterminethat:

\frac{c}{2^{s}} = \frac{z}

Wecantrytoobtain$r$fromhereusingcontinuedfractions.If$z,r$dontsharecommondivisors,namely$gcd(z,r)=1$,then:

\frac{c}{2^{s}} = a_{0} + \frac{1}{a_{1} + \frac{1}{a_{2} + \frac{1}{a_{3} + \dots}}}

Theapproximationtotheratio$zr$canbecalculatedusingthiscontinuedfraction:

\begin{align}
z_{0} &= a_{0} \
r_{0} &= 1 \
z_{1} &= a_{0}a_{1} + 1 \
r_{1} &= a_{1} \
\vdots \
z_{n} &= a_{n}z_{n-1} + z_{n-2} \
r_{n} &= a_{n}r_{n-1} + r_{n-2}
\end