-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraphplotter.py
More file actions
947 lines (852 loc) · 54.8 KB
/
graphplotter.py
File metadata and controls
947 lines (852 loc) · 54.8 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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
#!/usr/bin/env python3
import os, sys, pickle
import matplotlib.pyplot as plt
import numpy as np
from os import listdir
from os.path import isfile
import seaborn as sns
import matplotlib.transforms as mtransforms
from matplotlib.pyplot import figure
def get_all_sorted_circ_results(base_path, cnot_count, number_of_qubits, result_str="result"):
'''Gets the sorted (by error idx, i.e., smallest single qubit error to largest) test results for
the given cnot count and number of qubits.'''
all_files=[f for f in listdir(base_path) if isfile(os.path.join(base_path, f))]
# print(all_files)
target_files=[]
for file in all_files:
name_split=file.split("_")
# Get the digits in the names for checking
name_split_nums=[int(num) for num in name_split if num.isdigit()]
if (
".obj" in name_split and result_str in name_split
and name_split_nums[1]==cnot_count
and name_split_nums[0]==number_of_qubits): #and start_circ_number<=name_split_nums[2]<=end_circ_number:
target_files.append(file)
all_circ_results_sorted=[]
if target_files:
# Load the results
for file_name in target_files:
#We're looking at one circuit
# Load data from each pickle and get the results object for the circuit.
with open(os.path.join(base_path, file_name), "rb") as circ_file:
pickle_info=pickle.load(circ_file)
circ_result=pickle_info["results"]
# assert pickle_info["qubits"]==number_of_qubits, f"{file_name} does not have the required qubit count."
# assert pickle_info["cx"]==cnot_count, f"{file_name} does not have the required CNOT count."
# Get the results in order.
circ_result_sorted=sorted(circ_result, key=lambda d: d['error_idx'])
assert len(circ_result_sorted)==len(circ_result), "error when sorting outcomes."
all_circ_results_sorted.append(circ_result_sorted)
assert len(all_circ_results_sorted)==len(target_files), "lost some files from sorting."
return all_circ_results_sorted
def get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layers_count, result_str="result"):
'''Gets the sorted (by error idx, i.e., smallest single qubit error to largest) test results for
the given cnot count and number of qubits.'''
all_files=[f for f in listdir(base_path) if isfile(os.path.join(base_path, f))]
# print(all_files)
target_files=[]
for file in all_files:
name_split=file.split("_")
# Get the digits in the names for checking
name_split_nums=[int(num) for num in name_split if num.isdigit()]
if (f"layers_{layers_count}_" in file and
".obj" in name_split and result_str in name_split
and name_split_nums[1]==cnot_count
and name_split_nums[0]==number_of_qubits): #and start_circ_number<=name_split_nums[2]<=end_circ_number:
target_files.append(file)
all_circ_results_sorted=[]
if target_files:
# print(target_files)
# Load the results
for file_name in target_files:
#We're looking at one circuit
# Load data from each pickle and get the results object for the circuit.
with open(os.path.join(base_path, file_name), "rb") as circ_file:
pickle_info=pickle.load(circ_file)
circ_result=pickle_info["results"]
# assert pickle_info["qubits"]==number_of_qubits, f"{file_name} does not have the required qubit count."
# assert pickle_info["cx"]==cnot_count, f"{file_name} does not have the required CNOT count."
# Get the results in order.
circ_result_sorted=sorted(circ_result, key=lambda d: d['error_idx'])
assert len(circ_result_sorted)==len(circ_result), "error when sorting outcomes."
all_circ_results_sorted.append(circ_result_sorted)
assert len(all_circ_results_sorted)==len(target_files), "lost some files from sorting."
return all_circ_results_sorted
def create_fidelity_plot_cnot(base_path, number_of_qubits, cnot_count, one_qubit_error_space):
'''Creates plots based on CNOT counts.'''
# Gets the file path of the script.
all_sorted_circ_results=get_all_sorted_circ_results(base_path, cnot_count, number_of_qubits)
if all_sorted_circ_results:
average_fidelity_gains=[]
fidelity_gains=[]
for circ_result in all_sorted_circ_results:
circ_outcomes_temp=[]
for result in circ_result:
circ_outcomes_temp.append(result["state_fidelity_with_checks_with_errors"]-result["state_fidelity_no_checks_with_errors"])
fidelity_gains.append(circ_outcomes_temp)
# print("raw fidelity", fidelity_gains)
average_fidelity_gains=[sum(x)/len(x) for x in zip(*fidelity_gains)]
# print(average_fidelity_gains)
std_deviations=[np.std(x) for x in zip(*fidelity_gains)]
# print(std_deviations)
plt.clf()
plt.errorbar(one_qubit_error_space, average_fidelity_gains, yerr=std_deviations, capsize=3.0)
plt.title("Qubits: "+str(number_of_qubits)+ " CNOTS: " + str(cnot_count))
plt.xlabel("Single Qubit Error")
plt.ylabel("Average Fidelity Gain")
plt.xscale("log")
# plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_CNOTS_{cnot_count}_fidelity_.png"))
return (cnot_count, average_fidelity_gains, std_deviations)
def create_fidelity_plot_cnot_layers(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers_count, plot_result):
'''Creates plots based on CNOT counts and layers.'''
# Gets the file path of the script.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layers_count)
if all_sorted_circ_results:
average_fidelity_gains=[]
fidelity_gains=[]
# Looking at one circuit.
for circ_result in all_sorted_circ_results:
circ_outcomes_temp=[]
# Looking at one error idx for this specific circuit.
for result in circ_result:
circ_outcomes_temp.append(result["state_fidelity_with_checks_with_errors"]-result["state_fidelity_no_checks_with_errors"])
fidelity_gains.append(circ_outcomes_temp)
# print("raw fidelity", fidelity_gains)
average_fidelity_gains=[sum(x)/len(x) for x in zip(*fidelity_gains)]
# print("average gains ", average_fidelity_gains)
# print(len(average_fidelity_gains))
std_deviations=[np.std(x) for x in zip(*fidelity_gains)]
# print(len(std_deviations))
# print(len(one_qubit_error_space))
# print(std_deviations)
if plot_result:
plt.clf()
plt.errorbar(one_qubit_error_space, average_fidelity_gains, yerr=std_deviations, capsize=3.0)
plt.title(f"Compute Qubits: {str(number_of_qubits)}, CNOTS: {cnot_count}, Layers: {layers_count}")
plt.xlabel("Single Qubit Error")
plt.ylabel("Average Fidelity Gain")
plt.xscale("log")
plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_CNOTS_{cnot_count}_fidelity_layers_{layers_count}_.png"))
return (cnot_count, average_fidelity_gains, std_deviations)
def create_all_fidelity_plot_cnots_layers_rz(base_path, number_of_qubits, cnot_counts, one_qubit_error_space, palette, layers_count, rz_count, ax):
# plt.clf()
all_results=[]
for cnot_count in cnot_counts:
result=create_fidelity_plot_cnot_layers(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers_count, plot_result= False)
if result:
all_results.append(result)
for idx, elem in enumerate(all_results):
# print(f"fidelity gain for cnots {cnot_counts[idx]}: {elem[1]}")
ax.errorbar(one_qubit_error_space, elem[1], yerr=elem[2], capsize=3.0, color=palette[idx*5], label="CNOT Count: "+str(elem[0]))
ax.set_title(f"Compute Qubits: {str(number_of_qubits)}, Layers: {layers_count}, Rz: {rz_count}", fontsize=12)
# ax.set_xlabel("Single Qubit Error")
ax.set_ylabel("Average Fidelity Gain", fontsize=12)
ax.set_xscale("log")
# ax.legend()
# ax.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_allCNOTS_fidelity_gain_layers_{layers_count}_.png"))
def create_2qubit_plot_example_fidelity(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers, ax):
'''Creates plots based on CNOT counts.'''
NLINES = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis",NLINES)
for layer_idx, layer in enumerate(layers):
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
if all_sorted_circ_results:
# Looking at one circuit.
for circ_result in all_sorted_circ_results:
# Looking at one error value for that circuit.
temp_fidelity_no_checks=[]
temp_fidelity_with_checks=[]
for result in circ_result:
temp_fidelity_no_checks.append(result["state_fidelity_no_checks_with_errors"])
temp_fidelity_with_checks.append(result["state_fidelity_with_checks_with_errors"])
if layer_idx==0:
ax.errorbar(one_qubit_error_space, temp_fidelity_no_checks, capsize=3.0, color=palette[0], label="Number of layers: 0")
# Each temp_fidelity_with_checks is a complete circuit result. So plot each separately.
ax.errorbar(one_qubit_error_space, temp_fidelity_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_title(f"Compute Qubits: {number_of_qubits}, CNOTS: {cnot_count}", fontsize=12)
ax.set_ylabel("Fidelity", fontsize=12)
ax.set_xscale("log")
def create_count_percentage_plot(base_path, number_of_qubits, cnot_count, one_qubit_error_space):
'''Plots the average zero outcome for ancilla vs the single qubit error rate.'''
'''Creates plots based on CNOT counts.'''
# Gets the file path of the script.
all_sorted_circ_results=get_all_sorted_circ_results(base_path, cnot_count, number_of_qubits)
if all_sorted_circ_results:
count_diffs=[]
for circ_result in all_sorted_circ_results:
circ_outcomes_temp=[]
for result in circ_result:
# In our simulations we have the full density matrix so the percent results before post selecting is actually 1.
# We do it this way so the graphing can accomidate physical runs where these two values are actually counts.
circ_outcomes_temp.append(result["percent_results_after_postselect"]/result["percent_results_before_postselect"])
count_diffs.append(circ_outcomes_temp)
average_counts_diffs=[sum(x)/len(x) for x in zip(*count_diffs)]
# print(average_counts_diffs)
std_deviations=[np.std(x) for x in zip(*count_diffs)]
# print(std_deviations)
plt.clf()
plt.errorbar(one_qubit_error_space, average_counts_diffs, yerr=std_deviations, capsize=3.0)
plt.title(f"Compute Qubits: {number_of_qubits}, CNOTS: {cnot_count}")
plt.xlabel("Single Qubit Error")
plt.ylabel("Average Probability of Zero Measurement on Ancilla")
plt.xscale("log")
plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_CNOTS_{cnot_count}_counts_.png"))
return (cnot_count, average_counts_diffs, std_deviations)
def create_count_percentage_plot_layers(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers_count, plot_result):
'''Plots the average zero outcome for ancilla vs the single qubit error rate.'''
'''Creates plots based on CNOT counts.'''
# Gets the file path of the script.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layers_count = layers_count)
if all_sorted_circ_results:
count_diffs=[]
for circ_result in all_sorted_circ_results:
circ_outcomes_temp=[]
for result in circ_result:
# In our simulations we have the full density matrix so the percent results before post selecting is actually 1.
# We do it this way so the graphing can accomidate physical runs where these two values are actually counts.
circ_outcomes_temp.append(result["percent_results_after_postselect"]/result["percent_results_before_postselect"])
count_diffs.append(circ_outcomes_temp)
average_counts_diffs=[sum(x)/len(x) for x in zip(*count_diffs)]
# print(average_counts_diffs)
std_deviations=[np.std(x) for x in zip(*count_diffs)]
# print(std_deviations)
if plot_result:
plt.clf()
plt.errorbar(one_qubit_error_space, average_counts_diffs, yerr=std_deviations, capsize=3.0)
plt.title(f"Compute Qubits: {number_of_qubits}, CNOTS: {cnot_count}, Layers: {layers_count}")
plt.xlabel("Single Qubit Error")
plt.ylabel("Average Probability of Zero Measurement on Ancilla")
plt.xscale("log")
plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_CNOTS_{cnot_count}_counts_layers_{layers_count}_.png"))
return (cnot_count, average_counts_diffs, std_deviations)
def create_2qubit_plot_example_count_percentage(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers, ax):
'''Plots the average zero outcome for ancilla vs the single qubit error rate.'''
'''Creates plots based on CNOT counts.'''
NLINES = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", NLINES)
for layer_idx, layer in enumerate(layers):
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
if all_sorted_circ_results:
# each element is a circuit.
for circ_result in all_sorted_circ_results:
counts_after_temp=[]
counts_before_temp=[]
# each element here is one error idx for the circuit.
for result in circ_result:
# In our simulations we have the full density matrix so the percent results before post selecting is actually 1.
# We do it this way so the graphing can accomidate physical runs where these two values are actually counts.
counts_after_temp.append(result["percent_results_after_postselect"])
counts_before_temp.append(result["percent_results_before_postselect"])
if layer_idx==0:
ax.errorbar(one_qubit_error_space, counts_before_temp, color=palette[0], capsize=3.0, label="Number of layers: 0")
ax.errorbar(one_qubit_error_space, counts_after_temp,
capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_xlabel("Single Qubit Error", fontsize=12)
ax.set_ylabel("Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xscale("log")
ax.legend()
def create_all_count_percentage_plots_layers(base_path, number_of_qubits, cnot_counts, one_qubit_error_space, palette, layers_count, ax):
'''Wrapper for create_count_percentage_plot(). Makes individual plots with all the lines and a single plot'''
# plt.clf()
all_results=[]
for cnot_count in cnot_counts:
result=create_count_percentage_plot_layers(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers_count=layers_count, plot_result=False)
if result:
all_results.append(result)
for idx, elem in enumerate(all_results):
ax.errorbar(one_qubit_error_space, elem[1], yerr=elem[2], capsize=3.0, color=palette[idx*5], label="CNOT Count: "+str(elem[0]))
# ax.set_title(f"Compute Qubits: {str(number_of_qubits)}, Layers: {layers_count}")
ax.set_xlabel("Single Qubit Error", fontsize=12)
ax.set_ylabel("Average Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xscale("log")
ax.legend()
# ax.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_allCNOTS_counts_layers_{layers_count}_.png"))
def create_all_count_percentage_plots(base_path, number_of_qubits, cnot_counts, one_qubit_error_space, palette, NLINES):
'''Wrapper for create_count_percentage_plot(). Makes individual plots with all the lines and a single plot'''
plt.clf()
all_results=[]
for cnot_count in cnot_counts:
result=create_count_percentage_plot(base_path, number_of_qubits, cnot_count, one_qubit_error_space)
if result:
all_results.append(result)
for idx, elem in enumerate(all_results):
plt.errorbar(one_qubit_error_space, elem[1], yerr=elem[2], capsize=3.0, color=palette[idx*5], label="CNOT Count: "+str(elem[0]))
plt.title(f"Compute Qubits: {number_of_qubits}, Layers: 1")
plt.xlabel("Single Qubit Error")
plt.ylabel("Average Probability of Zero Measurement on Ancilla")
plt.xscale("log")
plt.legend()
plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_allCNOTS_counts_.png"))
def create_split_layers_fidelity_plot_cnots_subplots(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers, ax, rz_count):
'''Creates plots based on CNOT counts.'''
# Gets the file path of the script.
NLINES = len(layers)+1 #plus one for the zero checks.
# NLINES = 45
palette = sns.color_palette("viridis",NLINES)
for layer_idx, layer in enumerate(layers):
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
# print(all_sorted_circ_results)
if all_sorted_circ_results:
#Regular plots
# average_fidelity_gains=[]
# fidelity_gains=[]
# Each element contains results for one error value
fidelity_no_checks=[]
fidelity_with_checks=[]
# Looking at one circuit.
for circ_result in all_sorted_circ_results:
# circ_outcomes_temp=[]
# Looking at one error value for that circuit.
temp_fidelity_no_checks=[]
temp_fidelity_with_checks=[]
for result in circ_result:
temp_fidelity_no_checks.append(result["state_fidelity_no_checks_with_errors"])
temp_fidelity_with_checks.append(result["state_fidelity_with_checks_with_errors"])
# circ_outcomes_temp.append(result["state_fidelity_with_checks_with_errors"]-result["state_fidelity_no_checks_with_errors"])
# fidelity_gains.append(circ_outcomes_temp)
fidelity_no_checks.append(temp_fidelity_no_checks)
fidelity_with_checks.append(temp_fidelity_with_checks)
average_fidelity_with_checks=[sum(x)/len(x) for x in zip(*fidelity_with_checks)]
# if cnot_count==40:
# print(f"layer: {layer}, fidelity with check: {average_fidelity_with_checks}")
# print("raw fidelity", fidelity_gains)
# average_fidelity_with_checks=[sum(x)/len(x) for x in zip(*fidelity_with_checks)]
# print(f"average fidelity with checks {average_fidelity_with_checks}")
std_deviations_with_checks=[np.std(x) for x in zip(*fidelity_with_checks)]
# print(f"std deviation with checks {std_deviations_with_checks}")
average_fidelity_no_checks=[sum(x)/len(x) for x in zip(*fidelity_no_checks)]
# if cnot_count==40:
# print(f"layer: {layer}, fidelity with no check: {average_fidelity_no_checks}")
# print(f"average fidelity no checks {average_fidelity_no_checks}")
std_deviations_no_checks=[np.std(x) for x in zip(*fidelity_no_checks)]
# print(f"std deviation no checks {std_deviations_no_checks}")
if layer_idx==0:
ax.errorbar(one_qubit_error_space, average_fidelity_no_checks, std_deviations_no_checks, capsize=3.0, color=palette[0], label="Number of layers: 0")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_title(f"Compute Qubits: {number_of_qubits}, CNOTS: {cnot_count}, Rz: {rz_count}", fontsize=12)
ax.set_ylabel("Average Fidelity", fontsize=12)
ax.set_xscale("log")
# ax.legend()
# return (cnot_count, average_fidelity_gains, std_deviations)
def create_fidelity_vs_cnots_plots_filtered_zero_prob(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax):
# plt.clf()
nlines = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for layer_idx, layer in enumerate(layers):
average_fidelity_forlayers=[]
stddev_of_avg_fidelities_forlayer=[]
#No mitigation stuff.
average_fidelity_fornolayers=[]
stddev_of_avg_fidelities_fornolayers=[]
for cnot_idx, cnot_count in enumerate(cnot_counts):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
#Each element contains the result of one circuit.
temp_fidelities=[result[one_qubit_err_idx]["state_fidelity_with_checks_with_errors"] for result in all_sorted_circ_results
if result[0]["percent_results_after_postselect"]>0.001]
#Get the zero layers stuff.
if layer_idx==0:
#Each element contains the result of one circuit.
temp_fidelities_nolayers=[result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"] for result in all_sorted_circ_results
if result[0]["percent_results_after_postselect"]>0.001]
# Average over the results and append.
if temp_fidelities_nolayers:
average_fidelity_fornolayers.append(sum(temp_fidelities_nolayers)/len(temp_fidelities_nolayers))
stddev_of_avg_fidelities_fornolayers.append(np.std(temp_fidelities_nolayers))
#Average over the results and append.
if temp_fidelities:
average_fidelity_forlayers.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_fidelities_forlayer.append(np.std(temp_fidelities))
if layer_idx==0 and average_fidelity_fornolayers:
ax.errorbar(cnot_counts, average_fidelity_fornolayers, stddev_of_avg_fidelities_fornolayers, capsize=3.0, color=palette[layer_idx], label=f"Layers: {0}")
if average_fidelity_forlayers:
# layers_plot=list(range(1, len(average_fidelity_gains_forlayer)+1, 1))
ax.errorbar(cnot_counts, average_fidelity_forlayers, stddev_of_avg_fidelities_forlayer, capsize=3.0, color=palette[layer_idx+1], label=f"Layers: {layer}")
# if layer_idx==0:#len(layers)-1:
# print(f"layers {layer_idx}: has avg fidelities of: {average_fidelity_fornolayers}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_title(f"Compute Qubits: {number_of_qubits}, Single Qubit Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Fidelity", fontsize=12)
# ax.set_xscale("log")
# ax.legend()
def create_counts_vs_cnots_plots_filtered_zero_prob(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax):
# plt.clf()
nlines = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for layer_idx, layer in enumerate(layers):
average_counts_forlayer=[]
stddev_of_avg_counts_forlayer=[]
#No mitigation stuff.
average_counts_fornolayers=[]
stddev_of_avg_counts_fornolayers=[]
for cnot_idx, cnot_count in enumerate(cnot_counts):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
#Each element contains the result of one circuit.
temp_fidelities=[result[one_qubit_err_idx]["percent_results_after_postselect"] for result in all_sorted_circ_results
if result[0]["percent_results_after_postselect"]>0.001]
#Get the zero layers stuff.
if layer_idx==0:
#Each element contains the result of one circuit.
temp_counts_nolayers=[result[one_qubit_err_idx]["percent_results_before_postselect"] for result in all_sorted_circ_results
if result[0]["percent_results_after_postselect"]>0.001]
# Average over the results and append.
if temp_counts_nolayers:
average_counts_fornolayers.append(sum(temp_counts_nolayers)/len(temp_counts_nolayers))
stddev_of_avg_counts_fornolayers.append(np.std(temp_counts_nolayers))
if temp_fidelities:
average_counts_forlayer.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_counts_forlayer.append(np.std(temp_fidelities))
if layer_idx==0:
ax.errorbar(cnot_counts, average_counts_fornolayers, stddev_of_avg_counts_fornolayers, capsize=3.0, color=palette[layer_idx], label=f"Layers: {0}")
if average_counts_forlayer:
# layers_plot=list(range(1, len(average_counts_forlayer)+1, 1))
ax.errorbar(cnot_counts, average_counts_forlayer, stddev_of_avg_counts_forlayer, capsize=3.0, color=palette[layer_idx+1], label=f"Layers: {layer}")
# if layer_idx==len(layers)-1:
# print(f"layers {layer_idx+1}: has avg post selection prob of: {average_counts_forlayer}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
# ax.set_title(f"Compute Qubits: {number_of_qubits}, Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xlabel("Number of CNOTs", fontsize=12)
# ax.set_xscale("log")
ax.legend()
def create_fidelity_vs_cnots_plots(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax):
# plt.clf()
nlines = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for layer_idx, layer in enumerate(layers):
average_fidelity_forlayers=[]
stddev_of_avg_fidelities_forlayer=[]
#No mitigation stuff.
average_fidelity_fornolayers=[]
stddev_of_avg_fidelities_fornolayers=[]
for cnot_idx, cnot_count in enumerate(cnot_counts):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
#Each element contains the result of one circuit.
# temp_fidelities=[result[one_qubit_err_idx]["state_fidelity_with_checks_with_errors"] for result in all_sorted_circ_results]
temp_fidelities=[result[0]["state_fidelity_with_checks_with_errors"] for result in all_sorted_circ_results]
#Get the zero layers stuff.
if layer_idx==0:
#Each element contains the result of one circuit.
temp_fidelities_nolayers=[result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"] for result in all_sorted_circ_results]
temp_fidelities_nolayers=[result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"] for result in all_sorted_circ_results]
# Average over the results and append.
if temp_fidelities_nolayers:
average_fidelity_fornolayers.append(sum(temp_fidelities_nolayers)/len(temp_fidelities_nolayers))
stddev_of_avg_fidelities_fornolayers.append(np.std(temp_fidelities_nolayers))
#Average over the results and append.
if temp_fidelities:
average_fidelity_forlayers.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_fidelities_forlayer.append(np.std(temp_fidelities))
if layer_idx==0 and average_fidelity_fornolayers:
ax.errorbar(cnot_counts, average_fidelity_fornolayers, stddev_of_avg_fidelities_fornolayers, capsize=3.0, color=palette[layer_idx], label=f"Layers: {0}")
if average_fidelity_forlayers:
# layers_plot=list(range(1, len(average_fidelity_gains_forlayer)+1, 1))
ax.errorbar(cnot_counts, average_fidelity_forlayers, stddev_of_avg_fidelities_forlayer, capsize=3.0, color=palette[layer_idx+1], label=f"Layers: {layer}")
# if layer_idx==0:#len(layers)-1:
# print(f"layers {layer_idx}: has avg fidelities of: {average_fidelity_fornolayers}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_title(f"Compute Qubits: {number_of_qubits}, Single Qubit Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Fidelity", fontsize=12)
# ax.set_xscale("log")
# ax.legend()
def create_counts_vs_cnots_plots(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax):
# plt.clf()
nlines = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for layer_idx, layer in enumerate(layers):
average_counts_forlayer=[]
stddev_of_avg_counts_forlayer=[]
#No mitigation stuff.
average_counts_fornolayers=[]
stddev_of_avg_counts_fornolayers=[]
for cnot_idx, cnot_count in enumerate(cnot_counts):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
#Each element contains the result of one circuit.
temp_fidelities=[result[one_qubit_err_idx]["percent_results_after_postselect"] for result in all_sorted_circ_results]
#Get the zero layers stuff.
if layer_idx==0:
#Each element contains the result of one circuit.
temp_counts_nolayers=[result[one_qubit_err_idx]["percent_results_before_postselect"] for result in all_sorted_circ_results]
# Average over the results and append.
if temp_counts_nolayers:
average_counts_fornolayers.append(sum(temp_counts_nolayers)/len(temp_counts_nolayers))
stddev_of_avg_counts_fornolayers.append(np.std(temp_counts_nolayers))
if temp_fidelities:
average_counts_forlayer.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_counts_forlayer.append(np.std(temp_fidelities))
if layer_idx==0:
ax.errorbar(cnot_counts, average_counts_fornolayers, stddev_of_avg_counts_fornolayers, capsize=3.0, color=palette[layer_idx], label=f"Layers: {0}")
if average_counts_forlayer:
# layers_plot=list(range(1, len(average_counts_forlayer)+1, 1))
ax.errorbar(cnot_counts, average_counts_forlayer, stddev_of_avg_counts_forlayer, capsize=3.0, color=palette[layer_idx+1], label=f"Layers: {layer}")
# if layer_idx==len(layers)-1:
# print(f"layers {layer_idx+1}: has avg post selection prob of: {average_counts_forlayer}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
# ax.set_title(f"Compute Qubits: {number_of_qubits}, Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xlabel("Number of CNOTs", fontsize=12)
# ax.set_xscale("log")
ax.legend()
def create_allcnot_fidelity_vs_layers_plots(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax, rz_count):
# plt.clf()
nlines = len(cnot_counts)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for cnot_idx, cnot_count in enumerate(cnot_counts):
average_fidelity_gains_forcnot=[]
stddev_of_avg_fidelities_forcnot=[]
for layer_idx, layer in enumerate(layers):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
temp_fidelities=[result[one_qubit_err_idx]["state_fidelity_with_checks_with_errors"]-
result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"] for result in all_sorted_circ_results] #Each element contains the result of one circuit.
if layer_idx==0 and all_sorted_circ_results:
no_layers_fidelity_gain=[result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"]-
result[one_qubit_err_idx]["state_fidelity_no_checks_with_errors"] for result in all_sorted_circ_results]
average_fidelity_gains_forcnot.insert(0,sum(no_layers_fidelity_gain)/len(no_layers_fidelity_gain))
stddev_of_avg_fidelities_forcnot.insert(0, np.std(no_layers_fidelity_gain))
# for result in all_sorted_circ_results:
# #We only append the result for the specific error value.
# temp_fidelities.append(result[one_qubit_err_idx]["state_fidelity_with_checks_with_errors"])
# print(result[one_qubit_err_idx])
#Average over the results and append.
if temp_fidelities:
average_fidelity_gains_forcnot.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_fidelities_forcnot.append(np.std(temp_fidelities))
# print(len(average_fidelity_gains_forcnot))
# print(average_fidelity_gains_forcnot)
# print(stddev_of_avg_fidelities_forcnot)
if average_fidelity_gains_forcnot:
layers_plot=list(range(0, len(average_fidelity_gains_forcnot), 1))
# print(layers_plot)
ax.errorbar(layers_plot, average_fidelity_gains_forcnot, stddev_of_avg_fidelities_forcnot, capsize=3.0, color=palette[cnot_idx], label=f"CNOT Count: {cnot_count}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_title(f"Compute Qubits: {number_of_qubits}, Rz: {rz_count}, Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Fidelity Gain", fontsize=12)
# ax.set_xscale("log")
# ax.legend()
def create_allcnot_counts_vs_layers_plots(base_path, number_of_qubits, cnot_counts, one_qubit_err, one_qubit_err_idx, layers, ax):
# plt.clf()
nlines = len(cnot_counts)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", nlines)
# Get the results.
for cnot_idx, cnot_count in enumerate(cnot_counts):
average_counts_forcnot=[]
stddev_of_avg_counts_forcnot=[]
for layer_idx, layer in enumerate(layers):
# looking at current layer and cnot count.
# Each element in contains one circuit.
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
temp_fidelities=[result[one_qubit_err_idx]["percent_results_after_postselect"] for result in all_sorted_circ_results] #Each element contains the result of one circuit.
# for result in all_sorted_circ_results:
# #We only append the result for the specific error value.
# temp_fidelities.append(result[one_qubit_err_idx]["state_fidelity_with_checks_with_errors"])
# print(result[one_qubit_err_idx])
#Average over the results and append.
if layer_idx==0 and all_sorted_circ_results:
no_layers_postselect=[result[one_qubit_err_idx]["percent_results_before_postselect"] for result in all_sorted_circ_results]
average_counts_forcnot.insert(0,sum(no_layers_postselect)/len(no_layers_postselect))
stddev_of_avg_counts_forcnot.insert(0, np.std(no_layers_postselect))
if temp_fidelities:
average_counts_forcnot.append(sum(temp_fidelities)/len(temp_fidelities))
stddev_of_avg_counts_forcnot.append(np.std(temp_fidelities))
# if layer_idx==0:
if average_counts_forcnot:
layers_plot=list(range(0, len(average_counts_forcnot), 1))
ax.errorbar(layers_plot, average_counts_forcnot, stddev_of_avg_counts_forcnot, capsize=3.0, color=palette[cnot_idx], label=f"CNOT Count: {cnot_count}")
# # Each element of fidelity_with_checks is a complete circuit result. So plot each separately.
# for circ_idx, circ_result in enumerate(fidelity_with_checks):
# layer_count=circ_idx+1
# ax.errorbar(one_qubit_error_space, average_fidelity_with_checks, std_deviations_with_checks, capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
# ax.set_title(f"Compute Qubits: {number_of_qubits}, Error: {round(one_qubit_err, 5)}", fontsize=12)
ax.set_ylabel("Average Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xlabel("Number of Layers", fontsize=12)
# ax.set_xscale("log")
ax.legend()
def create_split_layers_count_percentage_subplots(base_path, number_of_qubits, cnot_count, one_qubit_error_space, layers, ax):
'''Plots the average zero outcome for ancilla vs the single qubit error rate.'''
'''Creates plots based on CNOT counts.'''
# Gets the file path of the script.
NLINES = len(layers)+1 #plus one for the zero checks.
palette = sns.color_palette("viridis", NLINES)
for layer_idx, layer in enumerate(layers):
all_sorted_circ_results=get_all_sorted_circ_results_for_layer(base_path, cnot_count, number_of_qubits, layer)
# print(all_sorted_circ_results)
if all_sorted_circ_results:
#Regular plots
# NLINES = 45
# each element is a circuit.
counts_after=[]
counts_before=[]
for circ_result in all_sorted_circ_results:
counts_after_temp=[]
counts_before_temp=[]
# each element here is one error idx for the circuit.
for result in circ_result:
# In our simulations we have the full density matrix so the percent results before post selecting is actually 1.
# We do it this way so the graphing can accomidate physical runs where these two values are actually counts.
counts_after_temp.append(result["percent_results_after_postselect"])
counts_before_temp.append(result["percent_results_before_postselect"])
counts_after.append(counts_after_temp)
counts_before.append(counts_before_temp)
average_counts_after=[sum(x)/len(x) for x in zip(*counts_after)]
# print(average_counts_diffs)
std_deviations_after=[np.std(x) for x in zip(*counts_after)]
# print(std_deviations)
if layer_idx==0:
ax.errorbar(one_qubit_error_space, counts_before[0], color=palette[0], capsize=3.0, label="Number of layers: 0")
# for layer_idx, circ_count_after in enumerate(counts_after):
# layer_count=layer_idx+1
ax.errorbar(one_qubit_error_space, average_counts_after, std_deviations_after,
capsize=3.0, color=palette[layer_idx+1], label=f"Number of layers: {layer}")
ax.set_xlabel("Single Qubit Error", fontsize=12)
ax.set_ylabel("Average Probability of Zero\n Measurement on All Ancillas", fontsize=12)
ax.set_xscale("log")
ax.legend()
def get_5qubits_fid_and_counts_vs_error_low_weightchks(rz_count, layers_all):
'''Creates fidelity vs error rate and post select prob vs error rate
on a single plot. Low weight checks.'''
cnot_counts=[1,5,10,15,20,25,30,35,40]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
NLINES = 45
palette = sns.color_palette("viridis",NLINES)
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=5
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "low_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
for layers in layers_all:
# if layers == 4:
# fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
fig, axs = plt.subplots(2,1, figsize=(7,10.5), sharex=True)
create_all_fidelity_plot_cnots_layers_rz(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, rz_count, axs[0])
create_all_count_percentage_plots_layers(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, axs[1])
plt.subplots_adjust(hspace=0.05)
# create_all_count_percentage_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, NLINES)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
# trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
trans = mtransforms.ScaledTranslation(-65/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH, f"qubits_{NUMBER_OF_QUBITS}_allCNOTS_fidelity_and_counts_layers_{layers}_rz_{rz_count}_.png"))
def create_all_maxweight_fidelity_vs_error(base_path, number_of_qubits, cnot_counts, one_qubit_error_space, palette, layers_count, rz_count):
plt.cla()
figure(figsize=(6.4, 4.8))
all_results=[]
for cnot_count in cnot_counts:
result=create_fidelity_plot_cnot(base_path, number_of_qubits, cnot_count, one_qubit_error_space)
if result:
all_results.append(result)
for idx, elem in enumerate(all_results):
# print(f"fidelity gain for cnots {cnot_counts[idx]}: {elem[1]}")
plt.errorbar(one_qubit_error_space, elem[1], yerr=elem[2], capsize=3.0, color=palette[idx*5], label="CNOT Count: "+str(elem[0]))
plt.title(f"Qubits: {str(number_of_qubits)}, Layers: {layers_count}, Rz: {rz_count}", fontsize=12)
plt.xlabel("Single Qubit Error", fontsize=12)
plt.ylabel("Average Fidelity Gain", fontsize=12)
plt.xscale("log")
plt.legend()
plt.savefig(os.path.join(base_path, f"qubits_{number_of_qubits}_allCNOTS_fidelity_rz_{rz_count}_maxweight_.png"))
def get_5qubits_fid_vs_error_max_weightchks(rz_count):
'''Creates fidelity vs error rate and post select prob vs error rate
on a single plot. High weight checks.'''
cnot_counts=[1,5,10,15,20,25,30,35,40]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
NLINES = 50
palette = sns.color_palette("viridis",NLINES)
layers_all=[1]
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=5
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "high_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
for layers in layers_all:
create_all_maxweight_fidelity_vs_error(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, rz_count)
def get_5qubits_fidelity_vs_layers(rz_count):
'''5 qubits and 10 qubits experiments.
Plots the average fidelity gain vs number of layers.
The single qubit error is fixed. All CNOTs are on each plot.'''
cnot_counts=[1,5,10,15,20,25,30,35,40]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
# NLINES = 45
# palette = sns.color_palette("viridis",NLINES)
layers_all=[1, 2, 3, 4, 5, 6]
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=5
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "low_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
for error_idx, error in enumerate(ONE_QUBIT_ERROR_SPACE):
if error_idx==16:
# if layers == 4:
fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
create_allcnot_fidelity_vs_layers_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[0], rz_count)
create_allcnot_counts_vs_layers_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[1])
plt.subplots_adjust(hspace=0.05)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH, f"qubits_{NUMBER_OF_QUBITS}_allCNOTS_fidelity_and_counts_vs_layers_rz_{rz_count}_.png"))
def get_2qubits_unit_fidel_ex():
'''Generates example plots for 2 qubits that give fidelity 1 for noise only effecting compute qubits.'''
cnot_count=30#[1,5,10,15,20,25,30,35,40]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE_2QUBIT_EXAMPLE=np.logspace(-5, -1, num=NUMBER_OF_ERROR_POINTS)
layers_all=[1, 2, 3, 4, 5, 6]
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=2
SUBDIR_2QUBITS_EXAMPLE=os.path.join("qubits_2_rz_0", "noiseless_checks_ex", "low_weight_checks", "results")
BASE_PATH_2QUBITS=os.path.join(CODE_DIR,SUBDIR_2QUBITS_EXAMPLE)
fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
create_2qubit_plot_example_fidelity(BASE_PATH_2QUBITS, NUMBER_OF_QUBITS, cnot_count, ONE_QUBIT_ERROR_SPACE_2QUBIT_EXAMPLE, layers_all, axs[0])
create_2qubit_plot_example_count_percentage(BASE_PATH_2QUBITS, NUMBER_OF_QUBITS, cnot_count, ONE_QUBIT_ERROR_SPACE_2QUBIT_EXAMPLE, layers_all, axs[1])
plt.subplots_adjust(hspace=0.05)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH_2QUBITS, f"qubits_{NUMBER_OF_QUBITS}_example_fidelity_and_counts_layers_all_.png"))
def get_5qubits_raw_fidel_and_counts_vs_error(rz_count):
'''5 qubits
Plots the average fidelity and post selection probability vs single qubit error.
CNOT count is fixed for each plot. All layers are on each plot.'''
cnot_counts=[40]
layers_all=[1, 2, 3, 4, 5, 6]
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=5
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "low_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
for cnot_count in cnot_counts:
plt.clf()
fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
create_split_layers_fidelity_plot_cnots_subplots(BASE_PATH, NUMBER_OF_QUBITS, cnot_count, ONE_QUBIT_ERROR_SPACE, layers_all, axs[0], rz_count)
create_split_layers_count_percentage_subplots(BASE_PATH, NUMBER_OF_QUBITS, cnot_count, ONE_QUBIT_ERROR_SPACE, layers_all, axs[1])
plt.subplots_adjust(hspace=0.05)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH, f"qubits_{NUMBER_OF_QUBITS}_CNOTS_{cnot_count}_fidelity_and_counts_layers_{layers_all[-1]}_rz_{rz_count}_.png"))
def get_2qubits_fidel_counts_vs_cnots():
''' Plots of fidelity vs CNOTs.
Plots the average fidelity gain vs number of layers.
The single qubit error is fixed.'''
cnot_counts=[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
layers_all=[1, 2, 3, 4]
CODE_DIR=sys.path[0]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
NUMBER_OF_QUBITS=2
SUBDIR_2QUBITS_EXAMPLE=os.path.join("qubits_2_rz_0", "low_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR_2QUBITS_EXAMPLE)
for error_idx, _ in enumerate(ONE_QUBIT_ERROR_SPACE):
if error_idx==14:
# if layers == 4:
fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
# create_fidelity_vs_cnots_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[0])
# create_counts_vs_cnots_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[1])
create_fidelity_vs_cnots_plots_filtered_zero_prob(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[0])
create_counts_vs_cnots_plots_filtered_zero_prob(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE[error_idx], error_idx, layers_all, axs[1])
plt.subplots_adjust(hspace=0.05)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH, f"qubits_{NUMBER_OF_QUBITS}_fidelity_and_counts_vs_cnots_.png"))
def get_10qubits_fid_and_counts_vs_error_low_weightchks(rz_count, layers_all):
'''Creates fidelity vs error rate and post select prob vs error rate
on a single plot. Low weight checks.'''
cnot_counts=[1,5,10,15,20,25,30,35,40, 80]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
NLINES = 50
palette = sns.color_palette("viridis",NLINES)
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=10
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "low_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
for layers in layers_all:
# if layers == 4:
# fig, axs = plt.subplots(2,1, figsize=(6.5,9.75), sharex=True)
fig, axs = plt.subplots(2,1, figsize=(7,10.5), sharex=True)
create_all_fidelity_plot_cnots_layers_rz(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, rz_count, axs[0])
create_all_count_percentage_plots_layers(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, axs[1])
plt.subplots_adjust(hspace=0.05)
# create_all_count_percentage_plots(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, NLINES)
str_labels=["(a)", "(b)"]
for idx, ax in enumerate(axs):
str_label=str_labels[idx]
# label physical distance to the left and up:
# trans = mtransforms.ScaledTranslation(-55/72, -10/72, fig.dpi_scale_trans)
trans = mtransforms.ScaledTranslation(-65/72, -10/72, fig.dpi_scale_trans)
ax.text(0.0, 1.0, str_label, transform=ax.transAxes + trans,
fontsize=16, weight="bold", va='center')
ax.autoscale(tight=False)
fig.savefig(os.path.join(BASE_PATH, f"qubits_{NUMBER_OF_QUBITS}_allCNOTS_fidelity_and_counts_layers_{layers}_rz_{rz_count}_.png"))
def get_10qubits_fid_vs_error_max_weightchks(rz_count):
'''Creates fidelity vs error rate and post select prob vs error rate
on a single plot. High weight checks.'''
cnot_counts=[1,5,10,15,20,25,30,35,40, 80]
NUMBER_OF_ERROR_POINTS=21
ONE_QUBIT_ERROR_SPACE=np.logspace(-5, -2, num=NUMBER_OF_ERROR_POINTS)
NLINES = 50
palette = sns.color_palette("viridis",NLINES)
layers_all=[1]
CODE_DIR=sys.path[0]
NUMBER_OF_QUBITS=10
SUBDIR=os.path.join(f"qubits_{NUMBER_OF_QUBITS}_rz_{rz_count}", "high_weight_checks", "results")
BASE_PATH=os.path.join(CODE_DIR,SUBDIR)
for layers in layers_all:
create_all_maxweight_fidelity_vs_error(BASE_PATH, NUMBER_OF_QUBITS, cnot_counts, ONE_QUBIT_ERROR_SPACE, palette, layers, rz_count)