Skip to content

Commit 261ed1e

Browse files
committed
update
1 parent 9cff2d4 commit 261ed1e

File tree

7 files changed

+1071
-92
lines changed

7 files changed

+1071
-92
lines changed

doc/pub/week12/html/week12-bs.html

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@
7272
None,
7373
'controlled-phase-rotation'),
7474
('Circuit Diagram', 2, None, 'circuit-diagram'),
75+
('Swap gates', 2, None, 'swap-gates'),
76+
('Swap gates and step-by-step mathematical derivation',
77+
2,
78+
None,
79+
'swap-gates-and-step-by-step-mathematical-derivation'),
80+
('Circuit‐level decomposition and implicit bit reversal',
81+
2,
82+
None,
83+
'circuit-level-decomposition-and-implicit-bit-reversal'),
84+
('Qubit ordering', 2, None, 'qubit-ordering'),
85+
('Change of basis order', 2, None, 'change-of-basis-order'),
86+
('The bit-reversal permutation and Swap gates',
87+
2,
88+
None,
89+
'the-bit-reversal-permutation-and-swap-gates'),
90+
('Can we avoid them?', 2, None, 'can-we-avoid-them'),
7591
('Python code with PennyLane',
7692
2,
7793
None,
@@ -213,6 +229,13 @@
213229
<!-- navigation toc: --> <li><a href="#qft-on-three-qubits" style="font-size: 80%;">QFT on three qubits</a></li>
214230
<!-- navigation toc: --> <li><a href="#controlled-phase-rotation" style="font-size: 80%;">Controlled Phase Rotation</a></li>
215231
<!-- navigation toc: --> <li><a href="#circuit-diagram" style="font-size: 80%;">Circuit Diagram</a></li>
232+
<!-- navigation toc: --> <li><a href="#swap-gates" style="font-size: 80%;">Swap gates</a></li>
233+
<!-- navigation toc: --> <li><a href="#swap-gates-and-step-by-step-mathematical-derivation" style="font-size: 80%;">Swap gates and step-by-step mathematical derivation</a></li>
234+
<!-- navigation toc: --> <li><a href="#circuit-level-decomposition-and-implicit-bit-reversal" style="font-size: 80%;">Circuit‐level decomposition and implicit bit reversal</a></li>
235+
<!-- navigation toc: --> <li><a href="#qubit-ordering" style="font-size: 80%;">Qubit ordering</a></li>
236+
<!-- navigation toc: --> <li><a href="#change-of-basis-order" style="font-size: 80%;">Change of basis order</a></li>
237+
<!-- navigation toc: --> <li><a href="#the-bit-reversal-permutation-and-swap-gates" style="font-size: 80%;">The bit-reversal permutation and Swap gates</a></li>
238+
<!-- navigation toc: --> <li><a href="#can-we-avoid-them" style="font-size: 80%;">Can we avoid them?</a></li>
216239
<!-- navigation toc: --> <li><a href="#python-code-with-pennylane" style="font-size: 80%;">Python code with PennyLane</a></li>
217240
<!-- navigation toc: --> <li><a href="#complexity" style="font-size: 80%;">Complexity</a></li>
218241
<!-- navigation toc: --> <li><a href="#inverse-qft-and-the-phase-estimation-algorithm" style="font-size: 80%;">Inverse QFT and the phase estimation algorithm</a></li>
@@ -464,6 +487,126 @@ <h2 id="circuit-diagram" class="anchor">Circuit Diagram </h2>
464487
</center>
465488
<br/><br/>
466489

490+
<!-- !split -->
491+
<h2 id="swap-gates" class="anchor">Swap gates </h2>
492+
493+
<p>In the circuit implementation of the \( n \)-qubit Quantum Fourier
494+
Transform (QFT), one finds at the end a cascade of Swap gates
495+
<em>crossing</em> qubit \( 1 \) with qubit \( n \), qubit \( 2 \) with qubit \( n-1 \), and
496+
so on. These are not arbitrary extra operations, but arise inevitably
497+
from the fact that the standard QFT&#8212;when written as a matrix&#8212;maps
498+
computational&#8208;basis states into <em>bit&#8208;reversed</em> amplitudes. The Swap
499+
gates simply undo that bit-reversal so that the output register has
500+
its qubits in the natural (most&#8208;significant&#8211;to&#8211;least&#8208;significant)
501+
order.
502+
</p>
503+
504+
<!-- !split -->
505+
<h2 id="swap-gates-and-step-by-step-mathematical-derivation" class="anchor">Swap gates and step-by-step mathematical derivation </h2>
506+
507+
<p>On an \( n \)-qubit register, label the computational basis states by integers</p>
508+
509+
$$
510+
\vert x\rangle \quad,\quad x\in\{0,1,\dots,2^n-1\}\,.
511+
$$
512+
513+
<p>The QFT is the linear map</p>
514+
$$
515+
\mathrm{QFT}n:\;\vert x \rangle \;\mapsto\;\frac{1}{\sqrt{2^n}}\sum{y=0}^{2^n-1}
516+
\exp{(2\pi i\,xy / 2^n)}\,\vert y\rangle.
517+
$$
518+
519+
<p>Viewed as a \( 2^n\times2^n \) unitary matrix \( F_{2^n} \), its \( (y,x) \)&#8211;entry is</p>
520+
$$
521+
[F_{2^n}]_{y,x} \;=\;\frac{1}{\sqrt{2^n}}\,e^{2\pi i\,x y / 2^n}\,.
522+
$$
523+
524+
<!-- !split -->
525+
<h2 id="circuit-level-decomposition-and-implicit-bit-reversal" class="anchor">Circuit&#8208;level decomposition and implicit bit reversal </h2>
526+
527+
<p>A standard circuit for \( \mathrm{QFT}_n \) proceeds qubit by qubit (from
528+
most significant qubit down to least significant), applying:
529+
</p>
530+
<ol>
531+
<li> A Hadamard on qubit \( j \);</li>
532+
<li> Controlled-\( R_2,R_3,\dots,R_{n-j+1} \) phase gates from qubit \( k>j \) to qubit \( j \), where</li>
533+
</ol>
534+
$$
535+
R_m \;=\;\begin{pmatrix}1&0\\0&e^{2\pi i/2^m}\end{pmatrix}.
536+
$$
537+
538+
<ol>
539+
<li> Move on to the next qubit \( j+1 \).</li>
540+
</ol>
541+
<!-- !split -->
542+
<h2 id="qubit-ordering" class="anchor">Qubit ordering </h2>
543+
544+
<p>If you label the input qubits as
545+
\( \vert x_{n-1}\rangle\otimes\vert x_{n-2}\rangle\otimes\cdots\otimes\vert x_0\rangle \) (with
546+
\( x_{n-1} \) the most&#8208;significant bit), then the order in which
547+
phase&#8208;gates tie those bits ends up producing output amplitudes in the
548+
reverse bit&#8208;order, that is
549+
</p>
550+
$$
551+
\mathrm{QFT}n \;=\; (\text{controlled‐phase and Hadamard layers})\;\times\; \underbrace{\bigl(\mathrm{bit\!-\!reversal}\bigr)}{P},
552+
$$
553+
554+
<p>where \( P \) is the permutation matrix that sends</p>
555+
$$
556+
\vert y_{n-1}y_{n-2}\dots y_0\rangle \longmapsto \vert y_{0}y_{1}\dots y_{n-1}\rangle.
557+
$$
558+
559+
560+
<!-- !split -->
561+
<h2 id="change-of-basis-order" class="anchor">Change of basis order </h2>
562+
563+
<p>Concretely, one finds for each basis vector</p>
564+
$$
565+
(\text{circuit without swaps})\;\vert x_{n-1}\dots x_0\rangle\;=\;\frac{1}{\sqrt{2^n}}
566+
\sum_{y} \exp{(2\pi i\,x\,y/2^n)}\;\vert y_{\text{reversed}}\rangle,
567+
$$
568+
569+
<p>where \( y_{\text{reversed}} = \sum_j y_j,2^{n-1-j} \) instead of the natural \( \sum_j y_j,2^j \).</p>
570+
571+
<!-- !split -->
572+
<h2 id="the-bit-reversal-permutation-and-swap-gates" class="anchor">The bit-reversal permutation and Swap gates </h2>
573+
574+
<p>The bit-reversal permutation \( P \) is exactly the unitary that swaps
575+
qubit \( j \) with qubit \( (n-1-j) \) for all
576+
\( j=0,\dots,\lfloor(n-1)/2\rfloor \). In matrix form,
577+
</p>
578+
$$
579+
\Pi \;=\;\prod_{j=0}^{\lfloor(n-1)/2\rfloor}\;\mathrm{SWAP}{j,\;n-1-j},
580+
$$
581+
582+
<p>where each \( \mathrm{SWAP}{a,b} \) acts as</p>
583+
$$
584+
\mathrm{SWAP}:\;\vert u\rangle_a\!\otimes\!\vert v\rangle_b \;\longmapsto\;\vert v\rangle_a\!\otimes\!\vert u\rangle_b.
585+
$$
586+
587+
<p>Since the circuit we built implements \( F{2^n},\Pi \) rather than
588+
\( F{2^n} \), appending those swaps at the end yields exactly the matrix
589+
\( F{2^n} \) in the natural qubit ordering.
590+
</p>
591+
592+
<!-- !split -->
593+
<h2 id="can-we-avoid-them" class="anchor">Can we avoid them? </h2>
594+
595+
<p>One might ask: <em>Couldn&#8217;t we just re&#8208;label wires?</em> In principle yes,
596+
but in a physical device the wiring corresponds to real qubit lines.
597+
If you want the logical output bit \( y_{n-1} \) on the same physical wire
598+
as the input \( x_{n-1} \), you must physically swap the states. Hence
599+
the Swap gates are unavoidable whenever you demand that qubit indices
600+
retain their original semantic meaning.
601+
</p>
602+
603+
<p>In summary, the Swap gates at the end of the QFT circuit correct the
604+
inherent bit-reversal that arises from the order in which Hadamard and
605+
controlled-phase gates entangle the qubits. Without them, the output
606+
register would hold the Fourier&#8208;transformed amplitudes in
607+
reverse&#8208;index order.
608+
</p>
609+
467610
<!-- !split -->
468611
<h2 id="python-code-with-pennylane" class="anchor">Python code with PennyLane </h2>
469612
<p>We now implement a full QFT circuit manually for three qubits using PennyLane.</p>

doc/pub/week12/html/week12-reveal.html

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,152 @@ <h2 id="circuit-diagram">Circuit Diagram </h2>
399399
<br/><br/>
400400
</section>
401401

402+
<section>
403+
<h2 id="swap-gates">Swap gates </h2>
404+
405+
<p>In the circuit implementation of the \( n \)-qubit Quantum Fourier
406+
Transform (QFT), one finds at the end a cascade of Swap gates
407+
<em>crossing</em> qubit \( 1 \) with qubit \( n \), qubit \( 2 \) with qubit \( n-1 \), and
408+
so on. These are not arbitrary extra operations, but arise inevitably
409+
from the fact that the standard QFT&#8212;when written as a matrix&#8212;maps
410+
computational&#8208;basis states into <em>bit&#8208;reversed</em> amplitudes. The Swap
411+
gates simply undo that bit-reversal so that the output register has
412+
its qubits in the natural (most&#8208;significant&#8211;to&#8211;least&#8208;significant)
413+
order.
414+
</p>
415+
</section>
416+
417+
<section>
418+
<h2 id="swap-gates-and-step-by-step-mathematical-derivation">Swap gates and step-by-step mathematical derivation </h2>
419+
420+
<p>On an \( n \)-qubit register, label the computational basis states by integers</p>
421+
422+
<p>&nbsp;<br>
423+
$$
424+
\vert x\rangle \quad,\quad x\in\{0,1,\dots,2^n-1\}\,.
425+
$$
426+
<p>&nbsp;<br>
427+
428+
<p>The QFT is the linear map</p>
429+
<p>&nbsp;<br>
430+
$$
431+
\mathrm{QFT}n:\;\vert x \rangle \;\mapsto\;\frac{1}{\sqrt{2^n}}\sum{y=0}^{2^n-1}
432+
\exp{(2\pi i\,xy / 2^n)}\,\vert y\rangle.
433+
$$
434+
<p>&nbsp;<br>
435+
436+
<p>Viewed as a \( 2^n\times2^n \) unitary matrix \( F_{2^n} \), its \( (y,x) \)&#8211;entry is</p>
437+
<p>&nbsp;<br>
438+
$$
439+
[F_{2^n}]_{y,x} \;=\;\frac{1}{\sqrt{2^n}}\,e^{2\pi i\,x y / 2^n}\,.
440+
$$
441+
<p>&nbsp;<br>
442+
</section>
443+
444+
<section>
445+
<h2 id="circuit-level-decomposition-and-implicit-bit-reversal">Circuit&#8208;level decomposition and implicit bit reversal </h2>
446+
447+
<p>A standard circuit for \( \mathrm{QFT}_n \) proceeds qubit by qubit (from
448+
most significant qubit down to least significant), applying:
449+
</p>
450+
<ol>
451+
<p><li> A Hadamard on qubit \( j \);</li>
452+
<p><li> Controlled-\( R_2,R_3,\dots,R_{n-j+1} \) phase gates from qubit \( k>j \) to qubit \( j \), where</li>
453+
</ol>
454+
<p>
455+
<p>&nbsp;<br>
456+
$$
457+
R_m \;=\;\begin{pmatrix}1&0\\0&e^{2\pi i/2^m}\end{pmatrix}.
458+
$$
459+
<p>&nbsp;<br>
460+
461+
<ol>
462+
<p><li> Move on to the next qubit \( j+1 \).</li>
463+
</ol>
464+
</section>
465+
466+
<section>
467+
<h2 id="qubit-ordering">Qubit ordering </h2>
468+
469+
<p>If you label the input qubits as
470+
\( \vert x_{n-1}\rangle\otimes\vert x_{n-2}\rangle\otimes\cdots\otimes\vert x_0\rangle \) (with
471+
\( x_{n-1} \) the most&#8208;significant bit), then the order in which
472+
phase&#8208;gates tie those bits ends up producing output amplitudes in the
473+
reverse bit&#8208;order, that is
474+
</p>
475+
<p>&nbsp;<br>
476+
$$
477+
\mathrm{QFT}n \;=\; (\text{controlled‐phase and Hadamard layers})\;\times\; \underbrace{\bigl(\mathrm{bit\!-\!reversal}\bigr)}{P},
478+
$$
479+
<p>&nbsp;<br>
480+
481+
<p>where \( P \) is the permutation matrix that sends</p>
482+
<p>&nbsp;<br>
483+
$$
484+
\vert y_{n-1}y_{n-2}\dots y_0\rangle \longmapsto \vert y_{0}y_{1}\dots y_{n-1}\rangle.
485+
$$
486+
<p>&nbsp;<br>
487+
</section>
488+
489+
<section>
490+
<h2 id="change-of-basis-order">Change of basis order </h2>
491+
492+
<p>Concretely, one finds for each basis vector</p>
493+
<p>&nbsp;<br>
494+
$$
495+
(\text{circuit without swaps})\;\vert x_{n-1}\dots x_0\rangle\;=\;\frac{1}{\sqrt{2^n}}
496+
\sum_{y} \exp{(2\pi i\,x\,y/2^n)}\;\vert y_{\text{reversed}}\rangle,
497+
$$
498+
<p>&nbsp;<br>
499+
500+
<p>where \( y_{\text{reversed}} = \sum_j y_j,2^{n-1-j} \) instead of the natural \( \sum_j y_j,2^j \).</p>
501+
</section>
502+
503+
<section>
504+
<h2 id="the-bit-reversal-permutation-and-swap-gates">The bit-reversal permutation and Swap gates </h2>
505+
506+
<p>The bit-reversal permutation \( P \) is exactly the unitary that swaps
507+
qubit \( j \) with qubit \( (n-1-j) \) for all
508+
\( j=0,\dots,\lfloor(n-1)/2\rfloor \). In matrix form,
509+
</p>
510+
<p>&nbsp;<br>
511+
$$
512+
\Pi \;=\;\prod_{j=0}^{\lfloor(n-1)/2\rfloor}\;\mathrm{SWAP}{j,\;n-1-j},
513+
$$
514+
<p>&nbsp;<br>
515+
516+
<p>where each \( \mathrm{SWAP}{a,b} \) acts as</p>
517+
<p>&nbsp;<br>
518+
$$
519+
\mathrm{SWAP}:\;\vert u\rangle_a\!\otimes\!\vert v\rangle_b \;\longmapsto\;\vert v\rangle_a\!\otimes\!\vert u\rangle_b.
520+
$$
521+
<p>&nbsp;<br>
522+
523+
<p>Since the circuit we built implements \( F{2^n},\Pi \) rather than
524+
\( F{2^n} \), appending those swaps at the end yields exactly the matrix
525+
\( F{2^n} \) in the natural qubit ordering.
526+
</p>
527+
</section>
528+
529+
<section>
530+
<h2 id="can-we-avoid-them">Can we avoid them? </h2>
531+
532+
<p>One might ask: <em>Couldn&#8217;t we just re&#8208;label wires?</em> In principle yes,
533+
but in a physical device the wiring corresponds to real qubit lines.
534+
If you want the logical output bit \( y_{n-1} \) on the same physical wire
535+
as the input \( x_{n-1} \), you must physically swap the states. Hence
536+
the Swap gates are unavoidable whenever you demand that qubit indices
537+
retain their original semantic meaning.
538+
</p>
539+
540+
<p>In summary, the Swap gates at the end of the QFT circuit correct the
541+
inherent bit-reversal that arises from the order in which Hadamard and
542+
controlled-phase gates entangle the qubits. Without them, the output
543+
register would hold the Fourier&#8208;transformed amplitudes in
544+
reverse&#8208;index order.
545+
</p>
546+
</section>
547+
402548
<section>
403549
<h2 id="python-code-with-pennylane">Python code with PennyLane </h2>
404550
<p>We now implement a full QFT circuit manually for three qubits using PennyLane.</p>

0 commit comments

Comments
 (0)