From 0c904e939e40f465e90cd50c3c1ab1df5ee47a00 Mon Sep 17 00:00:00 2001 From: amitjamadagni Date: Sun, 12 Jul 2015 18:55:40 +0800 Subject: [PATCH 1/3] Complex coeffs support. --- src/arrays/arraymath.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/arrays/arraymath.jl b/src/arrays/arraymath.jl index f73fb3e..5b12b49 100644 --- a/src/arrays/arraymath.jl +++ b/src/arrays/arraymath.jl @@ -121,6 +121,9 @@ 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(vec(qarr))), bases(qarr)) + # Vectorize QuArray Base.vec{B<:OrthonormalBasis}(vec1::AbstractQuArray{B}) = QuArray(vec(coeffs(vec1))) From 3fb56c820d9a24f60dd28c77b136c1cbc3119492 Mon Sep 17 00:00:00 2001 From: amitjamadagni Date: Mon, 13 Jul 2015 09:17:04 +0800 Subject: [PATCH 2/3] Tests added. --- src/arrays/arraymath.jl | 11 +++++++++-- test/multest.jl | 3 +++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/arrays/arraymath.jl b/src/arrays/arraymath.jl index 5b12b49..0c25ce9 100644 --- a/src/arrays/arraymath.jl +++ b/src/arrays/arraymath.jl @@ -122,7 +122,7 @@ function \{B<:OrthonormalBasis}(op::AbstractQuMatrix{B}, vec::AbstractQuVector{B end # convert coeffs to complex -Base.complex{B<:OrthonormalBasis}(qarr::AbstractQuArray{B}) = similar_type(qarr)(complex(coeffs(vec(qarr))), bases(qarr)) +Base.complex{B<:OrthonormalBasis}(qarr::AbstractQuArray{B}) = similar_type(qarr)(complex(coeffs(qarr)), bases(qarr)) # Vectorize QuArray Base.vec{B<:OrthonormalBasis}(vec1::AbstractQuArray{B}) = QuArray(vec(coeffs(vec1))) @@ -201,8 +201,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..946d10f 100644 --- a/test/multest.jl +++ b/test/multest.jl @@ -55,3 +55,6 @@ 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]) From d0137c9f6b03cbc55bdeecd7be0c87263a8ce474 Mon Sep 17 00:00:00 2001 From: amitjamadagni Date: Tue, 22 Sep 2015 16:38:41 +0800 Subject: [PATCH 3/3] Added float, int. Fixed `\` warning. --- src/arrays/arraymath.jl | 2 ++ test/multest.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/arrays/arraymath.jl b/src/arrays/arraymath.jl index 0c25ce9..3c13415 100644 --- a/src/arrays/arraymath.jl +++ b/src/arrays/arraymath.jl @@ -123,6 +123,8 @@ 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))) diff --git a/test/multest.jl b/test/multest.jl index 946d10f..056cfa6 100644 --- a/test/multest.jl +++ b/test/multest.jl @@ -58,3 +58,5 @@ qv1 = normalize!(QuArray(v1)) # 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])