Quantum Mechanics with Python: Hydrogen Wavefunctions and Electron Density Plots

Sebastian Mag.
13 min readDec 7, 2023

--

Photo by Aldebaran S on Unsplash

This article serves as a comprehensive practical walkthrough in the process of modeling and visualizing hydrogen atom wavefunctions and electron probability densities through the lens of theoretical physics and computational modeling with Python.

First we may establish the necessary theoretical foundations in quantum mechanics required to model the phenomena under study; with these theoretical principles in place, we may look through an in-depth breakdown of the Python code, focusing on how it can be used to visualize and apply these principles.

Table of Contents

1. Theoretical Background

  • 1.1 Quantum Mechanics and Atomic Systems: A Brief Overview
  • 1.2 Schrödinger Equation for Hydrogen Atom Wavefunctions

2. Practical Implementation

  • 2.1 Wavefunction Modeling with Python
  • 2.2 Probability Densities of Quantum States

1.1 Quantum Mechanics and Atomic Systems: A Brief Overview

Quantum mechanics (QM) is the fundamental theory in physics that provides a description of the physical properties of nature at the scale of atoms and subatomic particles. Unlike classical mechanics which describes macroscopic phenomena, QM addresses the behavior of matter and energy on the quantum (smallest discrete) level.

The hydrogen atom is especially significant as it is the simplest atom, containing just one electron. Its wavefunction can be treated analytically, providing profound insights into the nature of quantum systems.

Electron probability density for hydrogen atom orbitals shown as cross-sections — Figure by Sebastian Mag

Wavefunctions

A wavefunction, often denoted as (ψ), represents the quantum state of a particle in a system. It provides information about the probability amplitude of position and momentum states of the particle.

Electron Density | Probability Density

The square magnitude of the wavefunction |ψ|², gives the probability density for the particle’s position in space. For an electron in an atom, it describes the spatial distribution of the probability of locating the electron.

Atomic Orbitals

These are mathematical functions that describe the wave-like behavior of either one electron or a pair of electrons in an atom. These functions can be used to determine the probability of finding an electron in any specific region around the atom’s nucleus.

Quantum Numbers

We can describe quantum numbers as a set of numerical values that provide a complete definition of a quantum particle’s state. For electrons in an atom, there are typically four quantum numbers:

  • Principal quantum number (n): ( 1 <= n )
    Represents the electron's energy level and relative size of the orbital.
  • Azimuthal quantum number (l): ( 0 <= l <= n-1 )
    Relates to the shape of the atomic orbital.
  • Magnetic quantum number (mₗ): ( -l <= m <= l )
    Specifies the orientation of the orbital in space.
  • Spin quantum number (mₛ): ( +1/2 or -1/2 )
    Describes the electron's intrinsic spin.

Note: In the hydrogen atom, or any atom with a single electron (like ionized helium, lithium, etc.), the electron’s spin doesn’t interact with anything else to affect its spatial distribution.

For our specific application with the hydrogen atom, we will focus on the first three quantum numbers (n, l, m) . As the electron’s spin doesn’t influence the shape or distribution of the electron cloud.

Photo by Dario Brönnimann on Unsplash

1.2 Schrödinger Equation for Hydrogen Atom Wavefunctions

The Schrödinger equation serves as the foundation of quantum mechanics, it is a differential equation that determines the wavefunctions of a quantum system. For the hydrogen atom, we use the following representation of the time-independent Schrödinger equation:

The time-independent Schrödinger equation

Ĥ is the Hamiltonian operator, which represents the total energy (kinetic + potential) of the system, and E is the total energy of the system.

Given the spherical symmetry of the hydrogen atom, we may express it in terms of spherical coordinates (r, θ, φ) instead of rectangular coordinates (x, y, z). Where r is the radial coordinate, θ is the polar angle (relative to the vertical z-axis), and φ is the azimuthal angle (relative to the x-axis).

Relationship between the spherical and rectangular coordinate systems

