Applications of MATLAB To Problems in Quantum Mechanics For Research and Education (1995) : Dirac Notation Interpreter
Applications of MATLAB To Problems in Quantum Mechanics For Research and Education (1995) : Dirac Notation Interpreter
Spring 2009
Version: 27.02.2009
Dirac’s formulation of Quantum Mechanics is based on abstract Hilbert space,
which for problems in matrix formulation leads to a set of simple replacements
rules.
A matrix H corresponds to an abstract operator Ĥ and a column vector a
corresponds to an abstract vector |ai. The eigenvalue equation
Ĥ | a i = Ea | a i (1)
Ladislav Kocbach
1 Introduction
This contribution describes applications of MATLAB to quantum mechani-
cal problems which are formulated in terms of matrices. A special feature
of general interest is a demonstration that one can quite easily write small
specialized command interpreters using MATLAB’s character-string functions
and MATLAB’s command eval. In this paper, I first shortly review the Dirac
notation as used in Quantum Mechanics. Then I describe the Dirac notation
interpreter in MATLAB and discuss mostly the educational aspect of the work.
In Technical notes I discuss shortly how this interpreter works and how other
interpreters can be constructed. In order to illustrate how this idea emerged
and also for the sake of general information, a more detailed presentation of
our projects is included in this paper.
It should be well known that a large class of quantum mechanical problems
leads to matrix formulation. Our work is related to theory of atomic collisions,
but similar problems are found in parts of nuclear physics, optical physics,
quantum chemistry etc. We have used MATLAB for example for analysis of
problems with non-orthogonal basis. In course of this work, an idea emerged
to write a simple interpreter enabling us to enter the statements in easier
understandable form. Further developement of these ideas resulted in the
above mentioned interpreter package which I hope will be useful for teaching
quantum mechanics.
Turning to applications of computers to teaching natural sciences, let us remark
that the excellent abilities of the modern integrated systems (including MAT-
LAB, and e.g. Mathematica and Maple) are often difficult to exploit efficiently
in teaching. This is caused by specialized command languages and special syn-
tax rules. It takes too much time to learn the language before a simple concept
can be demonstrated. This project shows that it is possible to develop inter-
preters in the command language, which interpret a syntax presumably known
to the students. Because of MATLAB’s excellent character-string processing
repertoir, the interpreter was developed relatively easily.
We also note that the interpreter idea can be extended or adapted to other
problems, for example to define vectors (arrays) with vectors as elements (in-
dexed vectors) or multidimensional arrays. This is also shortly described.
2 Dirac Notation
Dirac’s formulation of Quantum Mechanics is based on abstract Hilbert space,
which for problems in matrix formulation leads to a set of simple replacements
rules.
A matrix H corresponds to an abstract operator Ĥ and a column vector a
corresponds to an abstract vector |ai. The eigenvalue equation
Ĥ | a i = Ea | a i (4)
3 The Interpreter
The Dirac interpreter enables the user to enter the Dirac expressions and
have them evaluated by normal MATLAB matrix operations. We have chosen
to show the corresponding MATLAB expression before evaluation. Having
declared and defined |a > and |b > as two dimensional vectors, we can enter e.g.
linear combinations and even assignments (the constants are c1 = 0.8; c2 = 0.6;
)
Dirac : |a>
MATLAB: --> a
a =
1
0
Dirac : |b>
MATLAB: --> b
b =
0
1
Dirac : |c> = c1 |a> + c2|b>
MATLAB: --> c=c1*a+c2*b
c =
0.8000
0.6000
Dirac : |d> = c2 |a> - c1|b>
MATLAB: --> d=c2*a-c1*b
d =
0.6000
-0.8000
Dirac : <c|d>
MATLAB: --> (c)’*d
ans =
0
Dirac : <c|c>
MATLAB: --> (c)’*c
ans =
1
5 Technical Notes
How does the interpreter work? The main part is a MATLAB function called
parse.m. This function translates a string of characters representing a Dirac
command into a new string with standard MATLAB syntax. For example
>> parse(’ <x| M |y> ’)
ans =
(x)’*M*y
>> parse(’( <a| + <b| ) ( M + N ) |c> ’)
ans =
((a)’+(b)’)*(M+N)*c
Dirac : H:
H: help
M: matlab line (execute a matlab line)
D: Declare a vector vec : |vec>
or an operator op : [op]
or a constant c1 : (c1)
example:
Dirac : D: |a> |psi> [L] [Xz] (c1)
..declares Dirac vectors a,psi
operators L, Xz
constant c1
G: show the global objects
example:
Dirac : G:
DECLARED : |a> |b> |psi>
[L] [Xz] (c1) (c2)
$ exit Dirac
a new call Dirac preserves the
global names and their values
General examples:
e: lists the 2-components examples
E: lists the 3-components examples
Naturally, the detailed behaviour can somewhat change in the future versions,
the described state is end of september 1995. The simplest version of Dirac.m,
would look about like this
% simplest possible version of Dirac.m
stopval=0
while stopval==0
InputStr=input(’Dirac : ’,’s’);
if InputStr(1) ==’$’ stopval=1; end
OutStr=parse(InputStr,0);
fprintf(1,’MATLAB: --> %s\n’,OutStr);
eval(OutStr);
end % while
In fact, the present version of Dirac.m is about one hundred lines and it does
many other things, as e.g. passing some of the command strings directly to
MATLAB, keeping track of assigned variables etc., as the help text indicates.
Inner working of parse. I think that an appropriate name for it is an heuris-
tic interpreter. The rules for Dirac notation itself can be easily spelled out, but
in combination with different uses of parantheses, functions and operators the
set of translating rules gets large. Therefore, I keep the parse.m in its orig-
inal style, with a small set of replacement rules and various post-processing
patches. In its present version, the function has about 250 lines, including
some comments. Function parse.m is a character string function and it uses
the following character string manipulating standard MATLAB functions:
The simplest first test version, which could only do the scalar product <a|b>
used only the strrep() and returned the new string. The present version
can handle all the combinations of signs which ocurred to the author. In fact,
recently a problem with parantheses has been encountered and the repair of
the inconsistency contributed some ten new lines of code.
For teaching and demonstration purposes the standard MATLAB output is
not always suitable. At present, we are thus also working with formatting
routines, as an example this is a printout of a 3 × 3 matrix
(2. ; 0. ; 0. )
(0. ; 1. ; 1.+ 1.i )
(0. ; 1.- 1.i ; -1. )
Concluding this section, I would like to repeat that it is in principle quite easy
to write an interpreter using the string functions mentioned in the above listing.
The interpreter (or perhaps parser) can then be used via a function shown in
the listing Simple Dirac or directly. Unfortunately, only single line codes
can be performed in this way, since both input() and eval() understand
newline-character as a terminator of the string. On the other hand, there
seems to be no limitation on the length of the string which can be executed by
eval() . An interpreter can pack longer commands in the same way as the
following example shows
The unknown quantities to be found are the expansion coefficients, which form
a vector. In this formulation, the time-dependent Schrödinger equation
d
i | ψ(t) i = Ĥ(t) | ψ(t) i (8)
dt
is replaced by a set of coupled differential equations, which are conveniently
expressed by matrix notation
c1 H11 H12 ... H1n c1
T = SU (11)
which are far from orthogonal with respect to the scalar product defined as
Z ∞
2 2
h αi | αj i → e−αi r e−αj r r2 dr
0
The expressions for all the relevant matrix elements are well known analyti-
2
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
0 1 2 3 4 5 6
cally and matrices for overlap and the atomic total energy (hamiltonian) are
thus easily constructed. The alternative orthogonalization procedure (eq.12)
has been applied in terms of a small set of MATLAB functions. The figures
1 and 2 show a typical output for a randomly chosen set of αi . Motivation
1.05
0.95
0.9
0.85
0.8
0 1 2 3 4 5 6
for this work is to explore the procedures for choosing sets of orbitals and
understanding the procedures used in quantum chemistry. It will also be used
in the course of Atomic and Molecular Physics. The MATLAB functions are
available as mentioned in the Conclusion.
10 Collision Code Debugging
Our general interest is to investigate the two mentioned orthonormalization
procedures with respect to questions which will become clear at the end of
this section. In the theory of atomic collisions, especially those directed to-
wards the studies of electron transfer, the basis states for the electron are
located on both atomic centers. When the atomic centers are far apart, the
overlaps are small and the basis is approximately orthonormal, if each of the
two subsets on each center were orthonormal. As the two atoms approach
each other, the two subsets gradually overlap and for the smallest distances
the effective dimensionality of the basis might become smaller (approaching
the dimensionality of one subset). The small subspace representing the differ-
ence between the two subsets is ’blown up’ by the normalization and this can
lead to numerical problems. This can be studied by investigating the deter-
minant of the overlap matrix. For well separated atoms it approaches one (if
the two subsets are internally orthonormal for each center), but as the atoms
approach each other, the determinant approaches zero.
All the matrix elements involved in the realistic atomic collision calculation are
quite complicated mathematical objects and they were thus evaluated by the
collision FORTRAN program (ref. [4]). The overlap matrices were dumped
as one large rectangular matrix and entered into MATLAB. They were then
investigated using simple MATLAB functions.
In particular, plotting the determinant of the overlap matrix eq. (9).
D(R) = det(O(R))
12 Conclusion
This paper described the applications of MATLAB. It should, however also be
mentioned that other modern integrated systems are also used in this work.
These include REDUCE, Mathematica and Maple. For numerical intensive
tasks MATLAB is unique. For the educational applications, along the lines of
the Dirac notation interpreter, the mainly algebraic systems may be even more
suitable. However, MATLAB’s internal representation of vectors, matrices and
character strings shortened the way from the idea to the first realization to
several hours.
The files and documentation for the Dirac interpreter can be found in the
World Wide Web at the address
http://www.fi.uib.no/AMOS/matlab/
Also some of the other mentioned functions can be found there. If necessary,
the files can be transfered in any other way on request to the author.
Acknowledgements
I would like to thank J. P. Hansen, A. Dubois and S.E. Nielsen for the im-
pulses from the collaboration on atomic physics codes, K. Børve for clarifying
discussions on the methods of Quantum Chemistry, and my son Jan Kocbach
for first introduction to MATLAB.
References
[1] E. Merzbacher: Quantum Mechanics (Wiley International Edition)
cos(kx − ωt)
cos(kx)
2π
L
Waves - harmonic response (oscillations)
00
y + k2y = 0
∂ 2u 1 ∂ 2u
− =0
∂x2 c2 ∂t2
Fourier Series
Charles Fourier: Not waves, but Heat Conduction
Expansions on a complete set of functions
(drawing, general function on interval of length L)
∞
X
f (x) = an un (x)
n=0
x
cos(n 2π)
L
x
cos(n 2π)
L
(drawing, approximating a double-step function inside of an interval of length
L)
Comment: outside of the interval - ghosts - or periodic ’images’ - bonus or
punishment
Connection with linear algebra: Linear function spaces, Linear vector Spaces
Eucledian 3-dim space
Drawing, components
system of unit vectors: unit - length 1
X
r= an un
n
as compared to
∞
X
f (x) = an un (x)
n=0
expansions on bases. How to obtain the expansion coefficients? For 3-dim
Euclid we know
Projections: