##// END OF EJS Templates
Merge branch 'stdin'...
Merge branch 'stdin' Changes stdin channel from REQ-REQ to ROUTER-DEALER, fixing the round-robin load-balancing of stdin_requests across frontends. stdin_requests now go to the client that made the execute_request that prompted the stdin request. stdin_requests from frontends that do not support stdin will raise an error, rather than hanging on input that will never arrive. reviewed by @fperez closes #673

File last commit:

r4637:d919e2ec
r4954:6e92ffd9 merge
Show More
qft.ipynb
163 lines | 35.6 KiB | text/plain | TextLexer

Quantum Fourier Transform

In [1]:
%load_ext sympyprinting
In [2]:
from sympy import sqrt, symbols, Rational
from sympy import expand, Eq, Symbol, simplify, exp, sin
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy.physics.quantum.gate import *
from sympy.physics.quantum.grover import *
from sympy.physics.quantum.qft import QFT, IQFT, Fourier
from sympy.physics.quantum.circuitplot import circuit_plot

QFT is useful for a quantum algorithm for factoring numbers which is exponentially faster than what is thought to be possible on a classical machine.
The transform does a DFT on the state of a quantum system
There is a simple decomposition of the QFT in terms of a few elementary gates.

QFT Gate and Circuit

Build a 3 qubit QFT and decompose it into primitive gates.

In [3]:
fourier = QFT(0,3).decompose(); fourier
Out[3]:
$$SWAP_{0,2} H_{0} C_{0}{\left(S_{1}\right)} H_{1} C_{0}{\left(T_{2}\right)} C_{1}{\left(S_{2}\right)} H_{2}$$
In [4]:
circuit_plot(fourier, nqubits=3)
Out[4]:
<sympy.physics.quantum.circuitplot.CircuitPlot object at 0x4992910>
No description has been provided for this image

The QFT circuit can be represented in various symbolic forms.

In [5]:
m = represent(fourier, nqubits=3)
In [6]:
m
Out[6]:
$$\left(\begin{smallmatrix}\frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2}\\\frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi}\\\frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath}\\\frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi}\\\frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2}\\\frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi}\\\frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath}\\\frac{1}{4} \sqrt{2} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} \mathbf{\imath} & - \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi} & - \frac{1}{4} \sqrt{2} & \frac{1}{4} \sqrt{2} \mathbf{\imath} e^{\frac{1}{4} \mathbf{\imath} \pi} & \frac{1}{4} \sqrt{2} \mathbf{\imath} & \frac{1}{4} \sqrt{2} e^{\frac{1}{4} \mathbf{\imath} \pi}\end{smallmatrix}\right)$$
In [7]:
represent(Fourier(0,3), nqubits=3)*4/sqrt(2)
Out[7]:
$$\left(\begin{smallmatrix}1 & 1 & 1 & 1 & 1 & 1 & 1 & 1\\1 & \omega & \omega^{2} & \omega^{3} & \omega^{4} & \omega^{5} & \omega^{6} & \omega^{7}\\1 & \omega^{2} & \omega^{4} & \omega^{6} & 1 & \omega^{2} & \omega^{4} & \omega^{6}\\1 & \omega^{3} & \omega^{6} & \omega & \omega^{4} & \omega^{7} & \omega^{2} & \omega^{5}\\1 & \omega^{4} & 1 & \omega^{4} & 1 & \omega^{4} & 1 & \omega^{4}\\1 & \omega^{5} & \omega^{2} & \omega^{7} & \omega^{4} & \omega & \omega^{6} & \omega^{3}\\1 & \omega^{6} & \omega^{4} & \omega^{2} & 1 & \omega^{6} & \omega^{4} & \omega^{2}\\1 & \omega^{7} & \omega^{6} & \omega^{5} & \omega^{4} & \omega^{3} & \omega^{2} & \omega\end{smallmatrix}\right)$$

QFT in Action

Build a 3 qubit state to take the QFT of.

In [8]:
state = (Qubit('000') + Qubit('010') + Qubit('100') + Qubit('110'))/sqrt(4); state
Out[8]:
$$\frac{1}{2} \left({\left|000\right\rangle } + {\left|010\right\rangle } + {\left|100\right\rangle } + {\left|110\right\rangle }\right)$$

Perform the QFT.

In [9]:
qapply(fourier*state)
Out[9]:
$$\frac{1}{2} \sqrt{2} {\left|000\right\rangle } + \frac{1}{2} \sqrt{2} {\left|100\right\rangle }$$