{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"Hartree-Fock Procedure for Approximate Quantum Chemistry\"\"\"\n",
"\n",
"__authors__ = [\"Ashley Ringer McDonald\",\"Dominic A. Sirianni\", \"Tricia D. Shepherd\", \"Sean Garrett-Roe\"]\n",
"__email__ = [\"armcdona@calpoly.edu\", \"sirianni.dom@gmail.com\", \"profshep@icloud.com\", \"sgr@pitt.edu\"]\n",
"__credits__ = [\"Daniel G.A. Smith\"]\n",
"__copyright__ = \"(c) 2008-2020, The Psi4Education Developers\"\n",
"__license__ = \"BSD-3-Clause\"\n",
"__date__ = \"2020-07-28\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hartree-Fock Procedure for Approximate Quantum Chemistry"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import psi4\n",
"import numpy as np\n",
"from scipy import linalg as splinalg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### I. Warm up with the H atom\n",
"By solving the Hydrogen atom Schrödinger equation, we saw that the expression for the different\n",
"energy levels is given by:\n",
"\n",
"$$E_{n} = -\\frac{m_e e^4}{8\\epsilon_0^2h^2n^2}=-\\frac{m_ee^4}{2(4\\pi\\epsilon_0)^2\\hbar^2n^2}\\qquad(1)$$\n",
"\n",
"Atomic units (a.u.) are based on fundamental quantities so that many physical constants have numerical values of 1: \n",
"\n",
"| Symbol | Quantity | Value in a.u. | Value in SI units\n",
"|---|---|---|---|\n",
"| $e$ | electron charge| 1 |$1.602\\times 10^{-19}$ C |\n",
"| $m_e$ | electron mass| 1 |$9.110\\times 10^{-31}$ kg |\n",
"| $\\hbar$ | angular momentum| 1 |$1.055\\times 10^{-34}$ J s |\n",
"| $a_0$ | Bohr radius (atomic distance unit)| 1 |$5.292\\times 10^{-11}$ m |\n",
"| $E$ | Hartree energy (atomic energy unit)| 1 |$4.360\\times 10^{-18}$ J |\n",
"| $4\\pi\\epsilon_0$ | vacuum permittivity| 1 |$1.113\\times 10^{-10}$ C$^2$/J m |. \n",
"\n",
"#### Question: Verify that the two forms of Eq.1 agree."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Rewrite $E_n$ in atomic units.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: In the cell below, write the formula to compute the exact H atom ground state energy in SI units and explicitly convert from SI to hartrees"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==> Toy Example: The Hydrogen Atom <==\n",
"\n",
"# Define fundamental constants in SI units\n",
"m_e = 9.1093837015e-31 # kg\n",
"e = 1.602176634e-19 # C\n",
"epsilon_0 = 8.8541878128e-12 # F / m\n",
"h = 6.62607015e-34 # J*s\n",
"n = 1\n",
"\n",
"# Define a.u. to SI energy conversion factor (https://en.wikipedia.org/wiki/Hartree)\n",
"hartree2joules = 4.359744650e-18\n",
"\n",
"# Compute ground state energy of H atom in SI units using constants above\n",
"E_1 = #\n",
"\n",
"# Convert to atomic units\n",
"E_1_au = #\n",
"\n",
"print(f'The exact ground state energy of the H atom in SI units is: {E_1} J')\n",
"print(f'The exact ground state energy of the H atom in atomic units is: {E_1_au} Eh')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We obtained the Hydrogen atom energy expression above by solving the Schrödinger equation exactly. But what happens if we cannot do this?\n",
"\n",
"That's where Hartree-Fock molecular orbital theory comes in! Just as a test case, let's use Psi4 to compute the Hartree-Fock wavefunction and energy for the Hydrogen atom:\n",
"\n",
"#### Calculate: In the cell below, use psi4 to compute the exact H atom ground state energy in SI units and hartrees"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# ==> Compute H atom energy with Hartree-Fock using Psi4 <==\n",
"\n",
"# the H atom has a charge of 0, spin multiplicity of 2 (m_s=1/2)\n",
"# and we place it at the xyz origin (0,0,0)\n",
"\n",
"h_atom = psi4.geometry(\"\"\"\n",
"0 2\n",
"H 0 0 0\n",
"\"\"\")\n",
"\n",
"# specify the basis\n",
"basis = 'd-aug-cc-pv5z'\n",
"\n",
"# set computation options\n",
"psi4.set_options({'basis': basis,\n",
" 'reference': 'rohf',\n",
" 'scf_type': 'pk'})\n",
"\n",
"# compute energy\n",
"e = psi4.energy('scf')\n",
"psi4.core.clean()\n",
"\n",
"print(f\"The Hartree-Fock ground state energy of the H atom in SI units is: {e * psi4.constants.hartree2J} J\")\n",
"print(f\"The Hartree-Fock ground state energy of the H atom in atomic units is: {e} Eh\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"In this lab activity, you will build and diagonalize the Fock matrix to determine the MO coefficients and energies for a molecule. We will be using the functions of the Psi4 quantum chemistry software package to compute the integrals we need. The following notebook will lead you through setting up your molecule, establishing the basis set, and forming and diagonalizing the Fock matrix. Be sure to run each cell as your proceed through the notebook.\n",
"\n",
"### II. The Hartree-Fock procedure\n",
"The Schrödinger equation has the structure of an eigenvalue equation\n",
"\n",
"$$\\hat{H}|\\psi\\rangle = E|\\psi\\rangle$$\n",
"\n",
"In Hartree-Fock theory, this is reexpresed in terms of the Fock matrix, $F$, a matrix of wavefunction amplitudes for each MO, $C$, and the overlap matrix, $S$,\n",
"\n",
"$$FC = SCE.\\qquad(\\text{2})$$\n",
"\n",
"The Fock matrix for a closed-shell system is \n",
"$$\n",
"F = H + 2J - K\n",
"$$\n",
"where $H$ is the one electron \"core\" Hamiltonian, $J$ is the Coulomb integral matrix, and $K$ is the exchange integral matrix. The definitions of $J$ and $K$ depend on the coefficients, $C$. We see here the central premise of SCF: To get the Fock matrix, we need the coefficient matrix, but to compute the coefficient matrix we need the Fock matrix. When $S$ is not equal to the identity matrix (i.e. the basis is not orthonormal), then this is a pseudo-eigenvalue problem and is even harder to solve. This is our task.\n",
"\n",
"We will\n",
"- review Dirac notation \n",
"- introduce the overlap matrix \n",
"- learn how to build an orthogonalization matrix\n",
"- learn how to calculate the density\n",
"- learn how to calculate the Coulomb and Exchange integral matrices\n",
"- learn how to diagonalize the Fock matrix\n",
"- build an iterative procedure to converge HF energy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### III. Orthogonalizing the AO basis set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Crash course in Dirac notation:\n",
"\n",
"The wavefunction, $\\psi(x)$ can be represented as a *column vector*, $|\\psi\\rangle$ . The complex conjugate of the wavefunction, $\\psi^*(x)$ is also represented by a vector which is the complex conjugate transpose of $|\\psi\\rangle$. \n",
"\n",
"\\begin{align}\n",
"\\psi(x)&\\rightarrow|\\psi\\rangle\\quad\\text{column vector}\\\\\n",
"\\psi^*(x)&\\rightarrow\\langle\\psi|\\quad\\text{row vector}\n",
"\\end{align}\n",
"\n",
"The normalization of a wavefunction $\\psi(x)$ is an integral\n",
"\n",
"$$\\int\\psi^*(x)\\psi(x)\\;\\mathrm{d}x=1.$$\n",
"\n",
"In Dirac notation, it is replaced with a vector equation\n",
"\n",
"$$\\langle\\psi|\\psi\\rangle=1.$$\n",
"\n",
"The orthogonality of two wavefunctions, $\\psi_i(x)$ and $\\psi_j(x)$, which, in integral notation, is\n",
"\n",
"$$\\int\\psi_i^*(x)\\psi_j(x)\\;\\mathrm{d}x=0,$$\n",
"\n",
"becomes, in Dirac notation,\n",
"\n",
"$$\\langle\\psi_i|\\psi_j\\rangle=0.$$\n",
" \n",
"#### Calculate: Define two vectors, `phi1` and `phi2`, with two elements each, that are normalized, in the sense $\\langle\\phi_i|\\phi_i\\rangle=1$, and orthogonal in sense that $\\langle\\phi_i|\\phi_j\\rangle=0$. Recall that one defines a vector `v` with the two elements `1` and `2` through the command `v=np.array([1,2])`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# define a first basis vector and a second, orthogonal vector\n",
"phi1 = #\n",
"phi2 = #\n",
"\n",
"print(F'Phi1: {phi1}')\n",
"print(F'Phi2: {phi2}')\n",
"print()#empty line"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Numpy commands for vector operations assuming `v`= vector:\n",
"\n",
"inner product (scalar product) of two vectors: `v.dot(v)`\n",
"\n",
"complex conjugate of a vector: `v.conj()`\n",
"\n",
"inner product of $v^\\dagger v$: `v.conj().dot(v)`\n",
"\n",
"\n",
"#### Calculate: Use the commands demonstrated above to show that `phi1` is normalized."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate normalization\n",
"\n",
"phi1_norm = #\n",
"\n",
"print(F' = {phi1_norm}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Similarly, enter the three remaining calculations below to show that `phi1` and `phi2` are orthonormal."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate normalization\n",
"\n",
"print(F' = {}')\n",
"\n",
"# calculate orthogonality\n",
"\n",
"print(F' = {}')\n",
"\n",
"print(F' = {}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IV. The overlap integrals\n",
"For a set of basis functions, $\\phi_i(\\tau)$, where $\\tau$ is a shorthand for all the coordinates of all the particles, we can calculate the overlap integrals between the basis functions in the following way\n",
"\n",
"$$S_{ij}=\\int {\\rm d}\\tau\\; \\phi_i^*(\\tau)\\phi_j(\\tau).$$\n",
"\n",
"#### Question: Define $S_{ij}$ using Dirac notation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: For an orthonormal basis, what does the overlap integral array, `S`, look like?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: the terms `S_ij` using the basis vectors `phi1` and `phi2`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate the overlap (inner product) of the vectors \n",
"S_11 = #\n",
"S_12 = \n",
"S_21 = \n",
"S_22 = \n",
"print('The ij elements of S:')\n",
"print(S_11,S_12)\n",
"print(S_21,S_22)\n",
"print()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### V. Constructing the overlap matrix\n",
"These overlap integrals, $S_{ij}$, can be interpreted as the elements on the $i$-th row and $j$-th column of a matrix, $S$. Let's propose a matrix, $S$, made of the overlap integrals $S_{ij}$. We can build $S$ systematically in the following way. First, make a matrix, $B$, composed of our basis vectors as columns,\n",
"\n",
"$$ B = \\left(\\begin{array}{ccc}|& |&|\\\\ \\phi_1 &\\phi_2&\\phi_3\\\\|&|&|\\end{array}\\right).$$\n",
"\n",
"We will use the symbol $\\dagger$ to indicate the complex conjugate of the transpose of a matrix. So\n",
"\n",
"$$B^\\dagger = (B^T)^*.$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# construct the overlap matrix from matrix of basis vectors\n",
"vector_length = phi1.size #length of the vector space\n",
"phi1_column = phi1.reshape(vector_length,1) #this makes phi a column vector\n",
"phi2_column = phi2.reshape(vector_length,1)\n",
"\n",
"# put together (concatenate) the vectors into the matrix B\n",
"B = np.concatenate((phi1_column,phi2_column),axis=1)\n",
"print(F'The matrix B:\\n{B}')\n",
"\n",
"B_dagger = B.conj().T\n",
"print(F'The matrix B^\\dagger:\\n{B_dagger}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, multiplying the rows of $B^\\dagger$ by the columns of $B$ (normal matrix multiplication) produces a matrix of the overlap integrals in the correct locations. we defined to be the matrix $S$.\n",
"\n",
"$$B^\\dagger B =\\left(\\begin{array}{ccc}-& \\phi_1^*&-\\\\-& \\phi_2^* &-\\\\-&\\phi_3&-\\end{array}\\right) \\left(\\begin{array}{ccc}|& |&|\\\\ \\phi_1 &\\phi_2&\\phi_3\\\\|&|&|\\end{array}\\right)\\equiv S.$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: Numpy commands for matrix operations assuming `v` = vector, and `M` = matrix:\n",
"\n",
"matrix vector product: `M.dot(v)`\n",
"\n",
"matrix matrix product: `M.dot(M)`\n",
"\n",
"matrix complex conjugate: `M.conj()`\n",
"\n",
"matrix transpose: `M.T`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Use `B` and `B_dagger` and the matrix rules above to calculate the matrix `S`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate S from matrix of basis vectors\n",
"S = #\n",
"print(F'The matrix of eigenvectors in columns B =\\n {B} \\n\\nand S = B^\\dagger B =\\n{S}\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### VI. Einstein implicit summation notation\n",
"Matrix multiplication is defined through\n",
"\n",
"$$(AB)_{pq}= \\sum_i A_{p,i}B_{i,q}\\qquad\\text{explicit summation}$$\n",
"\n",
"Note that there is a repeated index, $i$, in the summation. In implicit summation notation, Einstein notation, we do not write the $\\sum$ and treat the summation as understood. \n",
"\n",
"$$(AB)_{pq}=A_{p,i}B_{i,q}\\qquad\\text{implicit summation}$$\n",
"\n",
"Using implicit summation for the case at hand, $B^\\dagger B$ gives\n",
"\n",
"$$S_{pq}=(B^\\dagger B)_{pq}= (B^\\dagger)_{p,i}B_{i,q}\\qquad\\text{implicit summation}$$\n",
"$$= (B^*)_{i,p}B_{i,q}\\qquad\\text{implicit summation}$$\n",
"\n",
"where $B^*$ is the complex conjugate of $B$ (no transpose). Note that the two sets of indices, $(i,p)$ and $(i,q)$, in the input matrices become one set, $(p,q)$, in the product. \n",
"\n",
"*(Python programming aside: There is a convenient function in `numpy` called `einsum()`, which is one of the crown jewels of the numpy library. In short, `einsum` lets you perform various combinations of multiplying, summing, and transposing matrices very efficiently. A good tutorial about `einsum` can be found at http://ajcr.net/Basic-guide-to-einsum/. To specify the operations you want to do, you use the **Ein**stein **Sum**mation convention.)*\n",
"\n",
"To calculate the sum \n",
"\n",
"$$S_{pq} = (B^*)_{i,p}B_{i,q}\\qquad\\text{implicit summation}$$\n",
"\n",
"use `S=np.einsum('ip,iq->pq',B.conj(),B)`, as demonstrated below. We will use `einsum()` in several places.\n",
"\n",
"Let's try this with a simple basis set of two (perhaps) orthonormal vectors."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Describe how the notation of the `np.einsum` command coorelates to the implicit summation formula written above."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"****Answer**** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Use the function `np.einsum()` to calculate the matrix `S`, and confirm that your answer is the same as above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# calculate S from Einstein sum\n",
"S = #\n",
"print(F'S from Einstein notation:\\n{S}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Propose a different orthonormal basis, modify `phi1` and `phi2`, and verify that `S` still has the same form. There are infinitely many choices. It isn't complex... or *is* it?!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Propose what will happen to `S` if the vectors are not orthonormal. \n",
"#### Calculate: Test your prediction!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### VII. Gaussian atomic orbital basis set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"H$_2$O is a small but interesting molecule to use in our exploration. \n",
"\n",
"#### Question: Answer the following questions for the H$_2$O molecule:\n",
"##### How many electrons are there in total?\n",
"##### How many occupied molecular orbitals would you expect?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we can begin to implement the HF procedure, we need to specifcy the molecule and basis set that we will be using. We will also set the memory usage for our calcluation and the output file name. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==> Set Basic Psi4 Options <==\n",
"# Memory specification\n",
"psi4.set_memory('500 MB')\n",
"numpy_memory = 2 # No NumPy array can exceed 2 MB in size\n",
"\n",
"# set output file\n",
"psi4.core.set_output_file('output.dat', False)\n",
"\n",
"# specify the basis\n",
"# basis = 'cc-pvdz'\n",
"basis = 'sto-3g'\n",
"\n",
"\n",
"# Set computation options\n",
"psi4.set_options({'basis': basis,\n",
" 'scf_type': 'pk',\n",
" 'e_convergence': 1e-8})\n",
"\n",
"\n",
"# ==> Define Molecule <==\n",
"# Define our model of water -- \n",
"# we will distort the molecule later, which may require C1 symmetry\n",
"mol = psi4.geometry(\"\"\"\n",
"O\n",
"H 1 1.1\n",
"H 1 1.1 2 104\n",
"symmetry c1\n",
"\"\"\")\n",
"\n",
"# compute energy\n",
"\n",
"SCF_E_psi = psi4.energy('scf')\n",
"psi4.core.clean()\n",
"\n",
"print(f\"The Hartree-Fock ground state energy of the water is: {SCF_E_psi} Eh\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we need to build the wavefunction from the basis functions. We store the wavefunction in a variable called `wfn`. We use the function `nalpha()` provided by the wavefunction object we created above, `wfn`, to determine the number of orbitals with spin alpha, which will be doubly occupied orbitals for close shelled systems. We save this answer as a variable called `ndocc` (number of doubly occupied orbitals). \n",
"\n",
"#### Calculate: Execute the code below and confirm that the number of doubly occupied orbitals matches your expectation for the molecule you chose."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==> Compute static 1e- and 2e- quantities with Psi4 <==\n",
"wfn = psi4.core.Wavefunction.build(mol, psi4.core.get_global_option('basis'))\n",
"\n",
"# number of spin alpha orbitals (doubly occupied for closed-shell systems)\n",
"ndocc = wfn.nalpha()\n",
"nbf = wfn.basisset().nbf()\n",
"\n",
"print(F'Number of occupied orbitals: {ndocc}')\n",
"print(F'Number of basis functions: {nbf}') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we will examine the atomic orbital basis set. To do this, we have to set up a data structure, called a class, to calculate the molecular integrals. (Psi4 will do the nasty calculus for us.) We will call this data structure `mints` (Molecular INTegralS). We use the function `ao_overlap` to calculate the overlap integrals between all the AO basis functions. We cast the result to a numpy array called `S`. \n",
"\n",
"_(Python programming aside: `asarray()` is a special case of `array()` that does not copy arrays when compatible and converts array subclasses to base class ndarrays. https://stackoverflow.com/questions/14415741/what-is-the-difference-between-numpys-array-and-asarray-functions)_ "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Construct a molecular integrals object\n",
"mints = psi4.core.MintsHelper(wfn.basisset())\n",
"\n",
"# Overlap matrix as a psi4 Matrix object\n",
"S_matrix = mints.ao_overlap()\n",
"\n",
"# Overlap matrix converted into an ndarray\n",
"S = np.asarray(S_matrix) \n",
"\n",
"print(F'Shape of S is {S.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Explain the shape (number of rows and columns) of `S` in terms of the AO basis set we chose."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Examine the contents of `S`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(S) #the full matrix may be somewhat hard to read based on the basis set"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Look at the first few elements\n",
"def peak(S,nrows=4,ncols=4):\n",
" print(F'Here is a peak at the first {nrows} x {ncols} elements of the matrix:\\n{S[:nrows,:ncols]}')\n",
" \n",
"peak(S)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Based on your observations of `S` in the AO basis, answer the following questions\n",
"##### What do the diagonal elements of `S` indicate?\n",
"##### What do the off-diagonal elements of `S` indicate?\n",
"##### Does the Gaussian atomic orbital basis set form an orthonormal basis? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can perform this test programmatically as well, with a few python tricks. Construct an array of the same size as the overlap array (`S`) that has 1's along the diagonal and 0's everywhere else. Then compare that array to the `S` array to determine if the AO basis is orthonormal."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# example of testing for orthonormality\n",
"\n",
"# define a function\n",
"def isBasisOrthonormal(S):\n",
" # get the number of rows of S\n",
" size_S = S.shape[0] \n",
" \n",
" # construct an identity matrix, I -- \"eye\", get it?!? Ha ha! Math is so funny!\n",
" identity_matrix = np.eye(size_S) \n",
"\n",
" # are all elements of S numerically close to the identity matrix? \n",
" # We won't test for equality because there can be very small numerical \n",
" # differences that we don't care about\n",
" orthonormal_check = np.allclose(S, identity_matrix)\n",
"\n",
" print(F'Q:(T/F) The AO basis is orthonormal? A: {orthonormal_check}')\n",
" return orthonormal_check\n",
"\n",
"# use the function\n",
"isBasisOrthonormal(S)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Does the result agree with what you determined above? Explain."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### VIII. An orthogonalization matrix\n",
"Recall that if we had used the hydrogen atom wavefunctions as our basis set, the AO wavefunctions would all be orthonormal. Since we used a basis set of Gaussian wavefuctions, this may not be the case. We will now introduce some tools to fix it!\n",
"\n",
"Since our AO basis set was not orthonormal, we seek to construct an orthogonalization matrix, $A$, such that ${\\bf A}^{\\dagger}{\\bf SA} = {\\bf 1}$. \n",
"\n",
"**Motivation:** If ${\\bf A}$ and ${\\bf S}$ were real numbers $a$ and $s$ (not matrices), this would be simple to solve. First, $a^\\dagger=a$ because a real number is the same as its hermitian transpose. By simple algebra we can solve for a,\n",
"\n",
"$$a^\\dagger s a=1$$\n",
"$$a^\\dagger s a=a s a=a^2s=1$$\n",
"$$\\Rightarrow{}a=s^{-1/2}$$ \n",
"\n",
"In linear algebra (with matrices instead of numbers) this is more complicated, but numpy and the mints class can take care of the details for us! Leaving out the details, we will calculate\n",
"\n",
"$${\\bf A}={\\bf S}^{-1/2}$$.\n",
"\n",
"#### Calculate: Use the function `np.linalg.inv()` to calculate the inverse of `S`, and the function `splinalg.sqrtm()` to take its (matrix) square root. Execute the code below and examine the matrix `A`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==> Construct AO orthogonalization matrix A <==\n",
"\n",
"# inverse of S using np.linalg.inv\n",
"# matrix square root of the inverse of S using splinalg.sqrtm\n",
"\n",
"A = #\n",
"\n",
"peak(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: What do you observe about the elements of `A`? Is the matrix real or complex? Is the matrix symmetric or not?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Our basis set $B$ is not orthonormal, so we want to take linear combinations of its columns to make a new basis set, $B'$, that is orthonormal. We define a new matrix, $A$, in terms of that transformation,\n",
"\n",
"$$B' = BA.$$\n",
"\n",
"The new overlap matrix will be\n",
"\n",
"\\begin{align}\n",
" S' &= B'^\\dagger B',\\\\\n",
" &= (BA)^\\dagger (BA),\\\\\n",
"&= A^\\dagger B^\\dagger BA,\\\\\n",
"&= A^\\dagger S A.\n",
"\\end{align}\n",
"The matrix $A$ makes the proper linear combination of the columns of $B$ and $A^\\dagger$ makes the linear combinations of the rows of $B^\\dagger$. This is a very common structure of transformation matrices. Because $S$ is real and symmetric, $A$ is also real and symmetric, so $A^\\dagger=A$. The transformation becomes simply\n",
"\n",
"$$S' = A S A.$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Use the orthogonalization matrix `A` to transform the overlap matrix, `S`. Check the transformed overlap matrix, `S_p`, to make sure it represents an orthonormal basis."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Transform S with A and assign the result to S_p\n",
"\n",
"S_p = #\n",
"\n",
"isBasisOrthonormal(S_p)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: The product A S A does not take the complex conjugate transpose of A. What conditions (properties of A) make that ok?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IX. The Fock Matrix Transformed to an Orthonormal Basis\n",
"We can now return to Eq.2 using our orthogonalization matrix $A$. A common linear algebra trick is to \"insert one.\" In this case, the matrix $A$ times its inverse is, by definition the identity matrix, $AA^{-1}={\\bf1}$. We can put that factor of one anywhere in an equation that is useful to us, and, then, typically we regroup terms in a way we want. In this case, we insert one bewteen $FC$ and $SC$.\n",
"\n",
"\\begin{align}\n",
"FC&=SCE\\\\\n",
"F({\\bf1})C&=S({\\bf1})CE\\\\\n",
"FAA^{-1}C&=SAA^{-1}CE\n",
"\\end{align}\n",
"Multiplying on the left by $A$ then gives\n",
"\n",
"$$\n",
"AFAA^{-1}C=ASAA^{-1}CE\n",
"$$\n",
"\n",
"We can recognize the transformation $S'=ASA$ on the right hand side and similarly define $F'=AFA$ on the left hand side. Lastly, we define a transformed coefficient matrix, $C'=A^{-1}C$. Our transformed Fock equation reads\n",
"\n",
"\\begin{align}\n",
"F'C'&=S'C'E,\\\\\n",
"&=C'E.\n",
"\\end{align}\n",
"\n",
"The last line follows because $S'={\\bf1}$ in our new basis. We now have an eigenvalue problem that we can solve by matrix diagonalization.\n",
"\n",
"In the expression\n",
"\n",
"$$C'=A^{-1}C,$$\n",
"\n",
"the matrix $A^{-1}$ transforms the coefficients, $C$, into the orthogonalized basis set. We will also need a way to transform those coefficients, $C'$, back to the original AO basis.\n",
"\n",
"#### Question: Based on the definition of $C'$, propose a definition of $C$ in terms of $A$ and $C'$. Justify your equation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### X. Initial guess for the Fock Matrix is the one electron Hamiltonian\n",
"To get the Fock matrix, we need the coefficient matrix, but to compute the coefficient matrix we need the Fock matrix. So we start with a guess for the Fock matrix, which is the core Hamiltonian matrix."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Build core Hamiltonian\n",
"T = np.asarray(mints.ao_kinetic())\n",
"V = np.asarray(mints.ao_potential())\n",
"H = T + V"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: In the cell below, use the core Hamiltonian matrix as your initial guess for the Fock matrix. Transform it with the same A matrix you used above. To calculate the eigenvalues, `vals`, and eigenvectors, `vecs`, of matrix `M` using `vals, vecs = np.linalg.eigh(M)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Guess for the Fock matrix\n",
"\n",
"# Transformed Fock matrix, F_p\n",
"\n",
"# Diagonalize F_p for eigenvalues & eigenvectors with NumPy\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Display, i.e., `print`, the coefficent matrix and confirm it the correct size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the coefficents in the transformed basis, we need to go back and get the coefficients in the original AO basis.\n",
"\n",
"#### Calculate: Use `A` and the formula you proposed previously to transform the coefficient matrix back to the AO basis. Confirm that the resulting matrix appears reasonable, i.e., similar size and magnitude"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Transform the coefficient matrix back into AO basis\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XI. The Density Matrix\n",
"Recall, the Fock matrix is \n",
"\n",
"$$\n",
"F = H + 2J - K\n",
"$$\n",
"where $H$ is the one electron \"core\" Hamiltonian, $J$ is the Coulomb integral matrix, and $K$ is the exchange integral matrix. The HF energy can be expressed in explicit terms of one and two electron integrals\n",
"$$\n",
"E_{HF} = \\sum_i^{elec}\\langle i|h_i|i\\rangle + \\sum_{i>j}^{elec}[ii|jj]-[ij|ji]\n",
"$$\n",
"Expanding the orbitals in terms of basis functions, we find\n",
"$$\n",
"[ii|jj]=\\sum_{pqrs}c^*_{pi}c_{qi}c^*_{rj}c_{sj}\\int{\\rm d}\\tau\\; \\phi_p^*(1)\\phi_q(1)\\frac{1}{r_{ij}}\\phi_r^*(2)\\phi_s(2)\\qquad{\\text{(3)}}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"First, look at the coefficients in Eq.3. They come in two pairs of complex-conjugates, $c^*_{pi}c_{qi}$ and $c^*_{ri}c_{si}$. The diagonal terms, when $p=q$ for example, are the probability of some basis function $p$ contributing to the MO $i$. We will sum each term over the occupied orbitals, $i$, to form the \"density matrix\"\n",
"$$\n",
"D_{pq}=\\sum_i^{occ} c^*_{pi}c_{qi}.\n",
"$$\n",
"\n",
"We are going to construct the density matrix from the occupied orbitals. To get a matrix of just the occupied orbitals, use the coefficient matrix in the original AO basis, and take a slice to include all rows and just the columns that represent the occupied orbitals."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Grab occupied orbitals (recall: ndocc is the number of doubly occupied orbitals we found earlier)\n",
"C_occ = C[:, :ndocc]\n",
"print(F'The shape of C_occ is {C_occ.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Build the density matrix, `D`, from the occupied orbitals, `C_occ`, using the function `np.einsum()`. (Recall Section VI.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Build density matrix from occupied orbitals\n",
"\n",
"D = #\n",
"\n",
"print(F'The shape of D is {D.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XII. Coulomb and Exchange Integrals and the SCF Energy\n",
"\n",
"The integral on the right of Eq.3 is super important. It has four indicies, $p,q,r,s$, so formally it is a tensor. It accounts for the repulsion between pairs of electrons, so it is called the electron repulsion integral tensor, $I$,\n",
"$$\n",
"I_{pqrs} = \\int{\\rm d}\\tau\\; \\phi_p^*(1)\\phi_q(1)\\frac{1}{r_{ij}}\\phi_r^*(2)\\phi_s(2).\n",
"$$\n",
"First, we can build the electron-repulsion integral (ERI) tensor, which stores the electron repulsion between the atomic orbital wavefunctions. Mints does all the work for us!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Build electron repulsion integral (ERI) Tensor\n",
"I = np.asarray(mints.ao_eri())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Eq.3 can be expressed in terms of $I$ and $D$ as\n",
"\\begin{align}\n",
"[ii|jj] &= \\sum_{pqrs}D_{pq}D_{rs}I_{pqrs},\\\\\n",
"&=\\sum_{pq}D_{pq}\\sum_{rs}D_{rs}I_{pqrs}.\n",
"\\end{align}\n",
"The term $\\sum_{rs}D_{rs}I_{pqrs}$ is the effective repulsion felt by one electron due to the other electrons in the system. This term is the Coulomb integral matrix\n",
"$$\n",
"J_{pq}=\\sum_{rs}D_{rs}I_{pqrs}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Define J in terms of the density matrix, `D`, and the electron repulsion integral tensor, `I`, using `np.einsum()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Define J"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, \n",
"\\begin{align}\n",
"[ij|ji] &= \\sum_{pqrs}D_{ps}D_{rq}I_{pqrs},\\\\\n",
" &= \\sum_{ps}D_{ps}\\sum_{rq}D_{rq}I_{pqrs}.\n",
"\\end{align}\n",
"corrects the repulsion due to electrons \"avoiding each other\" due to their Fermionic (antisymmetric w.r.t. exchange) character. This term is the exchange integral matrix\n",
"$$\n",
"K_{ps}=\\sum_{rq}D_{rq}I_{pqrs}.\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Define K in terms of the density matrix, `D`, and the electron repulsion integral tensor, `I`, using `einsum()`. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Define K"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Define F in terms of H, J, and K. (Recall Section XI.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Define F as a function of H, J, and K"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A more convenient form of the SCF energy is in terms of a sum over the AO basis functions\n",
"$$\n",
"E = E_{nuc} + \\sum_{pq}(H_{pq} + F_{pq})D_{pq},\n",
"$$\n",
"where $E_{nuc}$ is the nuclear repulsion, which psi4 can calculate for us as well."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: SCF energy based on H, F, and D using `np.einsum()`. \n",
"*(Hint: The right hand side of the equation is the sum of the product of two terms, each of which has two indices (p and q). The result, E, is a number, so it has no indices. In `einsum()` notation, this case will be represented with the indices for the matrices on the left of the `->`, and no index on the right of the `->`. For example, in the case of just one matrix, the sum of all its elements of a matrix `M` is `sum_of_m = np.einsum('pq->',M )`. In your answer below, be sure to account for any modifications required of an element-wise product of two matrices.)* "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"E_nuc = mol.nuclear_repulsion_energy()\n",
"SCF_E = #\n",
"print(F'Energy = {SCF_E:.8f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Based on the result of the calculation in Section VII, is this a reasonable answer? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer:** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Describe a procedure (i.e. identify the steps/commands) that will updated coefficients and compute a new density matrix based on the new definition of the Fock matrix. (Recall Section X)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer** "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Calculate: Using the procedure proposed above, calculate the updated coefficients"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Update density matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You have just completed one cycle of the SCF calculation!\n",
"\n",
"\n",
"Now we will use the density matrix to build the Fock matrix. The code block below sets up a skeleton of the Hartree-Fock procedure. The basic steps are:\n",
"1. Calculate the Fock Matrix based on the density matrix previously defined from a one electron hamiltonian\n",
"2. Calculate the energy from the Fock matrix.\n",
"3. Check and see if the energy has converged by comparing the current energy to the previous energy and seeing if it is within the convergence threshold.\n",
"4. If the energy has not converged, transform the Fock matrix, and diagonalize the transformed Fock matrix to get the energy and MO coefficients. Then transform back to the original AO basis, pull the occupied orbitals, and reconstruct the density matrix. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# ==> Nuclear Repulsion Energy <==\n",
"E_nuc = mol.nuclear_repulsion_energy()\n",
"\n",
"# ==> SCF Iterations <==\n",
"# Pre-iteration energy declarations\n",
"SCF_E = 0.0\n",
"E_old = 0.0\n",
"\n",
"# ==> Set default program options <==\n",
"# We continue recalculating the energy until it converges to the level we specify. \n",
"# The varible `E_conv` is where we set this level of convergence. We also set a \n",
"# maximum number of iterations so that if our calculation does not converge, it \n",
"# eventually stops and lets us know that it did not converge. \n",
"# Maximum SCF iterations\n",
"MAXITER = 40\n",
"# Energy convergence criterion\n",
"E_conv = 1.0e-6\n",
"\n",
"print('==> Starting SCF Iterations <==\\n')\n",
"\n",
"# Begin Iterations\n",
"for scf_iter in range(1, MAXITER + 1):\n",
" \n",
" # Build Fock matrix (Section XII)\n",
" \n",
" # \n",
"\n",
" # Compute SCF energy\n",
" \n",
" SCF_E = #\n",
" \n",
" print(F'SCF Iteration {scf_iter}: Energy = {SCF_E:.8f} dE = {SCF_E - E_old:.8f}')\n",
" \n",
" # Check to see if the energy is converged. If it is break out of the loop.\n",
" # If it is not, set the current energy E_old\n",
" \n",
" if (abs(SCF_E - E_old) < E_conv):\n",
" break\n",
" E_old = SCF_E\n",
" \n",
" # Compute new coefficient & density matrices (Section X & XI) \n",
" \n",
" # \n",
" \n",
" # MAXITER exceeded?\n",
" if (scf_iter == MAXITER):\n",
" psi4.core.clean()\n",
" raise Exception(\"Maximum number of SCF iterations exceeded.\")\n",
"\n",
"# Post iterations\n",
"print('\\nSCF converged.')\n",
"print(F'Final RHF Energy: {SCF_E:.6f} [Eh]')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compare your results to Psi4 by computing the energy using `psi4.energy()` in the cell below. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# ==> Compare our SCF to Psi4 <==\n",
"# Call psi4.energy() to compute the SCF energy\n",
"SCF_E_psi = psi4.energy('SCF')\n",
"psi4.core.clean()\n",
"\n",
"# Compare our energy value to what Psi4 computes\n",
"assert psi4.compare_values(SCF_E_psi, SCF_E, 6, 'My SCF Procedure')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Question: Modify the value of E_conv and describe its affect the number of iterations."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### XIII. Using Hartree-Fock to Justify Molecular Structure\n",
"\n",
"Why is CO$_2$ linear? Why is H$_2$O bent? Why is CH$_4$ tetrahedral? Why is FeF$_6$ octahedral? In general\n",
"chemistry, we used valence shell electron pair repulsion (VSEPR) theory to justify molecular structures\n",
"by invoking a _repulsion_ between both bonding and non-bonding pairs of electrons. The reality of molecular\n",
"structure is more complicated, however.\n",
"\n",
"In this section of the lab, we will use the same Hartree-Fock method that we implemented above to justify the\n",
"bent structure of water by computing the electronic energy of H$_2$O at a variety of bond angles. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ==> Scanning a Bond Angle: Flexible Water <==\n",
"# Import a library to visualize energy profile\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"# Define flexible water molecule using Z-matrix\n",
"flexible_water = \"\"\"\n",
"O\n",
"H 1 0.96\n",
"H 1 0.96 2 {}\n",
"\"\"\"\n",
"\n",
"# Scan over bond angle range between 90 & 180, in 5 degree increments\n",
"scan = {}\n",
"for angle in range(90, 181, 5):\n",
" # Make molecule\n",
" mol = psi4.geometry(flexible_water.format(angle))\n",
" # Call Psi4\n",
" e = psi4.energy('scf/cc-pvdz', molecule=mol)\n",
" #e = psi4.energy('scf/sto-3g', molecule=mol)\n",
"\n",
" # Save energy in dictionary\n",
" scan[angle] = e"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Visualize energy profile\n",
"x = list(scan.keys())\n",
"y = list(scan.values())\n",
"plt.plot(x,y,'ro-')\n",
"plt.xlabel('H-O-H Bond Angle ($^{\\circ}$)')\n",
"plt.ylabel('Molecular Energy ($E_h$)')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the energy profile we generated above, justify the experimentally measured water bond angle of 104.5$^{\\circ}$ in the cell below."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Answer**"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": true,
"user_envs_cfg": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}