-
Notifications
You must be signed in to change notification settings - Fork 8
/
qft.py
58 lines (48 loc) · 1.78 KB
/
qft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from math import pi
# Quantum Fourier transform of |q>, of length n.
# See Fig. 3 of Khosropour et al. (2011) for a circuit diagram.
def qft(circ, q, n):
# Iterate through the target.
for i in range(n,0,-1):
# Apply the Hadamard gate to the target.
circ.h(q[i-1])
# Iterate through the control.
for j in range(i-1,0,-1):
circ.cu1(2*pi/2**(i-j+1), q[j-1], q[i-1])
# Inverse Fourier transform of |q>, of length n.
def iqft(circ, q, n):
# Iterate through the target.
for i in range(1,n+1):
# Iterate through the control.
for j in range(1,i):
# The inverse Fourier transform just uses a negative phase.
circ.cu1(-2*pi/2**(i-j+1), q[j-1], q[i-1])
# Apply the Hadamard gate to the target.
circ.h(q[i-1])
# Controlled-controlled phase gate with phase theta.
# a and b are the controls, and c is the target.
def ccu1(circ, theta, a, b, c):
circ.cu1(theta/2, b, c)
circ.cx(a, b)
circ.cu1(-theta/2, b, c)
circ.cx(a, b)
circ.cu1(theta/2, a, c)
# Quantum Fourier transform of q, controlled by p.
def cqft(circ, p, q, n):
# All the operators are controlled by p.
# Iterate through the target.
for i in range(n,0,-1):
# Apply the Hadamard gate to the target.
circ.ch(p, q[i-1])
# Iterate through the control.
for j in range(i-1,0,-1):
ccu1(circ, 2*pi/2**(i-j+1), p, q[j-1], q[i-1])
# Inverse quantum Fourier transform of p, controlled by p.
def ciqft(circ, p, q, n):
# Iterate through the target.
for i in range(1,n+1):
# Iterate through the control.
for j in range(1,i):
ccu1(circ, -2*pi/2**(i-j+1), p, q[j-1], q[i-1])
# Apply the Hadamard gate to the target.
circ.ch(p, q[i-1])