Skip to content

Commit 2734d63

Browse files
committed
Added : Operators
Position and momentum, Spin operators, Related Tests.
1 parent c7fd446 commit 2734d63

File tree

7 files changed

+138
-11
lines changed

7 files changed

+138
-11
lines changed

src/arrays/arraymath.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ end
107107

108108
tensor{B<:OrthonormalBasis}(bra::DualVector{B}, ket::QuVector{B}) = tensor(ket, bra)
109109

110+
##############
111+
# Commutator #
112+
##############
113+
114+
function commutator{B<:OrthonormalBasis}(qm1::AbstractQuMatrix{B}, qm2::AbstractQuMatrix{B})
115+
return qm1*qm2-qm2*qm1
116+
end
117+
110118
export normalize,
111119
normalize!,
112-
tensor
120+
tensor,
121+
commutator

src/arrays/arrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
include("arraymath.jl")
66
include("constructors.jl")
77
include("ladderops.jl")
8-
8+
include("spinops.jl")

src/arrays/ladderops.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
###########################
22
# Ladder Operator Methods #
3-
###########################
4-
# n specifies which particle (a.k.a tensor product
3+
###########################
4+
# n specifies which particle (a.k.a tensor product
55
# factor) the operator acts on
66
raiseop(b::AbstractBasis, n=1) = QuArray(raisematrix(size(b), n), b, b)
77
raiseop(lens, n=1) = raiseop(FiniteBasis(lens), n)
8-
8+
99
lowerop(b::AbstractBasis, n=1) = QuArray(lowermatrix(size(b), n), b, b)
1010
lowerop(lens, n=1) = lowerop(FiniteBasis(lens), n)
1111

@@ -22,10 +22,23 @@
2222
before_eye(lens, pivot) = speye(prod(lens[[1:pivot-1]]))
2323
after_eye(lens, pivot) = speye(prod(lens[[pivot+1:length(lens)]]))
2424
function eye_sandwich(lens, pivot, op)
25-
return kron(before_eye(lens, pivot),
26-
op,
25+
return kron(before_eye(lens, pivot),
26+
op,
2727
after_eye(lens, pivot))
2828
end
2929

30+
function positionop(n::Int)
31+
cop = raiseop(n)
32+
return scale!(1/sqrt(2.),cop+cop')
33+
end
34+
35+
function momentumop(n::Int)
36+
cop = raiseop(n)
37+
return scale(im/sqrt(2.), cop-cop')
38+
end
39+
3040
export raiseop,
31-
lowerop
41+
lowerop,
42+
positionop,
43+
momentumop
44+

src/arrays/quarray.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535

3636
rawbases(qarr::QuArray, i) = qarr.bases[i]
3737
bases(qarr::QuArray, i) = rawbases(qarr, i)
38-
38+
39+
rawbases(qarr::AbstractQuArray) = qarr.bases
40+
41+
bases(qarr::QuArray, i) = rawbases(qarr, i)
3942
# works generally as long as the single index form is defined
4043
rawbases(qarr::AbstractQuArray) = ntuple(ndims(qarr), i->rawbases(qarr, i))
4144
bases(qarr::AbstractQuArray) = ntuple(ndims(qarr), i->bases(qarr, i))
@@ -105,11 +108,11 @@
105108
# LabelQuArray #
106109
################
107110
typealias LabelQuArray{B<:LabelBasis,T,N,A} QuArray{B,T,N,A}
108-
typealias TupleArray{T<:Tuple,N} Array{T,N}
111+
typealias TupleArray{T<:Tuple,N} Array{T,N}
109112

110113
Base.getindex(larr::LabelQuArray, tups::Union(Tuple,TupleArray)...) = getindex(larr, map(getindex, bases(larr), tups)...)
111114
Base.setindex!(larr::LabelQuArray, x, tups::Union(Tuple,TupleArray)...) = setindex!(larr, x, map(getindex, bases(larr), tups)...)
112-
115+
113116
######################
114117
# Printing Functions #
115118
######################

src/arrays/spinops.jl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#########################
2+
# Spin Operator Methods #
3+
#########################
4+
5+
immutable HalfSpin{S}
6+
function HalfSpin()
7+
S > 0 ? new() : error("Spin must be positive")
8+
end
9+
end
10+
11+
# The below function `spin` takes in a value and converts its to a type parameter
12+
# i.e., from value domain to type domain making it type unstable.
13+
14+
spin(j::Integer) = j > 0 ? HalfSpin{j}() : error("Spin must be positive")
15+
16+
function spin(j::Float64)
17+
if (j > 0) && isinteger(j)
18+
spin(int(j))
19+
elseif (j > 0) && isinteger(2*j)
20+
spin(int(2*j))
21+
else
22+
error("Spin must be positive and a multiple of 1/2.")
23+
end
24+
end
25+
26+
spin_value{S}(::HalfSpin{S}) = S/2
27+
28+
#####################
29+
# Auxiliary methods #
30+
#####################
31+
32+
function mat_coeffs_1(j::Float64)
33+
m = [j-1:-1:-j]
34+
N = length(m)+1
35+
m_coeffs = [sqrt(j*(j+1.0) - (x+1.0)*x) for x in m]
36+
return spdiagm(m_coeffs,1,N,N)
37+
end
38+
39+
function mat_coeffs_2(j::Float64)
40+
m = [j:-1:-j]
41+
N = length(m)
42+
return spdiagm(m,0,N,N)
43+
end
44+
45+
function spin_h1(k::HalfSpin)
46+
j = spin_value(k)
47+
return mat_coeffs_1(j)
48+
end
49+
50+
function spin_h2(k::HalfSpin)
51+
j = spin_value(k)
52+
return mat_coeffs_2(j)
53+
end
54+
55+
################################
56+
# Jx, Jy, Jz, Jp, Jm operators #
57+
################################
58+
59+
spin_Jx(j) = QuArray(0.5*(spin_h1(spin(j))+spin_h1(spin(j))'))
60+
spin_Jy(j) = QuArray(-0.5*im*(spin_h1(spin(j))-spin_h1(spin(j))'))
61+
spin_Jz(j) = QuArray(spin_h2(spin(j)))
62+
spin_Jp(j) = QuArray(spin_h1(spin(j)))
63+
spin_Jm(j) = QuArray(spin_h1(spin(j))')
64+
65+
############################
66+
# Pauli spin 1/2 matrices #
67+
############################
68+
69+
const sigma_x = 2.0 * spin_Jx(1/2)
70+
const sigma_y = 2.0 * spin_Jy(1/2)
71+
const sigma_z = 2.0 * spin_Jz(1/2)
72+
73+
# TODO : unicode sigma_y, sigma_z
74+
const σₓ = sigma_x
75+
76+
export spin_Jx,
77+
spin_Jy,
78+
spin_Jz,
79+
spin_Jp,
80+
spin_Jm,
81+
sigma_x,
82+
sigma_y,
83+
sigma_z,
84+
σₓ

test/operatortest.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#######################
2+
# Spin Operators Test #
3+
#######################
4+
5+
@assert commutator(sigma_x, sigma_y) == 2*im*sigma_z
6+
@assert commutator(sigma_y, sigma_z) == 2*im*sigma_x
7+
@assert commutator(sigma_z, sigma_x) == 2*im*sigma_y
8+
@assert coeffs(commutator(sigma_x, sigma_x)) == spzeros(2,2)
9+
10+
######################################
11+
# Position & Momentum Operators Test #
12+
######################################
13+
14+
p = positionop(2)
15+
m = momentumop(2)
16+
@assert coeffs(commutator(sigma_x, p)) == spzeros(2,2)
17+
@assert coeffs(commutator(sigma_y, m)) == spzeros(2,2)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ using Base.Test
44
include("multest.jl")
55
include("tensortest.jl")
66
include("labelbasistest.jl")
7+
include("operatortest.jl")

0 commit comments

Comments
 (0)