diff --git a/src/arrays/arraymath.jl b/src/arrays/arraymath.jl index f73fb3e..3c13415 100644 --- a/src/arrays/arraymath.jl +++ b/src/arrays/arraymath.jl @@ -121,6 +121,11 @@ function \{B<:OrthonormalBasis}(op::AbstractQuMatrix{B}, vec::AbstractQuVector{B return QAT(div, bases(op,1)) end +# convert coeffs to complex +Base.complex{B<:OrthonormalBasis}(qarr::AbstractQuArray{B}) = similar_type(qarr)(complex(coeffs(qarr)), bases(qarr)) +Base.float{B<:OrthonormalBasis}(qarr::AbstractQuArray{B}) = similar_type(qarr)(float(coeffs(qarr)), bases(qarr)) +Base.int{B<:OrthonormalBasis}(qarr::AbstractQuArray{B}) = similar_type(qarr)(int(coeffs(qarr)), bases(qarr)) + # Vectorize QuArray Base.vec{B<:OrthonormalBasis}(vec1::AbstractQuArray{B}) = QuArray(vec(coeffs(vec1))) @@ -198,8 +203,15 @@ tensor(bra::DualVector, ket::AbstractQuVector) = tensor(ket, bra) commute(a::AbstractQuMatrix, b::AbstractQuMatrix) = (a*b) - (b*a) anticommute(a::AbstractQuMatrix, b::AbstractQuMatrix) = (a*b) + (b*a) +############### +# Expectaion # +############### +expectation(qarr::AbstractQuVector, op::AbstractQuMatrix) = vec(qarr)'*op*vec(qarr) +expectation(qarr::AbstractQuMatrix, op::AbstractQuMatrix) = trace(qarr*op) + export normalize, normalize!, tensor, commute, - anticommute + anticommute, + expectation diff --git a/test/multest.jl b/test/multest.jl index fb93748..056cfa6 100644 --- a/test/multest.jl +++ b/test/multest.jl @@ -55,3 +55,8 @@ qv1 = normalize!(QuArray(v1)) # Vectorize @assert vec(qv) == vec(qv') + +# Complex coeffs +@assert complex(statevec(1, FiniteBasis(2))) == QuArray([1.+0.*im, 0.+0.*im]) +@assert float(statevec(1, FiniteBasis(2))) == QuArray([1., 0.]) +@assert int(statevec(1, FiniteBasis(2))) == QuArray([1, 0])