-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
154 lines (129 loc) · 5.18 KB
/
plot.py
File metadata and controls
154 lines (129 loc) · 5.18 KB
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
150
151
152
153
154
import matplotlib.pyplot as plt
import numpy as np
data = {}
def parse(v):
if v.isspace():
return None, None
try:
return (int(v), int(v))
except:
a, b = v.split('*')
_, a = a.split('^')
b = b[2:-2]
return (2**(int(a)) * int(b), (int(a), int(b)))
with open('result.txt') as f:
for line in f.readlines():
if line.isspace():
continue
d, var = line.split(':')
s = d.split('-')
if len(s) == 4:
ansatz, n, l, h = s
p = 0
else:
ansatz, n, l, h, p = s
if ansatz not in data:
data[ansatz] = []
var, v = parse(var)
data[ansatz].append((int(n), int(l), h, int(p), var, v))
plt.rcParams.update({
"text.usetex": True,
"font.family": "serif",
"font.serif": ["Computer Modern"],
"font.size": 8,
"pgf.texsystem": "pdflatex"
})
sim_ansatze = ["sim1", "sim2", "sim9", "sim10", "sim11", "sim12", "sim15"]
iqp_ansatze = ["iqp1", "iqp2", "iqp3"]
mpl_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
colors_sim = { ansatz: c for (ansatz, c) in zip(sim_ansatze, mpl_colors) }
colors_iqp = { ansatz: c for (ansatz, c) in zip(iqp_ansatze, mpl_colors) }
colors = {**colors_sim, **colors_iqp}
def plot_variance(ansatze, selector, title, max_qubits, path, figsize=(5.5,2.5), by_layers=False,
ticks=1, legend=True, ylabel="", xlabel="", legend_label="sim", log_plot=True,
legend_pos="lower left", legend_path=None):
def plot_single_ansatz(ansatz):
points = [(n, v) if not by_layers else (l,v)
for (n,l,h,p,v,_) in data[ansatz] if selector(n,l,h,p) and (n if not by_layers else l) <= max_qubits]
points = [(n, v) for (n, v) in points if v != None and v != 0]
points.sort()
if len(points) == 0:
return
label = legend_label + r"$_{" + ansatz[3:] + "}$"
plt.plot([x for (x, _) in points], [y for (_, y) in points], marker='.', label=label, c=colors[ansatz])
fig, ax = plt.subplots(figsize=figsize)
for ansatz in ansatze:
plot_single_ansatz(ansatz)
if legend:
legend = plt.legend(loc=legend_pos, shadow=False, fancybox=False, framealpha=1, facecolor='white',
edgecolor='black')
ax.xaxis.set_ticks(np.arange(2 if not by_layers else 1, max_qubits+1, ticks))
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
ax.grid(True, alpha=0.2)
if log_plot:
ax.set_yscale('log', base=2)
plt.tight_layout()
fig.savefig(path)
if legend_path is not None:
figlegend = plt.figure(figsize=(2,2.5))
figlegend.legend(ax.get_legend_handles_labels()[0], ax.get_legend_handles_labels()[1], loc='center',
shadow=False, fancybox=False, framealpha=1, facecolor='white',edgecolor='black')
figlegend.tight_layout()
figlegend.savefig(legend_path)
plot_variance(sim_ansatze,
legend_label="Sim",
max_qubits=25,
ticks=2,
selector=lambda n, l, h, p: l == 1 and h == "Z" and p == 0,
title="",
xlabel="Number of qubits $n$",
ylabel=r"Var$\left(\frac{\partial\langle H\rangle}{\partial\theta_1}\right)$",
path='sim-singlelayer-p0.pdf',
legend_path='sim-legend.pdf')
for i in range(1,10):
plot_variance(sim_ansatze,
figsize=(2,2.5),
ticks=4,
legend=False,
max_qubits=25,
selector=lambda n, l, h, p: l == 1 and h == "Z" and p == i,
title=r"Variance for $\theta_{" + str(i+1) + "}$",
xlabel="$n$",
path=f'sim-singlelayer-p{i}.pdf')
plot_variance(iqp_ansatze,
legend_label="IQP",
figsize=(4,2),
max_qubits=25,
ticks=2,
selector=lambda n, l, h, p: l == 1,
title="",
xlabel="Number of qubits $n$",
ylabel=r"Var$\left(\frac{\partial\langle H\rangle}{\partial\theta_1}\right)$",
path='iqp-singlelayer.pdf')
for i in range(2,5):
plot_variance(iqp_ansatze,
legend_label="IQP",
log_plot=False,
max_qubits=25,
figsize=(2,1.5),
ticks=4,
selector=lambda n, l, h, p: l == i,
title=r"$\ell =" + str(i) + "$",
xlabel="$n$",
ylabel=r"Var$\left(\frac{\partial\langle H\rangle}{\partial\theta_1}\right)$" if i == 2 else "",
path=f'iqp1-layer{i}.pdf')
plot_variance(iqp_ansatze,
legend_label="IQP",
figsize=(5.5,2),
legend_pos='lower right',
by_layers=True,
log_plot=False,
max_qubits=20,
ticks=2,
selector=lambda n, l, h, p: n == 3,
title="$n = 3$",
xlabel="Number of layers $\ell$",
ylabel=r"Var$\left(\frac{\partial\langle H\rangle}{\partial\theta_1}\right)$",
path='iqp1-layers.pdf')