-
Notifications
You must be signed in to change notification settings - Fork 8
/
plotroutines.py
149 lines (124 loc) · 4.12 KB
/
plotroutines.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import matplotlib.pyplot as pl
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.ticker import MaxNLocator
import numpy as np
from qaoa import QAOA
from qaoa.util import Statistic
def __plot_landscape(A, extent, fig):
if not fig:
fig = pl.figure(figsize=(6, 6), dpi=80, facecolor="w", edgecolor="k")
_ = pl.xlabel(r"$\gamma$")
_ = pl.ylabel(r"$\beta$")
ax = fig.gca()
_ = pl.title("Expectation value")
im = ax.imshow(A, interpolation="bicubic", origin="lower", extent=extent)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
_ = pl.colorbar(im, cax=cax)
def plot_E(qaoa_instance, fig=None):
angles = qaoa_instance.landscape_p1_angles
extent = [
angles["gamma"][0],
angles["gamma"][1],
angles["beta"][0],
angles["beta"][1],
]
return __plot_landscape(qaoa_instance.exp_landscape(), extent, fig=fig)
def plot_Var(qaoa_instance, fig=None):
angles = qaoa_instance.landscape_p1_angles
extent = [
angles["gamma"][0],
angles["gamma"][1],
angles["beta"][0],
angles["beta"][1],
]
return __plot_landscape(qaoa_instance.var_landscape(), extent, fig=fig)
def plot_ApproximationRatio(
qaoa_instance, maxdepth, mincost, maxcost, label, style="", fig=None, shots=None
):
if not shots:
exp = np.array(qaoa_instance.get_Exp())
else:
exp = []
for p in range(1, qaoa_instance.current_depth + 1):
ar, sp = __apprrat_successprob(qaoa_instance, p, shots=shots)
exp.append(ar)
exp = np.array(exp)
if not fig:
ax = pl.figure().gca()
else:
ax = fig.gca()
pl.hlines(1, 1, maxdepth, linestyles="solid", colors="black")
pl.plot(
np.arange(1, maxdepth + 1),
(maxcost - exp) / (maxcost - mincost),
style,
label=label,
)
pl.ylim(0, 1.01)
pl.xlim(1 - 0.25, maxdepth + 0.25)
_ = pl.ylabel("appr. ratio")
_ = pl.xlabel("depth")
_ = pl.legend(loc="lower right", framealpha=1)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
def plot_successprob(qaoa_instance, maxdepth, label, style="", fig=None, shots=10**4):
successp = []
for p in range(1, qaoa_instance.current_depth + 1):
ar, sp = __apprrat_successprob(qaoa_instance, p, shots=shots)
successp.append(sp)
successp = np.array(successp)
if not fig:
ax = pl.figure().gca()
else:
ax = fig.gca()
pl.hlines(1, 1, maxdepth, linestyles="solid", colors="black")
pl.plot(
np.arange(1, maxdepth + 1),
successp,
style,
label=label,
)
pl.ylim(0, 1.01)
pl.xlim(1 - 0.25, maxdepth + 0.25)
_ = pl.ylabel("success prob")
_ = pl.xlabel("depth")
_ = pl.legend(loc="lower right", framealpha=1)
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
def __apprrat_successprob(qaoa_instance, depth, shots=10**4):
"""
approximation ratio post processed with feasibility and success probability
"""
hist = qaoa_instance.hist(
qaoa_instance.optimization_results[depth].get_best_angles(), shots=shots
)
counts = 0
stat = Statistic(cvar=qaoa_instance.cvar)
for string in hist:
if qaoa_instance.problem.isFeasible(string):
cost = qaoa_instance.problem.cost(string)
counts += hist[string]
stat.add_sample(cost, hist[string], string)
return -stat.get_CVaR(), counts / shots
def plot_angles(qaoa_instance, depth, label, style="", fig=None):
angles = qaoa_instance.optimization_results[depth].get_best_angles()
if not fig:
ax = pl.figure().gca()
else:
ax = fig.gca()
pl.plot(
np.arange(1, depth + 1),
angles[::2],
"--" + style,
label=r"$\gamma$ " + label,
)
pl.plot(
np.arange(1, depth + 1),
angles[1::2],
"-" + style,
label=r"$\beta$ " + label,
)
pl.xlim(1 - 0.25, depth + 0.25)
_ = pl.ylabel("parameter")
_ = pl.xlabel("depth")
_ = pl.legend()
ax.xaxis.set_major_locator(MaxNLocator(integer=True))