-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_beta_sweep.py
More file actions
283 lines (254 loc) · 9.53 KB
/
Copy pathparallel_beta_sweep.py
File metadata and controls
283 lines (254 loc) · 9.53 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
from pathlib import Path
import argparse
from concurrent.futures import ProcessPoolExecutor
from functools import partial
import numpy as np
from cooling_channel import construct_opset, transverse_ising_hamiltonian
from superoperator import (
check_if_TFIM_gibbs,
get_averaged_channel,
get_transition_generator_and_classical_populations,
get_normality_residual,
num_iterations,
get_superoperator_spectral_data,
linear_operator_to_dense,
next_running_number,
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--N", type=int, default=4)
parser.add_argument("--T", type=float, default=25.0)
parser.add_argument("--alpha", type=float, default=0.5)
parser.add_argument("--sigma", type=float, default=2.0)
parser.add_argument("--omega_max", type=float, default=2.5)
parser.add_argument("--tau", type=float, default=0.1)
parser.add_argument("--J", type=float, default=1.0)
parser.add_argument("--h", type=float, default=1.2)
parser.add_argument("--eps_fit", type=float, default=0.05)
parser.add_argument("--skip-iterations", action="store_true", help="Skip fixed-point iteration-count calculations.")
parser.add_argument("--beta_min", type=float, default=0.1)
parser.add_argument("--beta_max", type=float, default=10.0)
parser.add_argument("--beta_points", type=int, default=25)
parser.add_argument(
"--beta-values", type=float, nargs="+", default=None,
help="Explicit beta values. Overrides --beta_min, --beta_max, and --beta_points.",
)
parser.add_argument(
"--normalize_Jh",
help="Whether to normalize the Hamiltonian.",
action="store_true",
)
parser.add_argument(
"--save-channel",
help="Whether to store the full channel matrices in the .npz file.",
action="store_true",
)
parser.add_argument(
"--save-classical-populations",
help="Store the transition generator and classical population map.",
action="store_true",
)
parser.add_argument(
"--dense-spectrum",
help="Diagonalize the materialized channel matrix instead of using ARPACK.",
action="store_true",
)
parser.add_argument("--workers", type=int, default=None)
parser.add_argument("--data-dir", type=Path, default=Path("data"))
parser.add_argument("--save-as-nr", type=int, default=-1)
return parser.parse_args()
def validate_dense_size(N, dense_requested, *, max_dense_dim=4096):
d_so = 2 ** (2 * N)
if dense_requested and d_so > max_dense_dim:
raise ValueError(
f"Dense channel use would require a {d_so}x{d_so} matrix. "
f"Refusing because max_dense_dim={max_dense_dim}."
)
def save_sweep(sweep_data, snapshot_path, save_channel, save_classical_populations, dense_spectrum):
channel_entries = [entry["channel"] for entry in sweep_data] if save_channel else []
transition_generators = [entry["transition_generator"] for entry in sweep_data] if save_classical_populations else []
classical_populations = [entry["classical_populations"] for entry in sweep_data] if save_classical_populations else []
np.savez_compressed(
snapshot_path,
h=np.array([entry["h"] for entry in sweep_data]),
beta=np.array([entry["beta"] for entry in sweep_data]),
channels=np.stack(channel_entries) if save_channel else np.array([]),
transition_generator=np.stack(transition_generators) if save_classical_populations else np.array([]),
classical_populations=np.stack(classical_populations) if save_classical_populations else np.array([]),
eigvals=np.stack([entry["spectrum_data"]["eigvals"] for entry in sweep_data]),
Delta_sep=np.array([entry["spectrum_data"]["Delta_sep"] for entry in sweep_data]),
Delta_gap=np.array([entry["spectrum_data"]["Delta_gap"] for entry in sweep_data]),
Delta_th=np.array([entry["spectrum_data"]["Delta_th"] for entry in sweep_data]),
num_closer=np.array([entry["spectrum_data"]["num_closer"] for entry in sweep_data]),
trace_distance=np.array([entry["spectrum_data"]["trace_distance"] for entry in sweep_data]),
normality_residual=np.array([entry["spectrum_data"]["normality_residual"] for entry in sweep_data]),
num_iterations=np.array([entry["spectrum_data"]["num_iterations"] for entry in sweep_data]),
dense_spectrum=dense_spectrum,
save_classical_populations=save_classical_populations,
)
def compute_single_beta(
beta,
*,
N,
T,
alpha,
sigma,
omega_max,
tau,
J,
h,
eps_fit,
skip_iterations,
normalize_Jh,
save_channel,
save_classical_populations,
dense_spectrum,
):
print(f"Computing beta={beta:.4g}", flush=True)
J_hot, h_hot = J, h
if normalize_Jh:
H_norm = N * np.sqrt(J_hot**2 + h_hot**2)
J_hot = J_hot / H_norm
h_hot = h_hot / H_norm
op_set = construct_opset(N, type="XZ")
h_sys = transverse_ising_hamiltonian(J_hot, h_hot, N)
channel, channel_params = get_averaged_channel(
N,
tau,
T,
sigma,
op_set,
omega_max,
h_sys,
alpha,
beta,
)
analysis_channel = linear_operator_to_dense(channel) if dense_spectrum else channel
eigvals, fixedpoint, num_closer, Delta_sep, Delta_gap, Delta_th = get_superoperator_spectral_data(
analysis_channel,
beta,
[N, J_hot, h_hot],
full_spectrum=dense_spectrum,
)
spectral_success = np.all(np.isfinite(eigvals)) and np.all(np.isfinite(fixedpoint))
if spectral_success:
_, _, trace_distance = check_if_TFIM_gibbs(fixedpoint, beta, [N, J_hot, h_hot])
iteration_count = None if skip_iterations else num_iterations(analysis_channel, fixedpoint, eps=eps_fit)
num_closer_value = int(num_closer)
else:
print(f"Spectral computation failed for beta={beta:.4g}; storing NaN diagnostics", flush=True)
trace_distance = np.nan
iteration_count = None
num_closer_value = np.nan
normality_residual = get_normality_residual(analysis_channel)
if not skip_iterations:
print(f"iterations for eps={eps_fit:.4g}: {iteration_count}", flush=True)
result = {
"h": h,
"beta": beta,
"channel_params": channel_params,
"spectrum_data": {
"eigvals": eigvals,
"Delta_sep": float(Delta_sep),
"Delta_gap": float(Delta_gap),
"Delta_th": float(Delta_th),
"num_closer": num_closer_value,
"trace_distance": float(trace_distance),
"normality_residual": float(normality_residual),
"num_iterations": np.nan if iteration_count is None else float(iteration_count),
},
}
if save_channel:
result["channel"] = analysis_channel if dense_spectrum else linear_operator_to_dense(channel)
if save_classical_populations:
result["transition_generator"], result["classical_populations"] = get_transition_generator_and_classical_populations(
channel, h_sys, op_set, beta, omega_max, sigma
)
return result
def compute_sweep(
*,
N,
T,
alpha,
sigma,
omega_max,
tau,
J,
h,
eps_fit,
skip_iterations,
normalize_Jh,
beta_values,
save_channel,
save_classical_populations,
dense_spectrum,
workers,
):
worker = partial(
compute_single_beta,
N=N,
T=T,
alpha=alpha,
sigma=sigma,
omega_max=omega_max,
tau=tau,
J=J,
h=h,
eps_fit=eps_fit,
skip_iterations=skip_iterations,
normalize_Jh=normalize_Jh,
save_channel=save_channel,
save_classical_populations=save_classical_populations,
dense_spectrum=dense_spectrum,
)
with ProcessPoolExecutor(max_workers=workers) as executor:
return list(executor.map(worker, beta_values))
def main():
args = parse_args()
N = args.N
T = args.T
alpha = args.alpha
sigma = args.sigma
omega_max = args.omega_max
tau = args.tau
J = args.J
h = args.h
eps_fit = args.eps_fit
normalize_Jh = args.normalize_Jh
save_channel = args.save_channel
save_classical_populations = args.save_classical_populations
dense_spectrum = args.dense_spectrum
validate_dense_size(N, save_channel or dense_spectrum)
workers = args.workers
save_as_nr = args.save_as_nr
beta_values = np.asarray(args.beta_values, dtype=float) if args.beta_values is not None else np.geomspace(args.beta_min, args.beta_max, args.beta_points)
print("Sweep over beta:", beta_values)
data_dir = args.data_dir
data_dir.mkdir(parents=True, exist_ok=True)
if save_as_nr == -1:
snapshot_number = next_running_number(data_dir, "npz")
else:
snapshot_number = save_as_nr
snapshot_path = data_dir / f"superoperator_N{N}_beta_sweep_{snapshot_number}.npz"
print("File will be saved as", snapshot_path)
sweep_data = compute_sweep(
N=N,
T=T,
alpha=alpha,
sigma=sigma,
omega_max=omega_max,
tau=tau,
J=J,
h=h,
eps_fit=eps_fit,
skip_iterations=args.skip_iterations,
normalize_Jh=normalize_Jh,
beta_values=beta_values,
save_channel=save_channel,
save_classical_populations=save_classical_populations,
dense_spectrum=dense_spectrum,
workers=workers,
)
save_sweep(sweep_data, snapshot_path, save_channel, save_classical_populations, dense_spectrum)
if __name__ == "__main__":
main()