You are on page 1of 10

AC274: Quantum codelets

Sauro Succi
October 6, 2016

1 Quantum codelets
We present three sample codelets
1. harmo.f: Harmonic oscillator Schroedinger, using Modified Visscher Method
2. kg.f: Free particle Klein-Gordon, using standard Leapfrog
3. qlb.f: Free particle Dirac, using Quantum Lattice Boltzmann

1
2 Schroedinger for the Harmonic Oscillator
It can be used for other potentials as well, including the free-particle case
(V (x) = 0).
c solve the 1d Schroedinger equation for the harmonic oscillator
c using Modified Visscher method
c S. Succi, AC274 Fall Semester 2016,
c --------------------------------------------------------
parameter (nx=501)
dimension Aold(nx),A(nx),Anew(nx)
dimension Bold(nx),B(nx),Bnew(nx)
dimension x(nx),vpot(nx)
dimension hc(nx),hw(nx),he(nx)
dimension jw(nx),je(nx)
c ------------------------------------
open(21,file=’rhoAB.out’)
open(31,file=’movie.out’)
open(9,file=’diagno.out’)
c ------------- -------------------------
pi = 4.0*atan(1.0)
dif = 1.0 ! hbar/m in natural units hbar=m=1
omh = 1.0 ! harmonic frequency
vamp = 0.0 ! set zero to run free particle
vel = 10. ! initial speed of the wavepacket
sigma = 1./2. ! initial spread of the coherent wavepacket
c the harmonic length hl = sqrt(dif/omh)
hlen = sqrt(dif/omh)
htim = 2.0*pi/omh

sizex = 5.0*hlen ! the particle lives in -sizex<x<sizex


sizet = 100*htim
dx = 2.*sizex/float(nx-1)
dt = 1.e-2 ! 1/100 oscillation time
nt = sizet/dt

vmax = vamp*sizex*sizex ! maximum value of the potential

write(6,*) ’dx,dt prior to CFL’,dx,dt


dtd = 1.0
if(dif.ne.0) dtd = 0.5*dx*dx/dif
dta = 1.0
if(vel.ne.0) dta = dx/abs(vel)
cfl = 0.5
dt = cfl*min(dta,dtd)

2
delta = dif*dt/(dx*dx)
alfa = vel*dt/dx
omega = vmax*dt

if(alfa .gt.1.0) stop ’alfa dt too large’


if(delta.gt.1.0) stop ’delta dt too large’
if(omega.gt.1.0) stop ’omega dt too large’

write(6,*) ’sizex,sigma’,sizex,sigma
write(6,*) ’dx,dt’,dx,dt
write(6,*) ’alfa,delta,omega’,alfa,delta,omega

c initial conditions

do j=1,nx
x(j) = dx*(float(j-(nx-1)/2))
xj = x(j)
rho = exp(-0.25*xj*xj/sigma/sigma)
Aold(j) = rho*cos(vel*xj)
Bold(j) = rho*sin(vel*xj)
rhoA = Aold(j)*Aold(j)
rhoB = Bold(j)*Bold(j)

vpot(j) = vmax*0.5*omh*omh*xj*xj

write(21,*) xj,Aold(j),Bold(j),vpot(j)
write(31,*) xj,rhoA,rhoB,rho,vpot(j)/vmax
end do
write(31,’(bn)’)
write(31,’(bn)’)
c -------------------------------------
c prepare time-independent quantities
do j=1,nx
c periodic bc via indirect address
je(j) = j+1
if(je(j).gt.nx) je(j)=1
jw(j) = j-1
if(jw(j).lt.1) jw(j)=nx
omega = vpot(j)*dt
c discretized Hamiltonian: once and for all for simplicity
hc(j) = delta+omega
hw(j) = -delta/2.
he(j) = -delta/2.

c time evolution begins here ------------------

