1+ """
2+ The Hamiltonian is a central concept in both classical and quantum mechanics.
3+ It represents the total energy of a system and is used to describe how that
4+ system evolves over time.
5+
6+ Classical mechanics:
7+ H = T + V
8+ where T is kinetic energy and V is potential energy.
9+
10+ Quantum mechanics (1D, finite-difference form):
11+ The time-independent Schrodinger equation, H|psi> = E|psi>, can be solved
12+ numerically by discretizing space into points and approximating the second
13+ derivative with the finite-difference method. This turns the continuous
14+ Hamiltonian operator into a matrix:
15+
16+ H[i][i] = hbar^2 / (m * dx^2) + V(x_i)
17+ H[i][i+1] = H[i][i-1] = -hbar^2 / (2 * m * dx^2)
18+
19+ References:
20+ - https://en.wikipedia.org/wiki/Hamiltonian_mechanics
21+ - https://en.wikipedia.org/wiki/Hamiltonian_(quantum_mechanics)
22+ - https://en.wikipedia.org/wiki/Finite_difference_method
23+ """
24+
25+
26+ def classical_hamiltonian (mass : float , velocity : float , potential_energy : float ) -> float :
27+ """
28+ Compute the classical Hamiltonian H = T + V for a particle,
29+ where T = 0.5 * m * v^2 is the kinetic energy.
30+
31+ >>> classical_hamiltonian(2, 3, 5)
32+ 14.0
33+ >>> classical_hamiltonian(1, 0, 10)
34+ 10.0
35+ >>> classical_hamiltonian(2, -4, 0)
36+ 16.0
37+ >>> classical_hamiltonian(-1, 2, 5)
38+ Traceback (most recent call last):
39+ ...
40+ ValueError: mass must be positive
41+ """
42+ if mass <= 0 :
43+ raise ValueError ("mass must be positive" )
44+
45+ kinetic_energy = 0.5 * mass * velocity ** 2
46+ return kinetic_energy + potential_energy
47+
48+
49+ def quantum_hamiltonian (
50+ num_points : int ,
51+ potential : list [float ],
52+ mass : float = 1.0 ,
53+ hbar : float = 1.0 ,
54+ dx : float = 1.0 ,
55+ ) -> list [list [float ]]:
56+ """
57+ Construct the Hamiltonian matrix for a particle in a 1D potential
58+ using finite-difference discretization of the time-independent
59+ Schrodinger equation.
60+
61+ >>> quantum_hamiltonian(3, [0.0, 0.0, 0.0])
62+ [[1.0, -0.5, 0.0], [-0.5, 1.0, -0.5], [0.0, -0.5, 1.0]]
63+ >>> quantum_hamiltonian(2, [1.0, 2.0], mass=2.0, hbar=1.0, dx=1.0)
64+ [[1.5, -0.25], [-0.25, 2.5]]
65+ >>> quantum_hamiltonian(3, [0.0, 0.0])
66+ Traceback (most recent call last):
67+ ...
68+ ValueError: potential must have length num_points
69+ >>> quantum_hamiltonian(0, [])
70+ Traceback (most recent call last):
71+ ...
72+ ValueError: num_points must be positive
73+ """
74+ if num_points <= 0 :
75+ raise ValueError ("num_points must be positive" )
76+ if len (potential ) != num_points :
77+ raise ValueError ("potential must have length num_points" )
78+
79+ diag_term = hbar ** 2 / (mass * dx ** 2 )
80+ off_diag_term = - (hbar ** 2 ) / (2 * mass * dx ** 2 )
81+
82+ hamiltonian = [[0.0 ] * num_points for _ in range (num_points )]
83+ for i in range (num_points ):
84+ hamiltonian [i ][i ] = diag_term + potential [i ]
85+ if i > 0 :
86+ hamiltonian [i ][i - 1 ] = off_diag_term
87+ if i < num_points - 1 :
88+ hamiltonian [i ][i + 1 ] = off_diag_term
89+
90+ return hamiltonian
91+
92+
93+ if __name__ == "__main__" :
94+ from doctest import testmod
95+
96+ testmod ()
97+
0 commit comments