(Picture taken from Javier Rozalen's github).
This repository contains several Python files for computing the ground state of the one-dimensional quantum anharmonic oscillator using variational Monte Carlo with a neural-network wavefunction. The implementation explores machine learning based variational ansatz to approximate the ground state energy of non-analytical quantum theories.
The theory we are dealing with is that of,
with a potential given by,
in natural units.
Repository structure:
.
├── animation.py # Animation and plots of the simulation results
├── data_pt.txt # Coefficients of perturbation series for E(lambda) [Bender & Wu - 1969]
├── env.yml # Conda environment
├── mc_chain.py # Construct Markov Chain
├── LICENSE # License
├── README.md
├── model.py # Neural Network model
├── parameters.py # Relevant parameters of the simulation
├── perturbed_energy.py # Calculate energy in perturbation theory
├── numerical_schrodinger.py # Calculate E(lambda) numerically
├── train.py # MAIN FILE. Training of the neural network.
└── requirements.txt # requirements
To install this project in your computer, follow these steps:
- Download the folder of this repository directly into your own computer.
- Create a conda enviroment from the
.ymlfile in the repository:
conda env create -f env.yml
- Activate the environment:
conda activate ml-osc
- Install further requirements:
pip install -r requirements.txt
The main files of this folder consist on:
-
train.py. This is the main script for training the neural quantum wavefunction. It performs the full training loop for the neural network, optimizing the parameters in the direction of minimum gradient, to minimize the energy, and outputs the computed energy at each epoch, until reaching the maximum number of epochs specified. The way the minimization algorithm works is by following the direction of minimum gradient in parameter space. Let${\alpha}$ be the parameter set, then,$\alpha_{\rm new} = \alpha_{\rm old} - \eta \nabla_\alpha E$ (1)with
$\alpha_{\rm new}$ being the new set of parameters after an epoch,$\nabla_\alpha E$ the gradient of the loss function (energy) in parameter space, and$\eta$ what is called the learning rate, which is set to 1 by default.When running this file, at the beginning the terminal will output the energy estimation in perturbation theory for that particular value of
$\lambda$ , and also the numerical estimation, calculated using the functions defined in the filenumerical_schrodinger.py. Then, the training process begins, where equation (1) is applied iteratively until reaching the last epoch of simulation, or eventually until the user pressesCtrl+Cto stop the simulation. In this case, the data will be saved safely in the corresponding files. The data of a simulation with certain$\lambda$ will be saved in a folder as follows:
├──results
├── (lambda=0.000)
├── energies.csv # File where: step, kinetic energy, potential energy, total energy, monte carlo error, are stored in different columns.
├── metadata.json # File where all the relevant parameters of the simulation are stored.
├── psi_nn.npy # Values of the raw (unnormalized) wavefunction at each epoch are stored, for later animation and plot purposes.
├── psi_morm.npy # Values of the normalized wavefunction at each epoch are stored, for later animation and plot purposes.
├── x_grid.npy # Values of the grid. For animation and plot purposes.
-
animation.py. This file is intended to be used after a simulation is already run. In this file there are a couple of important flags to mention:-
animation = True. If set toTrue, it will create a.mp4file of the evolution, where the user will be able to see the wavefunction convergence, and the energy convergence towards the expected value. -
plot_last = True. If set toTrue, a.pngfile will be created that plots the evolution of the wavefunction and energy convergence in a single figure. -
show_plot = False. If set toTrue, the lineplt.show()will be called and a pop-up of the animation video and plot will appear. For simplicity, we set it toFalseby default.
There is also a variable called
ax_energy_limsthat sets the y-axis limit for the energy subfigure. Also, afpsvariable is defined at the beginning to play with the velocity at which the video saved is being displayed. -
-
mc_chain.py. In this file, the functionmh_samples(...)is defined, which creates a Markov chain of samples following the distribution$|\psi(x)|^2$ at each epoch. The relevant Markov chain parameters, such as,- Number of samples of the chain
n_samples. Corresponds to the total number of samples of the chain. - Burn-in
burnin, - Number of samples we skip,
skip_size. We do this to reduce autocorrelation of the chain. - The value of
$\sigma$ of the proposal distribution,sigma. The proposal distribution is a Gaussian distribution centered at the previous value, and with standard deviation given by$\sigma$ .
These are all inputs of this function, and they can be tuned to whatever value wanted directly in the file
parameters.py.In the same file, there is a flag called
run = Falsethat is set toFalseby default. When set toTrue, the program will run some diagnostics of the Markov chain for the first epoch of the training process (i.e. the initial value of the wavefunction), and the user will see some plots of samples vs index, or an histogram of density vs sample value, which can be compared to the desired$|\psi(x)|^2$ . - Number of samples of the chain
-
model.py. In this file, the Neural Network is defined. It consists of a single inputNin=1(value of$x$ ) and single outputNout=2(value of $\psi(x)$), with a hidden layer of four nodes. We have in total twelve parameters:Nhid=4biases$B_k$ ,Nhid=4weights between the input and the hidden layer,$(W_1)_k$ , andNhid=4weights between the hidden layer and the output,$(W_2)_k$ . The activation function is a sigmoid,$\sigma(x) = (1+e^{-x})^{-1}$ . The output can be written in terms of these parameters as:
where the final result is being multiplied by a Gaussian envelope HarmonicExact is defined, which outputs the true ground state of the harmonic (
-
numerical_schrodinger.py. This file computes the ground-state energy of the 1D anharmonic oscillator using finite difference discretization of the Schrodinger equation and solving the resulting tridiagonal eigenvalue problem. The functionnumericalEnergy_Schrodinger(lam, L, N)returns the lowest eigenvalue, corresponding to the ground state. The grid is 1D from$[-L,L]$ with$L=10$ by default, and$N=2000$ number of points by default. These values can be changed, though the ground state energy will not change considerably.There are two flags.
-
single = False. If set toTrue, the program will output the value of$\lambda$ and the corresponding ground state energy. -
loop = False. If set toTrue, it will calculate the ground state energy for a wide range of values of$\lambda$ , and plot the corresponding curve$E(\lambda)$ .
-
-
perturbed_energy.py. This file calculates the energy using perturbation theory. In this formalism, the energy can be written as a sum of powers of the coupling$\lambda$ as
where data_pt.txt to construct the coefficients
- First column: Order
$n$ of the series. - Second column: Absolute value of
$A_n$ up to some power of 10. - Third column: The corresponding power of 10 of the coefficient.
The sign of the coefficients is alternating,
After having constructed the coefficients
-
parameters.py. In this file, the user will find all the relevant parameters of the simulation, such as:- Network hyperparameters:
-
Nin=1. Inputs to the neural network. Set to 1 by default, and we encourage the user not to change this value. -
Nout=1. Outputs of the neural network. Set to 1 by default, and we encourage the user not to change this value. -
Nhid=4. Nodes of the hidden layer. It can be changed to other values.
-
- Training hyperparameters:
-
epochs=100. Maximum number of epochs per simulation. For slightly larger values of$\lambda$ , we recommend the user to increase this value. Also, if the simulation seems to have converged way sooner than expected, the user can pressCtrl+Csafely, and all the data of the simulation up until the stopping epoch will be saved in their corresponding files. -
lr=1. Learning rate. After several tests, we recommend the user to work with values close to 1.
-
- Mesh parameters: This is only for animation purposes.
-
Nx=120. Number of points of$\psi(x)$ in the grid. -
train_a=-8. Leftmost point of the grid. -
train_b=+8. Rightmost point of the grid.
-
- Network parameters:
-
seed=5. Seed of the random number generator. It can be changed to whatever value the user may want to try. -
W1. The first set of weights of the neural network. Initialized randomly between [1,3]. -
B. The set of biases of the neural network. Initialized randomly between [-1,1]. -
W2. Second set of weights of the neural network. Initialized randomly between [0,1).
-
- Markov chain parameters:
-
n_samples=1000. Total number of samples for the Markov chain (after burn in and thinning). -
sigma=1. Value of standard deviation of proposal distribution. -
skip_size=5. Number of samples we skip (thinning). -
burn_in=500. We burn the firstburn_insamples generated, to reduce correlation with the initial value of the chain. -
save_chain = True. When set to true, the values of the Markov chain at each epoch of the training process will be saved in a file calledchains.h5. This file, as one may expect, can be quite large, so if the user does not want to have this file created in the first place, set this flag toFalse. Otherwise, set toTrue. -
SAVE_CHAIN_EVERY=1. At how many epochs of the training process we choose to save the values of the Markov chain. Set to 1 (i.e. at every epoch) by default.
-
- Physical model features:
-
L=0.. Value of the coupling$\lambda$ for the anharmonic oscillator.
-
- Network hyperparameters:
To remove the virtual environment created follow the steps below:
- Make sure your current environment is not
ml-osc, or if it is, type:
conda deactivate
- Remove the environment.
conda remove -n ml-osc --all
- Optional: Delete the folder on your computer.
If you have any questions or issues, please contact at [email protected]