Skip to content
Snippets Groups Projects
Commit 0421d41f authored by Stefan Stump's avatar Stefan Stump
Browse files

Changes to 10.4 and 10.5

parent 933356d4
No related branches found
No related tags found
1 merge request!3Summer25 dev
No preview for this file type
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -42,10 +42,10 @@
</url>
<url>
<loc>https://qis.rwth-aachen.de/teaching/25ss/intro-quantum-computing/script/content/shorsAlgorithm.html</loc>
<lastmod>2025-06-03T15:46:14.791Z</lastmod>
<lastmod>2025-06-03T17:03:57.368Z</lastmod>
</url>
<url>
<loc>https://qis.rwth-aachen.de/teaching/25ss/intro-quantum-computing/script/Introduction-to-Quantum-Computing.pdf</loc>
<lastmod>2025-06-03T15:53:37.480Z</lastmod>
<lastmod>2025-06-03T17:05:56.764Z</lastmod>
</url>
</urlset>
No preview for this file type
This diff is collapsed.
......@@ -4,7 +4,7 @@
\begin{document}
\begin{quantikz}
\lstick{$\ket{j_1}$} &\gate{H} &\gate{R_2} &\gate{R_3} &\qw &\qw &\qw &\swap{2} & \rstick{$\psi_1$} \\
\lstick{$\ket{j_1}$} &\gate{H} &\gate{R_2} &\gate{R_3}\slice{1} &\qw &\qw\slice{\ket{2}} &\qw\slice{3} &\swap{2} & \rstick{$\psi_1$} \\
\lstick{$\ket{j_2}$} &\qw &\ctrl{-1} &\qw &\gate{H} &\gate{R_2} &\qw &\qw & \rstick{$\psi_2$} \\
\lstick{$\ket{j_3}$} &\qw &\qw &\ctrl{-2} &\qw &\ctrl{-1} &\gate{H} &\targX{} & \rstick{$\psi_3$}
\end{quantikz}
......
......@@ -152,7 +152,7 @@ So far we have seen the DFT to analyze the period of a quantum state, we have se
::: {.therorem #thm-shor-post-process}
##
Iff $f: \{0,1\}^n \rightarrow \{0,1\}^m$ is $r$-periodic, the following holds with probability $\Omega(1/\log\log r)$:
Iff $f: \{0,1\}^n \rightarrow \{0,1\}^m$ is $r$-periodic, the following holds with probability $\Omega(\frac{1}{\log\log r})$:
$$
\frac{-r}{2} \leq rc\bmod 2^n \leq \frac{r}{2}
$$
......@@ -160,25 +160,23 @@ where $c$ is the output of the second measurement of the quantum circuit describ
:::
:::
We assume that the theorem holds for our outcome $c$ of the second measurement (if that is not the case, our result will be wrong and we can just run the quantum algorithm again to get a different outcome):
We assume that the theorem holds for our outcome $c$ of the second measurement (if that is not the case, our result will be wrong and we can just run the quantum algorithm again to get a different outcome) and $R$ is an upper bound on $r$:
Then exists an integer $d$ such that:
$$
\begin{aligned}
&\lvert rc - d2^n\rvert \leq \frac{r}{2} \\
\iff&\lvert \frac{c}{2^n} - \frac{d}{r}\rvert \leq \frac{1}{2^{n+1}} && |\text{ division by } r\cdot 2^n
&\lvert rc - d2^n\rvert \leq \frac{r}{2} && ||\text{ divide by } r\cdot 2^n\\
\iff&\lvert \frac{c}{2^n} - \frac{d}{r}\rvert \leq \frac{1}{2^{n+1}}
\end{aligned}
$$
The fraction $\frac{c}{2^n}$ is known, so the goal is to find a fraction $\frac{d}{r}$ that is $\frac{1}{2^{n+1}}$-close to $\frac{c}{2^n}$.
The fraction $\varphi \coloneqq \frac{c}{2^n}$ is known, but the fraction $\frac{d}{r}$ is unknown. All we know is that it is a fraction and that denominator is $\leq R$. The goal is to find a fraction $\frac{1}{2^{n+1}}$-close to $\frac{c}{2^n}$.
Since $n$ is the number of qubits used in the quantum circuit and was chosen, such that $n \geq 2\log_2(r)$ and thus $2^{n} \geq r^2$ holds and from this we know that $\frac{1}{2^{n+1}} \leq \frac{1}{2r^2}$ holds as well.
So if @thm-shor-post-process holds, we now $\lvert \frac{c}{2^n} - \frac{d}{r} \rvert \leq \frac{1}{2r^2}$ also holds. Our task is now rewritten to find $\frac{d}{r}$ under this condition. For this we use another theorem:
For this we use another theorem:
::: {.callout-note appearance="minimal" icon=false}
::: {.theorem #thm-shor-post-process-frac}
For a given real number $\varphi \geq 0$ and integer $q > 0$ there is at most one fraction $\frac{d}{r}$ with $r \leq q$ and $\lvert \varphi - \frac{d}{r} \rvert \leq \frac{1}{2q}$. In this case, this $\frac{d}{r}$ is a convergent of the continued fraction expansion of $\varphi$.
Under the conditions $r^2 \leq 2^n$ and $2^n$ is in the order of $R^2$, the fraction $\frac{d}{r}$ is a convergent of the continued fraction expansion of $\frac{c}{2^n}$, where are $c,d,r$ and $n$ are the variables defined above.
:::
:::
......@@ -228,44 +226,41 @@ This completes the postprocessing of Shor's algorithm.
## Constructing the DFT
So far we have described everything necessary for Shor's algorithm, but only described the matrix representation of the $\operatorname{DFT}_M$. We will now take a closer look into implementing the $\operatorname{DFT}_M$ as a quantum circuit. Since we only use the $\operatorname{DFT}_M$ for Shor's algorithm so far, we will only look at $M=2^n$, which is the $\operatorname{DFT}$ applied on $n$ qubits.
So far we have described everything necessary for Shor's algorithm, but only described the matrix representation of the $\operatorname{DFT}_N$. We will now take a closer look into implementing the $\operatorname{DFT}_M$ as a quantum circuit. Since we only use the $\operatorname{DFT}_N$ for Shor's algorithm so far, we will only look at $N=2^n$, which is the $\operatorname{DFT}$ applied on $n$ qubits.
To start the circuit, we recall the definition of the $\operatorname{DFT}_{2^n}$ from @def-shor-dft: $\operatorname{DFT}_{2^n} := \frac{1}{\sqrt{{2^n}}} (\omega^{kl})_{kl}$ with $\omega:= e^{2\pi i / 2^n}$. To apply the $\operatorname{DFT}_{2^n}$ to a quantum state $\ket{j}$ we calculate
$$
\operatorname{DFT}_{2^n}\ket{j} = \frac{1}{\sqrt{{2^n}}} \sum_k e^{2\pi i j k 2^{-n}} \ket{k}
$$
We can rewrite this as follows:
To start the circuit, we recall the definition of the $\operatorname{DFT}_N$ from @def-shor-dft: $\operatorname{DFT}_N := \frac{1}{\sqrt{{2^n}}} (\omega^{kl})_{kl}$ with $\omega:= e^{2\pi i / 2^n}$. To apply the $\operatorname{DFT}_N$ to a quantum state $\ket{j}$ we calculate
$$
\begin{aligned}
\operatorname{DFT}_{2^n}\ket{j} =& \frac{1}{\sqrt{{2^n}}} \sum_k e^{2\pi i j k 2^{-n}} \ket{k}\\
=& \frac{1}{\sqrt{{2^n}}} \sum_{k_1} \dots \sum_{k_n} e^{2\pi i j (\sum_l k_l 2^{-l})} \ket{k_1 \dots k_n}\\
=& \frac{1}{\sqrt{{2^n}}} \sum_{k_1} \dots \sum_{k_n} \bigotimes^n_{l=1} e^{2\pi i j (k_l 2^{-l})} \ket{k_l}\\
=& \frac{1}{\sqrt{{2^n}}} \bigotimes_{l=1}^n \sum_{k_l} e^{2\pi i j (k_l 2^{-l})} \ket{k_l}\\
=& \frac{1}{\sqrt{{2^n}}} \bigotimes_{l=1}^n (\ket{0} + e^{2\pi i j 2^{-l}} \ket{1})\\
=& \frac{1}{\sqrt{{2^n}}} \bigotimes_{l=1}^n (\ket{0} + e^{2\pi i 0.j_{n-(l-1)} \dots j_{n}} \ket{1})\\
=& \bigotimes_{l=1}^n \frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_{n-(l-1)} \dots j_{n}} \ket{1})
\operatorname{DFT}_{N}\ket{j}
=& \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} \omega^{jk} \ket{k}\\
=& \frac{1}{\sqrt{N}} \sum_{k=0}^{N-1} e^{2\pi i j k / N} \ket{k}\\
=& \frac{1}{\sqrt{N}} \sum_{k_1 \in \{0,1\}} \dots \sum_{k_n \in \{0,1\}} e^{2\pi i j (\sum_{l=1}^n k_l 2^{-l})} \ket{k_1 \dots k_n}\\
=& \frac{1}{\sqrt{N}} \sum_{k_1 \in \{0,1\}} \dots \sum_{k_n \in \{0,1\}} \bigotimes_{l=1}^n e^{2\pi i j k_l 2^{-l}} \ket{k_l}\\
=& \frac{1}{\sqrt{N}} \bigotimes_{l=1}^n \sum_{k_l \in \{0,1\}} e^{2\pi i j k_l 2^{-l}} \ket{k_l}\\
=& \frac{1}{\sqrt{N}} \bigotimes_{l=1}^n (\ket{0} + e^{2\pi i j 2^{-l}} \ket{1})\\
=& \frac{1}{\sqrt{N}} \bigotimes_{l=1}^n (\ket{0} + e^{2\pi i 0.j_{n-l+1} \dots j_{n}} \ket{1}).
\end{aligned}
$$
The expression $0.j$ expresses a binary fraction (e.g. $0.101 = \frac{1}{2} + \frac{1}{8} = \frac{5}{8}$).
With this we have shown, that we can write $\operatorname{DFT}_{2^n}\ket{j}$ as the following tensor product of quantum states
With this we have shown, that we can write $\operatorname{DFT}_N \ket{j}$ as the following tensor product of quantum states
$$
\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_n} \ket{1}) \otimes \frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_{n-1}j_n} \ket{1}) \otimes \dots \otimes \frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1\dots j_n} \ket{1})
\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_n} \ket{1}) \otimes \frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_{n-1}j_n} \ket{1}) \otimes \dots \otimes \frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1\dots j_n} \ket{1}).
$$
From this rewritten tensor product, we can get an idea on how to construct the quantum circuit for the $\operatorname{DFT}_{2^n}$. Namely, we can construct a quantum circuit for each element of the tensor product and from this build the general circuit.
From this rewritten tensor product, we can get an idea on how to construct the quantum circuit for the $\operatorname{DFT}_N$. Namely, we can construct a quantum circuit for each element of the tensor product and from this build the general circuit.
For this, we segment the tensor product into different elements $\psi$ as follows:
$$
\underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_n} \ket{1})}_{\psi_1} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_{n-1}j_n} \ket{1})}_{\psi_2} \otimes \dots \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1\dots j_n} \ket{1})}_{\psi_n}
\underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_n} \ket{1})}_{\psi_1} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_{n-1}j_n} \ket{1})}_{\psi_2} \otimes \dots \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1\dots j_n} \ket{1})}_{\psi_n}.
$$
We also introduce a new gate $R_k$ which is defined by the following matrix:
$$
R_k:=\begin{pmatrix} 1 & 0 \\ 0 & e^{2 \pi i / 2^k} \end{pmatrix}
R_k:=\begin{pmatrix} 1 & 0 \\ 0 & e^{2 \pi i / 2^k} \end{pmatrix}.
$$
To understand the construction of the circuit from these elements, we will look at an example for $n=3$ first:
......@@ -277,21 +272,21 @@ To understand the construction of the circuit from these elements, we will look
We start by building the tensor product for $n=3$. The input for the DFT circuit is $\ket{j} = \ket{j_1j_2j_3}$. Using the formula from above, we can write the result of $\operatorname{DFT}_{2^3} \ket{j}$ as the following tensor product:
$$
\underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_3} \ket{1})}_{\psi_1} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_2j_3} \ket{1})}_{\psi_2} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1j_2j_3} \ket{1})}_{\psi_3}
\underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_3} \ket{1})}_{\psi_1} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_2j_3} \ket{1})}_{\psi_2} \otimes \underbrace{\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_1j_2j_3} \ket{1})}_{\psi_3}.
$$
First we construct the $\psi_3$ element. Contrary to the intuition, we use the top wire containing $\ket{j_1}$ for this. We use a Hadamad-gate to bring $\ket{j_1}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + (-1)^{j_1} \ket{1}) = \frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1} \ket{1})$. This looks close to $\psi_3$ already, we now need to add the last two decimal places $j_2j_3$ to the state. For this we use $R_2$ and $R_3$. We apply $R_2$ controlled by the wire $j_2$ and $R_3$ controlled by the wire $j_3$. This means, that we only apply the $R$-gate, if the corresponding wire contains a $1$. You can see this written as a quantum circuit at the figure below. After applying $R_2$ we have the state $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1j_2} \ket{1})$ and after applying $R_3$ we have the state $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1j_2j_3} \ket{1})$ on the top wire. This is the same as $\psi_3$, so we are done on the first wire (We are at slice 3 in the figure below at this point).
![The DTF for three qubits](img/dft_3_qubits){width=100%}
The next step is to construct the $\psi_2$ state on the middle wire. We again use a Hadamad-gate to bring $\ket{j_2}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_2} \ket{1})$. We now need to include the last decimal point $j_3$, for which we use $R_2$ again, this time controlled by $j_3$. The resulting superposition is now $\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_2j_3} \ket{1})$, which is $\psi_2$.
First we construct the $\psi_3$ element. Contrary to the intuition, we use the top wire containing $\ket{j_1}$ for this. We use a Hadamad-gate to bring $\ket{j_1}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + (-1)^{j_1} \ket{1}) = \frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1} \ket{1})$. This looks close to $\psi_3$ already, we now need to add the last two decimal places $j_2j_3$ to the state. For this we use $R_2$ and $R_3$. We apply $R_2$ controlled by the wire $j_2$ and $R_3$ controlled by the wire $j_3$. This means, that we only apply the $R$-gate, if the corresponding wire contains a $1$. You can see this written as a quantum circuit at the figure below. After applying $R_2$ we have the state $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1j_2} \ket{1})$ and after applying $R_3$ we have the state $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_1j_2j_3} \ket{1})$ on the top wire. This is the same as $\psi_3$, so we are done on the first wire (We are at the first slice in the figure).
On the bottom wire, we can just do a Hadamad-gate to bring $\ket{j_3}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_3} \ket{1})$. We then have $\psi_1$ on the bottom wire. The full circuit is described in this figure:
The next step is to construct the $\psi_2$ state on the middle wire. We again use a Hadamad-gate to bring $\ket{j_2}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_2} \ket{1})$. We now need to include the last decimal point $j_3$, for which we use $R_2$ again, this time controlled by $j_3$. The resulting superposition is now $\frac{1}{\sqrt{2}}(\ket{0} + e^{2\pi i 0.j_2j_3} \ket{1})$, which is $\psi_2$. (We are at the second slice in the figure).
![The DTF for three qubits](img/dft_3_qubits){width=100%}
On the bottom wire, we can just do a Hadamad-gate to bring $\ket{j_3}$ into the superposition $\frac{1}{\sqrt{2}}(\ket{0} + e^{2 \pi i 0.j_3} \ket{1})$. We then have $\psi_1$ on the bottom wire. (We are at the third slice in the figure).
When applying this circuit, we get the state $\psi_3 \otimes \psi_2 \otimes \psi_1$ as a result. This very close to our desired state $\psi_1 \otimes \psi_2 \otimes \psi_3$, just the order of the wires is flipped. To solve this, we apply a $\operatorname{SWAP}$ onto all wires, which flips the order of the wires an delivers the correct output for $\operatorname{DFT}_{2^3}$.
:::
The more general approach to construct the $\operatorname{DFT}_{2^n}$ as a quantum circuit with $n$ qubits works as follows:
The more general approach to construct the $\operatorname{DFT}_N$ as a quantum circuit with $n$ qubits ($N = 2^n$) works as follows:
1. Initialize wires with input $\ket{j}$, so that $\ket{j_1}$ is on the top wire and $\ket{j_n}$ is on the bottom wire. Note, that this is not part of the circuit yet.
2. Start with the top wire. For each wire $j_i$ do the following:
......@@ -301,6 +296,6 @@ The more general approach to construct the $\operatorname{DFT}_{2^n}$ as a quant
Note: If the the output of the DFT circuit is measured right after applying it (as in Shor's algorithm) or if the rest of the algorithm allows for it, it is more efficient to perform the $\operatorname{SWAP}$ classically, since this is considered to be the cheaper operation.
The more general layout of the quantum circuit for the $\operatorname{DFT_{2^n}}$ with the $\operatorname{SWAP}$ is shown in this figure.
The more general layout of the quantum circuit for the $\operatorname{DFT_N}$ with the $\operatorname{SWAP}$ is shown in this figure.
![The DTF for $n$ qubits](img/dft_n_qubits){width=100%}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment