-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphResults.py
116 lines (96 loc) · 3.56 KB
/
graphResults.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
import pandas as pd
from core.buildHam import QHO
import numpy as np
import matplotlib.pyplot as plt
def shortlist(path, msize, basis):
# Grab a shortlist of accurate ground states
vals, vecs = np.linalg.eig(QHO(msize,basis))
exact = np.sort(vals)[0]
df = pd.read_csv(path)
header1 = df.columns.values[1]
header2 = df.columns.values[2]
acc = 50
for i in range(len(df[header1])):
removebracks = df[header1][i].replace("[","").replace("]","")
mylist = removebracks.split()
numlist = [float(i) for i in mylist]
run_time = float(df[header2][i])
if abs(numlist[-1] - exact) < acc:
acc = abs(numlist[-1] - exact)
slvals = []
sltimes = []
slvals.append(numlist)
sltimes.append(run_time)
elif abs(numlist[-1] - exact) == acc:
slvals.append(numlist)
sltimes.append(run_time)
return slvals, sltimes, exact
def count_conv(path, basis):
# Count number of converged runs
if basis == 'en':
conv = 0.5
elif basis == 'pos':
conv = 0.52
df = pd.read_csv(path)
header1 = df.columns.values[1]
count = 0
count_anoms = 0
for i in range(len(df[header1])):
removebracks = df[header1][i].replace("[","").replace("]","")
mylist = removebracks.split()
numlist = [float(i) for i in mylist]
if np.sort(numlist)[0] <= conv:
count += 1
if len(numlist) == 200:
count_anoms += 1
print(count_anoms)
return count
# Edit paths and basis for graphing
mypath1 = "genData/4QHEAPosBasis.csv"
mypath2 = "genData/2QUAPosBasis.csv"
basis = "en"
print(count_conv(mypath1,basis))
print(count_conv(mypath2,basis))
# Edit paths, matrix size and basis here for graphing
mypathen = "genData/2QHEAEnBasis.csv"
slvalsen, sltimesen, exacten = shortlist(mypathen,4,"en")
mypathpos = "genData/2QHEAPosBasis.csv"
slvalspos, sltimespos, exactpos = shortlist(mypathpos,4,"pos")
# Edit img path here
imgpath ="genFigs/2QHEA.png"
"""
maxlen = 500
# Grab shortest number of iterations and graph
import matplotlib.pyplot as plt
for i in range(len(slvalsen)):
if len(slvalsen[i]) < maxlen:
tographen = []
maxlen = len(slvalsen[i])
tographen.append(slvalsen[i])
tographen.append(sltimesen[i])
maxlen = 500
for i in range(len(slvalspos)):
if len(slvalspos[i]) < maxlen:
tographpos = []
maxlen = len(slvalspos[i])
tographpos.append(slvalspos[i])
tographpos.append(sltimespos[i])
print(tographpos[1])
fig, ax = plt.subplots(1, 1, figsize=(6,5))
ax.plot(range(0,len(tographpos[0])),tographpos[0],marker="o", markersize = 7,
markevery=10, linewidth = 1, linestyle = 'dotted')
ax.plot(range(0,len(tographen[0])),tographen[0],marker="^", markersize = 7,
markevery=10, linewidth = 1, linestyle = 'dotted')
ax.plot((0,300),(exactpos,exactpos),color='red', linewidth = 1, linestyle="dashed")
ax.plot((0,300),(exacten,exacten),color='blue', linewidth = 1, linestyle="dashed")
ax.set_ylabel("Cost Function")
ax.set_xlabel("nth Iteration")
ax.legend(["position basis; runtime = "+str(round(tographpos[1],3))
+ " s\n" r"$E_{VQE} = $" + str(np.round(tographpos[0][-1],4)),
"energy basis; runtime = "+str(round(tographen[1],3))
+ " s\n" r"$E_{VQE} = $" + str(np.round(tographen[0][-1],4)),
"position basis "+ r"$E_0$ = "+str(np.real(np.round(exactpos,4))),
"energy basis " + r"$E_0$ = "+str(exacten)],loc='best')
plt.savefig(imgpath, dpi=600)
plt.show()
"""