Obtaining the final wave function after RBM #1490
-
My code defines a 1D Heisenberg model and uses RBM to find the ground state. code: # Import netket library
import netket as nk
# # Import Json, this will be needed to load log files
import json
# # Helper libraries
import numpy as np
import matplotlib.pyplot as plt
import time
plt.figure(figsize=(15,12))
niter = 150
L = 8
for i in range(6):
#build state
g = nk.graph.Hypercube(length=L, n_dim=1, pbc=True)
hi = nk.hilbert.Spin(s=0.5, total_sz=0, N=g.n_nodes)
ha = nk.operator.Heisenberg(hilbert=hi, graph=g)
# RBM ansatz with alpha=1
ma = nk.models.RBM(alpha=1)
# Build the sampler
sa = nk.sampler.MetropolisExchange(hilbert=hi,graph=g)
# Optimizer
op = nk.optimizer.Sgd(learning_rate=0.01+0.01*i)
# Stochastic Reconfiguration
sr = nk.optimizer.SR(diag_shift=0.1)
# The variational state
vs = nk.vqs.MCState(sa, ma, n_samples=1000)
# The ground-state optimization loop
gs = nk.VMC(
hamiltonian=ha,
optimizer=op,
preconditioner=sr,
variational_state=vs)
start = time.time()
gs.run(out='RBM', n_iter=niter)
end = time.time()
print('### RBM calculation')
print('Has',vs.n_parameters,'parameters')
print('The RBM calculation took',end-start,'seconds')
# import the data from log file
data=json.load(open("RBM.log"))
# Extract the relevant information
iters_RBM = data["Energy"]["iters"]
energy_RBM = data["Energy"]["Mean"]
plt.subplot(2,3,i+1)
plt.plot(iters_RBM, energy_RBM, color='red', label='Energy (RBM)')
plt.plot(iters_RBM, [eigvals]*len(iters_RBM), '--', label = f'Exact = {round(eigvals[0],3)}',)
plt.title(f'learning_rate={round(0.01+0.01*i,2)}')
plt.ylabel('Energy')
plt.xlabel('Iteration')
# plt.axis([0,iters_RBM[-1],exact_gs_energy-0.03,exact_gs_energy+0.2])
# plt.axhline(y=exact_gs_energy, xmin=0,
# xmax=iters_RBM[-1], linewidth=2, color='k', label='Exact')
plt.legend()
plt.show() |
Beta Was this translation helpful? Give feedback.
Answered by
PhilipVinc
Jun 1, 2023
Replies: 1 comment 6 replies
-
|
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
lynkeryn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
vs.to_array()
unpacks the NQS into a dense vector, though this will only work on relatively small Hilbert spaces where the full vector can fit in memory.