The wavefunction ψ(r,θ,φ) can be represented as a product of radial and angular functions:

Separation of variables in the wavefunction into radial and angular components

When the Hamiltonian is expressed in spherical coordinates, it contains both radial and angular parts. By substituting this into the Schrödinger equation, we separate the equation into two parts: one that depends only on r (radial part) and another that depends on θ and φ (angular part).

1. Radial Component

Formula for the radial wavefunction of a hydrogen atom

The radial wavefunction gives us information about the probability distribution of the electron as a function of distance r from the nucleus.
Its form encompasses three major terms:

  • 1.1 Exponential Decay: Signifies the probability decay of finding an electron as we move away from the nucleus. Here, a₀ is the Bohr radius which sets a characteristic scale for atomic dimensions:
Exponential decay term in the hydrogen atom’s radial wavefunction
  • 1.2 Power term: Dictates how the probability changes with r. The azimuthal quantum number l plays a significant role in determining the number of nodes in the radial distribution:
Radial distance scaling term in the hydrogen atom’s wavefunction
  • 1.3 Associated Laguerre Polynomials: These polynomials contribute to the finer structure of the radial part, especially defining nodes (regions where the probability is zero):
Associated Laguerre polynomial in the hydrogen radial wavefunction

2. Angular Component

Spherical harmonic function for angular wavefunction components

The angular wavefunction yields the spherical harmonics, which gives the angular dependence of the wavefunction in terms of the polar (θ) and azimuthal ) angles.

These spherical harmonics provide a detailed account of the shapes and orientations of atomic orbitals, characterizing how electron probability distributions are spread out in space. It has two components:

  • 2.1 Associated Legendre Polynomials: These dictate the shape of the orbital in the polar (θ) direction, helping to define the characteristic shapes (s, p, d, etc.) we often associate with atomic orbitals:
Associated Legendre Polynomial for angular wavefunction components
  • 2.2 Exponential Azimuthal Term: This term provides the orientation of the orbital in the azimuthal plane, as determined by the magnetic quantum number m:
Exponential azimuthal term in wavefunction phase

Normalized wavefunction

The resultant normalized wavefunction for the hydrogen atom is the product of the solutions of the radial and angular components:

Complete hydrogen atom wavefunction in spherical coordinates

To determine the probability density of the electron being in a certain location, we integrate the square magnitude of the wavefunction over all space: ₙₗₘ

Probability density function for hydrogen electron location
Photo by Tengyart on Unsplash

2.1 Wavefunction Modeling with Python

After covering the theoretical foundation in the previous section, we are now ready to apply these principles to model hydrogen atom wavefunctions and electron probability densities with Python.

For this purpose, we will implement the following dynamics:

  1. Describe a Normalized Radial Function Rₙₗ(r)
    Defining the radial component of the wavefunction, focusing on electron probabilities relative to their distance from the nucleus.
  2. Describe a Normalized Angular Function Yₗₘ(θ,φ)
    Defining the angular component of the wavefunction, crucial for understanding electron orbital shapes and orientations.
  3. Compute the Normalized wavefunction ψₙₗₘ(r,θ,φ) = Rₙₗ(r) · Yₗₘ(θ,φ)
    Here we will combine the radial and angular functions to form the complete, normalized wavefunction representing the quantum state of the electron.
  4. Compute the Probability density |ψ|²
    This involves calculating the square of the wavefunction’s magnitude to derive the electron’s spatial probability distribution.
  5. Plot the Probability Density
    The final step is to visually represent the probability density, providing a graphical depiction of where the electron is likely to be found within a given quantum state.

First, import the necessary libraries required for scientific computing and data visualization, SciPy physical constants and special functions for precise scientific calculation and complex mathematical operations,
Matplotlib pyplot and seaborn for enhanced visualizations of data, and NumPy for efficient numerical computations with arrays.

Requirements.txt
• Python 3.11.4
• Matplotlib 3.7.2
• Seaborn 0.12.2
• NumPy 1.25.2
• SciPy 1.11.1

