0% found this document useful (0 votes)
2 views7 pages

unit 2 quantum input

The document contains Python code for simulating quantum mechanical systems, specifically the harmonic oscillator, particle in a box, and particle in a finite potential well. It includes calculations for eigenvalues and eigenfunctions, as well as visualizations of these functions and their probability densities. The code utilizes NumPy for numerical computations and Matplotlib for plotting the results.

Uploaded by

Chirag Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

unit 2 quantum input

The document contains Python code for simulating quantum mechanical systems, specifically the harmonic oscillator, particle in a box, and particle in a finite potential well. It includes calculations for eigenvalues and eigenfunctions, as well as visualizations of these functions and their probability densities. The code utilizes NumPy for numerical computations and Matplotlib for plotting the results.

Uploaded by

Chirag Bisht
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

# HARMONIC OSCILLATOR

import numpy as np

import matplotlib.pyplot as plt

# Constants

hbar = 1.055e-34 # Planck's constant (normalized)

m = 9.1e-31 # mass of particle

omega = 1.0 # angular frequency

X_max = 0.05 # range of x-axis ([-X_max, X_max])

N = 250 # number of grid points

dx = 2 * X_max / (N - 1) # grid spacing

# Discretize space

x = np.linspace(-X_max, X_max, N)

# Potential V(x) for harmonic oscillator

V = 0.5 * m * omega**2 * x**2

# Kinetic energy term (T) and potential energy term (V) in the Hamiltonian

T = -0.5 * hbar**2 / m / dx**2 * (np.diag(np.ones(N-1), -1) - 2 * np.diag(np.ones(N)) +


np.diag(np.ones(N-1), 1))

H = T + np.diag(V) # Hamiltonian matrix

# Solve the eigenvalue problem

eigenvalues, eigenfunctions = np.linalg.eigh(H)

# Normalize the eigenfunctions

eigenfunctions = eigenfunctions / np.sqrt(np.trapz(np.abs(eigenfunctions)**2, x)) # Normalize each


eigenfunction
# Plot all eigenfunctions in one graph

plt.figure(figsize=(12, 6))

for i in range(3): # First three eigenstates

plt.plot(x, eigenfunctions[:, i], label=f'E_{i} = {eigenvalues[i]:.3f}')

plt.title('Eigenfunctions of the Harmonic Oscillator')

plt.xlabel('x')

plt.ylabel('ψ(x)')

plt.legend()

plt.grid(True)

# Plot all probability densities in one graph

plt.figure(figsize=(12, 6))

a=0

for i in range(3): # First three eigenstates

plt.plot(x, np.abs(eigenfunctions[:, i])**2+a, label=f'E_{i} = {eigenvalues[i]:.3f}')

a=a+70

plt.title('Probability Densities |ψ(x)|² of the Harmonic Oscillator')

plt.xlabel('x')

plt.ylabel('|ψ(x)|²')

plt.legend()

plt.grid(True)

plt.tight_layout()

plt.show()

# Print the first few eigenvalues


print("First few eigenvalues:")

for i in range(3):

print(f"E_{i} = {eigenvalues[i]:.3f}")

# Particle in a box
import numpy as np

import matplotlib.pyplot as plt

# Constants

hbar = 1.055e-34 # Planck's constant (normalized)

m = 9.1e-31 # mass of particle

L = 0.75 # length of the box

N = 250 # number of grid points

dx = L / (N - 1) # grid spacing

# Discretize space

x = np.linspace(0, L, N)

# Kinetic energy term (T) and potential energy term (V) in the Hamiltonian

T = -0.5 * hbar**2 / m / dx**2 * (np.diag(np.ones(N-1), -1) - 2 * np.diag(np.ones(N)) +


np.diag(np.ones(N-1), 1))

V = np.zeros(N) # Potential V(x) is zero inside the box

H = T + np.diag(V) # Hamiltonian matrix

# Solve the eigenvalue problem

eigenvalues, eigenfunctions = np.linalg.eigh(H)

