# Hartree–Fock theory

We now have the pieces: an electronic Schrödinger equation ([chapter 1](many-electron-and-bo.md)), the
variational principle, and a finite basis ([chapter 2](variational-lcao-basis.md)). **Hartree–Fock (HF)**
puts them together with one decisive simplification of the wavefunction, and is the foundation on which
almost every other method builds. This is exactly what `mychk.scf(ref="r").run()` computes.

## One determinant: the Hartree–Fock ansatz

The exact wavefunction of $N$ interacting electrons is hopelessly complicated. Hartree–Fock makes the
**simplest possible antisymmetric guess**: put each electron in its own **spin-orbital** $\chi_i$ (a spatial
orbital times a spin function) and combine them into a single **Slater determinant**.

:::{prf:definition} Slater determinant
:label: def-slater

Given $N$ orthonormal spin-orbitals $\{\chi_i\}$, the Slater determinant

$$
\Psi(\mathbf x_1,\dots,\mathbf x_N)
= \frac{1}{\sqrt{N!}}
\begin{vmatrix}
\chi_1(\mathbf x_1) & \chi_2(\mathbf x_1) & \cdots & \chi_N(\mathbf x_1) \\
\chi_1(\mathbf x_2) & \chi_2(\mathbf x_2) & \cdots & \chi_N(\mathbf x_2) \\
\vdots & \vdots & \ddots & \vdots \\
\chi_1(\mathbf x_N) & \chi_2(\mathbf x_N) & \cdots & \chi_N(\mathbf x_N)
\end{vmatrix}
$$

is automatically **antisymmetric** (swapping two electrons swaps two rows, flipping the sign) and vanishes
if two spin-orbitals are equal (two equal columns) — so it builds in the [Pauli principle](many-electron-and-bo.md)
for free.
:::

A single determinant means each electron feels only the **average** field of the others, not their
instantaneous positions — a **mean-field** approximation. That is the one big simplification; everything
else is exact.

## The Hartree–Fock energy

Take the expectation value of the electronic Hamiltonian over this determinant. The result is a sum of a
one-electron part and a two-electron part,

$$
E_{\text{HF}} = \sum_{i} \langle i|\hat h|i\rangle
              + \frac{1}{2}\sum_{i,j}\big( J_{ij} - K_{ij}\big),
$$

where $\hat h = -\tfrac12\nabla^2 - \sum_A Z_A/r_A$ is the one-electron (kinetic + nuclear-attraction)
operator, and the two-electron terms are

$$
J_{ij} = \langle ij | ij \rangle
       = \iint \frac{|\chi_i(\mathbf x_1)|^2\,|\chi_j(\mathbf x_2)|^2}{r_{12}}\,d\mathbf x_1 d\mathbf x_2,
\qquad
K_{ij} = \langle ij | ji \rangle .
$$

$J_{ij}$ is the **Coulomb** repulsion between the charge clouds of orbitals $i$ and $j$ — classical and
intuitive. $K_{ij}$ is the **exchange** term: it has no classical analogue and arises *purely* from
antisymmetry. It lowers the energy for same-spin electrons (they avoid each other automatically — the
"Fermi hole"), and, importantly, the $i=j$ terms of $J$ and $K$ cancel, so an electron does not
spuriously repel itself.

## The Fock operator and the self-consistent field

To *find* the best orbitals, apply the variational principle: minimize $E_{\text{HF}}$ over the
spin-orbitals (keeping them orthonormal). The condition that comes out is a one-electron eigenvalue
equation — each orbital is an eigenfunction of an effective operator, the **Fock operator**,

$$
\hat f\,\chi_i = \varepsilon_i\,\chi_i,
\qquad
\hat f = \hat h + \sum_{j}\big(\hat J_j - \hat K_j\big),
$$

where $\hat J_j,\hat K_j$ are the Coulomb and exchange operators built from the *occupied* orbitals. The
$\varepsilon_i$ are the **orbital energies**. In the finite LCAO basis of chapter 2 this becomes the matrix
form we previewed — the **Roothaan equations**,

$$
\mathbf{F}\,\mathbf{C} = \mathbf{S}\,\mathbf{C}\,\boldsymbol{\varepsilon},
$$

with the **Fock matrix** $F_{\mu\nu} = \langle \phi_\mu | \hat f | \phi_\nu\rangle$ and the overlap
$\mathbf S$.

Here is the catch that shapes every SCF program: **$\hat f$ depends on the orbitals it is supposed to
produce** (through $\hat J$ and $\hat K$, which are built from the occupied orbitals). The equation is
*nonlinear*, so we solve it **iteratively** — the **self-consistent field (SCF)** procedure:

1. Start from a **guess** density (qc-rs's default is `sad`).
2. Build the Fock matrix $\mathbf F$ from the current density.
3. Solve $\mathbf{F}\mathbf{C}=\mathbf{S}\mathbf{C}\boldsymbol\varepsilon$ for new orbitals; fill the lowest
   ones with electrons to get a new density.
4. Repeat until the density (and energy) stop changing — **self-consistency**.

This is exactly the loop you watched in the [`run(log=...)` transcript](../00-intro/editor-vscode.md):
the cycle-by-cycle table of the energy and its gradient, converging in a handful of iterations, accelerated
by **DIIS**. The [SCF chapter](../20-guide/scf.md) covers the convergence toolkit in depth.

## Closed and open shells: RHF, UHF, ROHF

How the spin-orbitals are constrained gives the three flavours you select with `ref=`:

- **RHF** (`ref="r"`) — *restricted*: each spatial orbital holds a paired $\alpha$ and $\beta$ electron.
  For closed-shell molecules (the water in the [quickstart](../00-intro/quickstart.md)).
- **UHF** (`ref="u"`) — *unrestricted*: $\alpha$ and $\beta$ electrons use *different* spatial orbitals.
  The natural choice for radicals (the methyl-radical example), at the cost of some
  [spin contamination](../00-intro/quickstart.md).
- **ROHF** (`ref="ro"`) — *restricted-open*: open-shell but keeping paired electrons in common spatial
  orbitals; a spin-pure compromise.

## What Hartree–Fock gets right — and wrong

Hartree–Fock is remarkably good: for a molecule like water it already recovers about **99%** of the total
electronic energy, and it gives sensible structures and trends. But the mean-field ansatz has a built-in
blind spot. Because each electron sees only the *average* field of the others, HF misses the way electrons
**instantaneously avoid** one another. The energy this costs is, by definition, the **correlation energy**

$$
E_{\text{corr}} = E_{\text{exact}} - E_{\text{HF}} \;<\; 0 .
$$

It is a small fraction of the total (that leftover ~1%), but it is *chemically decisive* — bond energies,
reaction barriers, and dispersion all live there. Recovering it is the job of the **post-Hartree–Fock**
methods (MP2, coupled cluster, …; qc-rs's RI-MP2 family, in the [User guide](../20-guide/post-scf.md)) — and
of the alternative route taken by **[density functional theory](dft-kohn-sham.md)**, next, which folds
correlation into an approximate functional of the density instead.

:::{note} Why the water dipole was a little large
The [tutorial](../00-intro/tutorial-dft-to-properties.md) dipole and the
[basis-convergence table](variational-lcao-basis.md) both hinted at a "basis *and* method" error. You have
now met both halves: finite **basis** (chapter 2) and the missing **correlation** of the mean-field
**method** (this chapter).
:::
