1 Quantum Fourier Transform
The discrete Fourier transform is a complex vector x 0 , . . . , x N − 1 {x_0},...,{x_{N - 1}} x0,...,xN−1 is the input, and the output data is the following complex vector y 0 , . . . , y N − 1 {y_0},...,{y_{N - 1} } and0,...,andN−1:
y k ≡ 1 N ∑ j = 0 N − 1 x j e 2 π i j k / N {y_k} \equiv \frac{1}{
{\sqrt N }}\sum\limits_{j = 0}^{N - 1} {
{x_j}{e^{ 2\pi ijk/N}}} andk≡N1j=0∑N−1xjIt is2πijk/N
The quantum Fourier transform is strictly the same transformation as it. The quantum Fourier transform is defined as, on a set of standard orthonormal basis ∣ 0 , . . . , ∣ N − 1 \left| 0 \right\rangle ,...,\left | {N - 1} \right\rangle ∣0⟩,...,∣N−A linear operator on 1 has the effect on the ground state of
∣ j → 1 N ∑ k = 0 N − 1 e 2 π i j k / N ∣ k \left| j \right\rangle \to \frac{1}{
{\sqrt N }}\sum\limits_{k = 0}^{N - 1} {
{e^{2\pi ijk/N}}} \left| k \right\rangle ∣j⟩→N1k=0∑N−1It is2πijk/N∣k⟩
We will state j j j is written in binary form j = j 1 j 2 . . . j n j=j_1j_2...j_n j=j1j2...jn, using notation 0. j l j l + 1 . . . j m 0.j_lj_{l+1}...j_m 0.jljl+1...jmRepresents a binary fraction.
容易推导出量子Fourier变换的积形式:
∣ j 1 , . . . , j n ⟩ → 1 2 n / 2 [ ( ∣ 0 ⟩ + e 2 π i 0. j n ∣ 1 ⟩ ) ( ∣ 0 ⟩ + e 2 π i 0. j n − 1 j n ∣ 1 ⟩ ) . . . ( ∣ 0 ⟩ + e 2 π i 0. j 1 . . . j n − 1 j n ∣ 1 ⟩ ) ] \begin{array}{l} \left| {
{j_1},...,{j_n}} \right\rangle \to \\ \frac{1}{
{
{2^{n/2}}}}\left[ {\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_n}}}\left| 1 \right\rangle } \right)\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_{n - 1}}{j_n}}}\left| 1 \right\rangle } \right)...\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_1}...{j_{n - 1}}{j_n}}}\left| 1 \right\rangle } \right)} \right] \end{array} ∣j1,...,jn⟩→2n/21[(∣0⟩+It is2πi0.jn∣1⟩)(∣0⟩+It is2πi0.jn−1jn∣1⟩)...(∣0⟩+It is2πi0.j1...jn−1jn∣1⟩)]
The effective circuit implementation of quantum Fourier transform is as follows:
where gate R k R_k Rk represents unitary transformation
Use exchange operation to reverse the order of qubits, the state of qubits is
1 2 n / 2 ( ∣ 0 + e 2 π i 0. j n ∣ 1 ) ( ∣ 0 + e 2 π i 0. j n − 1 j n ∣ 1 ) . . . ( ∣ 0 + e 2 π i 0. j 1 . . . j n − 1 j n ∣ 1 ) \frac{1}{
{
{2^{n/2}}}} {\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_n}}}\left | 1 \right\rangle } \right)\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_{n - 1}}{j_n}}}\left| 1 \ right\rangle } \right)...\left( {\left| 0 \right\rangle + {e^{2\pi i0.{j_1}...{j_{n - 1}}{j_n}} }\left| 1 \right\rangle } \right)} 2n/21(∣0⟩+It is2πi0.jn∣1⟩)(∣0⟩+It is2πi0.jn−1jn∣1⟩)...(∣0⟩+It is2πi0.j1...jn−1jn∣1⟩)
Quantum Fourier transform can be implemented using the following statement
from mindquantum.algorithm.library import qft
circ = qft([2,1,0])
circ.svg()
The PS gate in the picture is the phase rotation gate PhaseShift.
It should be noted that the quantum state corresponding to the q0 q1 q2 register is ∣ q 2 q 1 q 0 \left| { {q_2}{q_1}{q_0}} \right\rangle ∣q2q1q0⟩。
2 Quantum Adder
Wakaari a = a 1 a 2 . . . a n a=a_1a_2...a_n a=a1a2...an和 b = b 1 b 2 . . . b n b=b_1b_2...b_n b=b1b2...bn,欲计算 c = a + b c=a+b c=a+b, based on the knowledge of quantum Fourier transform, we can first prepare the following quantum state ∣ c f |cf\rangle ∣cf⟩
1 2 n / 2 ( ∣ 0 ⟩ + e 2 π i 0. c n ∣ 1 ⟩ ) ( ∣ 0 ⟩ + e 2 π i 0. c n − 1 c n ∣ 1 ⟩ ) . . . ( ∣ 0 ⟩ + e 2 π i 0. c 1 . . . c n − 1 c n ∣ 1 ⟩ ) \frac{1}{
{
{2^{n/2}}}}\left( {\left| 0 \right\rangle + {e^{2\pi i0.{c_n}}}\left| 1 \right\rangle } \right)\left( {\left| 0 \right\rangle + {e^{2\pi i0.{c_{n - 1}}{c_n}}}\left| 1 \right\rangle } \right)...\left( {\left| 0 \right\rangle + {e^{2\pi i0.{c_1}...{c_{n - 1}}{c_n}}}\left| 1 \right\rangle } \right) 2n/21(∣0⟩+It is2πi0.cn∣1⟩)(∣0⟩+It is2πi0.cn−1cn∣1⟩)...(∣0⟩+It is2πi0.c1...cn−1cn∣1⟩)
Reuse reverse Fourier change Immediately available ∣ c 1 c 2 . . . c n ⟩ |c_1c_2...c_n\rangle ∣c1c2...cn⟩。
2.1 Carry from the highest bit is not considered
Observed
So the following line can be used to prepare ∣ c f |cf\rangle ∣cf , we will not consider it here. There is a carry in the highest bit.
Let this line be called MAdder (Modular Adder). Therefore, the circuit of the quantum adder is as follows (not considering the carry of the highest bit):
2.2 Consider the carry from the highest bit
Thoughts a = a 1 a 2 . . . a n a=a_1a_2...a_n a=a1a2...an, b = b 1 b 2 . . . b n b=b_1b_2...b_n b=b1b2...bn, c = a + b = c 1 c 2 . . . c n + 1 c = a + b = {c_1}{c_2}...{c_{n + 1}} c=a+b=c1c2...cn+1。 c 1 = 0 c_1=0 c1=0 means there is no carry from the highest bit, c 1 = 1 c_1=1 c1=1 means there is a carry in the highest bit.
If you consider the carry from the highest bit, you need to give a a aAdd a qubit to store the carry term.
∣ a = ∣ 0 a 1 , . . . , a n \left|a \right\rangle = \left| {0{a_1},...,{a_n}} \right\rangle ∣a⟩=∣0a1,...,an⟩
对 ∣ a ⟩ \left|a \right\rangle ∣a⟩After the quantum Fourier change
Observation到
于将备 ∣ c f ⟩ |cf\rangle The lines of ∣cf are as follows
Note: The rotation angle of the line in the red box is 2 more than that of the previous line.
Let the circuit in the figure above be denoted as Adder. The complete quantum adder circuit is as follows
3 code implementation
Import some used packages
from mindquantum.algorithm.library import qft
from mindquantum.simulator import Simulator
from mindquantum.core.gates import X, PhaseShift
from mindquantum.core.circuit import Circuit,dagger
import numpy as np
Define controlled phase rotation operations
def _rn(k):
return PhaseShift(2 * np.pi / 2 ** k)
def C_R(tq, cq, k):
circ = Circuit()
circ += _rn(k).on(tq, cq)
return circ
Define adder, where the carry term is considered
def m_adder(qa, qb):
circ = Circuit()
qa_num = len(qa)
qb_num = len(qb)
for q in range(qb_num):
for i in range(qb_num - q - 1, qb_num):
circ += C_R(qa[qa_num - q - 1], qb[qb_num - i - 1], i - (qb_num - q - 1) + 1)
if qa_num > qb_num and q == (qb_num - 1): # 进位的控制
for i in range(qb_num - q - 1, qb_num):
circ += C_R(qa[qa_num - q - 1 - 1], qb[qb_num - i - 1], i - (qb_num - q - 1) + 1 + 1)
return circ
Input the values of a and b (assuming that a and b are both 3-bit binary numbers), and execute the adder
#定义存储a和b的寄存器qa,qb
qa = [3, 4, 5, 6]
qb = [0, 1, 2]
circ = Circuit()
# qa 输入a的值 001
# circ += X.on(5)
# circ += X.on(4)
circ += X.on(3)
# qb 输入b的值 111
circ += X.on(2)
circ += X.on(1)
circ += X.on(0)
#对qa执行量子Fourier操作
circ += qft([6, 5, 4, 3])
#执行加法操作
adder = m_adder(qa, qb)
circ += adder
#使用量子逆Four变换恢复出结果
circ += dagger(qft([6, 5, 4, 3]))
sim = Simulator('projectq', 7)
sim.apply_circuit(circ)
print(sim.get_qs(True))
circ.svg() #查看整个线路
1¦1000111
The result is saved in the qa register, that is, c=1000
The value in the qb register remains unchanged, that is, b=111
Attached: Complete code of carry-less adder
from mindquantum import Circuit, UN, H, Z
from mindquantum.algorithm.library import amplitude_encoder,qft
from mindquantum.simulator import Simulator
from mindquantum.core.gates import T, H, X, Power, BARRIER,SWAP,PhaseShift
from mindquantum.core.circuit import Circuit, UN
from mindquantum.core.circuit import controlled
from mindquantum.core.circuit import dagger
import numpy as np
from mindquantum.core.gates import Measure
def _rn(k):
return PhaseShift(2 * np.pi / 2**k)
def C_R(tq, cq, k):
circ = Circuit()
circ += _rn(k).on(tq, cq)
return circ
def m_adder(qa,qb):
circ = Circuit()
qa_num = len(qa)
qb_num = len(qb)
for q in range(qb_num):
#无进位
for i in range(qb_num-q-1,qb_num):
circ += C_R(qa[qa_num-q-1], qb[qb_num-i-1], i-(qb_num-q-1)+1)
return circ
qa = [3,4,5]
qb = [0,1,2]
cc = m_adder(qa,qb)
circ = Circuit()
#qa
# circ += X.on(5)
# circ += X.on(4)
circ += X.on(3)
#qb
circ += X.on(2)
circ += X.on(1)
circ += X.on(0)
circ += qft([5,4,3])
circ += cc
circ += dagger(qft([5,4,3]))
sim = Simulator('projectq', 6)
sim.apply_circuit(circ)
print(sim.get_qs(True))
circ.svg()
#基于qft的加法,结果保存到qa寄存器
#quantum adder based on Fourier Transform
References
[1] Engin Şahin. Quantum arithmetic operations based on quantum fourier transform on signed integers. International Journal of Quantum Information. (2020) 2050035 (21 pages).