-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantumdot.py
259 lines (196 loc) · 5.59 KB
/
quantumdot.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:light
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.15.1
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# +
import netket as nk
nk.config.netket_experimental_fft_autocorrelation = True
# %load_ext autoreload
# %autoreload 2
import numpy as np
import netket as nk
import netket.experimental as nkx
import jax
import jax.numpy as jnp
import flax
import matplotlib.pyplot as plt
from functools import partial
import json
import optax
import pickle
import matplotlib.pyplot as plt
from src.utils import tree_size_cumsum, is_complex_dtype, real_dtype, mpi_shuffle_along_axis
import warnings
from src.quantumdot.potential import quantum_dot_V0, quantum_dot_Vint
import os, sys
from netket.utils import mpi
import netket
from netket.utils.mpi import n_nodes, mpi_sum_jax
from netket.utils.mpi import mpi_sum as mpi_sum_np
# +
# this is an example script of the quantum dot model
# this file is a great simplification of the original scripts and
# intended for (1) illustrative purposes, and (2) providing reproducible subroutines
# so if anything is missing or deviating from the manuscript, please contact us
# -
DTYPE = jnp.complex128
# +
n_per_spin = (6, 0)
sdim = 2
N = sum(n_per_spin)
kappa_0 = 1.0
omega = 1.0
rs = 1.0
effective_scale = np.sqrt(N)
hi = nk.hilbert.Particle(n_per_spin, D=sdim, pbc=False)
# +
from src.quantumdot.model import (
HOOrbs, HOModel,
BasisJastrow, BasisOrbitalBackflow,
)
# take low numbers to run locally
n_max = 4
Ecutoff = 4
orbitals = HOOrbs(
n_max,
n_per_spin=n_per_spin,
param_dtype=DTYPE,
Ecutoff=Ecutoff,
)
n_max_jastrow = 2
Ecutoff_jastrow = 4
ma_jastrow = BasisJastrow(
n_max=n_max_jastrow,
n_per_spin=n_per_spin,
param_dtype=DTYPE,
Ecutoff=Ecutoff_jastrow,
)
n_max_backflow = 2
Ecutoff_backflow = 4
ma_backflow = BasisOrbitalBackflow(
n_orbitals=max(n_per_spin),
n_max=n_max_backflow,
n_per_spin=n_per_spin,
param_dtype=DTYPE,
Ecutoff=Ecutoff_backflow,
)
ma = HOModel(
orbitals,
n_per_spin,
sdim,
param_dtype=DTYPE,
jastrow=ma_jastrow,
orbital_backflow=ma_backflow,
)
print("MODEL:")
print(ma)
# +
from src.sampler import MetropolisGaussAdaptive
sigma = 0.16
sa_fn = partial(MetropolisGaussAdaptive, target_acceptance=0.6, initial_sigma=sigma)
sa = sa_fn(hi, n_chains=64, n_sweeps=hi.size*5, dtype=real_dtype(DTYPE))
print("SAMPLER = ", sa)
# -
n_samples = 1024 # higher in practice
vs = nk.vqs.MCState(sa, ma, n_samples=n_samples, n_discard_per_chain=4, chunk_size=16)
print("n_parameters:", vs.n_parameters)
if mpi.node_number == 0:
print(jax.tree_util.tree_map(lambda x: f"{x.shape} = {x.size} : {x.dtype}", vs.parameters))
# +
from src.operator import KineticEnergy
rs = 1.0
omega = 1.0
pot_fn_0 = partial(quantum_dot_V0, sdim=sdim, omega2=omega**2, m=rs**2)
epot_0 = nk.operator.PotentialEnergy(hi, pot_fn_0)
pot_fn_int = partial(quantum_dot_Vint, sdim=sdim, kappa=(kappa_0/rs))
epot_int = nk.operator.PotentialEnergy(hi, pot_fn_int)
ekin = KineticEnergy(hi, mass=rs**2)
ham = ekin + epot_0 + epot_int
# +
total_steps = 10 # higher in practice
lr_schedule = optax.piecewise_constant_schedule(
init_value=1e-1,
boundaries_and_scales={int(total_steps*0.3):0.2, int(total_steps*0.6):0.2, int(total_steps*0.95):0.2}
)
print("lr_schedule:", lr_schedule)
opt = nk.optimizer.Sgd(lr_schedule)
qgt = nk.optimizer.qgt.QGTJacobianPyTree(diag_shift=1e-5, holomorphic=True)
sr = nk.optimizer.SR(qgt)
gs = nk.driver.VMC(ham, opt, variational_state=vs, preconditioner=sr)
gs
# +
from src.quantumdot.model import get_ns_list
Efree = 0.
for ns in n_per_spin:
ns = np.array(get_ns_list(sdim, n_max))[:ns,:]
Efree += ns.sum() + sdim/2
print("Free QHO energy = ", Efree, Efree*omega)
# -
print("n_parameters:", vs.n_parameters)
gs.run(total_steps, out='vmc')
vs.n_samples = 1024 #512*1024
print("n_samples for tevo:", vs.n_samples)
# run hot
for i_hot in range(10):
print("i_hot = ", i_hot)
vs.reset()
vs.samples
print("ACCEPTANCE:", vs.sampler_state.acceptance, flush=True)
print("Final sampler_scale:", vs.sampler_state.rule_state, flush=True)
E0expect = vs.expect(ham)
dt = 1e-2
# +
kappa_t = 2.0
kappa_t, rs_t, omega_t, effective_scale_t = interpret_kappa(kappa_t)
Tmax = 1.0
ekin_t = KineticEnergy(hi, mass=rs**2)
epot_fn_0_t = partial(quantum_dot_V0, sdim=sdim, omega2=omega_t**2, m=rs**2)
epot_0_t = nk.operator.PotentialEnergy(hi, epot_fn_0_t)
pot_fn_int_t = partial(quantum_dot_Vint, sdim=sdim, kappa=(kappa_t/rs))
epot_int_t = nk.operator.PotentialEnergy(hi, pot_fn_int_t)
ham_obj_t = ekin_t + epot_0_t + epot_int_t
def ham_t(t):
return ham_obj_t
# +
# from netket.experimental.driver import TDVP
from src.solver import smooth_svd
from functools import partial
from netket.experimental.dynamics import RK45, Heun, RK23
# Create integrator for time propagation
integrator = Heun(
dt=float(dt),
)
print("Integrator = ", integrator)
qgt = nk.optimizer.qgt.QGTJacobianPyTree(holomorphic=True, diag_shift=0, diag_scale=0)
solver = partial(smooth_svd, acond=1e-8, rcond=1e-14)
print("QGT = ", qgt)
# Quenched hamiltonian: this has a different transverse field than `ha`
te = nkx.driver.TDVP(
ham_t,
variational_state=vs,
integrator=integrator,
t0=0,
qgt=qgt,
propagation_type='real',
error_norm="qgt",
linear_solver=solver
)
# +
print("Gradient:", vs.expect_and_grad(ham_t(0)))
# vs.chunk_size = 1
te.run(
T=Tmax,
out='tdvp',
show_progress=True,
)
# -