1
0
mirror of https://github.com/TREX-CoE/qmc-lttc.git synced 2024-12-21 20:04:10 +01:00

Gaussian sampling

This commit is contained in:
Anthony Scemama 2021-01-07 11:07:18 +01:00
parent 8594cdfa39
commit 45bdaf2d41
6 changed files with 516 additions and 392 deletions

690
QMC.org
View File

@ -6,185 +6,185 @@
* Introduction * Introduction
We propose different exercises to understand quantum Monte Carlo (QMC) We propose different exercises to understand quantum Monte Carlo (QMC)
methods. In the first section, we propose to compute the energy of a methods. In the first section, we propose to compute the energy of a
hydrogen atom using numerical integration. The goal of this section is hydrogen atom using numerical integration. The goal of this section is
to introduce the /local energy/. to introduce the /local energy/.
Then we introduce the variational Monte Carlo (VMC) method which Then we introduce the variational Monte Carlo (VMC) method which
computes a statistical estimate of the expectation value of the energy computes a statistical estimate of the expectation value of the energy
associated with a given wave function. associated with a given wave function.
Finally, we introduce the diffusion Monte Carlo (DMC) method which Finally, we introduce the diffusion Monte Carlo (DMC) method which
gives the exact energy of the H$_2$ molecule. gives the exact energy of the H$_2$ molecule.
Code examples will be given in Python and Fortran. Whatever language Code examples will be given in Python and Fortran. Whatever language
can be chosen. can be chosen.
** Python ** Python
** Fortran ** Fortran
- 1.d0 - 1.d0
- external - external
- r(:) = 0.d0 - r(:) = 0.d0
- a = (/ 0.1, 0.2 /) - a = (/ 0.1, 0.2 /)
- size(x) - size(x)
* Numerical evaluation of the energy * Numerical evaluation of the energy
In this section we consider the Hydrogen atom with the following In this section we consider the Hydrogen atom with the following
wave function: wave function:
$$ $$
\Psi(\mathbf{r}) = \exp(-a |\mathbf{r}|) \Psi(\mathbf{r}) = \exp(-a |\mathbf{r}|)
$$ $$
We will first verify that $\Psi$ is an eigenfunction of the Hamiltonian We will first verify that $\Psi$ is an eigenfunction of the Hamiltonian
$$ $$
\hat{H} = \hat{T} + \hat{V} = - \frac{1}{2} \Delta - \frac{1}{|\mathbf{r}|} \hat{H} = \hat{T} + \hat{V} = - \frac{1}{2} \Delta - \frac{1}{|\mathbf{r}|}
$$ $$
when $a=1$, by checking that $\hat{H}\Psi(\mathbf{r}) = E\Psi(\mathbf{r})$ for when $a=1$, by checking that $\hat{H}\Psi(\mathbf{r}) = E\Psi(\mathbf{r})$ for
all $\mathbf{r}$: we will check that the local energy, defined as all $\mathbf{r}$: we will check that the local energy, defined as
$$ $$
E_L(\mathbf{r}) = \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}, E_L(\mathbf{r}) = \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})},
$$ $$
is constant. is constant.
** Local energy ** Local energy
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle hydrogen.py :header-args:python: :tangle hydrogen.py
:header-args:f90: :tangle hydrogen.f90 :header-args:f90: :tangle hydrogen.f90
:END: :END:
*** Write a function which computes the potential at $\mathbf{r}$ *** Write a function which computes the potential at $\mathbf{r}$
The function accepts a 3-dimensional vector =r= as input arguments The function accepts a 3-dimensional vector =r= as input arguments
and returns the potential. and returns the potential.
$\mathbf{r}=\sqrt{x^2 + y^2 + z^2})$, so $\mathbf{r}=\sqrt{x^2 + y^2 + z^2})$, so
$$ $$
V(x,y,z) = -\frac{1}{\sqrt{x^2 + y^2 + z^2})$ V(x,y,z) = -\frac{1}{\sqrt{x^2 + y^2 + z^2})$
$$ $$
#+BEGIN_SRC python :results none #+BEGIN_SRC python :results none
import numpy as np import numpy as np
def potential(r): def potential(r):
return -1. / np.sqrt(np.dot(r,r)) return -1. / np.sqrt(np.dot(r,r))
#+END_SRC #+END_SRC
#+BEGIN_SRC f90 #+BEGIN_SRC f90
double precision function potential(r) double precision function potential(r)
implicit none implicit none
double precision, intent(in) :: r(3) double precision, intent(in) :: r(3)
potential = -1.d0 / dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ) potential = -1.d0 / dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) )
end function potential end function potential
#+END_SRC #+END_SRC
*** Write a function which computes the wave function at $\mathbf{r}$ *** Write a function which computes the wave function at $\mathbf{r}$
The function accepts a scalar =a= and a 3-dimensional vector =r= as The function accepts a scalar =a= and a 3-dimensional vector =r= as
input arguments, and returns a scalar. input arguments, and returns a scalar.
#+BEGIN_SRC python :results none #+BEGIN_SRC python :results none
def psi(a, r): def psi(a, r):
return np.exp(-a*np.sqrt(np.dot(r,r))) return np.exp(-a*np.sqrt(np.dot(r,r)))
#+END_SRC #+END_SRC
#+BEGIN_SRC f90 #+BEGIN_SRC f90
double precision function psi(a, r) double precision function psi(a, r)
implicit none implicit none
double precision, intent(in) :: a, r(3) double precision, intent(in) :: a, r(3)
psi = dexp(-a * dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) )) psi = dexp(-a * dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ))
end function psi end function psi
#+END_SRC #+END_SRC
*** Write a function which computes the local kinetic energy at $\mathbf{r}$ *** Write a function which computes the local kinetic energy at $\mathbf{r}$
The function accepts =a= and =r= as input arguments and returns the The function accepts =a= and =r= as input arguments and returns the
local kinetic energy. local kinetic energy.
The local kinetic energy is defined as $$-\frac{1}{2}\frac{\Delta \Psi}{\Psi}$$. The local kinetic energy is defined as $$-\frac{1}{2}\frac{\Delta \Psi}{\Psi}$$.
$$ $$
\Psi(x,y,z) = \exp(-a\,\sqrt{x^2 + y^2 + z^2}). \Psi(x,y,z) = \exp(-a\,\sqrt{x^2 + y^2 + z^2}).
$$ $$
We differentiate $\Psi$ with respect to $x$: We differentiate $\Psi$ with respect to $x$:
$$ $$
\frac{\partial \Psi}{\partial x} \frac{\partial \Psi}{\partial x}
= \frac{\partial \Psi}{\partial r} \frac{\partial r}{\partial x} = \frac{\partial \Psi}{\partial r} \frac{\partial r}{\partial x}
= - \frac{a\,x}{|\mathbf{r}|} \Psi(x,y,z) = - \frac{a\,x}{|\mathbf{r}|} \Psi(x,y,z)
$$ $$
and we differentiate a second time: and we differentiate a second time:
$$ $$
\frac{\partial^2 \Psi}{\partial x^2} = \frac{\partial^2 \Psi}{\partial x^2} =
\left( \frac{a^2\,x^2}{|\mathbf{r}|^2} - \frac{a(y^2+z^2)}{|\mathbf{r}|^{3}} \right) \Psi(x,y,z). \left( \frac{a^2\,x^2}{|\mathbf{r}|^2} - \frac{a(y^2+z^2)}{|\mathbf{r}|^{3}} \right) \Psi(x,y,z).
$$ $$
The Laplacian operator $\Delta = \frac{\partial^2}{\partial x^2} + The Laplacian operator $\Delta = \frac{\partial^2}{\partial x^2} +
\frac{\partial^2}{\partial y^2} + \frac{\partial^2}{\partial z^2}$ \frac{\partial^2}{\partial y^2} + \frac{\partial^2}{\partial z^2}$
applied to the wave function gives: applied to the wave function gives:
$$ $$
\Delta \Psi (x,y,z) = \left(a^2 - \frac{2a}{\mathbf{|r|}} \right) \Psi(x,y,z) \Delta \Psi (x,y,z) = \left(a^2 - \frac{2a}{\mathbf{|r|}} \right) \Psi(x,y,z)
$$ $$
So the local kinetic energy is So the local kinetic energy is
$$ $$
-\frac{1}{2} \frac{\Delta \Psi}{\Psi} (x,y,z) = -\frac{1}{2}\left(a^2 - \frac{2a}{\mathbf{|r|}} \right) -\frac{1}{2} \frac{\Delta \Psi}{\Psi} (x,y,z) = -\frac{1}{2}\left(a^2 - \frac{2a}{\mathbf{|r|}} \right)
$$ $$
#+BEGIN_SRC python :results none #+BEGIN_SRC python :results none
def kinetic(a,r): def kinetic(a,r):
return -0.5 * (a**2 - (2.*a)/np.sqrt(np.dot(r,r))) return -0.5 * (a**2 - (2.*a)/np.sqrt(np.dot(r,r)))
#+END_SRC #+END_SRC
#+BEGIN_SRC f90 #+BEGIN_SRC f90
double precision function kinetic(a,r) double precision function kinetic(a,r)
implicit none implicit none
double precision, intent(in) :: a, r(3) double precision, intent(in) :: a, r(3)
kinetic = -0.5d0 * (a*a - (2.d0*a) / & kinetic = -0.5d0 * (a*a - (2.d0*a) / &
dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ) ) dsqrt( r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ) )
end function kinetic end function kinetic
#+END_SRC #+END_SRC
*** Write a function which computes the local energy at $\mathbf{r}$ *** Write a function which computes the local energy at $\mathbf{r}$
The function accepts =x,y,z= as input arguments and returns the The function accepts =x,y,z= as input arguments and returns the
local energy. local energy.
$$ $$
E_L(x,y,z) = -\frac{1}{2} \frac{\Delta \Psi}{\Psi} (x,y,z) + V(x,y,z) E_L(x,y,z) = -\frac{1}{2} \frac{\Delta \Psi}{\Psi} (x,y,z) + V(x,y,z)
$$ $$
#+BEGIN_SRC python :results none #+BEGIN_SRC python :results none
def e_loc(a,r): def e_loc(a,r):
return kinetic(a,r) + potential(r) return kinetic(a,r) + potential(r)
#+END_SRC #+END_SRC
#+BEGIN_SRC f90 #+BEGIN_SRC f90
double precision function e_loc(a,r) double precision function e_loc(a,r)
implicit none implicit none
double precision, intent(in) :: a, r(3) double precision, intent(in) :: a, r(3)
double precision, external :: kinetic, potential double precision, external :: kinetic, potential
e_loc = kinetic(a,r) + potential(r) e_loc = kinetic(a,r) + potential(r)
end function e_loc end function e_loc
#+END_SRC #+END_SRC
** Plot the local energy along the x axis ** Plot the local energy along the x axis
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle plot_hydrogen.py :header-args:python: :tangle plot_hydrogen.py
:header-args:f90: :tangle plot_hydrogen.f90 :header-args:f90: :tangle plot_hydrogen.f90
:END: :END:
For multiple values of $a$ (0.1, 0.2, 0.5, 1., 1.5, 2.), plot the For multiple values of $a$ (0.1, 0.2, 0.5, 1., 1.5, 2.), plot the
local energy along the $x$ axis. local energy along the $x$ axis.
#+begin_src python :results output #+begin_src python :results output
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@ -206,14 +206,14 @@ plt.tight_layout()
plt.legend() plt.legend()
plt.savefig("plot_py.png") plt.savefig("plot_py.png")
#+end_src #+end_src
#+RESULTS: #+RESULTS:
[[./plot_py.png]] [[./plot_py.png]]
#+begin_src f90 #+begin_src f90
program plot program plot
implicit none implicit none
double precision, external :: e_loc double precision, external :: e_loc
@ -242,20 +242,20 @@ program plot
end do end do
end program plot end program plot
#+end_src #+end_src
To compile and run: To compile and run:
#+begin_src sh :exports both #+begin_src sh :exports both
gfortran hydrogen.f90 plot_hydrogen.f90 -o plot_hydrogen gfortran hydrogen.f90 plot_hydrogen.f90 -o plot_hydrogen
./plot_hydrogen > data ./plot_hydrogen > data
#+end_src #+end_src
#+RESULTS: #+RESULTS:
To plot the data using gnuplot" To plot the data using gnuplot"
#+begin_src gnuplot :file plot.png :exports both #+begin_src gnuplot :file plot.png :exports both
set grid set grid
set xrange [-5:5] set xrange [-5:5]
set yrange [-2:1] set yrange [-2:1]
@ -265,39 +265,39 @@ plot './data' index 0 using 1:2 with lines title 'a=0.1', \
'./data' index 3 using 1:2 with lines title 'a=1.0', \ './data' index 3 using 1:2 with lines title 'a=1.0', \
'./data' index 4 using 1:2 with lines title 'a=1.5', \ './data' index 4 using 1:2 with lines title 'a=1.5', \
'./data' index 5 using 1:2 with lines title 'a=2.0' './data' index 5 using 1:2 with lines title 'a=2.0'
#+end_src #+end_src
#+RESULTS: #+RESULTS:
[[file:plot.png]] [[file:plot.png]]
** Compute numerically the average energy ** Compute numerically the average energy
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle energy_hydrogen.py :header-args:python: :tangle energy_hydrogen.py
:header-args:f90: :tangle energy_hydrogen.f90 :header-args:f90: :tangle energy_hydrogen.f90
:END: :END:
We want to compute We want to compute
\begin{eqnarray} \begin{eqnarray}
E & = & \frac{\langle \Psi| \hat{H} | \Psi\rangle}{\langle \Psi |\Psi \rangle} \\ E & = & \frac{\langle \Psi| \hat{H} | \Psi\rangle}{\langle \Psi |\Psi \rangle} \\
& = & \frac{\int \Psi(\mathbf{r})\, \hat{H} \Psi(\mathbf{r})\, d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}} \\ & = & \frac{\int \Psi(\mathbf{r})\, \hat{H} \Psi(\mathbf{r})\, d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}} \\
& = & \frac{\int \left[\Psi(\mathbf{r})\right]^2\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}} & = & \frac{\int \left[\Psi(\mathbf{r})\right]^2\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}}
\end{eqnarray} \end{eqnarray}
If the space is discretized in small volume elements $\delta If the space is discretized in small volume elements $\delta
\mathbf{r}$, this last equation corresponds to a weighted average of \mathbf{r}$, this last equation corresponds to a weighted average of
the local energy, where the weights are the values of the square of the local energy, where the weights are the values of the square of
the wave function at $\mathbf{r}$ multiplied by the volume element: the wave function at $\mathbf{r}$ multiplied by the volume element:
$$ $$
E \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\; E \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\;
w_i = \left[\Psi(\mathbf{r}_i)\right]^2 \delta \mathbf{r} w_i = \left[\Psi(\mathbf{r}_i)\right]^2 \delta \mathbf{r}
$$ $$
We now compute an numerical estimate of the energy in a grid of We now compute an numerical estimate of the energy in a grid of
$50\times50\times50$ points in the range $(-5,-5,-5) \le \mathbf{r} \le (5,5,5)$. $50\times50\times50$ points in the range $(-5,-5,-5) \le \mathbf{r} \le (5,5,5)$.
Note: the energy is biased because: Note: the energy is biased because:
- The energy is evaluated only inside the box - The energy is evaluated only inside the box
- The volume elements are not infinitely small - The volume elements are not infinitely small
@ -305,12 +305,12 @@ Note: the energy is biased because:
import numpy as np import numpy as np
from hydrogen import e_loc, psi from hydrogen import e_loc, psi
interval = np.linspace(-5,5,num=50) interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3 delta = (interval[1]-interval[0])**3
r = np.array([0.,0.,0.]) r = np.array([0.,0.,0.])
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]: for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
E = 0. E = 0.
norm = 0. norm = 0.
for x in interval: for x in interval:
@ -338,7 +338,7 @@ for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
: a = 2.0 E = -0.08086980667844901 : a = 2.0 E = -0.08086980667844901
#+begin_src f90 #+begin_src f90
program energy_hydrogen program energy_hydrogen
implicit none implicit none
double precision, external :: e_loc, psi double precision, external :: e_loc, psi
@ -378,43 +378,43 @@ program energy_hydrogen
end do end do
end program energy_hydrogen end program energy_hydrogen
#+end_src #+end_src
To compile and run: To compile and run:
#+begin_src sh :results output :exports both #+begin_src sh :results output :exports both
gfortran hydrogen.f90 energy_hydrogen.f90 -o energy_hydrogen gfortran hydrogen.f90 energy_hydrogen.f90 -o energy_hydrogen
./energy_hydrogen ./energy_hydrogen
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: a = 0.10000000000000001 E = -0.24518438948809140 : a = 0.10000000000000001 E = -0.24518438948809140
: a = 0.20000000000000001 E = -0.26966057967803236 : a = 0.20000000000000001 E = -0.26966057967803236
: a = 0.50000000000000000 E = -0.38563576125173815 : a = 0.50000000000000000 E = -0.38563576125173815
: a = 1.0000000000000000 E = -0.50000000000000000 : a = 1.0000000000000000 E = -0.50000000000000000
: a = 1.5000000000000000 E = -0.39242967082602065 : a = 1.5000000000000000 E = -0.39242967082602065
: a = 2.0000000000000000 E = -8.0869806678448772E-002 : a = 2.0000000000000000 E = -8.0869806678448772E-002
** Compute the variance of the local energy ** Compute the variance of the local energy
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle variance_hydrogen.py :header-args:python: :tangle variance_hydrogen.py
:header-args:f90: :tangle variance_hydrogen.f90 :header-args:f90: :tangle variance_hydrogen.f90
:END: :END:
The variance of the local energy measures the magnitude of the The variance of the local energy measures the magnitude of the
fluctuations of the local energy around the average. If the local fluctuations of the local energy around the average. If the local
energy is constant (i.e. $\Psi$ is an eigenfunction of $\hat{H}$) energy is constant (i.e. $\Psi$ is an eigenfunction of $\hat{H}$)
the variance is zero. the variance is zero.
$$ $$
\sigma^2(E_L) = \frac{\int \left[\Psi(\mathbf{r})\right]^2\, \left[ \sigma^2(E_L) = \frac{\int \left[\Psi(\mathbf{r})\right]^2\, \left[
E_L(\mathbf{r}) - E \right]^2 \, d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}} E_L(\mathbf{r}) - E \right]^2 \, d\mathbf{r}}{\int \left[\Psi(\mathbf{r}) \right]^2 d\mathbf{r}}
$$ $$
Compute a numerical estimate of the variance of the local energy Compute a numerical estimate of the variance of the local energy
in a grid of $50\times50\times50$ points in the range $(-5,-5,-5) \le \mathbf{r} \le (5,5,5)$. in a grid of $50\times50\times50$ points in the range $(-5,-5,-5) \le \mathbf{r} \le (5,5,5)$.
#+BEGIN_SRC python :results output :exports both #+BEGIN_SRC python :results output :exports both
import numpy as np import numpy as np
from hydrogen import e_loc, psi from hydrogen import e_loc, psi
@ -452,18 +452,18 @@ for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
s2 = s2 / norm s2 = s2 / norm
print(f"a = {a} \t E = {E:10.8f} \t \sigma^2 = {s2:10.8f}") print(f"a = {a} \t E = {E:10.8f} \t \sigma^2 = {s2:10.8f}")
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: a = 0.1 E = -0.24518439 \sigma^2 = 0.02696522 : a = 0.1 E = -0.24518439 \sigma^2 = 0.02696522
: a = 0.2 E = -0.26966058 \sigma^2 = 0.03719707 : a = 0.2 E = -0.26966058 \sigma^2 = 0.03719707
: a = 0.5 E = -0.38563576 \sigma^2 = 0.05318597 : a = 0.5 E = -0.38563576 \sigma^2 = 0.05318597
: a = 0.9 E = -0.49435710 \sigma^2 = 0.00577812 : a = 0.9 E = -0.49435710 \sigma^2 = 0.00577812
: a = 1.0 E = -0.50000000 \sigma^2 = 0.00000000 : a = 1.0 E = -0.50000000 \sigma^2 = 0.00000000
: a = 1.5 E = -0.39242967 \sigma^2 = 0.31449671 : a = 1.5 E = -0.39242967 \sigma^2 = 0.31449671
: a = 2.0 E = -0.08086981 \sigma^2 = 1.80688143 : a = 2.0 E = -0.08086981 \sigma^2 = 1.80688143
#+begin_src f90 #+begin_src f90
program variance_hydrogen program variance_hydrogen
implicit none implicit none
double precision, external :: e_loc, psi double precision, external :: e_loc, psi
@ -521,69 +521,68 @@ program variance_hydrogen
end do end do
end program variance_hydrogen end program variance_hydrogen
#+end_src #+end_src
To compile and run: To compile and run:
#+begin_src sh :results output :exports both #+begin_src sh :results output :exports both
gfortran hydrogen.f90 variance_hydrogen.f90 -o variance_hydrogen gfortran hydrogen.f90 variance_hydrogen.f90 -o variance_hydrogen
./variance_hydrogen ./variance_hydrogen
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: a = 0.10000000000000001 E = -0.24518438948809140 s2 = 2.6965218719733813E-002 : a = 0.10000000000000001 E = -0.24518438948809140 s2 = 2.6965218719733813E-002
: a = 0.20000000000000001 E = -0.26966057967803236 s2 = 3.7197072370217653E-002 : a = 0.20000000000000001 E = -0.26966057967803236 s2 = 3.7197072370217653E-002
: a = 0.50000000000000000 E = -0.38563576125173815 s2 = 5.3185967578488862E-002 : a = 0.50000000000000000 E = -0.38563576125173815 s2 = 5.3185967578488862E-002
: a = 1.0000000000000000 E = -0.50000000000000000 s2 = 0.0000000000000000 : a = 1.0000000000000000 E = -0.50000000000000000 s2 = 0.0000000000000000
: a = 1.5000000000000000 E = -0.39242967082602065 s2 = 0.31449670909180444 : a = 1.5000000000000000 E = -0.39242967082602065 s2 = 0.31449670909180444
: a = 2.0000000000000000 E = -8.0869806678448772E-002 s2 = 1.8068814270851303 : a = 2.0000000000000000 E = -8.0869806678448772E-002 s2 = 1.8068814270851303
* Variational Monte Carlo * Variational Monte Carlo
Instead of computing the average energy as a numerical integration Instead of computing the average energy as a numerical integration
on a grid, we will do a Monte Carlo sampling, which is an extremely on a grid, we will do a Monte Carlo sampling, which is an extremely
efficient method to compute integrals when the number of dimensions is efficient method to compute integrals when the number of dimensions is
large. large.
Moreover, a Monte Carlo sampling will alow us to remove the bias due
to the discretization of space, and compute a statistical confidence
interval.
Moreover, a Monte Carlo sampling will alow us to remove the bias due
to the discretization of space, and compute a statistical confidence
interval.
** Computation of the statistical error ** Computation of the statistical error
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle qmc_stats.py :header-args:python: :tangle qmc_stats.py
:header-args:f90: :tangle qmc_stats.f90 :header-args:f90: :tangle qmc_stats.f90
:END: :END:
To compute the statistical error, you need to perform $M$ To compute the statistical error, you need to perform $M$
independent Monte Carlo calculations. You will obtain $M$ different independent Monte Carlo calculations. You will obtain $M$ different
estimates of the energy, which are expected to have a Gaussian estimates of the energy, which are expected to have a Gaussian
distribution by the central limit theorem. distribution by the central limit theorem.
The estimate of the energy is The estimate of the energy is
$$ $$
E = \frac{1}{M} \sum_{i=1}^M E_M E = \frac{1}{M} \sum_{i=1}^M E_M
$$ $$
The variance of the average energies can be computed as The variance of the average energies can be computed as
$$ $$
\sigma^2 = \frac{1}{M-1} \sum_{i=1}^{M} (E_M - E)^2 \sigma^2 = \frac{1}{M-1} \sum_{i=1}^{M} (E_M - E)^2
$$ $$
And the confidence interval is given by And the confidence interval is given by
$$ $$
E \pm \delta E, \text{ where } \delta E = \frac{\sigma}{\sqrt{M}} E \pm \delta E, \text{ where } \delta E = \frac{\sigma}{\sqrt{M}}
$$ $$
Write a function returning the average and statistical error of an Write a function returning the average and statistical error of an
input array. input array.
#+BEGIN_SRC python #+BEGIN_SRC python
from math import sqrt from math import sqrt
def ave_error(arr): def ave_error(arr):
M = len(arr) M = len(arr)
@ -591,9 +590,9 @@ def ave_error(arr):
average = sum(arr)/M average = sum(arr)/M
variance = 1./(M-1) * sum( [ (x - average)**2 for x in arr ] ) variance = 1./(M-1) * sum( [ (x - average)**2 for x in arr ] )
return (average, sqrt(variance/M)) return (average, sqrt(variance/M))
#+END_SRC #+END_SRC
#+BEGIN_SRC f90 #+BEGIN_SRC f90
subroutine ave_error(x,n,ave,err) subroutine ave_error(x,n,ave,err)
implicit none implicit none
integer, intent(in) :: n integer, intent(in) :: n
@ -609,23 +608,23 @@ subroutine ave_error(x,n,ave,err)
err = dsqrt(variance/dble(n)) err = dsqrt(variance/dble(n))
endif endif
end subroutine ave_error end subroutine ave_error
#+END_SRC #+END_SRC
** Uniform sampling in the box ** Uniform sampling in the box
:PROPERTIES: :PROPERTIES:
:header-args:python: :tangle qmc_uniform.py :header-args:python: :tangle qmc_uniform.py
:header-args:f90: :tangle qmc_uniform.f90 :header-args:f90: :tangle qmc_uniform.f90
:END: :END:
In this section we write a function to perform a Monte Carlo In this section we write a function to perform a Monte Carlo
calculation of the average energy. calculation of the average energy.
At every Monte Carlo step: At every Monte Carlo step:
- Draw 3 uniform random numbers in the interval $(-5,-5,-5) \le - Draw 3 uniform random numbers in the interval $(-5,-5,-5) \le
(x,y,z) \le (5,5,5)$ (x,y,z) \le (5,5,5)$
- Compute $\Psi^2 \times E_L$ at this point and accumulate the - Compute $\Psi^2 \times E_L$ at this point and accumulate the
result in E result in E
- Compute $\Psi^2$ at this point and accumulate the result in N - Compute $\Psi^2$ at this point and accumulate the result in N
Once all the steps have been computed, return the average energy Once all the steps have been computed, return the average energy
computed on the Monte Carlo calculation. computed on the Monte Carlo calculation.
@ -635,12 +634,11 @@ At every Monte Carlo step:
Compute the energy of the wave function with $a=0.9$. Compute the energy of the wave function with $a=0.9$.
#+BEGIN_SRC python :results output #+BEGIN_SRC python :results output
from hydrogen import * from hydrogen import *
from qmc_stats import * from qmc_stats import *
def MonteCarlo(a, nmax): def MonteCarlo(a, nmax):
E = 0. E = 0.
N = 0. N = 0.
for istep in range(nmax): for istep in range(nmax):
@ -651,17 +649,17 @@ def MonteCarlo(a, nmax):
E += w * e_loc(a,r) E += w * e_loc(a,r)
return E/N return E/N
a = 0.9 a = 0.9
nmax = 100000 nmax = 100000
X = [MonteCarlo(a,nmax) for i in range(30)] X = [MonteCarlo(a,nmax) for i in range(30)]
E, deltaE = ave_error(X) E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}") print(f"E = {E} +/- {deltaE}")
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
: E = -0.4956255109300764 +/- 0.0007082875482711226 : E = -0.4956255109300764 +/- 0.0007082875482711226
#+BEGIN_SRC f90 #+BEGIN_SRC f90
subroutine uniform_montecarlo(a,nmax,energy) subroutine uniform_montecarlo(a,nmax,energy)
implicit none implicit none
double precision, intent(in) :: a double precision, intent(in) :: a
@ -703,50 +701,93 @@ program qmc
call ave_error(X,nruns,ave,err) call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err print *, 'E = ', ave, '+/-', err
end program qmc end program qmc
#+END_SRC #+END_SRC
#+begin_src sh :results output :exports both #+begin_src sh :results output :exports both
gfortran hydrogen.f90 qmc_stats.f90 qmc_uniform.f90 -o qmc_uniform gfortran hydrogen.f90 qmc_stats.f90 qmc_uniform.f90 -o qmc_uniform
./qmc_uniform ./qmc_uniform
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: E = -0.49588321986667677 +/- 7.1758863546737969E-004 : E = -0.49588321986667677 +/- 7.1758863546737969E-004
** Gaussian sampling ** Gaussian sampling
:PROPERTIES:
:header-args:python: :tangle qmc_gaussian.py
:header-args:f90: :tangle qmc_gaussian.f90
:END:
We will now improve the sampling and allow to sample in the whole We will now improve the sampling and allow to sample in the whole
3D space, correcting the bias related to the sampling in the box. 3D space, correcting the bias related to the sampling in the box.
Instead of drawing uniform random numbers, we will draw Gaussian Instead of drawing uniform random numbers, we will draw Gaussian
random numbers centered on 0 and with a variance of 1. Now the random numbers centered on 0 and with a variance of 1.
equation for the energy is changed into
\[ To obtain Gaussian-distributed random numbers, you can apply the
E = \frac{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r})\right]^2}{P(\mathbf{r})}\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r}) \right]^2}{P(\mathbf{r})} d\mathbf{r}} [[https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform][Box Muller transform]] to uniform random numbers:
\]
with
\[
P(\mathbf{r}) = \frac{1}{(2 \pi)^{3/2}}\exp\left( -\frac{\mathbf{r}^2}{2} \right)
\]
As the coordinates are drawn with probability $P(\mathbf{r})$, the \begin{eqnarray*}
average energy can be computed as z_1 &=& \sqrt{-2 \ln u_1} \cos(2 \pi u_2) \\
z_2 &=& \sqrt{-2 \ln u_1} \sin(2 \pi u_2)
\end{eqnarray*}
$$ #+BEGIN_SRC f90 :tangle qmc_stats.f90
E \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\; subroutine random_gauss(z,n)
w_i = \frac{\left[\Psi(\mathbf{r}_i)\right]^2}{P(\mathbf{r}_i)} \delta \mathbf{r} implicit none
$$ integer, intent(in) :: n
double precision, intent(out) :: z(n)
double precision :: u(n+1)
double precision, parameter :: two_pi = 2.d0*dacos(-1.d0)
integer :: i
call random_number(u)
if (iand(n,1) == 0) then
! n is even
do i=1,n,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) + dsin( two_pi*u(i+1) )
z(i) = z(i) + dcos( two_pi*u(i+1) )
end do
else
! n is odd
do i=1,n-1,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) + dsin( two_pi*u(i+1) )
z(i) = z(i) + dcos( two_pi*u(i+1) )
end do
z(n) = dsqrt(-2.d0*dlog(u(n)))
z(n) = z(n) + dcos( two_pi*u(n+1) )
end if
end subroutine random_gauss
#+END_SRC
Now the equation for the energy is changed into
\[
E = \frac{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r})\right]^2}{P(\mathbf{r})}\, \frac{\hat{H} \Psi(\mathbf{r})}{\Psi(\mathbf{r})}\,d\mathbf{r}}{\int P(\mathbf{r}) \frac{\left[\Psi(\mathbf{r}) \right]^2}{P(\mathbf{r})} d\mathbf{r}}
\]
with
\[
P(\mathbf{r}) = \frac{1}{(2 \pi)^{3/2}}\exp\left( -\frac{\mathbf{r}^2}{2} \right)
\]
As the coordinates are drawn with probability $P(\mathbf{r})$, the
average energy can be computed as
$$
E \approx \frac{\sum_i w_i E_L(\mathbf{r}_i)}{\sum_i w_i}, \;\;
w_i = \frac{\left[\Psi(\mathbf{r}_i)\right]^2}{P(\mathbf{r}_i)} \delta \mathbf{r}
$$
#+BEGIN_SRC python :results output
from hydrogen import *
from qmc_stats import *
#+BEGIN_SRC python
norm_gauss = 1./(2.*np.pi)**(1.5) norm_gauss = 1./(2.*np.pi)**(1.5)
def gaussian(r): def gaussian(r):
return norm_gauss * np.exp(-np.dot(r,r)*0.5) return norm_gauss * np.exp(-np.dot(r,r)*0.5)
#+END_SRC
#+RESULTS:
#+BEGIN_SRC python
def MonteCarlo(a,nmax): def MonteCarlo(a,nmax):
E = 0. E = 0.
N = 0. N = 0.
@ -757,58 +798,113 @@ def MonteCarlo(a,nmax):
N += w N += w
E += w * e_loc(a,r) E += w * e_loc(a,r)
return E/N return E/N
#+END_SRC
#+RESULTS:
#+BEGIN_SRC python :results output
a = 0.9 a = 0.9
nmax = 100000 nmax = 100000
X = [MonteCarlo(a,nmax) for i in range(30)] X = [MonteCarlo(a,nmax) for i in range(30)]
E, deltaE = ave_error(X) E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}") print(f"E = {E} +/- {deltaE}")
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
: E = -0.4952488228427792 +/- 0.00011913174676540714 : E = -0.49507506093129827 +/- 0.00014164037765553668
#+BEGIN_SRC f90
double precision function gaussian(r)
implicit none
double precision, intent(in) :: r(3)
double precision, parameter :: norm_gauss = 1.d0/(2.d0*dacos(-1.d0))**(1.5d0)
gaussian = norm_gauss * dexp( -0.5d0 * dsqrt(r(1)*r(1) + r(2)*r(2) + r(3)*r(3) ))
end function gaussian
subroutine gaussian_montecarlo(a,nmax,energy)
implicit none
double precision, intent(in) :: a
integer , intent(in) :: nmax
double precision, intent(out) :: energy
integer*8 :: istep
double precision :: norm, r(3), w
double precision, external :: e_loc, psi, gaussian
energy = 0.d0
norm = 0.d0
do istep = 1,nmax
call random_gauss(r,3)
w = psi(a,r)
w = w*w / gaussian(r)
norm = norm + w
energy = energy + w * e_loc(a,r)
end do
energy = energy / norm
end subroutine gaussian_montecarlo
program qmc
implicit none
double precision, parameter :: a = 0.9
integer , parameter :: nmax = 100000
integer , parameter :: nruns = 30
integer :: irun
double precision :: X(nruns)
double precision :: ave, err
do irun=1,nruns
call gaussian_montecarlo(a,nmax,X(irun))
enddo
call ave_error(X,nruns,ave,err)
print *, 'E = ', ave, '+/-', err
end program qmc
#+END_SRC
#+begin_src sh :results output :exports both
gfortran hydrogen.f90 qmc_stats.f90 qmc_gaussian.f90 -o qmc_gaussian
./qmc_gaussian
#+end_src
#+RESULTS:
: E = -0.49606057056767766 +/- 1.3918807547836872E-004
** Sampling with $\Psi^2$ ** Sampling with $\Psi^2$
We will now use the square of the wave function to make the sampling: We will now use the square of the wave function to make the sampling:
\[ \[
P(\mathbf{r}) = \left[\Psi(\mathbf{r})\right]^2 P(\mathbf{r}) = \left[\Psi(\mathbf{r})\right]^2
\] \]
Now, the expression for the energy will be simplified to the Now, the expression for the energy will be simplified to the
average of the local energies, each with a weight of 1. average of the local energies, each with a weight of 1.
$$ $$
E \approx \frac{1}{M}\sum_{i=1}^M E_L(\mathbf{r}_i)} E \approx \frac{1}{M}\sum_{i=1}^M E_L(\mathbf{r}_i)}
$$ $$
To generate the probability density $\Psi^2$, we can use a drifted To generate the probability density $\Psi^2$, we can use a drifted
diffusion scheme: diffusion scheme:
\[ \[
\mathbf{r}_{n+1} = \mathbf{r}_{n} + \tau \frac{\nabla \mathbf{r}_{n+1} = \mathbf{r}_{n} + \tau \frac{\nabla
\Psi(r)}{\Psi(r)} + \eta \sqrt{\tau} \Psi(r)}{\Psi(r)} + \eta \sqrt{\tau}
\] \]
where $\eta$ is a normally-distributed Gaussian random number. where $\eta$ is a normally-distributed Gaussian random number.
First, write a function to compute the drift vector $\frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})}$. First, write a function to compute the drift vector $\frac{\nabla \Psi(\mathbf{r})}{\Psi(\mathbf{r})}$.
#+BEGIN_SRC python #+BEGIN_SRC python
def drift(a,r): def drift(a,r):
ar_inv = -a/np.sqrt(np.dot(r,r)) ar_inv = -a/np.sqrt(np.dot(r,r))
return r * ar_inv return r * ar_inv
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
#+BEGIN_SRC python #+BEGIN_SRC python
def MonteCarlo(a,tau,nmax): def MonteCarlo(a,tau,nmax):
E = 0. E = 0.
N = 0. N = 0.
@ -836,20 +932,20 @@ def MonteCarlo(a,tau,nmax):
N += 1. N += 1.
E += e_loc(a,r_old) E += e_loc(a,r_old)
return E/N return E/N
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
#+BEGIN_SRC python :results output #+BEGIN_SRC python :results output
nmax = 100000 nmax = 100000
tau = 0.1 tau = 0.1
X = [MonteCarlo(a,tau,nmax) for i in range(30)] X = [MonteCarlo(a,tau,nmax) for i in range(30)]
E, deltaE = ave_error(X) E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}") print(f"E = {E} +/- {deltaE}")
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
: E = -0.4951783346213532 +/- 0.00022067316984271938 : E = -0.4951783346213532 +/- 0.00022067316984271938
* Diffusion Monte Carlo * Diffusion Monte Carlo