1. Describe a Normalized Radial Function Rₙₗ(r)

The radial part of the wavefunction is constructed by the product of:

  • a) Constant factor: constant_factor
    Normalizes the radial wavefunction.
  • b) Exponential decay factor: np.exp(-p / 2)
    Reflects the decrease in probability of finding an electron as it moves away from the nucleus.
  • c) Power-law dependence on radial distance: p ** l
    Introduces a dependency based on the azimuthal quantum number l, indicating different radial behaviors for different orbitals.
  • d) Laguerre polynomial: laguerre(p)
    Captures oscillations in the electron density as a function of radial distance.

Laguerre polynomials describe how the electron density changes as the distance from the nucleus increases:
laguerre = sp.genlaguerre(n — l — 1, 2 * l + 1)

Normalized radial distance from the nucleus:
p = 2 * r / (n * a0)

2. Describe a Normalized Angular Function Yₗₘ(θ,φ)

The angular part of the wavefunction is constructed by the product of:

  • a) Constant factor: constant_factor
    Normalizes the angular wavefunction.
  • b) Legendre polynomial:
    Describes the angular dependence of the wavefunction based on the quantum numbers. Providing insight into the orientation and shape of electron orbitals around the nucleus for given quantum numbers.
  • c) Exponential factor: np.real(np.exp(1.j * m * phi))
    Introduces a phase shift dependent on the magnetic quantum number m and the azimuthal angle φ.

Legendre polynomials describe the spatial arrangement and directional characteristics of electron probability densities:
legendre = sp.lpmv(m, l, np.cos(theta))

3. Compute the Normalized wavefunction ψₙₗₘ(r,θ,φ) as a product of
Rₙₗ(r) · Yₗₘ(θ,φ)

The Bohr radius sets the scale of the wavefunction and determines the size of the atom. By scaling it, we adapt the wavefunction’s spatial extent for effective visualization:
a0 = a0_scale_factor * physical_constants[‘Bohr radius’][0] * 1e+12

Next, we establish a grid in the x-y plane, allowing the wavefunction to assign a probability value to each point. This grid aids in visualizing the electron’s spatial distribution:
x, y = np.meshgrid(x, y)

Then we compute the wavefunction by multiplying the radial and angular parts. The radial part considers the distance from the nucleus, whereas the angular part looks into the spatial orientation. Together, they define the electron’s behavior in the atom’s vicinity:
psi = radial_function(...) * angular_function(...)

Finally, we return the computed wavefunction, which encapsulates the quantum state of an electron in a hydrogen atom. The wavefunction contains complex amplitudes that provide information about the quantum state’s magnitude and phase:
return psi

4. Compute the Probability density |ψ|²

Here we return the square magnitude of the wavefunction, encapsulating the probability of the electron’s presence in different regions of the atom:
return np.abs(psi) ** 2

5. Plot the Probability Density

In this code snippet, you’ll notice that a significant portion is dedicated to styling, thematics and data validation. The core functionality that computes and visualizes the electron probability distribution is encapsulated in just a few lines. Specifically, the key steps involve:

  • a) Computing the wavefunction
    psi = compute_wavefunction(n, l, m, a0_scale_factor)
  • b) Computing the probability density
    prob_density = compute_probability_density(psi)
  • c) Plotting the data
    im = ax.imshow(np.sqrt(prob_density), cmap=sns.color_palette(...))

Note: By taking the square root of the probability density we reduce the dynamic range of the visualization, spreading out the values and making the electron’s presence in low and medium probability regions more distinguishable.

Photo by Aldebaran S on Unsplash

2.2 Probability Densities of Quantum States

Let’s now run our Python script to visually explore probability densities across various quantum states (n l m), showcasing the diverse electron distributions within the hydrogen atom.

a) (3, 2, 1)

plot_wf_probability_density(3, 2, 1, 0.3, True)
Table of Parameters for Visualizing a Quantum State (3,2,1) in the Hydrogen Atom
Electron probability density for a Hydrogen Atom in the quantum state (3, 2, 1)

