.. _lecture1: ===================================================================== Introduction to quantum chemistry and machine learning ===================================================================== - :ref:`Quantum chemical levels ` - :ref:`Finally, your first ML calculations! ` - :ref:`Homework ` - :ref:`Further reading ` Slides ------ :download:`Slides <_static/lecture1/Lecture1_wm.pdf>`: .. raw:: html .. _lecture1_qc_levels: Quantum chemical levels ------------------------------ Here are two exercises to warm up. .. admonition:: Excercise QC-intro-1. Calculate bond length in H\ :sub:`2` as fast as possible. Questions: 1. method you used 2. bond length you obtained 3. how much time it took to complete the calculations .. admonition:: Excercise QC-intro-2. Calculate bond length in H\ :sub:`2` as accurately as possible. Questions: 1. method you used 2. bond length you obtained 3. how much time it took to complete the calculations After you overcome the initial shock of starting a lecture with an exercise, let's see how we would approach this task. The system is very small, just a hydrogen molecule, and might seem to be boring, but it has the benefit that we can investigate it to death. Choosing an appropriate quantum mechanical (QM) method for running is the task number one of any computational chemistry endevour. Any computational chemist should therefor clearly understand the strengths and weaknesses of different available QM methods. While there is an ever-increasing number of QM methods and knowing all of them impossible for any human, :ref:`we should at least know the main classes and typical representatives `. A simplified hierarchy of computational chemsitry methods is shown below: .. image:: _static/lecture1/qm_methods.png :width: 400 :align: center :alt: Simplified hierarchy of computational chemsitry methods The least accuarate but fastest are semi-empirical QM methods such as AM1, PM6, GFN2-xTB, OM2, etc. They are used when we need to obtain electronic-structure information and other methods are too slow. Next are density functional theory (DFT) methods which are workhorse in computational chemistry because of their optimal balance of relatively good accuracy combined with affordable computational cost. Despite the theorems granting that DFT can be exact, practically, no exact DFT method (called *DFT functional*) actually exists. Some of the most popular DFT functionals are B3LYP and PBE. wB97XD is one of my favourite one because it both has better description of charge-transfer processes (as it belongs to range-separated functionals) and has decent accuracy for non-covalent interactions. The DFT functionals have their own hierarchy (check out `Jacob's ladder `__) with PBE belonging to GGA (generalized gradient approximation) type, B3LYP to hybrid functional, and on top of the ladder -- much slower but more accurate so-called double hybrid functionals such as B2-PLYP. The most accurate results we can obtain with the *ab initio* wavefunction-based methods which are systematically improvable. The starting point is so called Hartree--Fock method (HF) which is less accurate than modern DFT methods (and not faster either). HF's deficiency is that it does not describe electron correlation properly (electrons should avoid each other but HF single-determinant formulation is not treating it fully). To recover electron correlation, many post-HF methods were developed. For exact treatment, we need to consider the combination of all possible determinants corresponding to different electron excitations/configurations (single, double, etc.), as is done in the so called full configuration interaction (CI) method. Full CI is untreatable in most practical cases due to the combinatorial explosion of the number of determinants. Hence, many approximated post-HF methods were developed. The easiest to use are based on perturbation, e.g., MP2, and coupled-cluster (CC) theories. CC is not CI-based but also considers excitations to different extend, i.e., CCSD(T) includes singles (S) and doubles (D) excitations, '(T)' stands for perturbative triples, while CCSD is more truncated and CCSDTQ less truncated but more accurate. HF, MP2, CC, and full CI methods can be arranged as follows: .. image:: _static/lecture1/wfn_methods.png :width: 400 :align: center :alt: Simplified hierarchy of a selection of wavefunction-based methods CCSD(T) is considered the gold-standard due to its high accuracy. MP2 and CCSD(T) are single-reference methods and have limitations in many situations such as bond breaking. Alternative branch of post-HF wavefunction methods are based on CI formulations such as CASSCF, CASPT2, and multi-reference CI, and they offer a solution for difficult cases. However, their use is much less straightforward. In our example, H\ :sub:`2` has only two electrons, hence, its correlation energy is easily recovered when we include single and double excitations, i.e., full CI is easily achievable with CISD method which, in this case (but only in such a simple case!), is equivalent to CCSD and CCSD(T) calculations run with a quantum chemical program. Regardless which method you use, the accuracy hinges on the basis set you chose (except for semi-empirical QM methods which come with the predefined basis sets). The basis set is required for numerical calculations and the larger set allows for greater flexibility for electrons in occupying the space. There are also lots of basis sets, starting from the smallest minimal STO-3G to double-, triple-, quadruple, quintuple-, sixtuple-:math:`\zeta` (zeta) up to complete basis set (CBS) limit (infinitely large basis set): .. image:: _static/lecture1/basis_sets.png :width: 400 :align: center :alt: Simplified hierarchy of basis sets While CBS is rarely achievable, many extrapolation schemes were designed. In practice, double- and triple-:math:`\zeta` basis sets (often with additional polarization and diffusion functions) are very common and the minimum (valence-only) sets are used in semi-empirical methods. You might often see HF with minimum STO-3G set (denoted HF/STO-3G) as one of the earliest and fastest methods. No one is using it anymore in modern calculations. More common combination is, e.g., B3LYP/6-31G* with 6-31G* belonging to the Pople's basis sets. Alternative popular basis set types are the Dunning's (aug-)cc-VXZ (where X can be D (double), T (triple), Q (quadruple), 5 (quitubple), 6 (sixtuple) in the order of increasing size) and Ahlrichs' SVP, TZVP, etc. basis sets. Putting it all together, for Task 1.1, the fastest way to calculate the bond lengths would be to use a semi-empirical QM method (e.g., AM1). If you chose HF/STO-3G, it is slower and even many DFT would be faster. For Task 1.2, the most accurate answer would be to use full CI which is achievable in this case with, e.g., CCSD(T) and a large basis set. If you chose B3LYP/6-31G* because everyone is using it - the most popular does not mean the most accurate. As a computational chemist, you should know that better methods such as CCSD(T) are available and should be used whenever possible: .. image:: _static/lecture1/qm_methods_selected.png :width: 400 :align: center :alt: Simplified hierarchy of computational chemsitry methods with selected methods highlighted In general, DFT has a problem that there is no clear systematic way to guarantee its high accuracy. If you want to be sure and even get an exact answer, it is better to use wavefunction-based methods, where you know what to do: take methods describing electron correlation better and use larger basis set. This concept is summarized in the Pople diagram: .. image:: _static/lecture1/Pople_diagram.png :width: 400 :align: center :alt: Pople's diagram [Credit: Panisanun, `CC BY-SA 4.0 `__, via Wikimedia Commons.] Now, if you are completely new to computational chemistry, or want to try yourself calculating the bond length of H\ :sub:`2` with different methods using MLatom, here is a sample input file: .. code-block:: geomopt B3LYP/6-31G* XYZfile=' 2 H 0.000 0.000 0.000 H 0.000 0.000 0.800 ' or more advanced input: .. code-block:: geomopt # 1. requests geometry optimization method=B3LYP/6-31G* # 2. request running DFT optimization with B3LYP qmprog=PySCF # 3. request using PySCF for B3LYP calculations optprog=scipy # 4. request using scipy for geometry optimizations # optprog=Gaussian and qmprog=Gaussian can also be used but not online (due to license restrictions) XYZfile=' 2 H 0.000 0.000 0.000 H 0.000 0.000 0.800 ' optxyz=opt.xyz # 5. file with optimized geometry At the end, you can compare your result with the known experimental bond length of 0.7414 Angstrom. If you used not very large basis set, it took probably seconds to minutes to get the result. If you pushed hard and calculated with full CI and, e.g., large aug-cc-pV6Z basis set, you got 0.7415 Å, ~5 (CPU) days (if you have many CPUs available for calculations, they can be run in parallel and take much less time). As you see, even for such a small molecule, accurate calculations take substantial time. For larger molecules, time cost explodes fast (it is not even linear scaling, e.g., for DFT, twice larger molecule would take 8 times longer while with CCSD(T) -- more than 120 times longer). .. _lecture1_ml: Finally, your first ML calculations! ------------------------------------ If you are eagerly waiting to start ML, you might wonder why we spend so much time on QM basics. the reason is simple: *to use ML, you need to fisrt understand the physical or chemical problem*, because ML is only a tool in your hands and it does nont matter how well you do ML but it if it is trained on a bad QM method, it will also be bad ("garbage in - garbage out"). Without further ado, let's jump into our first ML calculations with Task 1.3 (in in-class exercises, you have :red:`10 min`, send answers on WeChat and after everyone finishes, write down the answer on a blackboard): .. include:: materials/lecture1/task1.3_h2_geomopt/task1.3_h2_geomopt.inc After you clicked the submit button, you should have gotten the result pretty fast. On my computer, it took only ~0.3 seconds to obtain the bond length of 0.7415 Å. This is remarkably close to both experiment and FCI/aug-cc-pV6Z, but obtained much faster! Now you see, why computational chemists are so excited about using ML as it breaks through the limitations of slow QM methods and delivers results fast. It can be as accurate as we need if we train it on enough data. It can even achieve full CI accuracy in less than a second. Indeed, this is what our ML model delivers - full CI quality result for the hydrogen molecule! ML can be also used to improve QM methods and many examples of ML-enhanced semi-empirical, DFT, and *ab initio* wavefunction methods emerge, also with contributions from our group like `AIQM methods `__ which achieves the gold-standard CCSD(T)/CBS accuracy at the speed of semi-empirical methods (for CHNO-containing molecules only but we are working on extensions). We will come back to the AIQM methods in the next lectures. The power of ML is that it can learn from experience (data) rather than being programmed explicitly by humans. It imparts ML with the versatality to learn QM properties (e.g., enregies), wavefunctions, and Hamiltonians. In contrast to quantum chemical programs, where humans have to derive equations and implement them in computer programs specifically for each property, the same ML program, in principle, can learn any property given data. In practice, domain knowledge is still required to make it work properly as we will see in the next lectures. Going back to H\ :sub:`2`, how did we get such a good ML model? Two key components are 1) data and 2) ML model. The data I calculated at FCI/aug-cc-pV6Z for 451 geometries of H\ :sub:`2` with different bond lengths from 0.5 to 5 Angstrom. ML model is my KREG model to which we come later. You can now proceed to train your own KREG model and use it to calculate the bond length: .. include:: materials/lecture1/task1.4_kreg_train/task1.4_kreg_train.inc At the end of calculation, you can find in the output file that the training and validation errors (in Hartree) are very tiny showing that the model learned well the data. Also, you got with this model the same bond length of 0.7415 Å as with FCI/aug-cc-pV6Z. Before you jump into the next lecture, please do the homework because the next lecture will start with it. .. _lecture1_homework: Homework -------- .. include:: materials/lecture1/homework1.1/homework1.1.inc .. include:: materials/lecture1/homework1.2/homework1.2.inc .. _lecture1_further_reading: Further reading --------------- To get better understanding of quantum mechanical methods, you can read dedicated literature. Good start is: - classical textbook: A. Szabo, N. S. Ostlund, Modern Quantum Chemistry: Introduction to Advanced Electronic Structure Theory. Dover Publications, Inc.: Mineola, New York, 1996; pp. 39-43. - and a book edited by this course's author: `Quantum Chemistry in the Age of Machine Learning `__. P. O. Dral, Ed. Elsevier: Amsterdam, Netherlands, 2023. - dated, but a nice introduction into DFT: Young, D. C. Computational Chemistry: A Practical Guide for Applying Techniques to Real-World Problems; John Wiley & Sons, Inc., 2001.