Download this notebook (.ipynb + data files)
1. 4×4 Square Lattice Hubbard Model#
Author: Kyle Eskridge
In this tutorial, we will perform AFQMC calculations for the Hubbard model on a 4×4 lattice. The Hamiltonian is given by,
The tutorial is structured as follows.
We will begin with an explanation of how to build lattice model Hamiltonians
using the safiretools Python package.
Next, we will walk through computing the ground state energy of this Hamiltonian at half filling and \(U=4\),
comparing a couple of options for trial wavefunctions.
At half filling there is no sign problem, so, if care is taken, AFQMC reproduces the exact energy.
Finally, we will compute the ground state charge and spin densities for the same filling and value of \(U\)
using back-propagation.
1.1. Building a Lattice Model Hamiltonian#
safiretools provides a convenience lattice model Hamiltonian builder and writer which
will allow us to construct the standard Hubbard model with nearest-neighbor hopping and on-site U, among
other Hamiltonians.
The basic steps for using the lattice model Hamiltonian builder are:
build the lattice
build the lattice model Hamiltonian from the lattice and the Hamiltonian parameters
The lattice instance will also facilitate visualizing the charge and spin densities later on.
# create scratch directory in notebook path
%mkdir -p {_dh[0]}/data
%cd -q {_dh[0]}/data
from safiretools import Lattice
from safiretools import vis
# Step 1. build the lattice
lattice = Lattice.from_type(
'square',
(4, 4), # lattice dimensions along a1 and a2
boundaries=('periodic', 'periodic'), # boundary conditions along a1 and a2
twists=(0.0, 0.0), # twist angles in radians along a1 and a2
)
vis.plot_lattice(lattice)
from safiretools import LatticeHamiltonian
# set the number of electrons - we'll use this again
nelec = (8,8)
# Step 2. build the lattice model Hamiltonian
hamiltonian = LatticeHamiltonian.from_lattice(
lattice,
t=1.0,
U=4.0,
)
We now have a standard Hubbard model with nearest-neighbor hopping and on-site U. Next, we’ll walk through an AFQMC calculation where we’ll compute the ground state energy.
1.2. AFQMC Walkthrough: Ground State Energy#
Here, we will compute the ground state energy of the standard Hubbard model based on the Hamiltonian that we made previously - i.e.
AFQMC uses a trial wavefunction to control the sign problem at the cost of introducing a bias; however, the bias can be minimized by using a trial wavefunction which accurately captures the physics of the target system. It is, therefore, important to check that candidate trial wavefunctions have the correct ordering, symmetries, etc. before running an AFQMC calculation. In addition to this, some methods for computing a trial wavefunction will lead to better results than others for specific problems. For example, both free-electron trial wavefunctions and Hartree-Fock (HF) trial wavefunctions are reasonable for low- to intermediate-U values. Assuming that all candidate trial wavefunctions have the desired properties, it may be necessary to test which wavefunction is better by running AFQMC calculations with each, and comparing the results. In this walkthrough, we will compare free-electron trial wavefunctions to generalized Hartree-Fock trial wavefunctions.
1.2.1. Free-Electron Trial Wavefunction#
safiretools provides a convenience classmethod, Wavefunction.from_free_electron(),
for calculating free-electron trial wavefunctions.
At half filling this 4×4 lattice is open-shell: the highest occupied level is six-fold degenerate and holds only three electrons per spin, so which three you occupy is arbitrary. A small twist on the lattice breaks that degeneracy and fixes the choice.
The twist is a property of the lattice, so it has to be applied when the
Hamiltonian is built. from_free_electron() uses whatever Hamiltonian it is given.
We therefore build a second Hamiltonian with a twist
for the trial wavefunction alone; the untwisted one from Step 2 is what AFQMC
actually runs, and is also what we hand the Hartree-Fock solver further down.
from_free_electron() warns if you hand it a Hamiltonian whose shells are still
degenerate at the fill level.
# First, let's compute the Free-Electron trial wavefunction
from safiretools import Wavefunction
# a small irrational twist, in radians per axis, breaks the k-space degeneracy
twist = (1/3252409**0.5, 1/105976**0.5)
fe_lattice = Lattice.from_type('square', (4, 4), boundaries=('periodic', 'periodic'), twists=twist)
fe_hamiltonian = LatticeHamiltonian.from_lattice(fe_lattice, t=1.0, U=4.0)
wfn = Wavefunction.from_free_electron(
source=fe_hamiltonian,
nelec=nelec
)
Now that we have computed a free-electron wavefunction, we can inspect the results to validate it. Building a wavefunction and evaluating its energy are separate calls, so we compute the energy of the free-electron trial wavefunction for the interacting Hamiltonian by hand. For a single Slater determinant \(\Phi\), the one-rdm is \(\rho^\sigma_{ij} = \sum_n \Phi^{\sigma*}_{in} \Phi^\sigma_{jn}\), which gives the one-body energy \(E_\mathrm{K} = \sum_{\sigma ij} t^\sigma_{ij} \rho^\sigma_{ij}\) and the Hubbard energy \(E_U = U \sum_i \rho^\uparrow_{ii} \rho^\downarrow_{ii}\). For \(N_{\uparrow} = N_{\downarrow} = 8\), the total energy is \(E = -8.006\,t\) with a one-body contribution of \(E_\mathrm{K} = -24.006\,t\), and a Hubbard U contribution of \(E_U = 16.000\,t\).
Next, let’s take a look at the charge and spin densities of the trial wavefunction.
import numpy as np
# one-rdm of the free-electron determinant
alpha, beta = wfn.determinant(0)
rdm = np.array([alpha.conj() @ alpha.T, beta.conj() @ beta.T])
# the one-body term holds the spin-up and spin-down blocks one above the other
M = fe_lattice.N_sites
t_ij = fe_hamiltonian.get_one_body().toarray().reshape(2, M, M)
U_i = fe_hamiltonian.get_U().diagonal()
E_K = np.sum(t_ij * rdm).real
E_U = np.sum(U_i * rdm[0].diagonal() * rdm[1].diagonal()).real
print(f"E = {E_K + E_U:.6f}, E_K = {E_K:.6f}, E_U = {E_U:.6f}")
# get lattice dimensions from the Lattice instance
L = lattice.L
rho_up = rdm[0].diagonal().reshape(*L)
rho_down = rdm[1].diagonal().reshape(*L)
rho_charge = rho_up + rho_down
vis.plot_lattice(
lattice,
density=rho_charge.real,
density_label="charge density",
vmin = 0.0,
vmax = 2.0
)
rho_spin = 0.5*(rho_up - rho_down)
vis.plot_lattice(
lattice,
density=rho_spin.real,
density_label="spin density",
vmin = -0.5,
vmax = 0.5
)
As expected for a free-electron trial wavefunction with an even number of electrons, the charge and spin densities are uniform. For the charge density, we see that the uniform charge density is 1.0 electron/site, consistent with half filling. For the spin density, we see that the uniform spin density is 0.0 spin/site, which is expected for an even number of up and down electrons.
1.2.2. Running AFQMC#
Next, we will run an AFQMC calculation.
SAFIRE uses an input file in JSON format to define AFQMC run parameters.
safiretools comes with a convenience function to generate a JSON job file.
Below, we use it with reasonable AFQMC run parameters for this calculation.
from safiretools import write_jobfile
# make an input file, which also writes the wavefunction and the Hamiltonian to HDF5
write_jobfile("afqmc.json", {
"seed": 42, # just for reproducibility
"execute": {
"wavefunction": wfn,
"hamiltonian": hamiltonian,
"timestep": 0.01,
"steps": 12000,
"equilibration_steps": 500,
"num_walkers": 1600,
"population_control_interval": 5,
"walker_ortho_interval": 5,
"measure_interval": 5,
"propagator": {
"use_cp_constraint": True,
"project_force_bias": True
}
}
})
!mpirun -n 16 $AFQMC_EXEC afqmc.json
import matplotlib.pyplot as plt
from safiretools import Results
E_exact = -13.62185
results = Results("afqmc.results.h5")
vis.plot_timeseries(results)
plt.axhline(E_exact, color="crimson")
The Hubbard model at half filling is sign-free, so we would expect numerically exact results; for \(U/t=4\), the exact energy is \(-13.62185\), so why do we disagree?
Even if the underlying Hamiltonian is sign-free (i.e. \(\langle \Psi_\mathrm{T}|\Phi\rangle \ge 0\) for all walkers \(|\Phi\rangle\) during the AFQMC propagation), in the importance-sampled AFQMC random walk, an improperly chosen trial wavefunction can still induce a bias if its nodes \(\langle \Psi_\mathrm{T}|\Phi\rangle = 0\) do not coincide with those of the ground state. Especially free-electron trial wavefunctions in open-shell geometries are susceptible to this problem.
There are different approaches to dealing with this problem. One can either
move the nodes to the right position by choosing a trial wavefunction with the correct symmetry properties such as particle-hole symmetry or
allow the walkers to move around the nodes ergodically by using a noncollinear wavefunction that is not aligned with the spin axis of the Hubbard-Stratonovich decoupling.
Next, we will use a noncollinear Hartree-Fock trial wavefunction. For a more detailed treatment of half filling with a particle-hole symmetric trial wavefunction, see The Square Hubbard Model at Half Filling.
1.2.3. Hartree-Fock Trial Wavefunction#
Now, we will compute and characterize a noncollinear, generalized Hartree-Fock (GHF) wavefunction. We will use AutoHF to accomplish this.
from autohf import AutoHFHamiltonian, solve_hf
hf_settings = dict(
ansatz = 'SD',
steps = 200,
nelec = nelec,
batch_size = 50,
seed = 42,
measure_spin = True,
random_initial_phi0 = False,
noncollinear = True,
verbose = False
)
results = solve_hf(
hamiltonian=AutoHFHamiltonian(source=hamiltonian),
settings=hf_settings
)
# the noncollinear orbitals are spinors, spin-up components first
orbitals, = results[0]["orbitals"]
ghf_wfn = Wavefunction.from_single_determinant(
np.asarray(orbitals).reshape(2, lattice.N_sites, sum(nelec))
)
# get the density from AutoHF
state_results, functions = results
makeRDMs = functions['makeRDMs']
state = state_results['state']
rdm = makeRDMs(state)
# get lattice dimensions from the Lattice instance
L = lattice.L
# noncollinear
M = lattice.N_sites
rho_up = rdm[:M,:M].diagonal().reshape(*L)
rho_down = rdm[M:,M:].diagonal().reshape(*L)
# For collinear:
#rho_up = rdm[0].diagonal().reshape(*L)
#rho_down = rdm[1].diagonal().reshape(*L)
rho_charge = rho_up + rho_down
rho_spin = 0.5*(rho_up - rho_down)
vis.plot_lattice(
lattice,
density=rho_charge.real,
density_label="charge density",
vmin=0.0,
vmax=2.0
)
vis.plot_lattice(
lattice,
density=rho_spin.real,
density_label="spin density",
vmin=-0.5,
vmax=0.5
)
# make an input file
write_jobfile("afqmc.json", {
"seed": 42, # just for reproducibility
"execute": {
"wavefunction": ghf_wfn,
"hamiltonian": hamiltonian,
"timestep": 0.01,
"steps": 12000,
"equilibration_steps": 2000,
"num_walkers": 1600,
"population_control_interval": 5,
"walker_ortho_interval": 5,
"measure_interval": 5,
"propagator": {
"use_cp_constraint": True,
"project_force_bias": True
}
}
})
!mpirun -n 16 $AFQMC_EXEC afqmc.json
results = Results("afqmc.results.h5")
vis.plot_timeseries(results)
plt.axhline(E_exact, color="crimson")
If you used all of our settings, you should get an output compatible with
Energy = -13.6144 ± 0.0042
Again, we expect to achieve very close to the exact energy for the Hubbard model at half filling, which is \(-13.62185\) for \(U/t=4\). We see much better agreement now than we did with the free-electron trial wavefunction before; however, we’re still off by about 0.007 t, or about twice the stochastic uncertainty of our result. Why do we not quite agree?
We have residual systematic errors to remove. Specifically, we will want to remove the Trotter error. We can achieve this either by using a very small step size, or by extrapolating to \(\tau = 0\).
Let’s rerun with a smaller \(\tau = 0.005\).
1.2.4. Rerun AFQMC with a Smaller τ#
When we decrease the step size, we will also need to increase the number of steps, and each interval involved in order to maintain the same amount of statistics.
# make an input file
write_jobfile("afqmc.json", {
"seed": 42, # just for reproducibility
"execute": {
"wavefunction": ghf_wfn,
"hamiltonian": hamiltonian,
"timestep": 0.005,
"steps": 24000,
"equilibration_steps": 4000,
"num_walkers": 1600,
"population_control_interval": 10,
"walker_ortho_interval": 10,
"measure_interval": 10,
"propagator": {
"use_cp_constraint": True,
"project_force_bias": True
}
}
})
!mpirun -n 16 $AFQMC_EXEC afqmc.json
results = Results("afqmc.results.h5")
vis.plot_timeseries(results)
plt.axhline(E_exact, color="crimson")
Now, you should see an energy of about
Energy = -13.6184
which is off by about 0.0035 t compared with the exact energy, -13.62185. We can continue to smaller step sizes to remove the remaining disagreement.
1.3. Computing Observables with Back-Propagation#
General second-quantized observables can be computed using AFQMC via the back-propagation algorithm.
See Back-Propagation Estimator for more information on back-propagated observables.
To turn on back-propagation, add a backprop block to the estimators of an execute
block in the input file.
Here is a sample input file where an ellipsis has been used to hide the rest of the input.
{
/* ... */
"execute": {
/* ... */
"population_control_interval": 10,
"equilibration_steps": 500,
/* ... */
"estimators": {
"backprop": {
"walker_ortho_interval": 5,
"propagation_steps": [100, 150, 200],
"onerdm": {}
}
}
}
}
For back-propagation (BP), we will need to provide a BP length.
We will need to check for convergence in this parameter.
Notice that a BP estimator has no measure_interval: the back-propagation lengths, in steps, are
given by propagation_steps, and the longest of them doubles as the measurement interval. Specifying multiple lengths at once is useful to check the convergence within a single run.
Every BP length is saved as its own observable, BackPropEstimator/Steps=<n>, in the results file.
1.3.1. Run AFQMC with Back-Propagation#
Run SAFIRE using the following input file to compute the one-rdm using back-propagation.
bp_lengths = [200, 400, 600]
# make an input file
write_jobfile("afqmc_3.json", {
"seed": 42, # just for reproducibility
"execute": {
"wavefunction": ghf_wfn,
"hamiltonian": hamiltonian,
"timestep": 0.005,
"steps": 24000,
"num_walkers": 1600,
"population_control_interval": 10,
"walker_ortho_interval": 10,
"measure_interval": 10,
"equilibration_steps": 4200,
"propagator": {
"use_cp_constraint": True,
"project_force_bias": True
},
"estimators": {
"backprop": {
"walker_ortho_interval": 10,
"propagation_steps": bp_lengths,
"onerdm": {}
}
}
}
})
!mpirun -n 16 $AFQMC_EXEC afqmc_3.json
1.3.2. Analyze the AFQMC Output#
SAFIRE saves all of its results in the afqmc_3.results.h5 file.
We have already analyzed the energy using safiretools.Results.
We will now focus on analyzing the back-propagated observables.
1.3.3. Average Observables#
safiretools.Results reads the back-propagated one-rdm of each BP length from the results file.
SAFIRE writes the full matrix, with the spin-up block first for a noncollinear calculation.
The charge and spin densities follow from its diagonal, and results.evaluate() computes them
with a jackknife error that accounts for the autocorrelation of the samples.
In the code block below, we will plot the charge and spin densities for each BP length in order to check for convergence in BP length.
M = lattice.N_sites # get the number of sites to help with plotting
def densities(rdm):
"""Charge and spin density on each site, from the noncollinear one-rdm."""
n = rdm[0].diagonal().real
n_up, n_down = n[:M], n[M:]
return np.stack([n_up + n_down, 0.5*(n_up - n_down)])
results = Results("afqmc_3.results.h5")
# densities and their errors for each BP length, saved for the visualizations below
rho, rho_err = {}, {}
sites = np.arange(M)
fig, (ax_charge, ax_spin) = plt.subplots(1, 2, figsize=(12, 4))
for steps in bp_lengths:
rho[steps], rho_err[steps] = results.evaluate(densities, [f"BackPropEstimator/Steps={steps}/OneRDM"])
ax_charge.errorbar(x=sites,y=rho[steps][0],yerr=rho_err[steps][0],fmt='-o',label=f"{steps} steps")
ax_spin.errorbar(x=sites,y=rho[steps][1],yerr=rho_err[steps][1],fmt='-o',label=f"{steps} steps")
ax_charge.set_xlabel("site")
ax_charge.set_ylabel("charge density")
ax_spin.set_xlabel("site")
ax_spin.set_ylabel("spin density")
ax_charge.legend()
plt.show()
1.3.4. Visualizing Observables on the Lattice#
We used vis.plot_lattice() earlier in order to visualize the lattice itself.
The same function allows us to colorize each lattice site based on an observable.
Below, we plot the charge density, then the spin density on the lattice.
bp_to_plot = bp_lengths[-1]
rho_charge, rho_spin = rho[bp_to_plot]
vis.plot_lattice(
lattice,
density=rho_charge,
density_label="charge density",
vmin=0.0,
vmax=2.0
)
vis.plot_lattice(
lattice,
density=rho_spin,
density_label="spin density",
vmin=-0.5,
vmax=0.5
)