-
Notifications
You must be signed in to change notification settings - Fork 0
/
resultsvisualization.py
367 lines (348 loc) · 13.1 KB
/
resultsvisualization.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from typing import Optional, Union
import matplotlib.cm as cm
import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import torch
from matplotlib.markers import MarkerStyle
class ResultsVisualization:
def __init__(self, model, simulation_stats, theoretical_stats):
self.model = model
self.simulation_stats = simulation_stats
self.theoretical_stats = theoretical_stats
def plot_pannel_samples_coeffs(self):
coeffs_real = np.real(self.model.coefficients)
coeffs_imag = np.imag(self.model.coefficients)
bone_cmap = cm.get_cmap("bone_r")
# Define the start point (0 is the start, 1 is the end of the colormap)
start = 0.3 # Adjust this value to control the starting shade
# Create a new colormap starting from 'start' point of the original 'bone' colormap
darker_bone_cmap = mcolors.LinearSegmentedColormap.from_list(
"truncated_bone", bone_cmap(np.linspace(start, 1, 256))
)
fig, ax = plt.subplots(1, self.model.n_coeffs, figsize=(16, 2))
for idx, ax_ in enumerate(ax):
max_abs_val = (
torch.max(self.simulation_stats.abs_coefficients[:, idx]) * 1.1
) # Adding a 10% margin
ax_.set_title(r"c_{}".format(idx))
# Creating a hexbin plot for density
hb = ax_.hexbin(
coeffs_real[:, idx],
coeffs_imag[:, idx],
gridsize=50,
cmap=darker_bone_cmap,
mincnt=1,
)
# Extract bin counts and locations
counts = hb.get_array()
verts = hb.get_offsets()
x, y = verts[:, 0], verts[:, 1]
# Adding a color bar per subplot
cb = plt.colorbar(hb, ax=ax_)
cb.set_label("counts")
# ax_.set_xlim(-1,1)
# ax_.set_ylim(-1,1)
ax_.set_xlim(-max_abs_val, max_abs_val)
ax_.set_ylim(-max_abs_val, max_abs_val)
avg_real = torch.real(self.simulation_stats.average_coefficients)[
idx
].item()
avg_imag = torch.imag(self.simulation_stats.average_coefficients)[
idx
].item()
ax_.scatter(avg_real, avg_imag, color="red", s=3)
# If max abs value is 0, set the limits to (-1, 1)
# take into account that 0 could be 1e-16 or something like that
# plot grid
ax_.grid()
if max_abs_val < 5e-16:
ax_.set_xlim(-1, 1)
ax_.set_ylim(-1, 1)
plt.tight_layout()
def plot_abs_coeffs_bounds(self):
plt.figure()
if self.model.ansatz == "LocalTwoDesign":
if self.model.cost == "one_qubit":
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " N Wires: "
+ str(self.model.m_wires)
+ " N Subgroups: "
+ str(self.model.m_subgroups)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " Q Measured:"
+ str(self.model.qubit_measured)
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
else:
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " N Wires: "
+ str(self.model.m_wires)
+ " N Subgroups: "
+ str(self.model.m_subgroups)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
elif self.model.ansatz == "BackwardsLightCone":
if self.model.cost == "one_qubit":
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Encoding Qubits: "
+ str(self.model.n_encoding_qubits)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " Q Measured:"
+ str(self.model.qubit_measured)
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
else:
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Encoding Qubits: "
+ str(self.model.n_encoding_qubits)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
else:
if self.model.cost == "one_qubit":
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " Cost: "
+ self.model.cost
+ " Q Measured:"
+ str(self.model.qubit_measured)
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
else:
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " Cost: "
+ self.model.cost
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
plt.scatter(
self.model.freqs, torch.abs(self.simulation_stats.average_coefficients)
)
plt.xlabel("Frequency")
plt.ylabel("Amplitude")
plt.grid()
def plot_var_abs_coeffs_bounds(
self,
var_log_scale=False,
plot_square_term=True,
plot_upper_bound=True,
plot_upper_local_bound=True,
plot_simulation_variance=True,
plot_haar_random_variance=True,
):
plt.figure()
if self.model.cost == "one_qubit":
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " N Wires: "
+ str(self.model.m_wires)
+ " N Subgroups: "
+ str(self.model.m_subgroups)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " Q Measured:"
+ str(self.model.qubit_measured)
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
else:
plt.title(
self.model.encoding
+ " "
+ self.model.ansatz
+ " epsilon "
+ str(np.round(self.theoretical_stats.epsilon, 4))
+ "\n"
+ "N Qubits "
+ str(self.model.n_qubits)
+ " N Circuit Layers: "
+ str(self.model.n_circuit_layers)
+ " N Ansatz Layers "
+ str(self.model.n_periodic_layers)
+ " N Wires: "
+ str(self.model.m_wires)
+ " N Subgroups: "
+ str(self.model.m_subgroups)
+ "\n"
+ " N Subperiodic Layers: "
+ str(self.model.sub_l)
+ " Cost: "
+ self.model.cost
+ " N samples: "
+ str(self.model.n_samples),
fontsize=10,
)
# Plotting the data in the desired order for the legend
if (
self.model.ansatz == "LocalTwoDesign"
or self.model.ansatz == "BackwardsLightCone"
):
if plot_upper_local_bound:
plt.plot(
self.model.freqs,
self.theoretical_stats.upper_var_local,
marker=MarkerStyle("o"),
color="#91322f",
label="Upper Local Bound",
alpha=0.8,
)
if self.model.n_circuit_layers == 1 and self.model.ansatz != "BackwardsLightCone":
if plot_square_term:
plt.plot(
self.model.freqs,
self.theoretical_stats.upper_var_square_cardinality,
color="#91322f",
label="Upper bound w sq term",
alpha=0.7,
)
if plot_upper_bound:
plt.plot(
self.model.freqs,
self.theoretical_stats.upper_var,
marker=MarkerStyle("x"),
color="#91322f",
label="Upper Bound",
alpha=0.8,
)
if plot_simulation_variance:
plt.plot(
self.model.freqs,
self.simulation_stats.variance_coefficients[self.model.freqs],
marker=MarkerStyle("o"),
color="#ed802d",
label="Sampling",
alpha=0.7,
zorder=2,
)
if plot_haar_random_variance:
plt.plot(
self.model.freqs,
self.theoretical_stats.var_coeffs_theory,
marker=MarkerStyle("s"),
color="#91322f",
label="Theory 2-design",
zorder=1,
)
# if self.model.ansatz != "BackwardsLightCone":
# plt.plot(
# self.model.freqs,
# self.theoretical_stats.upper_bound_multiple_circuit_layers_epsilon_approximate,
# marker=MarkerStyle("x"),
# color="blue",
# label="Upper Bound Multiple C Layers",
# alpha=0.8,
# )
if var_log_scale:
plt.yscale("log")
plt.xlabel("Frequency")
plt.ylabel("Variance")
plt.legend()
plt.grid()