diff --git a/css/grover-figures.css b/css/grover-figures.css index c152cfb..301156f 100644 --- a/css/grover-figures.css +++ b/css/grover-figures.css @@ -26,13 +26,83 @@ div.svg-fig svg#fig1 #tooltip text { font-size: 13.5px; } -.graph-rect { +div.svg-fig svg#fig2 .graph-rect { fill: black; opacity: 0.85; } -.mean-line { +div.svg-fig svg#fig2 .mean-line { stroke-width: 0.85; stroke: red; stroke-dasharray: 3, 1; +} + +.info-div { + text-align: right; + background-color: #F0F0F0; + display: inline-block; + float: right; + padding: 3px 5px; + border-radius: 2px; + pointer-events: none; +} + +#steps-list { + float: left; + width: 600px; + display: block; + padding: 0; + padding-left: 35px; +} + +#steps-container { + width: 110%; + position: relative; + overflow: hidden; + display: block; + padding: 0; + margin-top: 0; + margin-bottom: 0; +} + +#fig2-container { + float: left; + width: 580px; + margin-left: 45px; + padding: 0; + opacity: 0; +} + +#fig2-container.graph-scroll-fixed { + position: fixed; + display: block; + top: 30%; + left: calc(600px - 150px + 45px + 35px + 12.5%); +} + +#fig2-container.graph-scroll-below { + position: absolute; + bottom: 0px; + margin-left: calc(600px - 150px + 35px + 32px + 12.5%); +} + +.play-icon { + margin: 0 0 -0.6rem 0; + opacity: 0.6; + width: 1.85rem; + display: inline-block; +} + +a.play-icon.in-list { + float: right; + margin-right: 15px; +} + +a.play-icon.in-list:hover { + opacity: 0.8; +} + +a.play-icon.in-list:active { + width: 2.1rem; + margin-right: 12px; } \ No newline at end of file diff --git a/css/tufte.css b/css/tufte.css index 7b09d6c..cf53c19 100644 --- a/css/tufte.css +++ b/css/tufte.css @@ -8,7 +8,8 @@ body { width: 87.5%; font-family: 'Computer Modern Serif'; padding-bottom: 20rem; color: #000; /* Changed to black */ - max-width: 1200px; } + max-width: 1200px; + overflow-x: hidden; } h1 { font-family: 'Computer Modern Serif'; font-weight: bold; diff --git a/fig2work.html b/fig2work.html index 4114d01..6b28bd8 100644 --- a/fig2work.html +++ b/fig2work.html @@ -33,7 +33,7 @@ +
-Grover’s algorithm is a quantum search algorithm, discovered by Lov Grover [1]. A search algorithm, given some database of things, aims to find one specific item - a “needle in a haystack”.
++ Grover’s algorithm is a quantum search algorithm, discovered by Lov Grover [1]. A search algorithm, given some database of things, aims to find one specific item - a “needle in a haystack”. +
+One of many full descriptions of the algorithm is given in the excellent book by Nielsen and Chuang [2] but we summarise it briefly here. We set up the algorithm as follows.
-We say that there are \(N\) items in our database, and we index them accordingly (although their order is unimportant) by the integers in the range \(0\) to \(N-1\), calling this index \(x\). We can represent any value of \(x\) with \(n\) classical bits11. We do this by using the ordering of the sequence 0s and 1s; each consecutive digit in the sequence represents an decreasing (starting from \(n-1\)) power of 2, and the integer is given by the sum of these powers of 2 multiplied by the digit. An example makes this clear: \[ 0101 \rightarrow 0 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 4 + 1 = 5 \] , where \(2^{n-1} \leq N \leq 2^n - 1\), and so for convenience we choose \(N\) to be some integer power \(n\) of 2. We identify the specific item we want - the needle - as having the index \(x_0\) and define an oracle function \(f\):
-\[f(x)= -\begin{cases} -1 & x = x_0 \\ -0 & x \neq x_0 -\end{cases}\]
-And so the search problem is to apply the oracle to successive values of \(x\) until it returns 1 (the needle is found). For our interactive demonstration, select a needle in Fig. 1.
++ One of many full descriptions of the algorithm is given in the excellent book by Nielsen and Chuang [2] but we summarise it briefly here. We set up the algorithm as follows. +
+ ++ We say that there are \(N\) items in our database, and we index them accordingly (although their order is unimportant) by the integers in the range \(0\) to \(N-1\), calling this index \(x\). We can represent any value of \(x\) with \(n\) classical bits1 + + + 1. We do this by using the ordering of the sequence 0s and 1s; each consecutive digit in the sequence represents an decreasing (starting from \(n-1\)) power of 2, and the integer is given by the sum of these powers of 2 multiplied by the digit. An example makes this clear: + + \[ 0101 \rightarrow 0 \cdot 2^3 + 1 \cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 4 + 1 = 5 \] + + + + , where \(2^{n-1} \leq N \leq 2^n - 1\), and so for convenience we choose \(N\) to be some integer power \(n\) of 2. We identify the specific item we want - the needle - as having the index \(x_0\) and define an oracle function \(f\): +
++ + \[f(x)= + \begin{cases} + 1 & x = x_0 \\ + 0 & x \neq x_0 + \end{cases}\] + +
++ And so the search problem is to apply the oracle to successive values of \(x\) until it returns 1 (the needle is found). For our interactive demonstration, select a needle in Fig. 1. +
As this is a quantum algorithm, we need to make the oracle work on quantum states and so we define an oracle operator \(O\):
-\[O( \ket{x} \ket{b} ) = \ket{x} \ket{b \oplus f(x)}\]
-Where \(\ket{b}\) is the oracle qubit and \(\oplus\) denotes binary modular addition22. Modular addition is like normal addition except the number line is periodic - when we reach a certain value we go back to the beginning. For example, on a 12-hour clock if we add 6 hours to 11 then rather than getting 17, we get \(11 + 6 \pmod {12} = 5\). Our oracle operator, as with most of information theory, uses addition modulo 2 - the only numbers we’re allowed are 0 and 1! For example, \(1 \oplus 0 = 1 \pmod 2 = 1\), but \(1 \oplus 1 = 2 \pmod 2 = 0\)..
-Now we perform the first “quantum” step: say that we put the oracle qubit \(\ket{b}\) in the \(\ket{-}\) state, equal to \((\ket{0} - \ket{1})/\sqrt{2}\). Now the action of the oracle operator is:
-\[\begin{align} -O( \ket{x} \ket{-} ) & = \frac{1}{\sqrt{2}} \ket{x} \left( \ket{0 \oplus f(x)} - \ket{1 \oplus f(x)} \right) \\ -& = (-1)^{f(x)} \ket{x} \left( \frac{\ket{0} - \ket{1}}{\sqrt{2}} \right) \\ -& = (-1)^{f(x)} \ket{x} \ket{-} -\end{align}\]
-So, for the special case of the oracle qubit being \(\ket{-}\), we can say that the oracle operator flips the phase of the index bit \(\ket{x}\) if and only if it is the needle (ie \(x=x_0\)). As the oracle qubit is unchanged by this operation, we can effectively ignore it.
++ As this is a quantum algorithm, we need to make the oracle work on quantum states and so we define an oracle operator \(O\): +
++ + \[O( \ket{x} \ket{b} ) = \ket{x} \ket{b \oplus f(x)}\] + +
++ Where \(\ket{b}\) is the oracle qubit and \(\oplus\) denotes binary modular addition2 + + + 2. Modular addition is like normal addition except the number line is periodic - when we reach a certain value we go back to the beginning. For example, on a 12-hour clock if we add 6 hours to 11 then rather than getting 17, we get \(11 + 6 \pmod {12} = 5\). Our oracle operator, as with most of information theory, uses addition modulo 2 - the only numbers we’re allowed are 0 and 1! For example, \(1 \oplus 0 = 1 \pmod 2 = 1\), but \(1 \oplus 1 = 2 \pmod 2 = 0\). + + + . +
++ Now we perform the first “quantum” step: say that we put the oracle qubit \(\ket{b}\) in the \(\ket{-}\) state, equal to \((\ket{0} - \ket{1})/\sqrt{2}\). Now the action of the oracle operator is: +
++ + \[\begin{align} + O( \ket{x} \ket{-} ) & = \frac{1}{\sqrt{2}} \ket{x} \left( \ket{0 \oplus f(x)} - \ket{1 \oplus f(x)} \right) \\ + & = (-1)^{f(x)} \ket{x} \left( \frac{\ket{0} - \ket{1}}{\sqrt{2}} \right) \\ + & = (-1)^{f(x)} \ket{x} \ket{-} + \end{align}\] + +
++ So, for the special case of the oracle qubit being \(\ket{-}\), we can say that the oracle operator flips the phase of the index bit \(\ket{x}\) if and only if it is the needle (ie \(x=x_0\)). As the oracle qubit is unchanged by this operation, we can effectively ignore it. +
+Now that we have set up the search problem and provided an operator that can change the phase of the needle qubit, we can give the procedure for the Grover search33. We use the obvious extension of the binary notation explained in the first sidenote: \[\ket{x} \equiv \ket{x_1 x_2 \cdots x_n}\] where \[x = x_1 2^{n-1} + x_2 2^{n-2} + \cdots + x_n 2^0\] For example, \(\ket{5} \equiv \ket{0101}\). The important thing here is to remember that some state \(\ket{x}\) is not a state of a single qubit; it is a state of \(n\) qubits.. Steps 1 and 2 are just to get the system in an appropriate starting state, and step 3 is where the “magic” happens.
-The repeated sign changes of the needle state combined with the flips about the mean increase the needle state’s amplitude compared to the rest of the haystack. When this is sufficiently high, a measurement on the haystack qubits will return the needle state with high probability (\(\propto [\text{needle amplitude}]^2\)).
+ ++ Now that we have set up the search problem and provided an operator that can change the phase of the needle qubit, we can give the procedure for the Grover search3 + + + 3. We use the obvious extension of the binary notation explained in the first sidenote: + + + \[\ket{x} \equiv \ket{x_1 x_2 \cdots x_n}\] + + + where + + + \[x = x_1 2^{n-1} + x_2 2^{n-2} + \cdots + x_n 2^0\] + + + For example, \(\ket{5} \equiv \ket{0101}\). The important thing here is to remember that some state \(\ket{x}\) is not a state of a single qubit; it is a state of \(n\) qubits. + + + . Steps 1 and 2 are just to get the system in an appropriate starting state, and step 3 is where the “magic” happens. +
+ ++ + 5. Applying steps i-iv is equivalent to applying: + + + \[\begin{align} + & \: H^{\otimes n}(2 \ket{0} \bra{0} - I)H^{\otimes n} O \\ + = & \: (2 H^{\otimes n} \ket{0} \bra{0} H^{\otimes n} - H^{\otimes n} H^{\otimes n}) O + \end{align}\] + + + We then recall that in this case \(\ket{0}\) is really representing an \(n\)-qubit state \(\ket{00 \cdots 0}\), and also that \(H \ket{0} = (\ket{0} + \ket{1})/\sqrt{2}\), so that: + + + \[\begin{align} + H^{\otimes n} \ket{0} & = \overbrace{(H \ket{0}) \otimes (H \ket{0}) \otimes \cdots \otimes (H \ket{0})}^{n \text{ times}} \\ + & = \frac{1}{\left(\sqrt{2}\right)^n} \Big( [\ket{0} + \ket{1}] \otimes [\ket{0} + \ket{1}] \otimes \cdots \otimes [\ket{0} + \ket{1}] \Big) \\ + & = \frac{1}{\sqrt{2^n}} \sum \big\{ \text{All permutations of } n \text{ 1s and 0s} \big\} \\ + & = \frac{1}{\sqrt{N}} \sum_{x=0}^{N-1} \ket{x} = \ket{\phi} + \end{align}\] + + + Finally, remember that \(\bra{\chi} A^\dagger = (A \ket{\chi})^\dagger\), that \(H = H^\dagger\), and that \(H^2 = I\). Then we can write: + + + \[\begin{align} + & \: (2 H^{\otimes n} \ket{0} \bra{0} H^{\otimes n} - H^{\otimes n} H^{\otimes n}) O \\ + = & \: (2 (H^{\otimes n} \ket{0})(H^{\otimes n} \ket{0})^\dagger - I) O \\ + = & \: (2 \ket{\phi} \bra{\phi} - I) O \qquad \qquad \qquad \qquad \Box + \end{align}\] + + +
+We said that it is necessary to apply step 3 some number \(R\) of times in order to obtain the needle state with sufficiently high probability. We’ll now look at what value \(R\) might take.
-First, write our initial state as: \[\ket{\psi} = \nu_0 \ket{x_0} + \sum_{x \neq x_0} \eta_0 \ket{x}\] Where the subscripts on \(\nu\) and \(\eta\) denote the number of Grover iterations performed. Referring to Step 2 of the above, note that \(\nu_0 = \eta_0 = 1/\sqrt{N}\). Now consider the action of the Grover iteration on the \(\nu_j\) and \(\eta_j\):
-It is rather difficult to solve these recurrence relations, but their solutions are given in Ref [3]. The solutions are: \[\begin{align} \nu_j & = \sin([2j+1]\theta) \\ \eta_j & = \frac{1}{\sqrt{N-1}}\cos([2j+1]\theta) \end{align}\] Where \(\sin^2 \theta = 1/N\). The validity of these solutions can be proven by induction66. We prove as follows.
Basis case. Set \(j=0\). Now insert into the solution: \[\begin{align} \nu_0 & = \sin(\theta) \\
-& = \sin(\arcsin(\frac{1}{\sqrt{N}})) \\
-& = \frac{1}{\sqrt{N}} \\
-\eta_0 & = \frac{1}{\sqrt{N-1}}\cos(\theta) \\
-& = \frac{1}{\sqrt{N-1}}\cos(\arcsin(\frac{1}{\sqrt{N}})) \\
-& = \frac{1}{\sqrt{N-1}} \sqrt{1 - \frac{1}{N}} \\
-& = \frac{1}{\sqrt{N-1}} \sqrt{\frac{N-1}{N}} = \frac{1}{\sqrt{N}}
-\end{align}\] Which satisfy the boundary conditions we noted in the main text.
Inductive step. Assume the solution is valid for \(j\), now test for \(j+1\): \[\begin{align} \nu_{j+1} & = \sin([2(j+1)+1]\theta) \\
-& = \sin([2j + 1]\theta + 2\theta) \\
-& = \sin([2j + 1]\theta) \cos(2\theta) + \cos([2j + 1]\theta) \sin(2 \theta) \\
-& = \nu_j (\cos^2\theta - \sin^2\theta) + \sqrt{N-1} \eta_j (2 \cos\theta \sin\theta) \\
-& = \nu_j (\frac{N-1}{N} - \frac{1}{N}) + \sqrt{N-1} \eta_j (2 \sqrt{\frac{N-1}{N}} \frac{1}{\sqrt{N}}) \\
-& = \frac{N-2}{N} \nu_j + \frac{2(N-1)}{N} \eta_j \\
-\eta_{j+1} & = \frac{1}{\sqrt{N-1}} \cos([2(j+1) + 1]\theta) \\
-& = \frac{1}{\sqrt{N-1}} \cos([2j + 1]\theta + 2 \theta) \\
-& = \frac{1}{\sqrt{N-1}} \big( \cos([2j+1]\theta) \cos(2\theta) - \sin([2j+1]\theta) \sin(2\theta) \big) \\
-& = \frac{1}{\sqrt{N-1}} ( \sqrt{N-1} \eta_j \frac{N-2}{N} - 2 \nu_j \frac{\sqrt{N-1}}{N} ) \\
-& = -\frac{2}{N} \nu_j + \frac{N-2}{N} \eta_j
-\end{align}\] Q.E.D.. As we want to maximise the probability of measuring the needle, we clearly want to maximise \(\nu_j\). The maximum of \(\sin(x)\) is at \(x = \pi/2\) and so we want \((2j + 1)\theta = \pi/2\). Considering that \(j\) must be an integer, we want: \[j = \left \lfloor \frac{\pi - 2\theta}{4\theta} \right \rfloor\] Now see that since \(\sin\theta \approx \theta\) for small \(\theta\) and recalling that \(\sin\theta = 1/\sqrt{N}\): \[j \approx \left \lfloor \frac{\pi - 2/\sqrt{N}}{4/\sqrt{N}} \right \rfloor \approx \left \lfloor \frac{\pi}{4}\sqrt{N} \right \rfloor\] And thus we choose \(R\) to be the above value. Note an interesting - and characteristically quantum - implication of the sinusoidal form for \(\nu_j\): we can perform too many iterations of the algorithm! In the classical world we’d expect our extra work to reap us some reward (or at least not to make things worse) but here the performance of the algorithm is periodic in \(R\) so we need to stop at exactly the right point in order to find our needle.
+ We said that it is necessary to apply step 3 some number \(R\) of times in order to obtain the needle state with sufficiently high probability. We’ll now look at what value \(R\) might take. +
+ ++ First, write our initial state as: + + + \[\ket{\psi} = \nu_0 \ket{x_0} + \sum_{x \neq x_0} \eta_0 \ket{x}\] + + + Where the subscripts on \(\nu\) and \(\eta\) denote the number of Grover iterations performed. Referring to Step 2 of the above, note that \(\nu_0 = \eta_0 = 1/\sqrt{N}\). Now consider the action of the Grover iteration on the \(\nu_j\) and \(\eta_j\): +
+ +
+ It is rather difficult to solve these recurrence relations, but their solutions are given in Ref [3]. The solutions are:
+
+
+ \[\begin{align} \nu_j & = \sin([2j+1]\theta) \\
+ \eta_j & = \frac{1}{\sqrt{N-1}}\cos([2j+1]\theta)
+ \end{align}\]
+
+
+ Where \(\sin^2 \theta = 1/N\). The validity of these solutions can be proven by induction6
+
+
+ 6. We prove as follows.
+
+ Basis case.
+ Set \(j=0\). Now insert into the solution:
+
+
+ \[\begin{align} \nu_0 & = \sin(\theta) \\
+ & = \sin(\arcsin(\frac{1}{\sqrt{N}})) \\
+ & = \frac{1}{\sqrt{N}} \\
+ \eta_0 & = \frac{1}{\sqrt{N-1}}\cos(\theta) \\
+ & = \frac{1}{\sqrt{N-1}}\cos(\arcsin(\frac{1}{\sqrt{N}})) \\
+ & = \frac{1}{\sqrt{N-1}} \sqrt{1 - \frac{1}{N}} \\
+ & = \frac{1}{\sqrt{N-1}} \sqrt{\frac{N-1}{N}} = \frac{1}{\sqrt{N}}
+ \end{align}\]
+
+
+ Which satisfy the boundary conditions we noted in the main text.
+
+
+ Inductive step.
+
+ Assume the solution is valid for \(j\), now test for \(j+1\):
+
+
+ \[\begin{align} \nu_{j+1} & = \sin([2(j+1)+1]\theta) \\
+ & = \sin([2j + 1]\theta + 2\theta) \\
+ & = \sin([2j + 1]\theta) \cos(2\theta) + \cos([2j + 1]\theta) \sin(2 \theta) \\
+ & = \nu_j (\cos^2\theta - \sin^2\theta) + \sqrt{N-1} \eta_j (2 \cos\theta \sin\theta) \\
+ & = \nu_j (\frac{N-1}{N} - \frac{1}{N}) + \sqrt{N-1} \eta_j (2 \sqrt{\frac{N-1}{N}} \frac{1}{\sqrt{N}}) \\
+ & = \frac{N-2}{N} \nu_j + \frac{2(N-1)}{N} \eta_j \\
+ \eta_{j+1} & = \frac{1}{\sqrt{N-1}} \cos([2(j+1) + 1]\theta) \\
+ & = \frac{1}{\sqrt{N-1}} \cos([2j + 1]\theta + 2 \theta) \\
+ & = \frac{1}{\sqrt{N-1}} \big( \cos([2j+1]\theta) \cos(2\theta) - \sin([2j+1]\theta) \sin(2\theta) \big) \\
+ & = \frac{1}{\sqrt{N-1}} ( \sqrt{N-1} \eta_j \frac{N-2}{N} - 2 \nu_j \frac{\sqrt{N-1}}{N} ) \\
+ & = -\frac{2}{N} \nu_j + \frac{N-2}{N} \eta_j
+ \end{align}\]
+
+
+ Q.E.D.
+
+
+
+ . As we want to maximise the probability of measuring the needle, we clearly want to maximise \(\nu_j\). The maximum of \(\sin(x)\) is at \(x = \pi/2\) and so we want \((2j + 1)\theta = \pi/2\). Considering that \(j\) must be an integer, we want:
+
+
+ \[j = \left \lfloor \frac{\pi - 2\theta}{4\theta} \right \rfloor\]
+
+
+ Now see that since \(\sin\theta \approx \theta\) for small \(\theta\) and recalling that \(\sin\theta = 1/\sqrt{N}\):
+
+
+ \[j \approx \left \lfloor \frac{\pi - 2/\sqrt{N}}{4/\sqrt{N}} \right \rfloor \approx \left \lfloor \frac{\pi}{4}\sqrt{N} \right \rfloor\]
+
+
+ And thus we choose \(R\) to be the above value. Note an interesting - and characteristically quantum - implication of the sinusoidal form for \(\nu_j\): we can perform too many iterations of the algorithm! In the classical world we’d expect our extra work to reap us some reward (or at least not to make things worse) but here the performance of the algorithm is periodic in \(R\) so we need to stop at exactly the right point in order to find our needle.
+
[1]L. K. Grover, “Quantum Mechanics Helps in Searching for a Needle in a Haystack,” Phys. Rev. Lett., vol. 79, no. 2, pp. 325–328, Jul. 1997.
-[2]M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th Anniversary Edition. Cambridge ; New York: Cambridge University Press, 2010.
-[3]M. Boyer, G. Brassard, P. Hoeyer, and A. Tapp, “Tight bounds on quantum searching,” Fortschritte der Physik, vol. 46, no. 4-5, pp. 493–505, Jun. 1998.
+[1] L. K. Grover, “Quantum Mechanics Helps in Searching for a Needle in a Haystack,” Phys. Rev. Lett., vol. 79, no. 2, pp. 325–328, Jul. 1997.
+[2] M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th Anniversary Edition. Cambridge ; New York: Cambridge University Press, 2010.
+[3] M. Boyer, G. Brassard, P. Hoeyer, and A. Tapp, “Tight bounds on quantum searching,” Fortschritte der Physik, vol. 46, no. 4-5, pp. 493–505, Jun. 1998.
+