3
c Euler start-up
hB = hc(j)*Bold(j)+hw(j)*Bold(jw(j))+he(j)*Bold(je(j))
A(j) = Aold(j)+hB
hA = hc(j)*Aold(j)+hw(j)*Aold(jw(j))+he(j)*Aold(je(j))
B(j) = Bold(j)-hA
end do

c now unleash the engine

do it=2,nt
do j=1,nx
hB = hc(j)*B(j)+hw(j)*B(jw(j))+he(j)*B(je(j))
Anew(j) = Aold(j)+2.0*hB
end do
do j=1,nx
hA = hc(j)*A(j)+hw(j)*A(jw(j))+he(j)*A(je(j))
Bnew(j) = Bold(j)-2.0*hA
end do

do j=1,nx
Aold(j) = A(j)
Bold(j) = B(j)
A(j) = Anew(j)
B(j) = Bnew(j)
end do

c diagnostic

if(mod(it,200).eq.1) then
tmass = 0.0
ave = 0.0
var = 0.0
epot = 0.0
ekin = 0.0
do j=1,nx
rhoA = A(j)*A(j)
rhoB = B(j)*B(j)
rho = rhoA + rhoB
tmass = tmass+ rho*dx
ave = ave + x(j)*rho*dx
var = var + x(j)*x(j)*rho*dx

dA = 0.5*(A(je(j))-A(jw(j)))/dx
dB = 0.5*(B(je(j))-B(jw(j)))/dx

ekin = ekin + 0.5*(dA*dA+dB*dB)*dx

4
epot = epot + vpot(j)*rho*dx

write(31,*) x(j),rhoA,rhoB,rho,vpot(j)/vmax
end do
ave = ave/tmass
rms = sqrt(var/tmass-ave*ave)
write(6,*) ’total mass’,it,tmass,ave,rms,epot,ekin
write(9,*) it,tmass,ave,rms,epot,ekin
write(31, ’(bn)’)
write(31, ’(bn)’)
endif
c ================================================
end do
stop
end

5
3 Klein-Gordon
This solves the Klein-Gordon equation in d=1 using a standard Leapfrog method.
The diagnostic has no physical meaning because the density of the KG field
needs to be defined via time derivatives. We did not implement it because it is
notoriously ill-posed (non-positive definite).

c solve klein-gordon for relativistic spinzero free particle


c second order leapfrog in space and tim
c SS: AC274 Fall 2016
c --------------------------------------------------------
parameter (nx=128)
dimension psiold(nx),psi(nx),psinew(nx)
dimension x(nx),vpot(nx)
dimension jw(nx),je(nx)
c ------------------------------------
open(31,file=’movie.out’)
open(9,file=’diagno.out’)
c ------------------ -------------------------
cvel = 1.0 ! lightspeed
omegac = 1.0 ! Compton frequency

sizex = 1.0
dx = 2*sizex/float(nx-1)
dt = dx/cvel
nt = 4*nx ! 4 is arbitrary
write(6,*) ’nx,nt,dx,dt’,nx,nt,dx,dt

alfa = 1.0
sigma = 10.0*dx ! play with 10

c initial conditions

do j=1,nx
x(j) = dx*(float(j-(nx-1)/2))
xj = x(j)
fj = exp(-0.25*xj*xj/sigma/sigma)
gj =-0.5*xj*fj/(sigma*sigma)
psiold(j) = fj
psi(j) = psiold(j)+0.0*cvel*gj*dt ! zero time derivative at t=0
c note rho is NOT a proper density
c because of interference between Left and right movers
rho = psi(j)*psi(j)
write(31,*) xj,rho,psi(j)
end do
write(31,’(bn)’)

6
write(31,’(bn)’)
c -------------------------------------
c periodic bc via index arrays
do j=1,nx
je(j) = j+1
if(je(j).gt.nx) je(j)=1
jw(j) = j-1
if(jw(j).lt.1) jw(j)=nx
end do
c now unleash the time engine
om2 = (omegac*dt)**2
alf2 = alfa*alfa
ct alf2 = 0.0 ! standing oscillations

c time evolution -----------------------------------