The plot depicts the electron probability density for a hydrogen atom in the quantum state n=3, l=2, and m=1. This corresponds to a 3d orbital, displaying a clover-shaped distribution pattern with two lobes, indicating regions of higher electron probability, as shown by the bright spots.

The m=1 indicates the orbital is oriented in a particular manner in space, not symmetric about the nucleus like m=0 would be. This state is an excited state but lower in energy than a 4f state.

b) (4, 3, 0)

plot_wf_probability_density(4, 3, 0, 0.2, dark_theme=True, colormap='magma')
Table of Parameters for Visualizing a Quantum State (4,3,0) in the Hydrogen Atom
Electron probability density for a Hydrogen Atom in the quantum state (4, 3, 0)

This plot represents the electron probability density for a hydrogen atom in the quantum state defined by the quantum numbers n=4, l=3, and m=0. The depicted orbital is a 4f orbital, characterized by its intricate, multi-lobed shape with pronounced nodes.

The m=0 quantum number indicates that this orbital has a symmetrical distribution about the nucleus, which is visible in the plot as the lobes are symmetrically arranged around the center. This state is a higher energy state with a significant probability of finding the electron in the outer lobes as indicated by the brighter areas.

c) (4, 3, 1)

plot_wf_probability_density(4, 3, 1, 0.2, dark_theme=True, colormap='mako')
Table of Parameters for Visualizing a Quantum State (4,3,1) in the Hydrogen Atom
Electron probability density for a Hydrogen Atom in the quantum state (4, 3, 1)

The plot presents the electron probability density for a hydrogen atom in the quantum state with quantum numbers n=4, l=3, and m=1, which corresponds to a 4f orbital.

The m=1 indicates a particular asymmetrical orientation of the orbital in space, which is reflected in the non-uniform distribution of the probability density.

As we examine the electron density plots, we notice that with increasing principal quantum number (n), the complexity of the wavefunction grows Specifically:

  • The number of nodes (regions where the probability density is zero) increases.
  • The electron’s spatial distribution expands.
  • The overall shape of the atomic orbital becomes more detailed.

d) (9, 6, 1)

plot_wf_probability_density(9, 6, 1, 0.04, True, colormap='mako')
Table of Parameters for Visualizing a Quantum State (9,6,1) in the Hydrogen Atom
Electron probability density for a Hydrogen Atom in the quantum state (9, 6, 1)

The plot shows the electron probability density for a highly excited quantum state of a hydrogen atom, specifically for an electron in a 9g orbital, as indicated by the quantum numbers n=9, l=6, and m=1.

For extremely high quantum numbers, the following effects can be observed:

  • The complexity increases even further, resulting in numerous nodes and intricate patterns.
  • Evaluating the wavefunction over a vast spatial domain becomes computationally intensive.
  • Visualization can become cluttered, making it harder to discern specific details or features.

e) (20, 10, 5)

plot_wf_probability_density(20, 10, 5, 0.01, True, colormap='mako')
Table of Parameters for Visualizing a Quantum State (20,10,5) in the Hydrogen Atom
Electron probability density for a Hydrogen Atom in the quantum state (20, 10, 5)

The plot displays the electron probability density for a hydrogen atom in the quantum state n=20, l=10, and m=5. This represents an extremely high-energy state, far above the ground state, within a hypothetical 20k orbital (where k corresponds to l=10).

The pattern is highly complex, with many nodal lines indicating regions of zero probability. The quantum number m=5 introduces asymmetry in the angular distribution of the electron density, and the high n value suggests a very diffuse and extensive orbital far from the nucleus.

This is a rare and abstract state, rarely depicted or discussed in basic quantum mechanics due to its high energy and complexity.

GitHub Repository:

References

--

--

Sebastian Mag.
Sebastian Mag.

Written by Sebastian Mag.

SWE | AI MSc Candidate @ Hull / Computational Physics & Research

Responses (1)