Skip to content

Commit 9d59b67

Browse files
committed
WIP : Spin operators.
1 parent f3b5be2 commit 9d59b67

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/arrays/arraymath.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ end
6161

6262
*(dm1::DualMatrix, dm2::DualMatrix) = (dm1.qarr*dm2.qarr)'
6363

64-
function +{B<:OrthonormalBasis,N}(qarr1::Union(QuArray{B,N},CTranspose{B,N}), qarr2::Union(QuArray{B,N},CTranspose{B,N}))
64+
function +{B<:OrthonormalBasis,N}(qarr1::AbstractQuArray{B,N}, qarr2::AbstractQuArray{B,N})
6565
if bases(qarr1) == bases(qarr2)
6666
return QuArray(coeffs(qarr1)+coeffs(qarr2), bases(qarr1))
6767
else
6868
error("Bases not compatible")
6969
end
7070
end
7171

72-
function -{B<:OrthonormalBasis,N}(qarr1::Union(QuArray{B,N},CTranspose{B,N}), qarr2::Union(QuArray{B,N},CTranspose{B,N}))
72+
function -{B<:OrthonormalBasis,N}(qarr1::AbstractQuArray{B,N}, qarr2::AbstractQuArray{B,N})
7373
if bases(qarr1) == bases(qarr2)
7474
return QuArray(coeffs(qarr1)-coeffs(qarr2), bases(qarr1))
7575
else

src/arrays/ladderops.jl

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,48 @@ end
3434

3535
function momentumop(n::Int)
3636
cop = raiseop(n)
37-
return scale!(1/sqrt(2.), im*(cop-cop'))
37+
return scale(im/sqrt(2.), cop-cop')
3838
end
3939

40+
function spin_h1(j::Float64)
41+
if (isinteger(2*j) && j>0)
42+
m = reverse([-j:j-1])
43+
N = length(m)+1
44+
m_coeffs = [sqrt(j*(j+1.0) - (x+1.0)*x) for x in m]
45+
return spdiagm(m_coeffs,1,N,N)
46+
else
47+
return error("j must be non-negative integer or half integer")
48+
end
49+
end
50+
51+
function spin_h2(j::Float64)
52+
if (isinteger(2*j) && j>0)
53+
m = reverse([-j:j])
54+
N = length(m)
55+
return spdiagm(m,0,N,N)
56+
else
57+
return error("j must be non-negative integer or half integer")
58+
end
59+
end
60+
61+
spin_jx(j::Float64) = QuArray(0.5*(spin_h1(j)+spin_h1(j)'))
62+
spin_jy(j::Float64) = QuArray(-0.5*im*(spin_h1(j)-spin_h1(j)'))
63+
spin_jz(j::Float64) = QuArray(spin_h2(j))
64+
spin_jp(j::Float64) = QuArray(spin_h1(j))
65+
spin_jm(j::Float64) = QuArray(spin_h1(j)')
66+
sigmax = 2.0 * spin_jx(0.5)
67+
sigmay = 2.0 * spin_jy(0.5)
68+
sigmaz = 2.0 * spin_jz(0.5)
69+
4070
export raiseop,
4171
lowerop,
4272
positionop,
43-
momentumop
73+
momentumop,
74+
spin_jx,
75+
spin_jy,
76+
spin_jz,
77+
spin_jp,
78+
spin_jm,
79+
sigmax,
80+
sigmay,
81+
sigmaz

src/arrays/quarray.jl

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

3636
rawbases(qarr::QuArray, i) = qarr.bases[i]
3737
bases(qarr::QuArray, i) = rawbases(qarr, i)
38-
38+
3939
# works generally as long as the single index form is defined
4040
rawbases(qarr::AbstractQuArray) = ntuple(ndims(qarr), i->rawbases(qarr, i))
4141
bases(qarr::AbstractQuArray) = ntuple(ndims(qarr), i->bases(qarr, i))
@@ -105,11 +105,11 @@
105105
# LabelQuArray #
106106
################
107107
typealias LabelQuArray{B<:LabelBasis,T,N,A} QuArray{B,T,N,A}
108-
typealias TupleArray{T<:Tuple,N} Array{T,N}
108+
typealias TupleArray{T<:Tuple,N} Array{T,N}
109109

110110
Base.getindex(larr::LabelQuArray, tups::Union(Tuple,TupleArray)...) = getindex(larr, map(getindex, bases(larr), tups)...)
111111
Base.setindex!(larr::LabelQuArray, x, tups::Union(Tuple,TupleArray)...) = setindex!(larr, x, map(getindex, bases(larr), tups)...)
112-
112+
113113
######################
114114
# Printing Functions #
115115
######################

0 commit comments

Comments
 (0)