-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_simulations.py
More file actions
147 lines (115 loc) · 3.94 KB
/
run_simulations.py
File metadata and controls
147 lines (115 loc) · 3.94 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
import os
import time
import numpy as np
import pandas as pd
from simulations.classical_client import run_simulation as run_classical_simulation
from simulations.quantum_client import run_simulation as run_quantum_simulation
from utils.theory_verification import theory_THT
def generate_quantum_data(
shots,
readout_err,
p_trap,
p_dummies,
deltas=(np.pi / 4, np.pi / 2, np.pi / 4),
):
t0 = time.perf_counter()
(client_results, server_results, server_dummy_results, trap_total, trap_hits) = (
run_quantum_simulation(shots, p_trap, p_dummies, readout_err, deltas)
)
elapsed = time.perf_counter() - t0
client_total = sum(client_results.values())
client_p0 = client_results.get("0", 0) / max(1, client_total)
dummy_total = sum(server_dummy_results.values())
trap_error = None if trap_total == 0 else 1 - (trap_hits / trap_total)
gates = ["T1", "H", "T2"]
row = {
"shots": shots,
"c_p0": client_p0,
**{
f"server_{g}_p0": sr.get("0", sr.get(0, 0)) / max(1, sum(sr.values()))
for g, sr in zip(gates, server_results)
},
"p_trap": p_trap,
"p_dummies": p_dummies,
"d_total": dummy_total,
"t_total": trap_total,
"t_hits": trap_hits,
"t_err": trap_error,
"r_err": readout_err,
"elapsed": elapsed,
}
df = pd.DataFrame([row])
return df
def generate_classical_data(
shots,
readout_err,
p_chsh,
deltas=(np.pi / 4, np.pi / 2, np.pi / 4),
):
t0 = time.perf_counter()
(client_results, cnt_A, cnt_B, chsh_tests, chsh_hits) = run_classical_simulation(
shots, p_chsh, readout_err, deltas
)
elapsed = time.perf_counter() - t0
client_total = sum(client_results.values())
client_p0 = client_results.get("0", 0) / max(1, client_total)
serverA_total = sum(cnt_A.values())
serverA_p0 = cnt_A.get("0", 0) / max(1, serverA_total)
serverB_total = sum(cnt_B.values())
serverB_p0 = cnt_B.get("0", 0) / max(1, serverB_total)
p_pass = chsh_hits / chsh_tests if chsh_tests else 0
row = {
"shots": shots,
"c_p0": client_p0,
"sA_p0": serverA_p0,
"sB_p0": serverB_p0,
"p_pass": p_pass,
"p_chsh": p_chsh,
"r_err": readout_err,
"elapsed": elapsed,
}
df = pd.DataFrame([row])
return df
def generate_theory_data(
shots,
):
p0 = theory_THT(shots)
row = {
"shots": shots,
"p0": p0,
}
df = pd.DataFrame([row])
return df
if __name__ == "__main__":
os.makedirs("data/quantum", exist_ok=True)
os.makedirs("data/classical", exist_ok=True)
quantum_path = "data/quantum/quantum_data.csv"
classical_path = "data/classical/classical_data.csv"
theory_path = "data/theory/theory_data.csv"
initial_shots = 32
shots_multiplier = 2
rounds = 7
readout_err = 0
p_trap = 0.75
p_dummies = 0
p_chsh = p_trap
for i in range(rounds):
write_quantum_header = (
not os.path.exists(quantum_path) or os.path.getsize(quantum_path) == 0
)
write_classical_header = (
not os.path.exists(classical_path) or os.path.getsize(classical_path) == 0
)
write_theory_header = (
not os.path.exists(theory_path) or os.path.getsize(theory_path) == 0
)
generate_quantum_data(
initial_shots * (shots_multiplier**i), readout_err, p_trap, p_dummies
).to_csv(quantum_path, index=False, mode="a", header=write_quantum_header)
generate_classical_data(
initial_shots * (shots_multiplier**i), readout_err, p_chsh
).to_csv(classical_path, index=False, mode="a", header=write_classical_header)
generate_theory_data(initial_shots * (shots_multiplier**i)).to_csv(
theory_path, index=False, mode="a", header=write_theory_header
)
print(f"Completed round {i + 1}/{rounds}", flush=True)