forked from MarcSerraPeralta/variational_QMonteCarlo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (56 loc) · 2.13 KB
/
main.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
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
import numpy as np
import lib as lib
from inputs import *
#############################################
# HARMONIC OSCILLATOR
E_local_f = E_local_Harmonic_Oscillator
prob_density = prob_density_Harmonic_Oscillator
dim = 1 # dimension of configuration space
opt_args = {"method":"scan1D", "init_alpha":np.array([0.25]), "step":0.05, "final":np.array([0.75])}
# Monte Carlo integration params
N_steps = 30000
N_walkers = 400
N_skip = 4000
L_start = 5
N_cores = -1 # if set to -1, it uses all cores available
# Results saving params
file_name = "output.csv"
#############################################
data_list = []
alpha_list = []
trial_move = None
optimizer = lib.Optimizer(opt_args)
while not optimizer.converged:
data = lib.MC_integration(E_local_f, prob_density, optimizer.alpha, dim, N_steps=N_steps, N_walkers=N_walkers,
N_skip=N_skip, L_start=L_start, trial_move=trial_move, N_cores=N_cores)
if abs(data[2] - 0.5) >= 0.05: #if acceptance ratio deviates from 0.5 by 0.05 we change the trial move
trial_move = None
print("The trial move will be updated")
else:
trial_move = data[3]
alpha = optimizer.alpha
data_list += [data]
alpha_list += [alpha]
# UPDATE ALPHA
if opt_args["method"] == "scan1D":
optimizer.update_alpha()
print("E({:0.7f}) = {:0.15f} | var(E) = {:0.15f}".format(*alpha, *data))
if opt_args["method"] == "steepest_descent1D":
optimizer.update_alpha(data)
print("E({:0.7f}) = {:0.15f} | var(E) = {:0.15f}".format(*alpha, *data))
if opt_args["method"] == "steepest_descent_ANY_D":
# calculate gradient
if np.all(optimizer.alpha_old != None):
E = np.zeros(optimizer.dim_alpha)
for j in range(optimizer.dim_alpha):
e_j = np.zeros(optimizer.dim_alpha)
e_j[j] = 1
alpha_j = optimizer.alpha_old+optimizer.step*e_j
data_j = lib.MC_integration(E_local_f, prob_density, alpha_j, dim,
N_steps=N_steps, N_walkers=N_walkers, N_skip=N_skip, L_start=L_start, trial_move=trial_move)
E[j] = data_j[0]
else:
E = data[0]
optimizer.update_alpha(E)
print("E(", alpha,") = {:0.15f} | var(E) = {:0.15f}".format(data[0], data[1]))
lib.save(file_name, alpha_list, data_list)