View File

@ -1,12 +1,12 @@
import numpy as np import numpy as np
from hydrogen import e_loc, psi from hydrogen import e_loc, psi
interval = np.linspace(-5,5,num=50) interval = np.linspace(-5,5,num=50)
delta = (interval[1]-interval[0])**3 delta = (interval[1]-interval[0])**3
r = np.array([0.,0.,0.]) r = np.array([0.,0.,0.])
for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]: for a in [0.1, 0.2, 0.5, 0.9, 1., 1.5, 2.]:
E = 0. E = 0.
norm = 0. norm = 0.
for x in interval: for x in interval:

View File

@ -13,3 +13,31 @@ subroutine ave_error(x,n,ave,err)
err = dsqrt(variance/dble(n)) err = dsqrt(variance/dble(n))
endif endif
end subroutine ave_error end subroutine ave_error
subroutine random_gauss(z,n)
implicit none
integer, intent(in) :: n
double precision, intent(out) :: z(n)
double precision :: u(n+1)
double precision, parameter :: two_pi = 2.d0*dacos(-1.d0)
integer :: i
call random_number(u)
if (iand(n,1) == 0) then
! n is even
do i=1,n,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) + dsin( two_pi*u(i+1) )
z(i) = z(i) + dcos( two_pi*u(i+1) )
end do
else
! n is odd
do i=1,n-1,2
z(i) = dsqrt(-2.d0*dlog(u(i)))
z(i+1) = z(i) + dsin( two_pi*u(i+1) )
z(i) = z(i) + dcos( two_pi*u(i+1) )
end do
z(n) = dsqrt(-2.d0*dlog(u(n)))
z(n) = z(n) + dcos( two_pi*u(n+1) )
end if
end subroutine random_gauss

View File

@ -1,7 +1,7 @@
from hydrogen import * from hydrogen import *
from qmc_stats import * from qmc_stats import *
def MonteCarlo(a, nmax): def MonteCarlo(a, nmax):
E = 0. E = 0.
N = 0. N = 0.
for istep in range(nmax): for istep in range(nmax):
@ -12,8 +12,8 @@ def MonteCarlo(a, nmax):
E += w * e_loc(a,r) E += w * e_loc(a,r)
return E/N return E/N
a = 0.9 a = 0.9
nmax = 100000 nmax = 100000
X = [MonteCarlo(a,nmax) for i in range(30)] X = [MonteCarlo(a,nmax) for i in range(30)]
E, deltaE = ave_error(X) E, deltaE = ave_error(X)
print(f"E = {E} +/- {deltaE}") print(f"E = {E} +/- {deltaE}")