Skip to content

Commit

Permalink
Dot product, Trace Implemented.Tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitjamadagni committed Jun 29, 2015
1 parent 055bce8 commit b457ec2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/arrays/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,21 @@ function \{B<:OrthonormalBasis}(op::AbstractQuMatrix{B}, vec::AbstractQuVector{B
return QAT(div, bases(op,1))
end

# Trace of AbstractQuMatrix
Base.trace{B<:OrthonormalBasis}(qm1::AbstractQuMatrix{B}) = trace(coeffs(qm1))

# dot product of QuArray's
Base.dot{B<:OrthonormalBasis}(vec1::AbstractQuVector{B}, vec2::AbstractQuVector{B}) = dot(coeffs(vec1), coeffs(vec2))
Base.dot{B<:OrthonormalBasis}(vec1::DualVector{B}, vec2::DualVector{B}) = dot(vec(coeffs(vec1)), vec(coeffs(vec2)))
Base.dot{B<:OrthonormalBasis}(vec1::AbstractQuVector{B}, vec2::DualVector{B}) = error("Inner product of a dual vector and a vector is not supported.")
Base.dot{B<:OrthonormalBasis}(vec1::DualVector{B}, vec2::AbstractQuVector{B}) = error("Inner product of a dual vector and a vector is not supported.")

# Reference : http://en.wikipedia.org/wiki/Dot_product#Dyadics_and_matrices
Base.dot{B<:OrthonormalBasis}(qm1::AbstractQuMatrix{B}, qm2::AbstractQuMatrix{B}) = trace(qm1'*qm2)
Base.dot{B<:OrthonormalBasis}(qm1::DualMatrix{B}, qm2::AbstractQuMatrix{B}) = trace(qm1*qm2)
Base.dot{B<:OrthonormalBasis}(qm1::DualMatrix{B}, qm2::DualMatrix{B}) = trace(qm1*qm2')
Base.dot{B<:OrthonormalBasis}(qm1::AbstractQuMatrix{B}, qm2::DualMatrix{B}) = trace(qm1'*qm2')

# matrix operations returning a scalar
# normalization
Base.norm(qarr::AbstractQuArray) = vecnorm(rawcoeffs(qarr))
Expand Down
13 changes: 13 additions & 0 deletions test/multest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ v1 = [0.5+0*im, 0.+0.*im]
qv1 = normalize!(QuArray(v1))
@assert coeffs(\(sigmax,qv1)) == [0.+0.*im, 1.+0.*im]
@assert coeffs(\(sigmaz, sigmax)) == [0. 1.;-1. 0.]

# Trace
# Pauli matrices sigmax, sigmay, sigmaz are traceless
# Ref : http://en.wikipedia.org/wiki/Pauli_matrices#Algebraic_properties
@assert trace(sigmax) == zero(eltype(sigmax))
@assert trace(sigmax) == trace(sigmaz)

# Dot product
# Being defined both for vectors and dual vectors
# Related Ref : https://github.com/JuliaLang/julia/issues/11064
@assert dot(qv,qv) == dot(qv',qv')
# dot(A,B)=trace(A'*B) for the case of sigmax, sigmay results in no trace.
@assert dot(sigmax, sigmay) == trace(sigmaz)

0 comments on commit b457ec2

Please sign in to comment.