Skip to content

Commit

Permalink
Merge pull request #25 from amitjamadagni/ops
Browse files Browse the repository at this point in the history
Add operators: squeezingop, displaceop + tests. Add function coherentstatevec + test. Add functions for QuArray: expm, full. Fix issue with multiplication of two DualMatrices.
  • Loading branch information
acroy committed Apr 21, 2015
2 parents e08b440 + eef548b commit 7e01ceb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/arrays/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function *{B<:OrthonormalBasis}(qm1::QuMatrix{B}, qm2::QuMatrix{B})
bases(qm2,2))
end

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

function +{B<:OrthonormalBasis,N}(qarr1::AbstractQuArray{B,N}, qarr2::AbstractQuArray{B,N})
if bases(qarr1) == bases(qarr2)
Expand Down Expand Up @@ -90,6 +90,12 @@ Base.scale(qarr::Union(QuArray,CTranspose), num::Number) = scale(num, qarr)
*(qarr::Union(QuArray,CTranspose), num::Number) = scale(qarr, num)
/(qarr::Union(QuArray,CTranspose), num::Number) = scale(1/num, qarr)

# sparse to dense
Base.full(qarr::AbstractQuMatrix) = QuArray(full(coeffs(qarr)),bases(qarr))

# exponential of dense matrix
Base.expm(qarr::AbstractQuMatrix) = QuArray(expm(full(coeffs(qarr))),bases(qarr))

# normalization
Base.norm(qarr::Union(QuArray,CTranspose)) = vecnorm(rawcoeffs(qarr))

Expand Down
19 changes: 18 additions & 1 deletion src/arrays/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,21 @@
one_at_ind!(arr, i) = setindex!(arr, one(eltype(arr)), i)
single_coeff(i, lens...) = one_at_ind!(zeros(lens), i)

export statevec
# Reference :
# Section - 1.1, http://cds.cern.ch/record/331607/files/9708012.pdf
function coherentstatevec_inf(n::Int,alpha::Number)
s = zeros(typeof(float(alpha)),n)
s[1] = one(alpha)
for i in [2:n]
s[i] = alpha/sqrt(i-1)*s[i-1]
end
z = QuArray(s)
return scale!(exp(-abs2(alpha)/2),z)
end

# Reference :
# 2nd Defintion : http://en.wikipedia.org/wiki/Coherent_states#Mathematical_features_of_the_canonical_coherent_states
coherentstatevec(n::Int, alpha::Number) = displaceop(n,alpha)*statevec(1,FiniteBasis(n))

export statevec,
coherentstatevec
10 changes: 9 additions & 1 deletion src/arrays/ladderops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ function momentumop(n::Int)
return scale(im/sqrt(2.), cop-cop')
end

squeeze_construct(a::AbstractQuMatrix,b::AbstractQuMatrix, z::Number) = scale!(0.5,(scale!(z',a*b)-scale!(z,a'*b')))
squeezingop(a::AbstractQuMatrix, b::AbstractQuMatrix, z::Number) = expm(squeeze_construct(a,b,z))
squeezingop(n::Int,z::Number) = squeezingop(lowerop(n),lowerop(n),z)

displaceop(n::Int,alpha::Number) = expm(scale(alpha,lowerop(n)')-scale(alpha',lowerop(n)))

export raiseop,
lowerop,
positionop,
momentumop
momentumop,
squeezingop,
displaceop
2 changes: 1 addition & 1 deletion test/multest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ qv = QuArray(v)
@test_approx_eq rawcoeffs(normalize(qv)) rawcoeffs(qv)/norm(v)

# a simple test of the `==` operator
@assert qm*3im == scale!(3im, copy(qm))
@assert qm*3im == scale!(3im, copy(qm))
17 changes: 14 additions & 3 deletions test/operatortest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@

@assert coeffs(commutator(sigma_x, sigma_x)) == spzeros(2,2)

######################################
# Position & Momentum Operators Test #
######################################
####################################################
# Position, Displacement & Momentum Operators Test #
####################################################

p = positionop(2)
m = momentumop(2)
@assert coeffs(commutator(sigma_x, p)) == spzeros(2,2)
@assert coeffs(commutator(sigma_y, m)) == spzeros(2,2)

coherentstate_inf = QuBase.coherentstatevec_inf(20,1)
coherentstate = displaceop(20,1)*statevec(1,FiniteBasis(20))
@test_approx_eq_eps coeffs(coherentstate_inf)[1] coeffs(coherentstate)[1] 1e-8

############################
# Squeezing Operators Test #
############################

@assert squeezingop(2,1.0)== QuArray(eye(2))
@test_approx_eq coeffs(squeezingop(lowerop(2), QuArray(eye(2)), 2.0)') coeffs(displaceop(2,1.0))

0 comments on commit 7e01ceb

Please sign in to comment.