-
Notifications
You must be signed in to change notification settings - Fork 0
/
convergence_figure.py
78 lines (63 loc) · 2.14 KB
/
convergence_figure.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
import numpy as np
from random import random
from a_eig_search_cheating import find_min, EigenvalueFinding, amp_est
from math import log2, ceil, sqrt, pi
from time import time
def find_min_steps(eigfinding):
# This is Algorithm 1 from Overleaf
y = 0.5 # y_0
tmp_y = []
for i in range(1, eigfinding.m + 2):
ptilde = amp_est(eigfinding=eigfinding, y=y)
if ptilde > eigfinding.q:
y -= 2 ** (-i)
else:
y += 2 ** (-i)
# print("y_{0} = {1}".format(i, y))
tmp_y.append(y)
return tmp_y
if __name__ == "__main__":
bitts = 6
error = 0.5 ** bitts
bits_of_precision = ceil(log2(1 / error))
dim = 2 ** 3
all_points = []
Ntrials = 1000
for k in range(Ntrials):
# Now we choose a random matrix to run the algorithm on
eigvals = [error + (1 - 2 * error) * random() for _ in range(dim)]
mat = np.diag(eigvals)
# print("M = {0}".format(mat))
# print("eigenvalues of M are {0}".format(eigvals))
#
# Find the answer
ef = EigenvalueFinding(mat, error)
# Print some stuff about accuracy
start = time()
all_steps = find_min_steps(ef)
end = time()
estimated_minimum = all_steps[-1]
diff_tmp = np.abs(np.array(all_steps) - min(eigvals))
all_points.append(diff_tmp)
# print("Time elapsed:", end-start)
# print("\nlambda_0 = {0}".format(min(eigvals)))
# print("Algorithm estimates lambda_0 ~ {0}".format(estimated_minimum))
# print("Algorithm all rel_diff |lambda0-y_i| ~ {0}".format(diff_tmp))
print("% line nr. ", k + 1)
print("\\addplot [ultra thin, lightredbkg]")
print("table {%")
for i, el in enumerate(diff_tmp):
print(i, el)
print("};")
print()
print()
# print(all_points)
avgs = [np.sum([el[i] for el in all_points]) / Ntrials for i in range(bitts + 1)]
# print(avgs)
print("% line with avg")
print("\\addplot [very thick, powerredmn, dashed]")
print("table {%")
for i, el in enumerate(avgs):
print(i, el)
print("};")
print()