Lecture4 Quantum Python
Transcript of Lecture4 Quantum Python
-
7/21/2019 Lecture4 Quantum Python
1/23
Quantum Mechanics with PythonNumerical Methods for Physicists, Lecture 4
Mat Medo, Yi-Cheng Zhang
Physics Department, Fribourg University, Switzerland
March 11, 2013
Medo & Zhang (UNIFR) Quantum Python 1 / 19
http://find/http://goback/ -
7/21/2019 Lecture4 Quantum Python
2/23
Schrdinger equation (here in 1D)
i(x, t)t
= 22m
2(x, t)x2
+V(x)(x, t)
PDE with complex numbers
scipy.integrate.complex_ode re-maps a given
complex-valued differential equation to a set of real-valued ones
and uses scipy.integrate.odeto integrate them
But we have partial derivatives. . .
Medo & Zhang (UNIFR) Quantum Python 2 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
3/23
Schrdinger equation (here in 1D)
i(x, t)t
= 22m
2(x, t)x2
+V(x)(x, t)
PDE with complex numbers
scipy.integrate.complex_ode re-maps a given
complex-valued differential equation to a set of real-valued ones
and uses scipy.integrate.odeto integrate them
But we have partial derivatives. . .
1 Do re-mapping to real values and use Finite-Difference
Time-Domain (FDTD) as we did for the heat equation
2 Use the split-step Fourier method
3 QuTiP: a quantum toolbox in Python
4 Re-mapping & Escript (fast finite elements for PDEs)
Medo & Zhang (UNIFR) Quantum Python 2 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
4/23
FDTD: re-mapping
Re-mapping to real-valued variables:
(x, t) =(x, t) +i(x, t)
Schrdinger equation decouples:
(x, t)
t =
2
2m
2(x, t)
x2 +V(x)(x, t)
(x, t)
t
=
2
2m
2(x, t)
x2
V(x)(x, t)
Discretization in time and space: xl=lx,tn=nt
(xl, tn) :=n(l), (xt, tn) :=
n(l)
Medo & Zhang (UNIFR) Quantum Python 3 / 19
http://find/http://goback/ -
7/21/2019 Lecture4 Quantum Python
5/23
FDTD: discretization
Real and complex part ofshifted by half-interval from eachotherthis allows us to use the second-order approximation for
time derivatives which is more precise (O(t)vsO(t2))
(xl, tn+1/2)t
(xl, tn+1) (xl, tn)t
= n+1
(l) n
(l)t
(xl, tn)
t (xl, tn+1/2) (xl, tn1/2)
t =
n+1/2(l) n1/2(l)t
Spatial derivatives exactly as before
2n(l)
x2
n(l+1) 2n(l) +n(l 1)x2
Medo & Zhang (UNIFR) Quantum Python 4 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
6/23
FDTD: equations
Compute the future state from the current state
{n, n1/2} {n+1, n+1/2}
Update equations are
n+1/2(l) =n1/2(l) c2V(l)n(l) ++c1
n(l 1) 2n(l) +n(l+1)
n+1
(l) =
n
(l) +c2V(l)
n+1/2
(l) c1
n+1/2(l 1) 2n+1/2(l) +n+1/2(l+1)
Wherec1 := t/(2mx2)andc2:= t/
Medo & Zhang (UNIFR) Quantum Python 5 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
7/23
FDTD: practical issues 1
It is easier to work in units where =m=1
The scheme is stable only ift
2
mx2+
maxx,tV(x,t)2
1
See http://www.scipy.org/Cookbook/SchrodingerFDTD
(there is the rest of theory as well)
This is only orientational (e.g.,V(x) somewhere does notnecessarily meant 0)Finer grid = smaller time steps neededTo reach the same physical time, simulation time grows as 1/x3
Check whether we are really in the stable (convergent) regime:halvetand see whether the results change substantially (theyshould not)
We are typically interested in the probability density, not inand themselves
Medo & Zhang (UNIFR) Quantum Python 6 / 19
http://www.scipy.org/Cookbook/SchrodingerFDTDhttp://www.scipy.org/Cookbook/SchrodingerFDTDhttp://find/ -
7/21/2019 Lecture4 Quantum Python
8/23
FDTD: practical issues 2
Its nice to have a reversible algorithm (as in the leap-frog kind ofintegration schemes)
Evolve your system to the future and back and get the initial state
Evolved wave-function needs to stay normalized
Force normalization by hand after each step (possible butnon-systematic)
Find a unitary algorithm which preserves the norm
See Numerical recipes in C (Section 19.2) by Press et al
We approximated the time evolution operatorU(t) =exp[iHt]as
U(t)
1
iHtwhereas it is better to use
U(t) =exp[iHt] 1 1
2iHt
1+ 12
iHt
Then naturallyU(t)U(t) =1
Medo & Zhang (UNIFR) Quantum Python 7 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
9/23
Detour: animations with Python
Sometimes better than static figures
Especially valuable if you want to impress. . .
First approach:
Write a script that saves many figures, say frame-001.gif,
frame-002.gif, etc.
Useavconv(formerly ffmpeg) to create an animationavconv -an -r 4 -i frame-%03d.png animation.avi
Or use gifsicleto create an animated gifgifsicle -delay=100 -loop -no-transparent -O2
list_of_files > output.gif
Second approach: animate with Matplotlib (v1.1 and higher)
http://jakevdp.github.com/blog/2012/08/18/
matplotlib-animation-tutorial/
Medo & Zhang (UNIFR) Quantum Python 8 / 19
http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/http://jakevdp.github.com/blog/2012/08/18/matplotlib-animation-tutorial/http://goforward/http://find/http://goback/ -
7/21/2019 Lecture4 Quantum Python
10/23
Split-step Fourier method: the idea
The potential part of the Schrdinger equation is simple to solve
i
t =V(x) = (x, t+ t) pot= (x, t)eiV(x)t/
Inverse Fourier transform of
(x, t) = 1
2
(k, t)eikx dk
By substituting this into the Schrdinger equation
i
t =
2k2
2m +iV
k = (k, t+ t) kin= (k, t)eik2t/2m
Fast Fourier transform (FFT) allows us to switch between and
Medo & Zhang (UNIFR) Quantum Python 9 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
11/23
Split-step Fourier method: the algorithm
1 Discretize thexandk-space by choosinga, b, N, k0:
x= (b a)/N, k=2/(b a), k0 = /x,xn=a+nx, km=k0+mk
This should be sufficient to represent states of (initial and later)
The choice can be tricky, experiment a bit to see if it works fine
2 Discretize the wave-functions:
n(t) := (xn, t), Vn:=V(xn), m(t) = (km, t)
3 Evolve the system by one time step
4 Repeat 3 until the end time is reached
Medo & Zhang (UNIFR) Quantum Python 10 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
12/23
Split-step Fourier method: point 3
1 A half step inx: n nexp[iVnt/2]2 FFT: computes mfromn
3 A full step ink: m mexp[ik2 t/2m]
4 Inverse FFT: computesnfrom m
5 A second half step inx: n nexp[iVnt/2]
Splitting thex-step in two parts produces a numerically more
stable algorithm (as in leapfrog integration in mechanics which isreversible and conserves energy)
See http://jakevdp.github.com/blog/2012/09/05/
quantum-python/for more details
Medo & Zhang (UNIFR) Quantum Python 11 / 19
1 . 01 . 01 . 01 . 0
http://jakevdp.github.com/blog/2012/09/05/quantum-python/http://jakevdp.github.com/blog/2012/09/05/quantum-python/http://jakevdp.github.com/blog/2012/09/05/quantum-python/http://jakevdp.github.com/blog/2012/09/05/quantum-python/http://find/http://goback/ -
7/21/2019 Lecture4 Quantum Python
13/23
Split-step Fourier method: tunelling
Output of ex4-1.py(see also the animation at the url above):1 5 0 1 0 0 5 0 0 5 0 1 0 0 1 5 0
0 . 0
0 . 2
0 . 4
0 . 6
0 . 8
t = 4 n o r m = 1 . 0 0 0
1 5 0 1 0 0 5 0 0 5 0 1 0 0 1 5 0
0 . 0
0 . 2
0 . 4
0 . 6
0 . 8
t = 6 n o r m = 1 . 0 0 0
1 5 0 1 0 0 5 0 0 5 0 1 0 0 1 5 0
0 . 0
0 . 2
0 . 4
0 . 6
0 . 8
t = 8 n o r m = 1 . 0 0 0
1 5 0 1 0 0 5 0 0 5 0 1 0 0 1 5 0
0 . 0
0 . 2
0 . 4
0 . 6
0 . 8
t = 1 0 n o r m = 1 . 0 0 0
Medo & Zhang (UNIFR) Quantum Python 12 / 19
http://find/http://goback/ -
7/21/2019 Lecture4 Quantum Python
14/23
Fast Fourier transform
Discrete Fourier transform is rather slow to compute: O(N2)whereNis the number of points where the transform is computed
The beauty of FFT:O(N2)is reduced toO(Nlog N)(similar asquicksort improving over bubble sort and alike)
The idea behind: discrete Fourier transform with Npoints can bewritten as a sum of two transforms with N/2 points
Using this recursively, we move to N/4 points, etc.
Thats why its best to use FFT for Nwhich is a power of two
fftandifftfrom scipy.fftpackimplement itFFT works best when is a power of two (because of how it isconstructed)
N=2x is possible but usually leads to slower computation
Medo & Zhang (UNIFR) Quantum Python 13 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
15/23
QuTiP & Escript
QuTiP:QM is not as difficult as one might think: Its only linear algebra!
States, operators, time evolution, gates, visualization
http://qutip.blogspot.ch andhttp://code.google.com/p/qutip
Escript:
For non-linear, time-dependent partial differential equations
https://launchpad.net/escript-finley
Medo & Zhang (UNIFR) Quantum Python 14 / 19
http://qutip.blogspot.ch/http://code.google.com/p/qutiphttps://launchpad.net/escript-finleyhttps://launchpad.net/escript-finleyhttp://code.google.com/p/qutiphttp://qutip.blogspot.ch/http://find/ -
7/21/2019 Lecture4 Quantum Python
16/23
What about the stationary states?
Until now we addressed only time evolution in QM
Stationary statensatisfies
Hn=Enn
2
2m2 +V(r)n=Enn
Boundary conditions in a box: (a) = (b) =0
We fix(a) =0 and findEso that(b)is zero too
In open space: limx (x) =limx (x) =0
For a symmetric potential, we simplify by realizing that eigenstatesare also symmetric
Even eigenstates: (0) =0; odd eigenstates: (0) =0
We needEfor which(x)does go to zero asxgrows
Medo & Zhang (UNIFR) Quantum Python 15 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
17/23
Stationary states: integration
Again(xn) := n,V(xn) =Vn
In addition, = m=1 andh= x
The approximation forf(x)plus the Schrdinger equation
1
2
n+Vnn=En =
n=2(Vn E)nn=
n1 2n+ n+1h2
= n+1=2nn1+2(Vn E)h2
Once0 and1 are given, the rest can be computed
Medo & Zhang (UNIFR) Quantum Python 16 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
18/23
Stationary states: integration
Again(xn) := n,V(xn) =Vn
In addition, = m=1 andh= x
The approximation forf(x)plus the Schrdinger equation
1
2
n+Vnn=En =
n=2(Vn E)nn=
n1 2n+ n+1h2
= n+1=2nn1+2(Vn E)h2
Once0 and1 are given, the rest can be computed
Much better (order of 4 instead of 2 above) is Numerovs method d2
dx2+f(x)
y(x) =0 = yn+1 =
(2 5h26 fn)yn (1 + h2
12 fn1)yn1
1 + h2
12 fn+1
Medo & Zhang (UNIFR) Quantum Python 16 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
19/23
Stationary states: continuation
How to find the right E: the shooting method
Medo & Zhang (UNIFR) Quantum Python 17 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
20/23
Stationary states: continuation
How to find the right E: the shooting method
It is best to write a function, say psi_end(E), which takesEasan argument and returns(x)at some (not too) distant pointx
FindingEefficiently: e.g., brentq(f,a,b)from
scipy.optimize finds a root of f betweenaandb
Medo & Zhang (UNIFR) Quantum Python 17 / 19
4 0
http://find/ -
7/21/2019 Lecture4 Quantum Python
21/23
Stationary states: LHO
We first plot(xm)for a range of energies
01
2 34
E
4 0
2 0
0
2 0
(
4
)
e v e n
o d d
Medo & Zhang (UNIFR) Quantum Python 18 / 19
4 0
http://find/ -
7/21/2019 Lecture4 Quantum Python
22/23
Stationary states: LHO
We first plot(xm)for a range of energies
01
2 34
E
4 0
2 0
0
2 0
(
4
)
e v e n
o d d
Then individual roots can be found
ex4-1.py gives 0.501 and 1.500 for the first two eigenstates
Medo & Zhang (UNIFR) Quantum Python 18 / 19
http://find/ -
7/21/2019 Lecture4 Quantum Python
23/23
Try this at home
1: Reversibility and unitarity
Implement the Finite-Difference Time-Domain algorithm. Is it reversible? Is it
unitary?
2: Ground state
Find the ground state energy of potential V(x) = exp(|x|)in the unitswhere = m=1. Can you find also the first excited state?
Medo & Zhang (UNIFR) Quantum Python 19 / 19
http://find/