Skip to content

Commit ac8731a

Browse files
Nearly touching 99% on TFIM
1 parent 720d904 commit ac8731a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

scripts/tfim_validation.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,23 @@ def main():
324324
# Add up the square residuals:
325325
r_squared = result["l2_difference"] ** 2
326326

327+
n_bias = n_qubits + 1
328+
bias_0 = np.zeros(n_bias, dtype=np.float64)
327329
magnetization_0, sqr_magnetization_0 = 0, 0
328330
for key, value in experiment_probs[0].items():
329331
m = 0
332+
h = 0
330333
for _ in range(n_qubits):
331-
m += -1 if (key & 1) else 1
334+
if key & 1:
335+
m -= 1
336+
h += 1
337+
else:
338+
m += 1
332339
key >>= 1
333340
m /= n_qubits
334341
magnetization_0 += value * m
335342
sqr_magnetization_0 += value * m * m
343+
bias_0[h] += value
336344

337345
c_magnetization, c_sqr_magnetization = 0, 0
338346
for p in range(1 << n_qubits):
@@ -381,8 +389,7 @@ def main():
381389
theta_c = ((np.pi if J > 0 else -np.pi) / 2) if abs(zJ) <= sys.float_info.epsilon else np.arcsin(max(-1.0, min(1.0, h / zJ)))
382390

383391
# The magnetization components are weighted by (n+1) symmetric "bias" terms over possible Hamming weights.
384-
n_bias = n_qubits + 1
385-
bias = [0] * n_bias
392+
bias = np.zeros(n_bias, dtype=np.float64)
386393
if h <= sys.float_info.epsilon:
387394
# This agrees with small perturbations away from h = 0.
388395
d_magnetization = 1
@@ -413,14 +420,15 @@ def main():
413420
# Normalize the results for 1.0 total marginal probability.
414421
d_magnetization /= tot_n
415422
d_sqr_magnetization /= tot_n
416-
for q in range(n_qubits + 1):
417-
bias[q] /= tot_n
423+
bias /= tot_n
418424

419425
if J > 0:
420426
# This is antiferromagnetism.
421-
bias.reverse()
427+
bias = bias[::-1]
422428
d_magnetization = -d_magnetization
423429

430+
bias = model * bias + (1 - model) * bias_0
431+
424432
# The full 2^n marginal probabilities will be produced in the statistics calculation,
425433
# but notice that the global magnetization value only requires (n+1) dimensions of marginal probability,
426434
# the marginal probability per each Hilbert space basis dimension is trivial to calculate by closed form,

0 commit comments

Comments
 (0)