-
Notifications
You must be signed in to change notification settings - Fork 0
/
qft_semi-classical.py
81 lines (55 loc) · 1.5 KB
/
qft_semi-classical.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import math
import numpy
import random
import sys
import time
from pyqrack import QrackSimulator
def iqft(n, sim):
if n == 0:
return
n -= 1
sim.h(n)
if sim.m(n):
for t in range(n):
sim.mtrx([1, 0, 0, numpy.exp(math.pi/2**(n-t)*1j)], t)
iqft(n, sim)
def qft(n, sim):
if n == 0:
return
n -= 1
qft(n, sim)
for c in range(n):
sim.mcmtrx([c], [1, 0, 0, numpy.exp(-math.pi/2**(n-c)*1j)], n)
sim.h(n)
sim.m(n)
def bench_qrack(n):
# This is a discrete Fourier transform, after initializing all qubits randomly but separably.
start = time.perf_counter()
qsim = QrackSimulator(n)
lcv_range = range(n)
# Single-qubit gates
for i in lcv_range:
qsim.u(i, random.uniform(0, 2 * math.pi), random.uniform(0, 2 * math.pi), random.uniform(0, 2 * math.pi))
iqft(n, qsim)
end = n - 1
for i in range(n // 2):
qsim.swap(i, end - i)
return time.perf_counter() - start
def main():
bench_qrack(1)
max_qb = 24
samples = 100
if len(sys.argv) > 1:
max_qb = int(sys.argv[1])
if len(sys.argv) > 2:
samples = int(sys.argv[2])
for n in range(1, max_qb + 1):
width_results = []
# Run the benchmarks
for i in range(samples):
width_results.append(bench_qrack(n))
time_result = sum(width_results) / samples
print(n, ": ", time_result, " seconds")
return 0
if __name__ == '__main__':
sys.exit(main())