-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficiency.py
129 lines (100 loc) · 3.17 KB
/
efficiency.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import grovers as gr
import time
import numpy as np
import matplotlib.pyplot as plt
#Run for different sizes of registers and see how time scales for each algorithm f
def writeData(n, f, file):
s = 1
Dt = []
DtS = []
nq = np.arange(n)+1
store = open(file, 'a')
store.write(str(nq))
store.close()
for n in nq:
#Run Grover's algorithm for the given parameters
print(f"Running Grover's, {int(np.pi/(4*np.arcsin(1/np.sqrt(2**n))))} times:")
start_time = time.time()
R, t = f(n,s,False)
Dt.append( time.time() - start_time)
store = open(file, 'a')
store.write(f"\n\n{Dt}")
store.close()
start_time = time.time()
R, t = f(n,s,False,True)
DtS.append( time.time() - start_time)
store = open(file, 'a')
store.write(f"\n{DtS}")
store.close()
def getData(file):
f = open(file, 'r')
d = f.read()
f.close()
data = d.split('\n')
nq = np.array([float(a) for a in (data[0])[1:-1].split(' ')])
Dt = np.array([float(a) for a in (data[1])[1:-1].split(', ')])
DtS = np.array([float(a) for a in (data[2])[1:-1].split(', ')])
print(f"{nq}\n{Dt}\n{DtS}")
return nq, Dt, DtS
def check_efficiency(data):
"""Short summary.
Parameters
----------
f : str
String for filename with data.
Returns
-------
type
Description of returned object.
"""
nq, Dt, DtS = data
fig = plt.figure()
dd = fig.add_subplot(111)
#f1 = fig.add_subplot(311)
#f2 = fig.add_subplot(323)
#f3 = fig.add_subplot(324)
#f4 = fig.add_subplot(325)
#f5 = fig.add_subplot(326)
#nq = 2**np.array(nq)
dd.set_title("Log of Time vs qbits per state for Sparse and Non Sparse Method")
dd.plot(nq, Dt*1000, label="Non-Sparse Method")
dd.plot(nq, DtS*1000, label="Sparse Method")
dd.legend()
dd.set_yscale('log')
dd.set_xlabel("Qbits in Register")
dd.set_ylabel("Log of Time")
dd.set_ylim((0,max(abs(Dt*1000))))
plt.show()
"""
f1.set_title("Time Difference")
f1.plot(nq, abs(np.array(Dt)-np.array(DtS))*1000, label="Not Sparse")
f1.set_yscale('log')
f1.set_xlabel("qbits per state")
f1.set_ylabel("Time (ms)")
f1.set_ylim((0,max(abs(np.array(Dt)-np.array(DtS))*1000)))
f2.set_title("Non Sparse Log Time")
f2.plot(nq, Dt*1000, label="Not Sparse")
f2.set_yscale('log')
f2.set_xlabel("qbits per state")
f2.set_ylabel("Time (ms)")
f2.set_ylim((0,max(abs(Dt*1000))))
f3.set_title("Sparse Log Time")
f3.plot(nq, DtS*1000, label="Sparse")
f3.set_yscale('log')
f3.set_xlabel("qbits per state")
f3.set_ylabel("Time (ms)")
f3.set_ylim((0,max(DtS*1000)))
f4.set_title("Non Sparse Time")
f4.plot(nq, Dt*1000, label="Not Sparse")
f4.set_xlabel("qbits per state")
f4.set_ylabel("Time (ms)")
f4.set_ylim((0,max(abs(Dt*1000))))
f5.set_title("Sparse Time")
f5.plot(nq, DtS*1000, label="Sparse")
f5.set_xlabel("qbits per state")
f5.set_ylabel("Time (ms)")
f1.set_ylim((0,max(DtS*1000)))
"""
file = "data1.txt"
#writeData(50, gr.Grovers, file)
check_efficiency(getData(file))