This code defines a quantum mechanical system consisting of a particle moving in a harmonic oscillator potential. It then numerically solves the time-dependent Schr￶dinger equation for this system using the explicit matrix exponential method. The resulting wave function is then plotted as a 3D surface.

The system is defined by specifying the parameters of the harmonic oscillator potential, including the mass of the particle, the angular frequency of the oscillator, and the size of the system. The initial wave function is also specified, which is a Gaussian wave packet centered at the origin with a certain momentum.

The Hamiltonian operator is then constructed as a matrix, which includes the kinetic energy and potential energy terms. The time evolution of the wave function is then computed using the matrix exponential of the Hamiltonian multiplied by a time step.

The wave function is then plotted as a 3D surface using matplotlib. The x and y axes represent the position and time, respectively, while the z-axis represents the probability density of finding the particle at a particular position and time. The resulting plot shows the spreading out of the wave packet as it evolves in time, reflecting the uncertainty principle in quantum mechanics.

import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import expm
from numpy.linalg import norm
from mpl_toolkits.mplot3d import Axes3D

hbar = 1.0
m = 1.0
omega = 2.0
dt = 0.005
dx = 0.03
L = 5.0
N = int(L/dx)  # Define x as an array from -L to L, and redefine the potential
x = np.linspace(-L, L, N)
V = 2 * m * omega**2 * x**2

# Define the initial wave function
k0 = m * omega / hbar
sigma = 1.0 / np.sqrt(2.0 * k0 * m * omega)
psi = np.exp(-0.5 * ((x)**2) / sigma**2) * np.exp(1.0j * k0 * x)
psi /= np.linalg.norm(psi) * np.sqrt(dx)

# Compute the Hamiltonian H
H = np.zeros((N,N), dtype=np.complex128)
for i in range(N):
    H[i,i] = 0.5 * hbar**2 / m / dx**2 + V[i]
    if i > 0:
        H[i,i-1] = -0.5 * hbar**2 / m / dx**2
    if i < N - 1:
        H[i,i+1] = -0.5 * hbar**2 / m / dx**2

# Create a 2D array to store the wave function at each time step
psi_array = np.zeros((N, N))

# Loop over time steps and compute the wave function
for i in range(N):
    psi_array[:, i] = np.abs(psi)**2
    psi = expm(-1.0j * H * dt / hbar) @ psi
    psi /= np.linalg.norm(psi) * np.sqrt(dx)

# Plot the wave function as a 3D surface
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
X, Y = np.meshgrid(x, x)
ax.plot_surface(X, Y, psi_array,cmap='rainbow')
ax.set_xlabel('Position')
ax.set_ylabel('Time')
ax.set_zlabel('$|\psi(x,t)|^2$')
plt.show()
Quantum Harmonic Oscillator Simulation: Time-Dependent Schrödinger Equation with Matrix Exponential Method