Repository navigation
Expand file tree
/
Copy patheloc.py
More file actions
110 lines (85 loc) · 3.38 KB
/
Copy patheloc.py
File metadata and controls
110 lines (85 loc) · 3.38 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
# %%
"""Reference implementation of the local-energy (Eloc) kernel for NQS.
Two versions of the same quantity are provided:
- ``eloc_naive``: recomputes the RBM wavefunction from scratch for every bond
flip. This is the easy-to-read, easy-to-verify baseline.
- ``eloc_lookup_table``: reuses theta and updates only the two entries that
change per bond flip.
Both take a spin configuration `s` (+-1 valued, shape (N,)), the RBM weights `W`
(shape (N, M)) and biases `b` (shape (M,)) with M = alpha*N, and a `bonds` array
(shape (nbonds, 2)) of 0-indexed site pairs. They return the local energy of the
Heisenberg exchange term only, in the same units as the couplings baked into
`bonds`/`s` (i.e. un-normalized; divide by N*4 to compare across system sizes,
see README.md).
The RBM wavefunction, the lookup-table update and the input helpers live in
`nqs_helpers.py`, which also lists the notation.
"""
import time
from nqs_helpers import (
delta_theta_pair_flip,
load_trained_rbm,
logpsi,
logpsi_from_theta,
random_rbm,
random_state,
square_lattice_bonds,
)
from numba import njit
import numpy as np
@njit
def eloc_naive(s, bonds, W, b, gamma=0.5):
"""Local energy via full re-evaluation of psi(s') per bond flip.
O(nbonds * N * M): for every bond, the whole matrix-vector product
s' @ W is recomputed from scratch inside logpsi.
"""
logpsi_s, _, _ = logpsi(s, W, b, gamma=gamma)
E_loc = 0.0
for k in range(bonds.shape[0]):
i, j = bonds[k, 0], bonds[k, 1]
E_loc += s[i] * s[j]
if s[i] != s[j]:
s_prime = s.copy()
s_prime[i] = -s_prime[i]
s_prime[j] = -s_prime[j]
logpsi_s_prime, _, _ = logpsi(s_prime, W, b, gamma=gamma)
E_loc -= 2 * np.exp(logpsi_s_prime - logpsi_s)
return E_loc
@njit
def eloc_lookup_table(s, alpha, bonds, W, b, gamma=0.5):
"""Local energy via incremental updates of theta (lookup-table method).
O(nbonds * M): theta is computed once; each bond flip only updates the
two terms of theta that depend on the flipped sites, so a flip costs
O(M) instead of O(N * M).
"""
logpsi_s, theta, _ = logpsi(s, W, b, gamma=gamma)
E_loc = 0.0
for k in range(bonds.shape[0]):
i, j = bonds[k, 0], bonds[k, 1]
E_loc += s[i] * s[j]
if s[i] != s[j]:
theta_prime = theta - delta_theta_pair_flip(s, i, j, W)
logpsi_s_prime = logpsi_from_theta(theta_prime, gamma)
E_loc -= 2 * np.exp(logpsi_s_prime - logpsi_s)
return E_loc
if __name__ == "__main__":
Lx, Ly, alpha = 16, 16, 2
N = Lx * Ly
bonds = square_lattice_bonds(Lx, Ly)
try:
W, b = load_trained_rbm(N, alpha)
print(f"using trained weights from data/weights/Ising_{N}_{alpha}_ti_*.csv")
except OSError:
W, b = random_rbm(N, alpha, seed=0)
print("data/weights not found -- using a random RBM instead")
for seed in range(10):
s = random_state(N, seed=seed)
start = time.time()
e_naive = eloc_naive(s, bonds, W, b) / (N * 4)
t_naive = time.time() - start
start = time.time()
e_lookup = eloc_lookup_table(s, alpha, bonds, W, b) / (N * 4)
t_lookup = time.time() - start
print(
f"seed={seed} naive={e_naive:.6f} ({t_naive * 1e3:.3f} ms) "
f"lookup_table={e_lookup:.6f} ({t_lookup * 1e3:.3f} ms)"
)