Learn PythonPython

How To Make A Python Script For Solving MP2 Equations

How To Make A Python Script For Solving MP2 Equations

Recently, we showed the basics of quantum chemistry is, and how to do it using Python. This will be a continuation of it. In this article, we will discuss how to write a python script for solving mp2 equations. Make sure you check out the previous article to learn the very basics. 

The theory behind it requires years of studying quantum mechanics, mathematics, chemistry, and sometimes even some biology, and combining these fields together to study nature using computer models. This is not what we are going to do here! However, I can give you some hints about how a complicated problem can be made much easier using Python script. We are going to use the PySCF package to perform some calculations.

In the examples in the previous article, you can perform calculations of the energy of a molecule using the Hartree-Fock theory and using Density Functional Theory. These methods are very important and useful, though in some cases, one wants to get to the next level in terms of accuracy.

What is correlation energy?

After multiple approximations, it is possible to recover about 99% of the exact energy of a molecule. While it may sound excellent, the whole of chemistry is in the remaining 1%, which is also called correlation energy. Among the methods improving on this starting point (i.e., Hartree-Fock theory) is perturbation theory.

The name sounds funny and weird at the same time. You can think of something that disturbs the molecule a bit. But not too much. Let’s say the molecule is placed in a magnetic field. The interaction between them affects the energy and the properties of the molecule. They can be modelled by defining new parameters in the Schrödinger equation.

How To Make A Python Script For Solving MP2 Equations an image of bubbles and stars

How to calculate the MP2 correlation energy?

If you read the previous article, you know that the electrons of atoms and molecules live in what we call orbitals. Within Hartree-Fock theory, we can model the orbitals using a set of mathematical functions called a basis set. There are coefficients telling what the weight of each basis function is. These coefficients are written in matrix form, which is called the Fock matrix.

After lots of mathematical operations, one obtains a set of equations that have to be solved. The solution gives the total energy of the molecule and optimises the orbital coefficients that give the best energy. However, these orbitals can be used as input in the perturbation theory approach. The most common method within it carries the names of two scientists – Møller–Plesset perturbation theory. It is usually truncated to the second order and abbreviated as MP2 for conciseness.

Here is an example of how to do it in practice using Python. We will use water as an example. First, we will define the coordinates, then specify the basis set and that the molecule is symmetric. Next, we have to run the Hartree-Fock calculation. Finally, we can also perform the MP2 calculation using the optimized wave function that was just calculated.

The code above prints the Hartree-Fock energy, as well as the MP2 energy. Their difference is the MP2 correlation energy.

A note about the units. Probably these numbers do not speak much to you. They are in what we call “atomic units”, also known as hartree units. It is a bit hard to give you a perspective about its magnitude in terms of everyday objects or phenomena, but I will try. If you are into tech, you might know that modern high-end CPUs run at less than 1.5 volts. 1 hartree is equal to about 27 electron-volts. Check out Wikipedia if you want to know more about the units of energy.

How To Make A Python Script For Solving MP2 Equations a person at a data center

What do we mean by the computational cost of calculations?

The example for water finishes immediately. However, some quantum chemical calculations can take hours and even days. Improving the results using second-order perturbation theory involves using the orbitals obtained in a Hartree-Fock calculation. This approach is more accurate, but it is computationally heavy. It depends on the number of integrals that the program needs to calculate in order to reach the final result.

Why are quantum chemistry calculations so computationally heavy?

There are mathematical techniques that introduce some approximations which do not affect the accuracy significantly, but they speed up the calculations. They carry names such as density fitting and resolution of identity. The hardest part of the equations is solving two-electron integrals. Essentially, they depict the electrostatic repulsion between two electrons occupying two possible orbitals. We place electron 1 in orbital 1 and electron 2 in orbital 2. Then each of them is squared in the integral expression. The other set of two-electron integrals describes the so-called exchange interaction, which has no analogue in the macroscopic world. The two electrons are placed each in two different orbitals – electron 1 in orbital 1 and electron 2 in orbital 2. Then, their places are flipped – electron 1 goes to orbital 2, and electron 2 goes to orbital 1. Sounds simple, but the math behind it is very demanding.

How To Make A Python Script For Solving MP2 Equations a person studying a laptop

How to simplify and speed up the calculations?

The two-electron integrals can be expanded using different functions called an auxiliary basis set. They give little penalty to the accuracy, but the speedup is quite good. Still, it is impossible to calculate a whole protein with millions of atoms using correlated quantum chemistry methods. They are usually performed using classical Newtonian mechanics, where the nuclei are moving in an electrostatic potential created by the electrons. But that is a story for another day.

How big molecules can be studied using MP2?

One little challenge would be to check how the number of atoms affects the speed of the calculation. Or more precisely, the number of electrons. A hydrogen atom only has one electron, but iron has 26. You can guess which one takes longer to calculate. You can test on your own how many seconds it takes to calculate water versus a big porphyrin molecule. Here are the coordinates of its atoms

This is what the molecule looks like if you open it in a molecular viewer such as Avogadro.

How To Make A Python Script For Solving MP2 Equations - a molecule

What have we learned in this article?

Do you feel more of a quantum chemist after this article? No worries if you don’t. You can always learn more. There is an excellent implementation of a wide variety of quantum chemistry methods available in PySCF that you can test on your own, and you will learn more about it. How about you try some fancier molecule? You can do that in the PyScripter IDE. You can find installation instructions for PySCF inside PyScripter in the previous article about quantum chemistry.

PyScripter is available for download for free at https://www.embarcadero.com/free-tools/pyscripter/free-download

About author

I am a quantum chemist and a photographer with a decade of experience in programming in various languages. I have worked with python in the past five years regularly on multiple projects and in my free time. I love sharing knowledge with the world and clearly explaining complex concepts.
Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *