01 Quantum Inspired Matlab
01 Quantum Inspired Matlab
d
|i = H|i
dt
(1)
(2)
when studying 1D quantum mechanics is a student treating (x) and |i interchangeably, ignoring the fact that
the first is a scalar but the ket corresponds to a column
vector. For example, they may write incorrectly
hp||i|xi = |ihp|xi
(incorrect!)
(4)
or some similar abberation. To avoid these types of misconceptions, a number of educators and textbook authors
have stressed incorporating a numerical calculation aspect to quantum courses.4,5,6,7,8,9 The motive is simple.
Anyone who has done numerical calculations cant help
but regard a ket |i as a column vector, a bra h| as a
row vector and an operator H as a matrix because that is
how they concretely represented in the computer. Introducing a computational aspect to the course provides one
further benefit: it gives the beginning quantum student
the sense that he or she is being empowered to solve real
problems that may not have simple, analytic solutions.
With these motivations in mind, we have developed
MATLAB codes10 for solving typical 1 D problems found
in the first part of a junior level quantum course based
on Griffiths book.11 We chose MATLAB for our programming environment because the MATLAB syntax is
especially simple for the typical matrix operations used
in 1D quantum mechanics problems and because of the
ease of plotting functions. While some MATLAB numerical recipes have previously been published by others,12,13
the exercises we share here are special because they emphasize simplicity and quantum pedagogy, not numerical
efficiency. Our point has been to provide exercises which
show students how to numerically solve 1 D problems in
such a way that emphasizes the column vector aspect of
kets, the row vector aspect of bras and the matrix aspect
of operators. Exercises using more efficient MATLAB
ODE solvers or finite-element techniques are omitted because they do not serve this immediate purpose. In part
II of this article, we hope to share MATLAB codes which
can be used in conjunction with teaching topics pertaining to angular momentum and non-commuting observables.
2
I.
FUNCTIONS AS VECTORS
to n = 1, etc...).
An exercise we suggest is for students to verify that
the derivative matrix is not Hermitian while the derivative matrix times the imaginary number i is. This can
be very valuable for promoting student understanding if
done in conjunction with the proof usually given for the
differential operator.
%****************************************************************
% Program 2: Calculate first and second derivative numerically
% showing how to write differential operator as a matrix
%****************************************************************
% Parameters for solving problem in the interval 0 < x < L
L = 2*pi;
% Interval Length
N = 100;
% No. of coordinate points
x = linspace(0,L,N);
% Coordinate vector
dx = x(2) - x(1);
% Coordinate step
% Two-point finite-difference representation of Derivative
D=(diag(ones((N-1),1),1)-diag(ones((N-1),1),-1))/(2*dx);
% Next modify D so that it is consistent with f(0) = f(L) = 0
D(1,1) = 0; D(1,2) = 0; D(2,1) = 0; % So that f(0) = 0
D(N,N-1) = 0; D(N-1,N) = 0; D(N,N) = 0; % So that f(L) = 0
% Three-point finite-difference representation of Laplacian
Lap = (-2*diag(ones(N,1),0) + diag(ones((N-1),1),1) ...
+ diag(ones((N-1),1),-1))/(dx^2);
% Next modify Lap so that it is consistent with f(0) = f(L) = 0
Lap(1,1) = 0; Lap(1,2) = 0; Lap(2,1) = 0;
% So that f(0) = 0
Lap(N,N-1) = 0; Lap(N-1,N) = 0; Lap(N,N) = 0;% So that f(L) = 0
% To verify that D*f corresponds to taking the derivative of f
% and Lap*f corresponds to taking a second derviative of f,
% define f = sin(x) or choose your own f
f = sin(x);
% And try the following:
Df = D*f; Lapf = Lap*f;
plot(x,f,b,x,Df,r, x,Lapf,g);
axis([0 5 -1.1 1.1]);
% Optimized axis parameters
% To display the matrix D on screen, simply type D and return ...
D % Displays the matrix D in the workspace
Lap % Displays the matrix Lap
% Plot a vector/function
plot(x,y);
% Plots vector y vs. x
%plot(x,real(y),r, x, imag(y), b); % Plots real&imag y vs. x
axis([-2 2 0 7]);
% Optimized axis parameters for sinx^2/pix^2
%axis([-2 2 -8 40]); % Optimized axis parameters for sinx/pix
% Numerical Integration
sum(y)*dx
% Simple numerical integral of y
trapz(y)*dx
% Integration using trapezoidal rule
III.
When solving Eq. (1), the method of separation of variables entails that as an intermediate step we look for the
separable solutions
II.
DIFFERENTIAL OPERATORS AS
MATRICES
|E (t)i = |E (0)iexp(iEt/~)
(5)
(6)
3
the eigenfunctions and eigenvalues for the infinite square
well Hamiltonian. For brevity, we omit the commands
setting the parameters L, N, x, and dx.
%****************************************************************
% Program 3: Matrix representation of differential operators,
% Solving for Eigenvectors & Eigenvalues of Infinite Square Well
%****************************************************************
% For brevity we omit the commands setting the parameters L, N,
% x and dx; We also omit the commands defining the matrix Lap.
% These would be the same as in Program 2 above.
% Total Hamiltonian where hbar=1 and m=1
hbar = 1; m = 1; H = -(1/2)*(hbar^2/m)*Lap;
% Solve for eigenvector matrix V and eigenvalue matrix E of H
[V,E] = eig(H);
% Plot lowest 3 eigenfunctions
plot(x,V(:,3),r,x,V(:,4),b,x,V(:,5),k); shg;
E % display eigenvalue matrix
diag(E) % display a vector containing the eigenvalues
ARBITRARY POTENTIALS
Numerical solution of Eq. (1) is not limited to any particular potential. Program 4 gives example MATLAB
codes solving the time independent Schrodinger equation for finite square well potentials, the harmonic oscillator potential and even for potentials that can only
solved numerically such as the quartic potential U = x4 .
In order to minimize the amount of RAM required, the
codes shown make use of sparse matrices, where only
the non-zero elements of the matrices are stored. The
commands for sparse matrices are very similar to those
for non-sparse matrices. For example, the command
[V, E] = eigs(H, nmodes..) provides the nmodes lowest
energy eigenvectors V of of the sparse matrix H.
Fig. 1 shows the plot obtained from Program 4 for the
potential U = 12 100 x2. Note that the 3 lowest energies
displayed in the figure are just as expected due to the
analytic formula
1
(7)
E = ~ n +
2
q
k
with n = integer and = m
= 10 rad/s.
V.
%****************************************************************
% Program 4: Find several lowest eigenmodes V(x) and
% eigenenergies E of 1D Schrodinger equation
%
-1/2*hbar^2/m(d2/dx2)V(x) + U(x)V(x) = EV(x)
% for arbitrary potentials U(x)
%****************************************************************
% Parameters for solving problem in the interval -L < x < L
% PARAMETERS:
L = 5;
% Interval Length
N = 1000;
% No of points
x = linspace(-L,L,N);
% Coordinate vector
dx = x(2) - x(1);
% Coordinate step
% POTENTIAL, choose one or make your own
U = 1/2*100*x.^(2);
% quadratic harmonic oscillator potential
%U = 1/2*x.^(4);
% quartic potential
% Finite square well of width 2w and depth given
%w = L/50;
%U = -500*(heaviside(x+w)-heaviside(x-w));
% Two finite square wells of width 2w and distance 2a apart
%w = L/50; a=3*w;
%U = -200*(heaviside(x+w-a) - heaviside(x-w-a) ...
%
+ heaviside(x+w+a) - heaviside(x-w+a));
%
%
%
e
% Total Hamiltonian
hbar = 1; m = 1;
% constants for Hamiltonian
H = -1/2*(hbar^2/m)*Lap + spdiags(U,0,N,N);
% Find lowest nmodes eigenvectors and eigenvalues of sparse matrix
nmodes = 3; options.disp = 0;
[V,E] = eigs(H,nmodes,sa,options);
% find eigs
[E,ind] = sort(diag(E));% convert E to vector and sort low to high
V = V(:,ind);
% rearrange corresponding eigenvectors
% Generate plot of lowest energy eigenvectors V(x) and U(x)
Usc = U*max(abs(V(:)))/max(abs(U));
% rescale U for plotting
plot(x,V,x,Usc,--k);
% plot V(x) and rescaled U(x)
% Add legend showing Energy of plotted V(x)
lgnd_str = [repmat(E = ,nmodes,1),num2str(E)];
legend(lgnd_str)
% place lengend string on plot
shg
order of unity. In the programs presented here, our focus being undergraduate physics students, we wanted to
avoid unnecessarily complicating matters. To make the
equations more familiar to the students, we explicitly left
constants such as ~ in the formulas and chose units such
that ~ = 1 and m = 1. We recognize that others may
have other opinions on how to address this issue. An alternative approach used in research is to recast the equations in terms of dimensionless variables, for example by
rescaling the energy to make it dimensionless by expressing it in terms of some characteristic energy in the problem. In a more advanced course for graduate students or
in a course in numerical methods, such is an approach
which would be preferable.
VI.
4
is time-dependent, oscillating between the (x) that corresponds to the particle being entirely in the right well
0.25
E = 4.99969
E = 14.9984
E = 24.9959
unnormalized wavefunction
0.2
(11)
0.15
and (x) for the particle being entirely in the left well
0.1
2
0.05
(12)
0
0.05
0.1
1.5
0.5
0
0.5
x [m]
%****************************************************************
% Program 5: Calculate Probability Density as a function of time
% for a particle trapped in a double-well potential
%****************************************************************
1.5
FIG. 1: Output of Program 4, which plots the energy eigenfunctions V (x) and a scaled version of the potential U (x) =
1/2 100 x2 . The corresponding energies displayed within
the figure legend, 4.99969, 14.9984 and 24.9959, are, within
rounding error, precisely those expected from Eq. (7) for the
three lowest-energy modes.
X
E
aE |E (0)iexp(iEt/~)
(8)
(9)
%
%
w
U
%
%
e
H
(10)
5
6
6
T = 0s
T = 2.8189s
5
probability density [1/m]
5
4
3
2
1
0
4
3
2
1
0
1
0.6 0.4 0.2
1
0.6 0.4 0.2
6
T = 5.6378s
T = 8.4566s
5
probability density [1/m]
5
probability density [1/m]
4
3
2
1
0
4
3
2
1
0
1
0.6 0.4 0.2
1
0.6 0.4 0.2
6
T = 11.2755s
5
4
3
2
1
0
1
0.6 0.4 0.2
VII.
(13)
E + E Uo
(14)
end
% Calculate Reflection probability
R=0;
for a=1:N/2;
R=R+rho(a);
end
R=R*dx
0.05
T = 14.4271s
0.1
0.05
0
0 10 20 30 40 50 60 70 80 90 100
x [m]
T = 0s
0.1
0
0 10 20 30 40 50 60 70 80 90 100
x [m]
%****************************************************************
% Program 6: Wavepacket propagation using exponential of H
%****************************************************************
% Parameters for solving the problem in the interval 0 < x < L
L = 100;
% Interval Length
N = 400;
% No of points
x = linspace(0,L,N);
% Coordinate vector
dx = x(2) - x(1);
% Coordinate step
T = 28.8543s
0.1
0.05
0
0 10 20 30 40 50 60 70 80 90 100
x [m]
FIG. 3: Output of Program 6 showing a wavepacket encountering step potential of height hEi located at x/L = 0.5 at
different times.
VIII.
CONCLUSIONS
7
8
!"#
10
11
12
13
14