/
ground_state_iter.py
51 lines (40 loc) · 1.12 KB
/
ground_state_iter.py
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
from __future__ import print_function
from mpi4py import MPI
import netket as nk
import sys
SEED = 3141592
# Constructing a 1d lattice
g = nk.graph.Hypercube(length=20, n_dim=1)
# Hilbert space of spins from given graph
hi = nk.hilbert.Spin(s=0.5, graph=g)
# Hamiltonian
ha = nk.operator.Ising(h=1.0, hilbert=hi)
# Machine
ma = nk.machine.RbmSpin(hilbert=hi, alpha=1)
ma.init_random_parameters(seed=SEED, sigma=0.01)
# Sampler
sa = nk.sampler.MetropolisLocal(machine=ma)
sa.seed(SEED)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.01)
# Variational Monte Carlo
vmc = nk.vmc.Vmc(
hamiltonian=ha,
sampler=sa,
optimizer=op,
n_samples=1000,
diag_shift=0.0,
method='Sr')
comm = MPI.COMM_WORLD
mpi_rank = comm.Get_rank()
for step in vmc.iter():
obs = vmc.get_observable_stats()
if mpi_rank == 0:
print("step={}".format(step))
print("acceptance={}".format(list(sa.acceptance)))
print("observables={}\n".format(obs))
# Print output to the console immediately
sys.stdout.flush()
# Save current parameters to file
ma.save('test.wf')
comm.barrier()