# Normalize the eigenfunctions (ensure they satisfy the boundary condition at x = 0 and x = L)
eigenfunctions = eigenfunctions / np.sqrt(np.trapz(np.abs(eigenfunctions)**2, x)) # Normalize each
eigenfunction

# Plot all eigenfunctions in one graph

plt.figure(figsize=(12, 6))

for i in range(3): # First three eigenstates

plt.plot(x, eigenfunctions[:, i], label=f'E_{i+1} = {eigenvalues[i]:.3f}')

plt.title('Eigenfunctions of the Particle in a Box')

plt.xlabel('x')

plt.ylabel('ψ(x)')

plt.legend()

plt.grid(True)

# Plot all probability densities in one graph

plt.figure(figsize=(12, 6))

a=0

for i in range(3): # First three eigenstates

plt.plot(x, np.abs(eigenfunctions[:, i])**2+a, label=f'E_{i+1} = {eigenvalues[i]:.3f}')

a=a+3

plt.title('Probability Densities |ψ(x)|² of the Particle in a Box')

plt.xlabel('x')

plt.ylabel('|ψ(x)|²')

plt.legend()

plt.grid(True)

plt.tight_layout()

plt.show()
# Print the first few eigenvalues

print("First few eigenvalues:")

for i in range(3):

print(f"E_{i+1} = {eigenvalues[i]:.3f}")

# particle in finite potential well


import numpy as np

import matplotlib.pyplot as plt

# Constants

hbar = 1 # Planck's constant (normalized)

m=1 # mass of particle

L = 10.0 # width of the well (arbitrary units)

V0 = 3.0 # depth of the well (arbitrary units)

X_max = 20.0 # range of x-axis ([-X_max, X_max])

N = 500 # number of grid points

dx = 2 * X_max / (N - 1) # grid spacing

# Discretize space

x = np.linspace(-X_max, X_max, N)

# Define the potential V(x) for the finite square well

V = np.zeros(N)

V[np.abs(x) <= L/2] = -V0 # Well is inside [-L/2, L/2]

# Kinetic energy term (T) and potential energy term (V) in the Hamiltonian
T = -0.5 * hbar**2 / m / dx**2 * (np.diag(np.ones(N-1), -1) - 2 * np.diag(np.ones(N)) +
np.diag(np.ones(N-1), 1))

H = T + np.diag(V) # Hamiltonian matrix

# Solve the eigenvalue problem

eigenvalues, eigenfunctions = np.linalg.eigh(H)

# Normalize the eigenfunctions

eigenfunctions = eigenfunctions / np.sqrt(np.trapz(np.abs(eigenfunctions)**2, x)) # Normalize each


eigenfunction

# Plot all eigenfunctions in one graph

plt.figure(figsize=(12, 6))

for i in range(4): # First four eigenstates

plt.plot(x, eigenfunctions[:, i], label=f'E_{i} = {eigenvalues[i]:.3f}')

plt.plot(x,V, label='Potential V(x)', color='black', linestyle='dashed')

plt.title('Eigenfunctions of the Finite Square Well')

plt.xlabel('x')

plt.ylabel('ψ(x)')

plt.legend()

plt.grid(True)

# Plot all probability densities in one graph

plt.figure(figsize=(12, 6))

a=0

for i in range(4): # First four eigenstates

plt.plot(x, np.abs(eigenfunctions[:, i])**2 + a, label=f'E_{i} = {eigenvalues[i]:.3f}')

a = a + 0.55 # Offset for visibility


# Plot the potential well for reference

plt.plot(x, V, label='Potential V(x)', color='black', linestyle='dashed')

plt.title('Probability Densities |ψ(x)|² of the Finite Square Well')

plt.xlabel('x')

plt.ylabel('|ψ(x)|²')

plt.legend()

plt.grid(True)

plt.tight_layout()

plt.show()

# Print the first few eigenvalues

print("First few eigenvalues:")

for i in range(4):

print(f"E_{i} = {eigenvalues[i]:.3f}")

You might also like