From fec60834e094f79f43d21a69e7ff6d9c046ee095 Mon Sep 17 00:00:00 2001 From: amitjamadagni Date: Mon, 29 Jun 2015 17:34:25 +0530 Subject: [PATCH] Dot, trace implemented. Test added. --- src/arrays/arraymath.jl | 15 +++++++++++++++ test/multest.jl | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/arrays/arraymath.jl b/src/arrays/arraymath.jl index d924ccb..ffec121 100644 --- a/src/arrays/arraymath.jl +++ b/src/arrays/arraymath.jl @@ -124,6 +124,21 @@ end # Vectorize QuArray Base.vec{B<:OrthonormalBasis}(vec1::AbstractQuArray{B}) = QuArray(vec(coeffs(vec1))) +# 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)) diff --git a/test/multest.jl b/test/multest.jl index fddce6e..542e280 100644 --- a/test/multest.jl +++ b/test/multest.jl @@ -38,5 +38,18 @@ 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) + # Vectorize @assert vec(qv) == vec(qv')