do it=1,nt
do j=1,nx
psilap=alf2*(psi(je(j))-2.0*psi(j)+psi(jw(j)))
psinew(j)=(2.0-om2)*psi(j)-psiold(j)+psilap
end do
c newstep
do j=1,nx
psiold(j) = psi(j)
psi(j) = psinew(j)
end do
c printout (but again, rho is NOT a proper density)
c so the moments have not much meaning, as you will see by yourself...
if(mod(it,10).eq.1) then
tmass = 0.0
ave = 0.0
var = 0.0
do j=1,nx
rho = psi(j)*psi(j)
tmass = tmass+rho*dx
ave = ave + x(j)*rho*dx
var = var + x(j)*x(j)*rho*dx
write(31,*) x(j),rho,psi(j)
end do
ave = ave/tmass
rms = sqrt(var/tmass -ave*ave)
write(6,*) ’total mass’,it,tmass,ave,rms
write(9,*) it,tmass,ave,rms
write(31, ’(bn)’)
write(31, ’(bn)’)
endif

7
c ================================================
end do
stop
end

8
4 Free Dirac particle in d=1, using Quantum
Lattice Boltzmann
This show that Qunatum LB scheme in action for d=1 Dirac equation. Two
complex wavefunctions, up and down movers which propagate and mix trough
a mass ”collision” matrix. They map to a slow and fast modes, the latter
vanishing in the non-relativistic limit β = v/c → 0. In the same limit, the slow
mode obeys the Schroedinger equation.

c Solve the d=1 free Dirac particle using Quantum LB


c Sauro Succi, AC274 Fall 2016
c u and d are the up and down movers (spin 1/2)
c psip and psim are the slow and fast movers
c
c *** lattice units dx=dt=c=1 throughout ***
c =========================================
parameter (nx=1024)
complex u(0:nx+1),d(0:nx+1),psip,psim
complex unew(0:nx+1),dnew(0:nx+1)
complex ci
c ========================================
open(31,file=’movie.out’)

ci = (0.0,1.0)
omc = 0.1 ! Compton
sigma = 50.0 ! 50 is a guideline value
beta = 0.1 ! v/c
om2 = omc*omc
sigma2 = sigma*sigma

c QLB transfer operator (all in lattice units dx=dt=c=1)

a = (1.0-0.25*om2)/(1.0+0.25*om2)
b = omc/(1.0+0.25*om2)
write(6,*) a*a+b*b
c -------------------------------------------
nstep = 1000
do j=1,nx
x = float(j-nx/2)
x2 = x*x/sigma2
rho = exp(-0.25*x2)
phi = beta*x
u(j) = rho*exp(+ci*phi)
d(j) = rho*exp(-ci*phi)
d(j) = -ci*u(j) ! kill the fast mode (u-id), play with it

9
end do

c time evolution ------------------------------

do 10 it=1,nstep
u(0) = u(nx)
d(0) = d(nx)
u(nx+1) = u(1)
d(nx+1) = d(1)
c qlb rule
do j=1,nx
unew(j) = a*u(j-1)+b*d(j-1)
dnew(j) =-b*u(j+1)+a*d(j+1)
end do
c new timestep
do j=1,nx
u(j) = unew(j)
d(j) = dnew(j)
end do
c output
if(mod(it,10).eq.1) then
phase = exp(ci*it)
rhomass = 0.
do j=1,nx
rho = u(j)*conjg(u(j))+d(j)*conjg(d(j))
rhomass = rhomass+rho
c these are the plus (slow) and minus (fast) modes
c the fast mode should scale like beta^2 hence disappear in the NR limit
psip = phase*(u(j)+ci*d(j))/sqrt(2.0)
psim = phase*(u(j)-ci*d(j))/sqrt(2.0)
rhop = psip*conjg(psip)
rhom = psim*conjg(psim)
write (31,*) j,rho,rhop,rhom
end do
write (31,’(bn)’)
write (31,’(bn)’)
write(6,*) ’mass’,rhomass
endif

10 continue

stop
end

10