-
Notifications
You must be signed in to change notification settings - Fork 3
/
04_entropiq_app.py
465 lines (353 loc) · 22.6 KB
/
04_entropiq_app.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import os, sys, json
import uuid
from datetime import date
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import streamlit as st
from streamlit_option_menu import option_menu
import hydralit_components as hc
from st_aggrid import AgGrid, DataReturnMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
import plotly.express as px
import plotly.express
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from palettable.scientific.sequential import Devon_20
import colour
from colour import Color
this_dir = os.getcwd()
repo_root_dir = this_dir.split("qc-repo")[0] + 'qc-repo/'
from helpers.data_helpers import *
from _app_parms import *
creds_path = os.getcwd() + "/db_creds.env"
load_environ(creds_path)
postgres_conn = get_postgres_conn()
# new customization in hydralit 1.0.10
info_card_custom_theme_experiments = {'bgcolor': '#f9f9f9','title_color': '#969696','content_color': 'black', 'icon_color': '#FF4B4B', 'icon': 'fa fa-flask'}
info_card_custom_theme_lastrun = {'bgcolor': '#f9f9f9','title_color': '#969696','content_color': 'black', 'icon_color': '#FF4B4B', 'icon': 'fa fa-calendar'}
info_card_custom_theme_layers = {'bgcolor': '#f9f9f9','title_color': '#969696','content_color': 'black', 'icon_color': '#FF4B4B', 'icon': 'fa fa-bars'}
info_card_custom_theme_sims = {'bgcolor': '#f9f9f9','title_color': '#969696','content_color': 'black', 'icon_color': '#FF4B4B', 'icon': 'fa fa-calculator'}
info_card_custom_theme_runtime = {'bgcolor': '#f9f9f9','title_color': '#969696','content_color': 'black', 'icon_color': '#FF4B4B', 'icon': 'fa fa-clock'}
experiment_metadata_df = get_table(conn = postgres_conn, table_name = experiments_metadata_table_name, schema_name = core_schema)
st.set_page_config(layout = "wide")
with st.sidebar:
selected = option_menu("EntropiQ", ["EntropiQ Stats", 'Launch Simulation', 'Discovery','Jordan-Wigner CPLC'],
icons=['stack', 'bricks','search','search'], menu_icon = "boxes", default_index=0)
if selected == "EntropiQ Stats":
col1, col2 = st.columns([4,4])
n_experiments = experiment_metadata_df.shape[0]
ave_layers = experiment_metadata_df['n_layers'].mean()
ave_simulations = experiment_metadata_df['n_simulations'].mean()
last_run_date = experiment_metadata_df['experiment_run_date'].max()
ave_runtime = experiment_metadata_df['runtime_in_seconds'].mean()
with col1:
hc.info_card(title='Experiments', content = str(int(n_experiments)), content_text_size= '2.5rem',theme_override=info_card_custom_theme_experiments)
hc.info_card(title='Last Run Date', content = str(last_run_date), content_text_size= '2.5rem',theme_override=info_card_custom_theme_lastrun)
with col2:
hc.info_card(title='Average Layers', content = str(int(ave_layers)), content_text_size= '2.5rem', theme_override=info_card_custom_theme_layers)
hc.info_card(title='Average Simulations', content = str(int(ave_simulations)), content_text_size= '2.5rem',theme_override=info_card_custom_theme_sims)
hc.info_card(title='Average Runtime (Min)', content = str(np.round(ave_runtime/60,2)), content_text_size= '2.5rem',theme_override=info_card_custom_theme_runtime)
elif selected == "Launch Simulation":
st.header('Experiment Configuration')
col1, col2 = st.columns([4,4])
experiment_id = str(uuid.uuid1())
experiment_run_date = date.today().strftime("%m-%d-%Y")
with col1:
st.subheader('General Info')
experiment_name = st.text_input('Experiment Name:')
experiment_description = st.text_area('Experiment Description:')
st.subheader('Qubits, Layers, and Simulatons')
num_qubit_space = st.slider('Number of Qubit Range',6, 50, (6, 10))
qubit_step = st.number_input('Step By:', 1)
num_qubit_space = list(range(num_qubit_space[0], num_qubit_space[1], int(qubit_step)))
n_layers = st.number_input('Number of Layers:', 10)
n_simulations = st.number_input('Number of Simulations:',100)
with col2:
st.subheader('Gates and Measurements')
operation_type_to_apply = st.radio("Unary or Binary Gates and Projective Measurements:", ('Binary', 'Unary'))
gate_types_to_apply = st.radio("Gate Group to Apply:", ('Random Unitaries', 'Random Cliffords'))
mr_values = st.slider('Measurement Rate Range (%)',0, 100, (0, 80))
mr_step = st.number_input('Step By Rate:',0.1)
measurement_rate_space = [x/10 for x in range(list(mr_values)[0], list(mr_values)[1] + int(10*mr_step), int(100*mr_step))]
st.subheader('Reduced Density Matrix')
use_constant_size = st.checkbox('Use Constant Sub-system Size for Reduced Density Matrix')
constant_size = 0
subsystem_range_divider = 0.50
if use_constant_size:
constant_size = st.number_input('Constant Sub-system Size for Reduced Density Matrix (# of Qubits):', 3)
else:
subsystem_range_divider = st.selectbox('Relative Sub-system Size for Reduced Density Matrix:',[0.50, 0.25, 0.20])
st.write('Reduced Density Matrix Based on Tracing Over:', str(np.round(100*(1-subsystem_range_divider),0)) + '% of the system.')
st.subheader('Experiment Configuration Summary')
this_experiment_metadata_df = pd.DataFrame.from_dict({'experiment_name': [experiment_name],
'experiment_description': [experiment_description],
'experiment_id' : [experiment_id],
'experiment_run_date' : [experiment_run_date],
'num_qubit_space' : [','.join([str(x) for x in num_qubit_space])],
'n_layers' : [n_layers],
'n_simulations' : [n_simulations],
'measurement_rate_space' : [','.join([str(x) for x in measurement_rate_space])],
'use_constant_size': [use_constant_size],
'constant_size': [constant_size],
'subsystem_range_divider' : [subsystem_range_divider],
'operation_type_to_apply' : [operation_type_to_apply],
'gate_types_to_apply' : [gate_types_to_apply]
})
this_experiment_metadata_df['runtime_in_seconds'] = 0
experiment_metadata_df_preview = this_experiment_metadata_df.T
experiment_metadata_df_preview[0] = experiment_metadata_df_preview[0].astype(str)
experiment_metadata_df_preview['Parameter'] = experiment_metadata_df_preview.index
experiment_metadata_df_preview.reset_index(inplace=True, drop=True)
experiment_metadata_df_preview.rename(columns={0:'Value'}, inplace=True)
experiment_metadata_df_preview = experiment_metadata_df_preview[['Parameter', 'Value']]
st.table(experiment_metadata_df_preview)
action_col1, action_col2 = st.columns([0.05,1])
with action_col1:
if st.button('Save'):
st.write('Saving...')
write_table(conn = postgres_conn,
df = this_experiment_metadata_df,
table_name = experiments_metadata_table_name,
schema_name = core_schema,
append = True)
st.write('Saved.')
with action_col2:
if st.button('Launch Simulation'):
st.write('Launching Simulation...')
elif selected == "Discovery":
st.header('Discovery')
st.subheader('Experiments')
experiment_metadata_df = get_table(conn = postgres_conn, table_name = experiments_metadata_table_name, schema_name = core_schema)
gb = GridOptionsBuilder.from_dataframe(experiment_metadata_df)
gb.configure_pagination()
grid_options = gb.build()
AgGrid(experiment_metadata_df, grid_options)
experiment_id = st.selectbox('Select Experiment ID', experiment_metadata_df.experiment_id)
st.subheader('Experiment Results:')
experiment_results_df = get_table(conn = postgres_conn, table_name = simulation_results_table_name, schema_name = core_schema, where_string = " where experiment_id = '"+experiment_id + "'")
experiment_results_df['num_qubits'] = experiment_results_df.num_qubits.astype(str)
experiment_results_df['mean_runtime_min'] = experiment_results_df['mean_runtime'].apply(lambda x: np.round(x/60,3))
AgGrid(experiment_results_df)
n_qubits = len(experiment_results_df.num_qubits.unique())
n_simulations = experiment_metadata_df.n_simulations.values[0]
n_qubit_color_palette = list(Color("#3f007d").range_to(Color("#dadaeb"),n_qubits))
n_qubit_color_palette = np.flip([c.hex for c in n_qubit_color_palette])
st.subheader('Average Entanglement Entropy by System Size and Measurement Rate')
main_fig = px.line(experiment_results_df,
x='measurement_rate',
y='mean_entropy',
color='num_qubits',
height=800, width=800,
color_discrete_sequence = n_qubit_color_palette,
labels={
"measurement_rate": "Measurement Rate (%)",
"mean_entropy": "Average Entanglement Entropy"
})
main_fig.update_traces(line = dict(width=3))
main_fig.update_layout(font = dict(size=20),legend_title_text='# of Qubits')
main_fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
st.plotly_chart(main_fig, use_container_width=True)
st.subheader('Runtime Analysis')
main_fig = px.line(experiment_results_df,
x='measurement_rate',
y='mean_runtime_min',
color='num_qubits',
height=800, width=800,
color_discrete_sequence=n_qubit_color_palette,
labels={
"measurement_rate": "Measurement Rate (%)",
"mean_runtime_min": "Average Simulation Runtime (Min)"
})
main_fig.update_traces(line = dict(width=3))
main_fig.update_layout(font = dict(size=20),
legend_title_text='# of Qubits')
main_fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
st.plotly_chart(main_fig, use_container_width=True)
entropy_tracking_df = get_table(conn = postgres_conn, table_name = entropy_tracking_table_name, schema_name = core_schema, where_string = " where experiment_id = '"+experiment_id + "'")
print("Dropping Experiment ID")
entropy_tracking_df.drop(columns=['experiment_id'],inplace=True)
entropy_tracking_df['log_state_index'] = entropy_tracking_df['ij'].apply(lambda x: np.log(x))
entropy_tracking_df['num_qubits'] = entropy_tracking_df['num_qubits'].astype(int)
st.subheader('State Probabilities - Preview')
AgGrid(entropy_tracking_df.head())
n_sim_color_palette = list(Color("#c7e9c0").range_to(Color("#006d2c"),n_simulations))
n_sim_color_palette = np.flip([c.hex for c in n_sim_color_palette])
st.subheader('Histogram - Evolution of State Probabilities')
x_axis_ticks = [x/100 for x in range(0,105,10)]
eti_fig_hist = px.histogram(entropy_tracking_df,
x="eigenvalue",
facet_col="num_qubits",
facet_col_wrap = 4,
color="num_qubits",
color_discrete_sequence = n_qubit_color_palette,
histnorm = 'probability',
nbins = 20,
animation_frame="measurement_rate",
category_orders={
"num_qubits": np.sort(entropy_tracking_df.num_qubits.unique()).tolist(),
},
labels={
"eigenvalue": "State Probability",
},
range_x = [0,1],
range_y = [0,1],
height=800, width=800,
)
eti_fig_hist.for_each_yaxis(lambda y: y.update(title = ''))
eti_fig_hist.update_traces(marker_line_width=1,marker_line_color="white")
eti_fig_hist.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
eti_fig_hist.update_layout(font = dict(size=20),
legend=dict(orientation="h",yanchor="bottom",y=1.02,xanchor="right",x=1),
yaxis_title = "%",
legend_title_text='# of Qubits')
eti_fig_hist.update_xaxes(ticktext=x_axis_ticks, tickvals=x_axis_ticks)
eti_fig_hist.for_each_xaxis(lambda x: x.update(ticktext=x_axis_ticks, tickvals=x_axis_ticks))
st.plotly_chart(eti_fig_hist, use_container_width=True)
st.subheader('Inspection - State Probability Distribution')
select_nq = st.selectbox('# Qubits:',entropy_tracking_df.num_qubits.unique())
select_mr = st.selectbox('Measurement rate:',entropy_tracking_df.measurement_rate.unique())
inspect_entropy_tracking_df = entropy_tracking_df[ (entropy_tracking_df.num_qubits == select_nq) & (entropy_tracking_df.measurement_rate == select_mr)]
eti_fig = px.histogram(inspect_entropy_tracking_df,
x="eigenvalue",
histnorm = 'probability',
nbins = 20,
labels={
"eigenvalue": "State Probability",
},
range_x = [0,1],
range_y = [0,1],
height=800, width=800,
)
eti_fig.for_each_yaxis(lambda y: y.update(title = ''))
eti_fig.update_traces(marker_line_width=1,marker_line_color="white")
eti_fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
eti_fig.update_layout(font = dict(size=20),
legend=dict(orientation="h",yanchor="bottom",y=1.02,xanchor="right",x=1),
yaxis_title = "%",
legend_title_text='# of Qubits')
eti_fig.update_xaxes(ticktext=x_axis_ticks, tickvals=x_axis_ticks)
st.plotly_chart(eti_fig, use_container_width=True)
elif selected == "CPLC":
st.header('Completely packed loop model with crossings (CPLC) Experimental Design')
st.subheader('Experiments')
experiment_metadata_df = get_table(conn = postgres_conn, table_name = experiments_metadata_cplc_table_name, schema_name = core_schema)
gb = GridOptionsBuilder.from_dataframe(experiment_metadata_df)
gb.configure_pagination()
grid_options = gb.build()
AgGrid(experiment_metadata_df, grid_options)
experiment_id = st.selectbox('Select Experiment ID', experiment_metadata_df.experiment_id)
st.subheader('Experiment Results:')
experiment_results_df = get_table(conn = postgres_conn, table_name = simulation_results_cplc_table_name, schema_name = core_schema, where_string = " where experiment_id = '"+experiment_id + "'")
experiment_results_df['num_qubits'] = experiment_results_df.num_qubits.astype(str)
experiment_results_df['mean_runtime_min'] = experiment_results_df['mean_runtime'].apply(lambda x: x/60)#np.round(
AgGrid(experiment_results_df)
n_qubits = len(experiment_results_df.num_qubits.unique())
n_simulations = experiment_metadata_df.n_simulations.values[0]
n_qubit_color_palette = list(Color("#3f007d").range_to(Color("#dadaeb"),n_qubits))
n_qubit_color_palette = np.flip([c.hex for c in n_qubit_color_palette])
st.subheader('Average Entanglement Entropy by p and q Parameters')
facet_plot_eg = experiment_results_df[['num_qubits','p','q','mean_entropy']]
facet_plot_eg.sort_values(["num_qubits", "p", "q"], ascending = [True, True, True], inplace = True)
facet_plot_eg['p'] = facet_plot_eg['p'].astype(str)
facet_plot_eg['q'] = facet_plot_eg['q'].astype(str)
facet_plot_eg['num_qubits'] = facet_plot_eg['num_qubits'].astype(str)
max_columns = st.number_input('Maximum Columns in Heatmap Grid', value = 3, min_value=1)
heatmap_grid_height = st.number_input('Heatmap Grid Height', value = 200, min_value=200)
heatmap_grid_width = st.number_input('Heatmap Grid Width', value = 600, min_value=200)
max_rows = int(np.ceil(len(facet_plot_eg['num_qubits'].unique())/max_columns))
titles = ['Qubits='+ str(x) for x in facet_plot_eg['num_qubits'].unique().astype(str)]
fig = make_subplots(rows=max_rows, cols=int(max_columns),
shared_yaxes=True,
subplot_titles = tuple(titles))
i_index = 1
max_index = len(facet_plot_eg['num_qubits'].unique())
for i in facet_plot_eg['num_qubits'].unique():
df = facet_plot_eg[facet_plot_eg['num_qubits'] == i]
row_index = int(np.floor(i_index/(max_columns+1))+1)
col_index = i_index % max_columns
if col_index == 0:
col_index = max_columns
col_index = int(col_index)
fig.add_trace(go.Heatmap(z=df.mean_entropy, x=df.q, y=df.p, hoverinfo='text', hovertemplate='p: %{x}<br>q: %{y}<br>Mean Entropy: %{z}<extra></extra>'), row=row_index, col=col_index)
fig.update_xaxes(title_text='q', row=row_index, col=col_index)
fig.update_yaxes(title_text='p', row=row_index, col=col_index)
fig.update_layout(plot_bgcolor='black',height=heatmap_grid_height, width=heatmap_grid_width)
fig.update_xaxes(showline=True, linewidth=0.75, linecolor='black', gridcolor='black')
fig.update_yaxes(showline=True, linewidth=0.75, linecolor='black', gridcolor='black')
i_index+=1
fig.update_traces(xgap=1,ygap=1,showscale = False)
st.plotly_chart(fig, use_container_width=True)
st.subheader('Average Simulation Runtime by p and q Parameters')
facet_plot_runtime_df = experiment_results_df[['num_qubits','p','q','mean_runtime_min']]
clipped_df = pd.DataFrame()
system_sizes = facet_plot_runtime_df.num_qubits.unique().tolist()
for system_size in system_sizes:
df = facet_plot_runtime_df[facet_plot_runtime_df.num_qubits == system_size]
clip_low,clip_high = df['mean_runtime_min'].quantile([0.0,0.95])
df['mean_runtime_min'] = df['mean_runtime_min'].clip(clip_low, clip_high)
clipped_df = clipped_df.append(df)
facet_plot_runtime_df = clipped_df
fig = make_subplots(rows=max_rows, cols=int(max_columns),
shared_yaxes=True,
subplot_titles = tuple(titles))
i_index = 1
max_index = len(facet_plot_runtime_df['num_qubits'].unique())
for i in facet_plot_runtime_df['num_qubits'].unique():
df = facet_plot_runtime_df[facet_plot_runtime_df['num_qubits'] == i]
row_index = int(np.floor(i_index/(max_columns+1))+1)
col_index = i_index % max_columns
if col_index == 0:
col_index = max_columns
col_index = int(col_index)
fig.add_trace(go.Heatmap(z=df.mean_runtime_min, x=df.q, y=df.p, hoverinfo='text', hovertemplate='p: %{x}<br>q: %{y}<br>Mean Simulation Runtime: %{z}<extra></extra>'), row=row_index, col=col_index)
fig.update_xaxes(title_text='q', row=row_index, col=col_index)
fig.update_yaxes(title_text='p', row=row_index, col=col_index)
fig.update_layout(plot_bgcolor='black',height=heatmap_grid_height, width=heatmap_grid_width)
fig.update_xaxes(showline=True, linewidth=0.75, linecolor='black', gridcolor='black')
fig.update_yaxes(showline=True, linewidth=0.75, linecolor='black', gridcolor='black')
i_index+=1
fig.update_traces(xgap=1,ygap=1,showscale = False)
st.plotly_chart(fig, use_container_width=True)
entropy_tracking_df = get_table(conn = postgres_conn, table_name = entropy_tracking_cplc_table_name, schema_name = core_schema, where_string = " where experiment_id = '"+experiment_id + "'")
entropy_tracking_df.drop(columns=['experiment_id'],inplace=True)
entropy_tracking_df['log_state_index'] = entropy_tracking_df['ij'].apply(lambda x: np.log(x))
entropy_tracking_df['num_qubits'] = entropy_tracking_df['num_qubits'].astype(int)
st.subheader('State Probabilities - Preview')
AgGrid(entropy_tracking_df.head())
n_sim_color_palette = list(Color("#c7e9c0").range_to(Color("#006d2c"),n_simulations))
n_sim_color_palette = np.flip([c.hex for c in n_sim_color_palette])
st.subheader('Histogram - Evolution of State Probabilities')
x_axis_ticks = [x/100 for x in range(0,105,10)]
eti_fig_hist = px.histogram(entropy_tracking_df,
x="eigenvalue",
facet_col="q",
facet_row="p",
facet_col_wrap = 4,
color="num_qubits",
color_discrete_sequence = n_qubit_color_palette,
histnorm = 'probability',
nbins = 20,
animation_frame="num_qubits",
category_orders={
"q": np.sort(entropy_tracking_df.q.unique()).tolist(),
"p": np.sort(entropy_tracking_df.p.unique()).tolist(),
},
labels={
"eigenvalue": "",
},
range_x = [0,1],
range_y = [0,1],
height=800, width=800,
)
eti_fig_hist.for_each_yaxis(lambda y: y.update(title = ''))
eti_fig_hist.update_traces(marker_line_width=1,marker_line_color="white")
eti_fig_hist.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
eti_fig_hist.update_layout(font = dict(size=20),
legend=dict(orientation="h",yanchor="bottom",y=1.02,xanchor="right",x=1),
yaxis_title = "%",
legend_title_text='# of Qubits')
eti_fig_hist.update_xaxes(ticktext=x_axis_ticks, tickvals=x_axis_ticks)
eti_fig_hist.for_each_xaxis(lambda x: x.update(ticktext=x_axis_ticks, tickvals=x_axis_ticks))
st.plotly_chart(eti_fig_hist, use_container_width=True)