Files
FYS-STK4155/doc/pub/BM/ipynb/BM.ipynb
T
2018-07-26 19:11:08 +02:00

1827 lines
67 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- dom:TITLE: Solving quantum mechanical problems with Machine Learning and Boltzmann machines -->\n",
"# Solving quantum mechanical problems with Machine Learning and Boltzmann machines\n",
"<!-- dom:AUTHOR: Morten Hjorth-Jensen at Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University and Department of Physics, University of Oslo, Norway -->\n",
"<!-- Author: --> \n",
"**Morten Hjorth-Jensen**, Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University and Department of Physics, University of Oslo, Norway\n",
"\n",
"Date: **Jul 26, 2018**\n",
"\n",
"Copyright 1999-2018, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
" \n",
"\n",
"\n",
"## What is this talk about?\n",
"The main aim is to give you a short and pedestrian introduction to how we can use Machine Learning methods\n",
"to solve quantum mechanical many-body problems. And why this could be of interest. \n",
"\n",
"The hope is that after this talk you have gotten the basic ideas to get you started. Peeping into <https://github.com/mhjensenseminars/MachineLearningTalk>, you'll find a Jupyter notebook, slides, codes etc that will allow you to reproduce the simulations discussed here, and perhaps run your own very first calculations.\n",
"\n",
"Furthermore, since we will also talk about Monte Carlo simulations, the exciting idea of Monte Carlo optimization and Machine Learning can be used to mesh strategies for reducing the curse of dimensionality.\n",
"\n",
"\n",
"\n",
"\n",
"## Why?\n",
"\n",
"How can we avoid the dimensionality curse? Many possibilities\n",
"1. smarter basis functions\n",
"\n",
"2. resummation of specific correlations\n",
"\n",
"3. stochastic sampling of high-lying states (stochastic FCI, CC and soon SRG)\n",
"\n",
"4. many more\n",
"\n",
"Machine Learning and Quantum Computing hold also great promise in tackling the \n",
"ever increasing dimensionalities. Here we will focus on Machine Learning.\n",
"\n",
"\n",
"## Overview\n",
"* Short intro to Machine Learning\n",
"\n",
"* Variational Monte Carlo (Markov Chain Monte Carlo, $\\mathrm{MC}^2$) and two-electron quantum dots, solving quantum mechanical problems in a stochastic way. It will serve as our motivation for switching to Machine Learning. \n",
"\n",
"* From Variational Monte Carlo to Boltzmann Machines and Machine Learning\n",
"\n",
"\n",
"\n",
"## What are the Machine Learning calculations here based on?\n",
"\n",
"This work is inspired by the idea of representing the wave function with\n",
"a restricted Boltzmann machine (RBM), presented recently by [G. Carleo and M. Troyer, Science **355**, Issue 6325, pp. 602-606 (2017)](http://science.sciencemag.org/content/355/6325/602). They\n",
"named such a wave function/network a *neural network quantum state* (NQS). In their article they apply it to the quantum mechanical\n",
"spin lattice systems of the Ising model and Heisenberg model, with\n",
"encouraging results.\n",
"\n",
"Thanks to Vilde Flugsrud and Alfred Alocias Mariadason for many discussions and interpretations of the above paper.\n",
"\n",
"\n",
"## A new world\n",
"Machine learning (ML) is an extremely rich field, in spite of its young age. The\n",
"increases we have seen during the last three decades in computational\n",
"capabilities have been followed by developments of methods and\n",
"techniques for analyzing and handling large date sets, relying heavily\n",
"on statistics, computer science and mathematics. The field is rather\n",
"new and developing rapidly. \n",
"\n",
"Popular software packages written in Python for ML are\n",
"\n",
"* [Scikit-learn](http://scikit-learn.org/stable/), \n",
"\n",
"* [Tensorflow](https://www.tensorflow.org/),\n",
"\n",
"* [PyTorch](http://pytorch.org/) and \n",
"\n",
"* [Keras](https://keras.io/).\n",
"\n",
"These are all freely available at their respective GitHub sites. They \n",
"encompass communities of developers in the thousands or more. And the number\n",
"of code developers and contributors keeps increasing.\n",
"\n",
"\n",
"\n",
"## Lots of room for creativity\n",
"Not all the\n",
"algorithms and methods can be given a rigorous mathematical\n",
"justification, opening up thereby for experimenting\n",
"and trial and error and thereby exciting new developments.\n",
"\n",
"\n",
"\n",
"A solid command of linear algebra, multivariate theory, \n",
"probability theory, statistical data analysis,\n",
"understanding errors and Monte Carlo methods is important in order to understand many of the \n",
"various algorithms and methods.\n",
"\n",
"\n",
"\n",
"****Job market, a personal statement**.**\n",
"\n",
"A familiarity with ML is almost becoming a prerequisite for many of the most exciting employment \n",
"opportunities. Add quantum computing and there you are.\n",
"\n",
"\n",
"\n",
"<!-- !split -->\n",
"## Some members of the ML family\n",
"\n",
"1. Linear regression and its variants, in essence polynomial regression\n",
"\n",
"2. Decision tree algorithms, from simpler to more complex ones like random forests\n",
"\n",
"3. Nearest neighbors models\n",
"\n",
"4. Bayesian statistics and regression\n",
"\n",
"5. Support vector machines and finally various variants of\n",
"\n",
"6. Artifical neural networks and deep learning\n",
"\n",
"7. ....\n",
"\n",
"\n",
"\n",
"## What are the basic ingredients?\n",
"Almost every problem in ML and data science starts with the same ingredients:\n",
"* The dataset $\\mathbf{x}$ (could be some observable quantity of the system we are studying)\n",
"\n",
"* A model which is a function of a set of parameters $\\mathbf{\\alpha}$ that relates to the dataset, say a likelihood function $p(\\mathbf{x}\\vert \\mathbf{\\alpha})$ or just a simple model $f(\\mathbf{\\alpha})$\n",
"\n",
"* A so-called **cost** function $\\mathcal{C} (\\mathbf{x}, f(\\mathbf{\\alpha}))$ which allows us to decide how well our model represents the dataset. \n",
"\n",
"We seek to minimize the function $\\mathcal{C} (\\mathbf{x}, f(\\mathbf{\\alpha}))$ by finding the parameter values which minimize $\\mathcal{C}$. This leads to various minimization algorithms.\n",
"\n",
"\n",
"\n",
"## What is Machine Learning?\n",
"Machine learning is the science of giving computers the ability to\n",
"learn without being explicitly programmed. The idea is that there\n",
"exist generic algorithms which can be used to find patterns in a broad\n",
"class of data sets without having to write code specifically for each\n",
"problem. The algorithm will build its own logic based on the data.\n",
"\n",
"Machine learning is a subfield of computer science, and is closely\n",
"related to computational statistics. It evolved from the study of\n",
"pattern recognition in artificial intelligence (AI) research, and has\n",
"made contributions to AI tasks like computer vision, natural language\n",
"processing and speech recognition. It has also, especially in later\n",
"years, found applications in a wide variety of other areas, including\n",
"bioinformatics, economy, physics, finance and marketing.\n",
"\n",
"You will notice however that many of the basic ideas discussed do come from Physics!\n",
"\n",
"\n",
"\n",
"## Types of Machine Learning\n",
"\n",
"The approaches to machine learning are many, but are often split into two main categories. \n",
"In *supervised learning* we know the answer to a problem,\n",
"and let the computer deduce the logic behind it. On the other hand, *unsupervised learning*\n",
"is a method for finding patterns and relationship in data sets without any prior knowledge of the system.\n",
"Some authours also operate with a third category, namely *reinforcement learning*. This is a paradigm \n",
"of learning inspired by behavioural psychology, where learning is achieved by trial-and-error, \n",
"solely from rewards and punishment.\n",
"\n",
"Another way to categorize machine learning tasks is to consider the desired output of a system.\n",
"Some of the most common tasks are:\n",
"\n",
" * Classification: Outputs are divided into two or more classes. The goal is to produce a model that assigns inputs into one of these classes. An example is to identify digits based on pictures of hand-written ones. Classification is typically supervised learning.\n",
"\n",
" * Regression: Finding a functional relationship between an input data set and a reference data set. The goal is to construct a function that maps input data to continuous output values.\n",
"\n",
" * Clustering: Data are divided into groups with certain common traits, without knowing the different groups beforehand. It is thus a form of unsupervised learning.\n",
"\n",
"\n",
"\n",
"\n",
"## References\n",
"* An excellent reference, [Mehta *et al.*, arXiv:1803.08823 and Physics Reports in press (2018)](https://arxiv.org/abs/1803.08823)\n",
"\n",
"* A cute paper by [Utama and Piekarewicz, Validating neural-network refinements of nuclear mass models, Phys. Rev. C 97, 014306 ](https://journals.aps.org/prc/abstract/10.1103/PhysRevC.97.014306)\n",
"\n",
"* [Every issue of Physical Review Letters has now one or more articles on ML](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.120.156001)\n",
"\n",
"* [Books and lectures notes](https://github.com/CompPhysics/MachineLearning)\n",
"\n",
"* See also Metha and Schwab, arXiv.1410.3831, how to link Variational renormalization group theory with deep learning (recommended read)\n",
"\n",
"\n",
"\n",
"\n",
"## Another interesting article\n",
"\n",
"Here we will use so-called **reduced Boltzmann Machines** to simulate quantum many-body problems. For Monte Carlo aficionados, there is a very close similarity with what are called **shadow wave functions**, see the work of [Pederiva and Kalos and collaborators, Phys Rev. E 90, 053304 (2014)](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.90.053304).\n",
"\n",
"## Just for fun illustration of scikit-learn: Hudson bay data\n",
"Most mammalian predators rely on a variety of prey, which complicates mathematical modeling; however, a few predators have become highly specialized and seek almost exclusively a single prey species. An example of this simplified predator-prey interaction is seen in Canadian northern forests, where the populations of the lynx and the snowshoe hare are intertwined in a life and death struggle.\n",
"\n",
"One reason that this particular system has been so extensively studied is that the Hudson Bay company kept careful records of all furs from the early 1800s into the 1900s. The records for the furs collected by the Hudson Bay company showed distinct oscillations (approximately 12 year periods), suggesting that these species caused almost periodic fluctuations of each other's populations. The table here shows data from 1900 to 1920.\n",
"\n",
"\n",
"\n",
"\n",
"## Hares and lynx in Hudson bay from 1900 to 1920\n",
"\n",
"<!-- dom:FIGURE: [figures/Hudson_Bay_data.png, width=700 frac=0.9] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/Hudson_Bay_data.png\" width=700>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"## Linear regression in Python"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import display\n",
"import sklearn\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.tree import DecisionTreeRegressor\n",
"\n",
"\n",
"data = np.loadtxt('src/Hudson_Bay.csv', delimiter=',', skiprows=1)\n",
"x = data[:,0]\n",
"y = data[:,1]\n",
"line = np.linspace(1900,1920,1000,endpoint=False).reshape(-1,1)\n",
"reg = DecisionTreeRegressor(min_samples_split=3).fit(x.reshape(-1,1),y.reshape(-1,1))\n",
"plt.plot(line, reg.predict(line), label=\"decision tree\")\n",
"regline = LinearRegression().fit(x.reshape(-1,1),y.reshape(-1,1))\n",
"plt.plot(line, regline.predict(line), label= \"Linear Regression\")\n",
"plt.plot(x, y, label= \"Linear Regression\")\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Artificial neurons\n",
"\n",
"The field of artificial neural networks has a long history of\n",
"development, and is closely connected with the advancement of computer\n",
"science and computers in general. A model of artificial neurons was\n",
"first developed by McCulloch and Pitts in 1943 to study signal\n",
"processing in the brain and has later been refined by others. The\n",
"general idea is to mimic neural networks in the human brain, which is\n",
"composed of billions of neurons that communicate with each other by\n",
"sending electrical signals. Each neuron accumulates its incoming\n",
"signals, which must exceed an activation threshold to yield an\n",
"output. If the threshold is not overcome, the neuron remains inactive,\n",
"i.e. has zero output.\n",
"\n",
"This behaviour has inspired a simple mathematical model for an artificial neuron."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"y = f\\left(\\sum_{i=1}^n w_ix_i\\right) = f(u)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here, the output $y$ of the neuron is the value of its activation function, which have as input\n",
"a weighted sum of signals $x_i, \\dots ,x_n$ received by $n$ other neurons.\n",
"\n",
"\n",
"\n",
"## A simple perceptron model\n",
"\n",
"<!-- dom:FIGURE: [figures/perceptron.png, width=700 frac=0.9] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/perceptron.png\" width=700>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"## Neural network types\n",
"An artificial neural network (NN), is a computational model that consists of layers of connected neurons, or *nodes*. \n",
"It is supposed to mimic a biological nervous system by letting each neuron interact with other neurons\n",
"by sending signals in the form of mathematical functions between layers. \n",
"A wide variety of different NNs have\n",
"been developed, but most of them consist of an input layer, an output layer and eventual layers in-between, called\n",
"*hidden layers*. All layers can contain an arbitrary number of nodes, and each connection between two nodes\n",
"is associated with a weight variable. \n",
"\n",
"<!-- dom:FIGURE: [figures/dnn.png, width=500 frac=0.6] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/dnn.png\" width=500>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## The system: two electrons in a harmonic oscillator trap in two dimensions\n",
"\n",
"The Hamiltonian of the quantum dot is given by"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\hat{H} = \\hat{H}_0 + \\hat{V},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where $\\hat{H}_0$ is the many-body HO Hamiltonian, and $\\hat{V}$ is the\n",
"inter-electron Coulomb interactions. In dimensionless units,"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\hat{V}= \\sum_{i<j}^N \\frac{1}{r_{ij}},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"with $r_{ij}=\\sqrt{\\mathbf{r}_i^2 - \\mathbf{r}_j^2}$.\n",
"\n",
"This leads to the separable Hamiltonian, with the relative motion part given by ($r_{ij}=r$)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\hat{H}_r=-\\nabla^2_r + \\frac{1}{4}\\omega^2r^2+ \\frac{1}{r},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"plus a standard Harmonic Oscillator problem for the center-of-mass motion.\n",
"This system has analytical solutions in two and three dimensions ([M. Taut 1993 and 1994](https://journals.aps.org/pra/abstract/10.1103/PhysRevA.48.3561)). \n",
"\n",
"## Quantum Monte Carlo Motivation\n",
"Given a hamiltonian $H$ and a trial wave function $\\Psi_T$, the variational principle states that the expectation value of $\\langle H \\rangle$, defined through"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\langle E \\rangle =\n",
" \\frac{\\int d\\boldsymbol{R}\\Psi^{\\ast}_T(\\boldsymbol{R})H(\\boldsymbol{R})\\Psi_T(\\boldsymbol{R})}\n",
" {\\int d\\boldsymbol{R}\\Psi^{\\ast}_T(\\boldsymbol{R})\\Psi_T(\\boldsymbol{R})},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"is an upper bound to the ground state energy $E_0$ of the hamiltonian $H$, that is"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"E_0 \\le \\langle H \\rangle .\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In general, the integrals involved in the calculation of various expectation values are multi-dimensional ones. Traditional integration methods such as the Gauss-Legendre will not be adequate for say the computation of the energy of a many-body system.\n",
"\n",
"\n",
"\n",
"\n",
"## Quantum Monte Carlo Motivation\n",
"**Basic steps.**\n",
"\n",
"Choose a trial wave function\n",
"$\\psi_T(\\boldsymbol{R})$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"P(\\boldsymbol{R},\\boldsymbol{\\alpha})= \\frac{\\left|\\psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})\\right|^2}{\\int \\left|\\psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})\\right|^2d\\boldsymbol{R}}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is our model, or likelihood/probability distribution function (PDF). It depends on some variational parameters $\\boldsymbol{\\alpha}$.\n",
"The approximation to the expectation value of the Hamiltonian is now"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\langle E[\\boldsymbol{\\alpha}] \\rangle = \n",
" \\frac{\\int d\\boldsymbol{R}\\Psi^{\\ast}_T(\\boldsymbol{R},\\boldsymbol{\\alpha})H(\\boldsymbol{R})\\Psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})}\n",
" {\\int d\\boldsymbol{R}\\Psi^{\\ast}_T(\\boldsymbol{R},\\boldsymbol{\\alpha})\\Psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quantum Monte Carlo Motivation\n",
"**Define a new quantity.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"E_L(\\boldsymbol{R},\\boldsymbol{\\alpha})=\\frac{1}{\\psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})}H\\psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha}),\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"called the local energy, which, together with our trial PDF yields"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"E[\\boldsymbol{\\alpha}]=\\int P(\\boldsymbol{R})E_L(\\boldsymbol{R},\\boldsymbol{\\alpha}) d\\boldsymbol{R}\\approx \\frac{1}{N}\\sum_{i=1}^NE_L(\\boldsymbol{R_i},\\boldsymbol{\\alpha})\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"with $N$ being the number of Monte Carlo samples.\n",
"\n",
"\n",
"\n",
"\n",
"## Quantum Monte Carlo\n",
"The Algorithm for performing a variational Monte Carlo calculations runs thus as this\n",
"\n",
" * Initialisation: Fix the number of Monte Carlo steps. Choose an initial $\\boldsymbol{R}$ and variational parameters $\\alpha$ and calculate $\\left|\\psi_T(\\boldsymbol{R},\\boldsymbol{\\alpha})\\right|^2$. \n",
"\n",
" * Initialise the energy and the variance and start the Monte Carlo calculation by looping over trials.\n",
"\n",
" * Calculate a trial position $\\boldsymbol{R}_p=\\boldsymbol{R}+r*step$ where $r$ is a random variable $r \\in [0,1]$.\n",
"\n",
" * Metropolis algorithm to accept or reject this move $w = P(\\boldsymbol{R}_p,\\boldsymbol{\\alpha})/P(\\boldsymbol{R},\\boldsymbol{\\alpha})$.\n",
"\n",
" * If the step is accepted, then we set $\\boldsymbol{R}=\\boldsymbol{R}_p$. \n",
"\n",
" * Update averages\n",
"\n",
"\n",
" * Finish and compute final averages.\n",
"\n",
"Observe that the jumping in space is governed by the variable *step*. This is often called brute-force sampling.\n",
"Need importance sampling to get more relevant sampling.\n",
"\n",
"\n",
"\n",
"## The trial wave function\n",
"We want to perform a Variational Monte Carlo calculation of the ground state of two electrons in a quantum dot well with different oscillator energies, assuming total spin $S=0$.\n",
"Our trial wave function has the following form"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"eq:trial\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
" \\psi_{T}(\\boldsymbol{r}_1,\\boldsymbol{r}_2) = \n",
" C\\exp{\\left(-\\alpha_1\\omega(r_1^2+r_2^2)/2\\right)}\n",
" \\exp{\\left(\\frac{r_{12}}{(1+\\alpha_2 r_{12})}\\right)}, \n",
"\\label{eq:trial} \\tag{1}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where the $\\alpha$s represent our variational parameters, two in this case.\n",
"\n",
"Why does the trial function look like this? How did we get there? **This will be our main motivation** for switching to\n",
"Machine Learning.\n",
"\n",
"\n",
"\n",
"## The correlation part of the wave function\n",
"\n",
"To find an ansatz for the correlated part of the wave function, it is useful to rewrite the two-particle\n",
"local energy in terms of the relative and center-of-mass motion.\n",
"Let us denote the distance between the two electrons as\n",
"$r_{12}$. We omit the center-of-mass motion since we are only interested in the case when\n",
"$r_{12} \\rightarrow 0$. The contribution from the center-of-mass (CoM) variable ${\\bf R}_{\\mathrm{CoM}}$\n",
"gives only a finite contribution.\n",
"We focus only on the terms that are relevant for $r_{12}$ and for three dimensions. The relevant local energy becomes then"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\lim_{r_{12} \\rightarrow 0}E_L(R)=\n",
" \\frac{1}{{\\cal R}_T(r_{12})}\\left(2\\frac{d^2}{dr_{ij}^2}+\\frac{4}{r_{ij}}\\frac{d}{dr_{ij}}+\n",
"\\frac{2}{r_{ij}}-\\frac{l(l+1)}{r_{ij}^2}+2E\n",
"\\right){\\cal R}_T(r_{12}) = 0.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set $l=0$ and we have the so-called **cusp** condition"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\frac{d {\\cal R}_T(r_{12})}{dr_{12}} = -\\frac{1}{2(l+1)}\n",
"{\\cal R}_T(r_{12})\\qquad r_{12}\\to 0\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Resulting ansatz\n",
"The above results in"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"{\\cal R}_T \\propto \\exp{(r_{ij}/2)},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"for anti-parallel spins and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"{\\cal R}_T \\propto \\exp{(r_{ij}/4)},\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"for anti-parallel spins. \n",
"This is the so-called cusp condition for the relative motion, resulting in a minimal requirement\n",
"for the correlation part of the wave fuction.\n",
"For general systems containing more than say two electrons, we have this\n",
"condition for each electron pair $ij$.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## The VMC code"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Importing various packages\n",
"from math import exp, sqrt\n",
"from random import random, seed\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from mpl_toolkits.mplot3d import Axes3D\n",
"from matplotlib import cm\n",
"from matplotlib.ticker import LinearLocator, FormatStrFormatter\n",
"import sys\n",
"\n",
"#Trial wave function for quantum dots in two dims\n",
"def WaveFunction(r,alpha,beta):\n",
" r1 = r[0,0]**2 + r[0,1]**2\n",
" r2 = r[1,0]**2 + r[1,1]**2\n",
" r12 = sqrt((r[0,0]-r[1,0])**2 + (r[0,1]-r[1,1])**2)\n",
" deno = r12/(1+beta*r12)\n",
" return exp(-0.5*alpha*(r1+r2)+deno)\n",
"\n",
"#Local energy for quantum dots in two dims, using analytical local energy\n",
"def LocalEnergy(r,alpha,beta):\n",
" \n",
" r1 = (r[0,0]**2 + r[0,1]**2)\n",
" r2 = (r[1,0]**2 + r[1,1]**2)\n",
" r12 = sqrt((r[0,0]-r[1,0])**2 + (r[0,1]-r[1,1])**2)\n",
" deno = 1.0/(1+beta*r12)\n",
" deno2 = deno*deno\n",
" return 0.5*(1-alpha*alpha)*(r1 + r2) +2.0*alpha + 1.0/r12+deno2*(alpha*r12-deno2+2*beta*deno-1.0/r12)\n",
"\n",
"# The Monte Carlo sampling with the Metropolis algo\n",
"def MonteCarloSampling():\n",
"\n",
" NumberMCcycles= 100000\n",
" StepSize = 1.0\n",
" # positions\n",
" PositionOld = np.zeros((NumberParticles,Dimension), np.double)\n",
" PositionNew = np.zeros((NumberParticles,Dimension), np.double)\n",
" # seed for rng generator\n",
" seed()\n",
" # start variational parameter\n",
" alpha = 0.9\n",
" for ia in range(MaxVariations):\n",
" alpha += .025\n",
" AlphaValues[ia] = alpha\n",
" beta = 0.2 \n",
" for jb in range(MaxVariations):\n",
" beta += .01\n",
" BetaValues[jb] = beta\n",
" energy = energy2 = 0.0\n",
" DeltaE = 0.0\n",
" #Initial position\n",
" for i in range(NumberParticles):\n",
" for j in range(Dimension):\n",
" PositionOld[i,j] = StepSize * (random() - .5)\n",
" wfold = WaveFunction(PositionOld,alpha,beta)\n",
"\n",
" #Loop over MC MCcycles\n",
" for MCcycle in range(NumberMCcycles):\n",
" #Trial position\n",
" for i in range(NumberParticles):\n",
" for j in range(Dimension):\n",
" PositionNew[i,j] = PositionOld[i,j] + StepSize * (random() - .5)\n",
" wfnew = WaveFunction(PositionNew,alpha,beta)\n",
"\n",
" #Metropolis test to see whether we accept the move\n",
" if random() < wfnew**2 / wfold**2:\n",
" PositionOld = PositionNew.copy()\n",
" wfold = wfnew\n",
" DeltaE = LocalEnergy(PositionOld,alpha,beta)\n",
" energy += DeltaE\n",
" energy2 += DeltaE**2\n",
"\n",
" #We calculate mean, variance and error ...\n",
" energy /= NumberMCcycles\n",
" energy2 /= NumberMCcycles\n",
" variance = energy2 - energy**2\n",
" error = sqrt(variance/NumberMCcycles)\n",
" Energies[ia,jb] = energy \n",
" return Energies, AlphaValues, BetaValues\n",
"\n",
"\n",
"#Here starts the main program with variable declarations\n",
"NumberParticles = 2\n",
"Dimension = 2\n",
"MaxVariations = 10\n",
"Energies = np.zeros((MaxVariations,MaxVariations))\n",
"AlphaValues = np.zeros(MaxVariations)\n",
"BetaValues = np.zeros(MaxVariations)\n",
"(Energies, AlphaValues, BetaValues) = MonteCarloSampling()\n",
"\n",
"# Prepare for plots\n",
"fig = plt.figure()\n",
"ax = fig.gca(projection='3d')\n",
"# Plot the surface.\n",
"X, Y = np.meshgrid(AlphaValues, BetaValues)\n",
"surf = ax.plot_surface(X, Y, Energies,cmap=cm.coolwarm,linewidth=0, antialiased=False)\n",
"# Customize the z axis.\n",
"zmin = np.matrix(Energies).min()\n",
"zmax = np.matrix(Energies).max()\n",
"ax.set_zlim(zmin, zmax)\n",
"ax.set_xlabel(r'$\\alpha$')\n",
"ax.set_ylabel(r'$\\beta$')\n",
"ax.set_zlabel(r'$\\langle E \\rangle$')\n",
"ax.zaxis.set_major_locator(LinearLocator(10))\n",
"ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))\n",
"# Add a color bar which maps values to colors.\n",
"fig.colorbar(surf, shrink=0.5, aspect=5)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Technical aspect, improvements and how to define the cost function\n",
"\n",
"**The above procedure is not the smartest one**. Looping over all variational parameters becomes expensive.\n",
"Also, we don't use importance sampling and optimizations of the standard deviation (blocking, bootstrap, jackknife). \n",
"Such codes are included in the above Github address.\n",
"\n",
"\n",
"We can also be smarter and use minimization methods to find the **optimal** variational parameters with fewer Monte Carlo cycles and then \n",
"fire up our heavy artillery. \n",
"\n",
"One way to achieve this is to minimize the energy as function of the variational parameters.\n",
"\n",
"\n",
"\n",
"## Energy derivatives\n",
"To find the derivatives of the local energy expectation value as function of the variational parameters, we can use the chain rule and the hermiticity of the Hamiltonian. \n",
"\n",
"Let us define"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\bar{E}_{\\alpha_i}=\\frac{d\\langle E_L\\rangle}{d\\alpha_i}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"as the derivative of the energy with respect to the variational parameter $\\alpha_i$\n",
"We define also the derivative of the trial function (skipping the subindex $T$) as"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\bar{\\Psi}_{i}=\\frac{d\\Psi}{d\\alpha_i}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Derivatives of the local energy\n",
"The elements of the gradient of the local energy are then (using the chain rule and the hermiticity of the Hamiltonian)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\bar{E}_{i}= 2\\left( \\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}E_L\\rangle -\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}\\rangle\\langle E_L \\rangle\\right).\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From a computational point of view it means that you need to compute the expectation values of"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}E_L\\rangle,\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}\\rangle\\langle E_L\\rangle\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"These integrals are evaluted using MC intergration (with all its possible error sources). \n",
"We can then use methods like stochastic gradient or other minimization methods to find the optimal variational parameters (I don't discuss this topic here, but these methods are very important in ML).\n",
"\n",
"\n",
"\n",
"## How do we define our cost function?\n",
"We have a model, our likelihood function. \n",
"\n",
"How should we define the cost function?\n",
"\n",
"\n",
"\n",
"## Meet the variance and its derivatives\n",
"**Why the variance?**\n",
"\n",
"Suppose the trial function (our model) is the exact wave function. The action of the hamiltionan\n",
"on the wave function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"H\\Psi = \\mathrm{constant}\\times \\Psi,\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The integral which defines various \n",
"expectation values involving moments of the hamiltonian becomes then"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\langle E^n \\rangle = \\langle H^n \\rangle =\n",
" \\frac{\\int d\\boldsymbol{R}\\Psi^{\\ast}(\\boldsymbol{R})H^n(\\boldsymbol{R})\\Psi(\\boldsymbol{R})}\n",
" {\\int d\\boldsymbol{R}\\Psi^{\\ast}(\\boldsymbol{R})\\Psi(\\boldsymbol{R})}=\n",
"\\mathrm{constant}\\times\\frac{\\int d\\boldsymbol{R}\\Psi^{\\ast}(\\boldsymbol{R})\\Psi(\\boldsymbol{R})}\n",
" {\\int d\\boldsymbol{R}\\Psi^{\\ast}(\\boldsymbol{R})\\Psi(\\boldsymbol{R})}=\\mathrm{constant}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**This gives an important information: If I want the variance, the exact wave function leads to zero variance!**\n",
"The variance is defined as"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\sigma_E = \\langle E^2\\rangle - \\langle E\\rangle^2.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variation is then performed by minimizing both the energy and the variance.\n",
"\n",
"\n",
"\n",
"\n",
"## The variance defines the cost function\n",
"We can then take the derivatives of"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\sigma_E = \\langle E^2\\rangle - \\langle E\\rangle^2,\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"with respect to the variational parameters. The derivatives of the variance can then be used to defined the\n",
"so-called Hessian matrix, which in turn allows us to use minimization methods like Newton's method or \n",
"standard gradient methods. \n",
"\n",
"This leads to however a more complicated expression, with obvious errors when evaluating integrals by Monte Carlo integration. Less used, see however [Filippi and Umrigar](https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.94.150201). The expression becomes complicated"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\n",
"\\bar{E}_{ij} = 2\\left[ \\langle (\\frac{\\bar{\\Psi}_{ij}}{\\Psi}+\\frac{\\bar{\\Psi}_{j}}{\\Psi}\\frac{\\bar{\\Psi}_{i}}{\\Psi})(E_L-\\langle E\\rangle)\\rangle -\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}\\rangle\\bar{E}_j-\\langle \\frac{\\bar{\\Psi}_{j}}{\\Psi}\\rangle\\bar{E}_i\\right] +\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}E_L{_j}\\rangle +\\langle \\frac{\\bar{\\Psi}_{j}}{\\Psi}E_L{_i}\\rangle -\\langle \\frac{\\bar{\\Psi}_{i}}{\\Psi}\\rangle\\langle E_L{_j}\\rangle \\langle \\frac{\\bar{\\Psi}_{j}}{\\Psi}\\rangle\\langle E_L{_i}\\rangle.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Evaluating the cost function means having to evaluate the above second derivative of the energy.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## Boltzmann Machines\n",
"\n",
"Why use a generative model rather than the more well known discriminative deep neural networks (DNN)? \n",
"\n",
"* Discriminitave methods have several limitations: They are mainly supervised learning methods, thus requiring labeled data. And there are tasks they cannot accomplish, like drawing new examples from an unknown probability distribution.\n",
"\n",
"* A generative model can learn to represent and sample from a probability distribution. The core idea is to learn a parametric model of the probability distribution from which the training data was drawn. As an example\n",
"\n",
" a. A model for images could learn to draw new examples of cats and dogs, given a training dataset of images of cats and dogs.\n",
"\n",
" b. Generate a sample of an ordered or disordered Ising model phase, having been given samples of such phases.\n",
"\n",
" c. Model the trial wave function for Monte Carlo calculations\n",
"\n",
"\n",
"## Some similarities and differences from DNNs\n",
"\n",
"1. Both use gradient-descent based learning procedures for minimizing cost functions\n",
"\n",
"2. Energy based models don't use backpropagation and automatic differentiation for computing gradients, instead turning to Markov Chain Monte Carlo methods.\n",
"\n",
"3. DNNs often have several hidden layers. A restricted Boltzmann machine has only one hidden layer, however several RBMs can be stacked to make up Deep Belief Networks, of which they constitute the building blocks.\n",
"\n",
"History: The RBM was developed by amongst others Geoffrey Hinton, called by some the \"Godfather of Deep Learning\", working with the University of Toronto and Google.\n",
"\n",
"\n",
"## The structure of the RBM network\n",
"\n",
"<!-- dom:FIGURE: [figures/RBM.png, width=800 frac=1.0] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/RBM.png\" width=800>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"\n",
"## The network\n",
"\n",
"**The network layers**:\n",
" 1. A function $\\mathbf{x}$ that represents the visible layer, a vector of $M$ elements (nodes). This layer represents both what the RBM might be given as training input, and what we want it to be able to reconstruct. This might for example be the pixels of an image, the spin values of the Ising model, or coefficients representing speech.\n",
"\n",
" 2. The function $\\mathbf{h}$ represents the hidden, or latent, layer. A vector of $N$ elements (nodes). Also called \"feature detectors\".\n",
"\n",
"## Goals\n",
"\n",
"The goal of the hidden layer is to increase the model's expressive power. We encode complex interactions between visible variables by introducing additional, hidden variables that interact with visible degrees of freedom in a simple manner, yet still reproduce the complex correlations between visible degrees in the data once marginalized over (integrated out).\n",
"\n",
"Examples of this trick being employed in physics: \n",
" 1. The Hubbard-Stratonovich transformation\n",
"\n",
" 2. The introduction of ghost fields in gauge theory\n",
"\n",
" 3. Shadow wave functions in Quantum Monte Carlo simulations\n",
"\n",
"**The network parameters, to be optimized/learned**:\n",
" 1. $\\mathbf{a}$ represents the visible bias, a vector of same length as $\\mathbf{x}$.\n",
"\n",
" 2. $\\mathbf{b}$ represents the hidden bias, a vector of same lenght as $\\mathbf{h}$.\n",
"\n",
" 3. $W$ represents the interaction weights, a matrix of size $M\\times N$.\n",
"\n",
"## Joint distribution and the Energy function\n",
"The restricted Boltzmann machine is described by a Bolztmann distribution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto1\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tP_{rbm}(\\mathbf{x},\\mathbf{h}) = \\frac{1}{Z} e^{-\\frac{1}{T_0}E(\\mathbf{x},\\mathbf{h})},\n",
"\\label{_auto1} \\tag{2}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where $Z$ is the normalization constant or partition function, defined as"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto2\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tZ = \\int \\int e^{-\\frac{1}{T_0}E(\\mathbf{x},\\mathbf{h})} d\\mathbf{x} d\\mathbf{h}.\n",
"\\label{_auto2} \\tag{3}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is common to ignore $T_0$ by setting it to one. \n",
"\n",
"## Network Elements\n",
"\n",
"The function $E(\\mathbf{x},\\mathbf{h})$ gives the **energy** of a\n",
"configuration (pair of vectors) $(\\mathbf{x}, \\mathbf{h})$. The lower\n",
"the energy of a configuration, the higher the probability of it. This\n",
"function also depends on the parameters $\\mathbf{a}$, $\\mathbf{b}$ and\n",
"$W$. Thus, when we adjust them during the learning procedure, we are\n",
"adjusting the energy function to best fit our problem.\n",
"\n",
"## Defining different types of RBMs\n",
"There are different variants of RBMs, and the differences lie in the types of visible and hidden units we choose as well as in the implementation of the energy function $E(\\mathbf{x},\\mathbf{h})$. \n",
"\n",
"**Binary-Binary RBM:**\n",
"\n",
"\n",
"RBMs were first developed using binary units in both the visible and hidden layer. The corresponding energy function is defined as follows:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto3\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tE(\\mathbf{x}, \\mathbf{h}) = - \\sum_i^M x_i a_i- \\sum_j^N b_j h_j - \\sum_{i,j}^{M,N} x_i w_{ij} h_j,\n",
"\\label{_auto3} \\tag{4}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where the binary values taken on by the nodes are most commonly 0 and 1.\n",
"\n",
"\n",
"**Gaussian-Binary RBM:**\n",
"\n",
"\n",
"Another varient is the RBM where the visible units are Gaussian while the hidden units remain binary:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto4\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tE(\\mathbf{x}, \\mathbf{h}) = \\sum_i^M \\frac{(x_i - a_i)^2}{2\\sigma_i^2} - \\sum_j^N b_j h_j - \\sum_{i,j}^{M,N} \\frac{x_i w_{ij} h_j}{\\sigma_i^2}. \n",
"\\label{_auto4} \\tag{5}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## More about RBMs\n",
"1. Useful when we model continuous data (i.e., we wish $\\mathbf{x}$ to be continuous)\n",
"\n",
"2. Requires a smaller learning rate, since there's no upper bound to the value a component might take in the reconstruction\n",
"\n",
"Other types of units include:\n",
"1. Softmax and multinomial units\n",
"\n",
"2. Gaussian visible and hidden units\n",
"\n",
"3. Binomial units\n",
"\n",
"4. Rectified linear units\n",
"\n",
"## Sampling: Metropolis sampling\n",
"In order to sample from the RBM probability distribution it is common to use Markov Chain Monte Carlo (MCMC) algorithms such as Metropolis-Hastings or Gibbs sampling.\n",
"\n",
"\n",
"\n",
"Metropolis sampling starts by suggesting a new configuration $\\boldsymbol{x}^{k+1}$. In the brute force method this is done by some random change of the visible units. The new configuration is then accepted with the acceptance probability"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto5\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tA(\\boldsymbol{x}^k \\rightarrow \\boldsymbol{x}^{k+1}) = \\text{min} (1, \\frac{P(\\boldsymbol{x}^{k+1})}{P(\\boldsymbol{x}^k)}),\n",
"\\label{_auto5} \\tag{6}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where we need the marginalized probability"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto6\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tP(\\boldsymbol{x}) = \\sum_\\mathbf{h} P_{rbm}(\\mathbf{x}, \\mathbf{h}) \n",
"\\label{_auto6} \\tag{7}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto7\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t\t\t\t= \\frac{1}{Z}\\sum_\\mathbf{h} e^{-E(\\mathbf{x}, \\mathbf{h})}.\n",
"\\label{_auto7} \\tag{8}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sampling: Gibbs sampling\n",
"\n",
"In this method we sample from the joint probability $P_{rbm} (\\mathbf{x}, \\mathbf{h})$ by way of a two step sampling process. We alternately update the visible and hidden units.\n",
"New samples are generated according to the conditional probabilities $P(x_i|\\mathbf{h})$ and $P(h_j|\\mathbf{x})$ respectively and accepted with the probability of $1$. While the the visible nodes are dependent on the hidden nodes and vice versa, the nodes are independent of other nodes within the same layer. This is due to there being no intra layer interactions in the restricted Boltzmann machine.\n",
"\n",
"The conditional probabilities are often referred to as the activitation functions in the neural networks context due to their role in determining the node outputs. For the binary-binary RBM they are"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto8\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tP(h_j = 1 | \\boldsymbol{x}) = \\frac{1}{1 + e^{-b_j - \\sum_i x_i w_{ij}}} \n",
"\\label{_auto8} \\tag{9}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto9\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\tP(x_i = 1 | \\boldsymbol{h}) = \\frac{1}{1 + e^{-a_j - \\sum_j h_j w_{ij}}},\n",
"\\label{_auto9} \\tag{10}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where we recognize the logistic sigmoid function $\\sigma (x) = 1/(1+exp(-x))$.\n",
"\n",
"## Gaussian RBM\n",
"For the Gaussian-Binary RBM the conditional probabilities are"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto10\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tP(x_i|\\mathbf{h}) = \\mathcal{N}(x_i; a_i+ \\sum_j h_j w_{ij}, \\sigma^2) \n",
"\\label{_auto10} \\tag{11}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto11\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\tP(h_j=1|\\mathbf{x}) = \\frac{1}{1+e^{-b_j-\\frac{1}{\\sigma^2} \\sum_i x_i w_{ij}}},\n",
"\\label{_auto11} \\tag{12}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"while the visible units now follow a normal distribution, we see the hidden units again follow the logistic sigmoid function.\n",
"\n",
"## Cost function\n",
"\n",
"When working with a training dataset, the most common training approach is maximizing the log-likelihood of the training data. The log likelihood characterizes the log-probability of generating the observed data using our generative model. Using this method our cost function is chosen as the negative log-likelihood. The learning then consists of trying to find parameters that maximize the probability of the dataset, and is known as Maximum Likelihood Estimation (MLE).\n",
"Denoting the parameters as $\\boldsymbol{\\theta} = a_1,...,a_M,b_1,...,b_N,w_{11},...,w_{MN}$, the log-likelihood is given by"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto12\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\mathcal{L}(\\{ \\theta_i \\}) = \\langle \\text{log} P_\\theta(\\boldsymbol{x}) \\rangle_{data} \n",
"\\label{_auto12} \\tag{13}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto13\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t= - \\langle E(\\boldsymbol{x}; \\{ \\theta_i\\}) \\rangle_{data} - \\text{log} Z(\\{ \\theta_i\\}),\n",
"\\label{_auto13} \\tag{14}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where we used that the normalization constant does not depend on the data, $\\langle \\text{log} Z(\\{ \\theta_i\\}) \\rangle = \\text{log} Z(\\{ \\theta_i\\})$\n",
"Our cost function is the negative log-likelihood, $\\mathcal{C}(\\{ \\theta_i \\}) = - \\mathcal{L}(\\{ \\theta_i \\})$\n",
"\n",
"## Optimization / Training\n",
"The training procedure of choice often is Stochastic Gradient Descent (SGD). It consists of a series of iterations where we update the parameters according to the equation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto14\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\boldsymbol{\\theta}_{k+1} = \\boldsymbol{\\theta}_k - \\eta \\nabla \\mathcal{C} (\\boldsymbol{\\theta}_k)\n",
"\\label{_auto14} \\tag{15}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"at each $k$-th iteration. There are a range of variants of the algorithm which aim at making the learning rate $\\eta$ more adaptive so the method might be more efficient while remaining stable.\n",
"\n",
"We now need the gradient of the cost function in order to minimize it. We find that"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto15\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\frac{\\partial \\mathcal{C}(\\{ \\theta_i\\})}{\\partial \\theta_i}\n",
"\t= \\langle \\frac{\\partial E(\\boldsymbol{x}; \\theta_i)}{\\partial \\theta_i} \\rangle_{data}\n",
"\t+ \\frac{\\partial \\text{log} Z(\\{ \\theta_i\\})}{\\partial \\theta_i} \n",
"\\label{_auto15} \\tag{16}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto16\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t= \\langle O_i(\\boldsymbol{x}) \\rangle_{data} - \\langle O_i(\\boldsymbol{x}) \\rangle_{model},\n",
"\\label{_auto16} \\tag{17}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where in order to simplify notation we defined the \"operator\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto17\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tO_i(\\boldsymbol{x}) = \\frac{\\partial E(\\boldsymbol{x}; \\theta_i)}{\\partial \\theta_i}, \n",
"\\label{_auto17} \\tag{18}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and used the statistical mechanics relationship between expectation values and the log-partition function:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto18\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\langle O_i(\\boldsymbol{x}) \\rangle_{model} = \\text{Tr} P_\\theta(\\boldsymbol{x})O_i(\\boldsymbol{x}) = - \\frac{\\partial \\text{log} Z(\\{ \\theta_i\\})}{\\partial \\theta_i}.\n",
"\\label{_auto18} \\tag{19}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- !split -->\n",
"## More on RBMs\n",
"\n",
"The data-dependent term in the gradient is known as the positive phase of the gradient, while the model-dependent term is known as the negative phase of the gradient. The aim of the training is to lower the energy of configurations that are near observed data points (increasing their probability), and raising the energy of configurations that are far from observed data points (decreasing their probability).\n",
"\n",
"The gradient of the negative log-likelihood cost function of a Binary-Binary RBM is then"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto19\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\frac{\\partial \\mathcal{C} (w_{ij}, a_i, b_j)}{\\partial w_{ij}} = \\langle x_i h_j \\rangle_{data} - \\langle x_i h_j \\rangle_{model} \n",
"\\label{_auto19} \\tag{20}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto20\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t\\frac{\\partial \\mathcal{C} (w_{ij}, a_i, b_j)}{\\partial a_{ij}} = \\langle x_i \\rangle_{data} - \\langle x_i \\rangle_{model} \n",
"\\label{_auto20} \\tag{21}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto21\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t\\frac{\\partial \\mathcal{C} (w_{ij}, a_i, b_j)}{\\partial b_{ij}} = \\langle h_i \\rangle_{data} - \\langle h_i \\rangle_{model}. \n",
"\\label{_auto21} \\tag{22}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto22\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\\label{_auto22} \\tag{23}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To get the expecation values with respect to the *data*, we set the visible units to each of the observed samples in the training data, then update the hidden units according to the conditional probability found before. We then average over all samples in the training data to calculate expectation values with respect to the data. \n",
"\n",
"## Which sampling to use\n",
"\n",
"To get the expectation values with respect to the *model*, we use Gibbs sampling. We can either initialize the $\\boldsymbol{x}$ randomly or with a training sample. While we ideally want a large number of Gibbs iterations $n\\rightarrow n$, one might decide to truncate it earlier for efficiency. Doing this while having intialized $\\boldsymbol{x}$ with a training data vector is referred to as contrastive divergence (CD), because one is then closer to approximating the gradient of this function than the negative log-likelihood. The contrastive divergence function is the difference between two Kullback-Leibler divergences (also called relative entropy), which measure how one probability distribution diverges from a second, expected probability distribution (in this case the estimated one from the ground truth one).\n",
"\n",
"## RBMs for the quantum many body problem\n",
"\n",
"The idea of applying RBMs to quantum many body problems was presented by G. Carleo and M. Troyer, working with ETH Zurich and Microsoft Research.\n",
"\n",
"Some of their motivation included\n",
"\n",
"* \"The wave function $\\Psi$ is a monolithic mathematical quantity that contains all the information on a quantum state, be it a single particle or a complex molecule. In principle, an exponential amount of information is needed to fully encode a generic many-body quantum state.\"\n",
"\n",
"* There are still interesting open problems, including fundamental questions ranging from the dynamical properties of high-dimensional systems to the exact ground-state properties of strongly interacting fermions.\n",
"\n",
"* The difficulty lies in finding a general strategy to reduce the exponential complexity of the full many-body wave function down to its most essential features. That is\n",
"\n",
"a. $\\rightarrow$ Dimensional reduction\n",
"\n",
"b. $\\rightarrow$ Feature extraction\n",
"\n",
"\n",
"* Among the most successful techniques to attack these challenges, artifical neural networks play a prominent role.\n",
"\n",
"* Want to understand whether an artifical neural network may adapt to describe a quantum system.\n",
"\n",
"## Choose the right RBM\n",
"\n",
"Carleo and Troyer applied the RBM to the quantum mechanical spin lattice systems of the Ising model and Heisenberg model, with encouraging results. Our goal is to test the method on systems of moving particles. For the spin lattice systems it was natural to use a binary-binary RBM, with the nodes taking values of 1 and -1. For moving particles, on the other hand, we want the visible nodes to be continuous, representing position coordinates. Thus, we start by choosing a Gaussian-binary RBM, where the visible nodes are continuous and hidden nodes take on values of 0 or 1. If eventually we would like the hidden nodes to be continuous as well the rectified linear units seem like the most relevant choice.\n",
"\n",
"## Representing the wave function\n",
"The wavefunction should be a probability amplitude depending on $\\boldsymbol{x}$. The RBM model is given by the joint distribution of $\\boldsymbol{x}$ and $\\boldsymbol{h}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto23\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tF_{rbm}(\\mathbf{x},\\mathbf{h}) = \\frac{1}{Z} e^{-\\frac{1}{T_0}E(\\mathbf{x},\\mathbf{h})}.\n",
"\\label{_auto23} \\tag{24}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To find the marginal distribution of $\\boldsymbol{x}$ we set:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto24\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tF_{rbm}(\\mathbf{x}) = \\sum_\\mathbf{h} F_{rbm}(\\mathbf{x}, \\mathbf{h}) \n",
"\\label{_auto24} \\tag{25}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto25\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t\t\t\t= \\frac{1}{Z}\\sum_\\mathbf{h} e^{-E(\\mathbf{x}, \\mathbf{h})}.\n",
"\\label{_auto25} \\tag{26}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now this is what we use to represent the wave function, calling it a neural-network quantum state (NQS)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto26\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\t\\Psi (\\mathbf{X}) = F_{rbm}(\\mathbf{x}) \n",
"\\label{_auto26} \\tag{27}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto27\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t= \\frac{1}{Z}\\sum_{\\boldsymbol{h}} e^{-E(\\mathbf{x}, \\mathbf{h})} \n",
"\\label{_auto27} \\tag{28}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto28\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t= \\frac{1}{Z} \\sum_{\\{h_j\\}} e^{-\\sum_i^M \\frac{(x_i - a_i)^2}{2\\sigma^2} + \\sum_j^N b_j h_j + \\sum_{i,j}^{M,N} \\frac{x_i w_{ij} h_j}{\\sigma^2}} \n",
"\\label{_auto28} \\tag{29}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto29\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\t= \\frac{1}{Z} e^{-\\sum_i^M \\frac{(x_i - a_i)^2}{2\\sigma^2}} \\prod_j^N (1 + e^{b_j + \\sum_i^M \\frac{x_i w_{ij}}{\\sigma^2}}). \n",
"\\label{_auto29} \\tag{30}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto30\"></div>\n",
"\n",
"$$\n",
"\\begin{equation} \n",
"\\label{_auto30} \\tag{31}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choose the cost function\n",
"Now we don't necessarily have training data (unless we generate it by using some other method). However, what we do have is the variational principle which allows us to obtain the ground state wave function by minimizing the expectation value of the energy of a trial wavefunction (corresponding to the untrained NQS). Similarly to the traditional variational Monte Carlo method then, it is the local energy we wish to minimize. The gradient to use for the stochastic gradient descent procedure is"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto31\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tG_i = \\frac{\\partial \\langle E_L \\rangle}{\\partial \\theta_i}\n",
"\t= 2(\\langle E_L \\frac{1}{\\Psi}\\frac{\\partial \\Psi}{\\partial \\theta_i} \\rangle - \\langle E_L \\rangle \\langle \\frac{1}{\\Psi}\\frac{\\partial \\Psi}{\\partial \\theta_i} \\rangle ),\n",
"\\label{_auto31} \\tag{32}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"where the local energy is given by"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
"<div id=\"_auto32\"></div>\n",
"\n",
"$$\n",
"\\begin{equation}\n",
"\tE_L = \\frac{1}{\\Psi} \\hat{\\mathbf{H}} \\Psi.\n",
"\\label{_auto32} \\tag{33}\n",
"\\end{equation}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Running the codes\n",
"You can find the codes for the simple two-electron case at the Github repository <https://github.com/mhjensenseminars/MachineLearningTalk/tree/master/doc/Programs/MLcpp/src>. Python codes to come, only c++ as of now. \n",
"\n",
"The trial wave function is based on the product of a Slater determinant with Gaussian orbitals, a simple Jastrow factor $\\exp{(r_{ij})}$ and the reduced Boltzmann machines. \n",
"\n",
"The Broyden-Fletcher-Goldfarb-Shanno algorithm was used to perform the minimization. We used $14$ hidden nodes in the calculations below.\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## Energy as function of iterations, $N=2$ electrons\n",
"<!-- dom:FIGURE: [figures/figN2.png, width=700 frac=0.9] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/figN2.png\" width=700>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"\n",
"## Energy as function of iterations, $N=6$ electrons\n",
"<!-- dom:FIGURE: [figures/figN6.png, width=700 frac=0.9] -->\n",
"<!-- begin figure -->\n",
"\n",
"<p></p>\n",
"<img src=\"figures/figN6.png\" width=700>\n",
"\n",
"<!-- end figure -->\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"## Conclusions and where do we stand\n",
"* A simple extension of the work of [G. Carleo and M. Troyer, Science **355**, Issue 6325, pp. 602-606 (2017)](http://science.sciencemag.org/content/355/6325/602) gives excellent results for two-electron systems as well as good agreement with standard VMC calculations for $N=6$ and $N=12$ electrons.\n",
"\n",
"* Minimization problem can be tricky.\n",
"\n",
"* Anti-symmetry dealt with multiplying the trail wave function with an optimized Slater determinant.\n",
"\n",
"* To come: Analysis of wave function from ML and compare with diffusion and Variational Monte Carlo calculations as well as the analytical results of Taut for the two-electron case.\n",
"\n",
"* Extend to more fermions. How do we deal with the antisymmetry of the multi-fermion wave function?\n",
"\n",
" a. Here we used standard Hartree-Fock theory to define an optimal Slater determinant. Takes care of the antisymmetry. What about constructing an anti-symmetrized network function?\n",
"\n",
" b. Use thereafter ML to determine the correlated part of the wafe function (including a standard Jastrow factor).\n",
"\n",
" c. Test this for multi-fermion systems and compare with other many-body methods.\n",
"\n",
"\n",
"* Can we use ML to find out which correlations are relevant and thereby diminish the dimensionality problem in say CC or SRG theories?"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}