From 3256979d327ffb0881dd050ceaefe774b17ac1d8 Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Tue, 20 Aug 2019 16:58:41 +0200 Subject: [PATCH 1/6] channelbasis added --- src/matrixbases.jl | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/matrixbases.jl b/src/matrixbases.jl index bb0768e..ea6ec67 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -1,13 +1,17 @@ import .Base: length, iterate -export AbstractMatrixBasisIterator, HermitianBasisIterator, AbstractBasis, - AbstractMatrixBasis, HermitianBasis, hermitianbasis, represent, combine +export AbstractMatrixBasisIterator, HermitianBasisIterator, AbstractBasis, ChannelBasisIterator, AbstractChannelBasis, ChannelBasis, AbstractMatrixBasis, HermitianBasis, hermitianbasis, represent, combine abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end struct HermitianBasisIterator{T} <: AbstractMatrixBasisIterator{T} dim::Int end - +abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end +struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} + idim::Int + odim::Int +end abstract type AbstractBasis end abstract type AbstractMatrixBasis{T} <: AbstractBasis where T<:AbstractMatrix{<:Number} end +abstract type AbstractChannelBasis{T} <: AbstractMatrixBasis where T<:AbstractMatrix{<:Number} end struct HermitianBasis{T} <: AbstractMatrixBasis{T} iterator::HermitianBasisIterator{T} @@ -17,16 +21,58 @@ struct HermitianBasis{T} <: AbstractMatrixBasis{T} end end +struct ChannelBasis{T} <: AbstractMatrixBasis{T} + iterator::ChannelBasisIterator{T} + + function ChannelBasis{T}(idim::Integer, odim::Integer) where T<:AbstractMatrix{<:Number} + new(ChannelBasisIterator{T}(idim, odim)) + end +end """ $(SIGNATURES) - `dim`: dimensions of the matrix. - Returns elementary hermitian matrices of dimension `dim` x `dim`. """ hermitianbasis(T::Type{<:AbstractMatrix{<:Number}}, dim::Int) = HermitianBasisIterator{T}(dim) hermitianbasis(dim::Int) = hermitianbasis(Matrix{ComplexF64}, dim) +channelbasis(T::Type{<:AbstractMatrix{<:Number}}, idim::Int, odim::Int=idim) = ChannelBasisIterator{T}(idim, odim) + +channelbasis(idim::Int, odim::Int=idim) = channelbasis(Matrix{ComplexF64}, idim, odim) + +function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:AbstractMatrix{<:Number} + idim=itr.idim + odim=itr.odim + (a,c,b,d)=state + (a == odim && c == odim) && return nothing + + Tn = eltype(T) + if a > c + x = ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) + ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim) + elseif a < c + x = im * ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) - im *ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim) + else + H = iterate(HermitianBasisIterator{T}(idim),(b,d)) + x = diagm(0 => hcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H + end + if d < idim + newstate = (a,c,b, d+1) + elseif d == idim && b < odim + newstate = (a,c,b+1,1) + elseif d == idim && b == odim && c < idim + newstate = (a,c+1,1,1) + else + newstate = (a+1,1,1,1) + end + return x, newstate +end +length(itr::ChannelBasisIterator) = itr.idim^2*itr.odim^2-itr.idim^2 + +function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<: AbstractMatrix{<:Number} + J = convert(DynamicalMatrix{T2}, Φ) + represent(basis, J.matrix) +end function iterate(itr::HermitianBasisIterator{T}, state=(1,1)) where T<:AbstractMatrix{<:Number} dim = itr.dim @@ -57,4 +103,6 @@ end function combine(basis::T, v::Vector{<:Number}) where T<:AbstractMatrixBasis sum(basis.iterator .* v) -end \ No newline at end of file +end + + From 3ec453670d8431be6a3091ab07eff81187d84311 Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Fri, 23 Aug 2019 15:55:42 +0200 Subject: [PATCH 2/6] channel basis correctly without tests --- src/matrixbases.jl | 116 ++++++++++++++++++++++++------------------- test/channelbases.jl | 37 ++++++++++++++ 2 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 test/channelbases.jl diff --git a/src/matrixbases.jl b/src/matrixbases.jl index ea6ec67..89d9ed1 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -1,17 +1,14 @@ import .Base: length, iterate -export AbstractMatrixBasisIterator, HermitianBasisIterator, AbstractBasis, ChannelBasisIterator, AbstractChannelBasis, ChannelBasis, AbstractMatrixBasis, HermitianBasis, hermitianbasis, represent, combine +export AbstractMatrixBasisIterator, HermitianBasisIterator, AbstractBasis, + ChannelBasisIterator, AbstractChannelBasis, ChannelBasis, AbstractMatrixBasis, + HermitianBasis, hermitianbasis, represent, combine, channelbasis abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end struct HermitianBasisIterator{T} <: AbstractMatrixBasisIterator{T} dim::Int end -abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end -struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} - idim::Int - odim::Int -end + abstract type AbstractBasis end abstract type AbstractMatrixBasis{T} <: AbstractBasis where T<:AbstractMatrix{<:Number} end -abstract type AbstractChannelBasis{T} <: AbstractMatrixBasis where T<:AbstractMatrix{<:Number} end struct HermitianBasis{T} <: AbstractMatrixBasis{T} iterator::HermitianBasisIterator{T} @@ -21,13 +18,8 @@ struct HermitianBasis{T} <: AbstractMatrixBasis{T} end end -struct ChannelBasis{T} <: AbstractMatrixBasis{T} - iterator::ChannelBasisIterator{T} - function ChannelBasis{T}(idim::Integer, odim::Integer) where T<:AbstractMatrix{<:Number} - new(ChannelBasisIterator{T}(idim, odim)) - end -end + """ $(SIGNATURES) - `dim`: dimensions of the matrix. @@ -37,43 +29,6 @@ hermitianbasis(T::Type{<:AbstractMatrix{<:Number}}, dim::Int) = HermitianBasisIt hermitianbasis(dim::Int) = hermitianbasis(Matrix{ComplexF64}, dim) -channelbasis(T::Type{<:AbstractMatrix{<:Number}}, idim::Int, odim::Int=idim) = ChannelBasisIterator{T}(idim, odim) - -channelbasis(idim::Int, odim::Int=idim) = channelbasis(Matrix{ComplexF64}, idim, odim) - -function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:AbstractMatrix{<:Number} - idim=itr.idim - odim=itr.odim - (a,c,b,d)=state - (a == odim && c == odim) && return nothing - - Tn = eltype(T) - if a > c - x = ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) + ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim) - elseif a < c - x = im * ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) - im *ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim) - else - H = iterate(HermitianBasisIterator{T}(idim),(b,d)) - x = diagm(0 => hcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H - end - if d < idim - newstate = (a,c,b, d+1) - elseif d == idim && b < odim - newstate = (a,c,b+1,1) - elseif d == idim && b == odim && c < idim - newstate = (a,c+1,1,1) - else - newstate = (a+1,1,1,1) - end - return x, newstate -end -length(itr::ChannelBasisIterator) = itr.idim^2*itr.odim^2-itr.idim^2 - -function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<: AbstractMatrix{<:Number} - J = convert(DynamicalMatrix{T2}, Φ) - represent(basis, J.matrix) -end - function iterate(itr::HermitianBasisIterator{T}, state=(1,1)) where T<:AbstractMatrix{<:Number} dim = itr.dim (a, b) = state @@ -101,8 +56,67 @@ function represent(basis::Type{T}, m::Matrix{<:Number}) where T<:AbstractMatrixB represent(basis{typeof(m)}(d), m) end -function combine(basis::T, v::Vector{<:Number}) where T<:AbstractMatrixBasis +function combine(basis::AbstractMatrixBasis{T}, v::Vector{<:Number}) where T<:AbstractMatrix{<:Number} sum(basis.iterator .* v) end +""" +$(SIGNATURES) + +""" +abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end +struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} + idim::Int + odim::Int +end +abstract type AbstractChannelBasis{T} <: AbstractMatrixBasis{T} end +struct ChannelBasis{T} <: AbstractMatrixBasis{T} + iterator::ChannelBasisIterator{T} + function ChannelBasis{T}(idim::Integer, odim::Integer) where T<:AbstractMatrix{<:Number} + new(ChannelBasisIterator{T}(idim, odim)) + end +end +channelbasis(T::Type{<:AbstractMatrix{<:Number}}, idim::Int, odim::Int=idim) = ChannelBasis{T}(idim, odim) + +channelbasis(idim::Int, odim::Int=idim) = channelbasis(Matrix{ComplexF64}, idim, odim) + +function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:AbstractMatrix{<:Number} + idim=itr.idim + odim=itr.odim + (a,c,b,d)=state + (a == odim && c == odim && d == 2) && return nothing + + Tn = eltype(T) + if a > c + x = (ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) + ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim))/sqrt(Tn(2)) + elseif a < c + x = (im * ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) - im *ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim))/sqrt(Tn(2)) + elseif a < odim + H = iterate(HermitianBasisIterator{T}(idim),(b,d))[1] + x = (diagm(0 => vcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H) /sqrt(Tn(a+a^2)) + else + x = Matrix{Tn}(I, idim*odim, idim*odim)/sqrt(Tn(idim*odim)) + end + if d < idim + newstate = (a,c,b, d+1) + elseif d == idim && b < idim + newstate = (a,c,b+1,1) + elseif d == idim && b == idim && c < odim + newstate = (a,c+1,1,1) + else + newstate = (a+1,1,1,1) + end + return x, newstate +end +length(itr::ChannelBasisIterator) = itr.idim^2*itr.odim^2-itr.idim^2+1 + +function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<: AbstractMatrix{<:Number} + J = convert(DynamicalMatrix{T2}, Φ) + represent(basis, J.matrix) +end +function combine(basis::ChannelBasis{T}, v::Vector{<:Number}) where T<:AbstractMatrix + m = sum(basis.iterator .* v) + @show 1 + DynamicalMatrix{T}(m, basis.iterator.idim, basis.iterator.odim) +end \ No newline at end of file diff --git a/test/channelbases.jl b/test/channelbases.jl new file mode 100644 index 0000000..8520745 --- /dev/null +++ b/test/channelbases.jl @@ -0,0 +1,37 @@ +@testset "MatrixBases" begin + +@testset "ChannelBasisIterator" begin + d1=2 + d2=2 + d=d1^2*d^2-d1^2+1 + m = collect(ChannelBasisIterator{Matrix{ComplexF64}}(d1,d2)) + @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) +end + +@testset "represent, combine" begin + d = 4 + A = reshape(collect(1:16), d, d) + reshape(collect(1:16), d, d)' + vA = represent(HermitianBasis{Matrix{ComplexF64}}(d), A) + Ap = combine(HermitianBasis{Matrix{ComplexF64}}(d), vA) + @test A ≈ Ap + B = A*A' + vB = represent(HermitianBasis{Matrix{ComplexF64}}(d), B) + Bp = combine(HermitianBasis{Matrix{ComplexF64}}(d), vB) + @test B ≈ Bp + + vB = represent(HermitianBasis{Matrix{ComplexF32}}(d), B) + @test eltype(vB) == Float32 + + C = Float16[1 2; 3 4] + C += C' + vC = represent(HermitianBasis, C) + @test eltype(vC) == eltype(C) + @test length(vC) == prod(size(C)) +end + +@testset "hermitainbasis" begin + @test hermitianbasis(Matrix{Float32}, 2) == HermitianBasisIterator{Matrix{Float32}}(2) + @test hermitianbasis(2) == HermitianBasisIterator{Matrix{ComplexF64}}(2) +end + +end \ No newline at end of file From 7aad859cf9dbceec1e02b7623eb11e57c57a3ec7 Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Fri, 23 Aug 2019 16:37:06 +0200 Subject: [PATCH 3/6] channel basis iterator finished --- src/matrixbases.jl | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/matrixbases.jl b/src/matrixbases.jl index 89d9ed1..948af2f 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -68,7 +68,12 @@ abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} idim::Int odim::Int + hitr::HermitianBasisIterator{T} + function ChannelBasisIterator{T}(idim::Int, odim::Int) where T<:AbstractMatrix{<:Number} + new(idim, odim, HermitianBasisIterator{T}(idim)) + end end + abstract type AbstractChannelBasis{T} <: AbstractMatrixBasis{T} end struct ChannelBasis{T} <: AbstractMatrixBasis{T} iterator::ChannelBasisIterator{T} @@ -81,34 +86,35 @@ channelbasis(T::Type{<:AbstractMatrix{<:Number}}, idim::Int, odim::Int=idim) = C channelbasis(idim::Int, odim::Int=idim) = channelbasis(Matrix{ComplexF64}, idim, odim) function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:AbstractMatrix{<:Number} - idim=itr.idim - odim=itr.odim - (a,c,b,d)=state + idim = itr.idim + odim = itr.odim + hitr = itr.hitr + (a, c, b, d) = state (a == odim && c == odim && d == 2) && return nothing Tn = eltype(T) if a > c - x = (ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) + ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim))/sqrt(Tn(2)) - elseif a < c - x = (im * ketbra(T,a,c,odim) ⊗ ketbra(T,b,d,idim) - im *ketbra(T,c,a,odim) ⊗ ketbra(T,d,b,idim))/sqrt(Tn(2)) + x = (ketbra(T, a, c, odim) ⊗ ketbra(T, b, d, idim) + ketbra(T, c, a, odim) ⊗ ketbra(T, d, b, idim)) / sqrt(Tn(2)) + elseif a < c + x = (im * ketbra(T, a, c, odim) ⊗ ketbra(T, b, d, idim) - im * ketbra(T, c, a, odim) ⊗ ketbra(T, d, b, idim)) / sqrt(Tn(2)) elseif a < odim - H = iterate(HermitianBasisIterator{T}(idim),(b,d))[1] - x = (diagm(0 => vcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H) /sqrt(Tn(a+a^2)) + H = iterate(hitr, (b, d))[1] + x = (diagm(0 => vcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H) / sqrt(Tn(a+a^2)) else - x = Matrix{Tn}(I, idim*odim, idim*odim)/sqrt(Tn(idim*odim)) + x = Matrix{Tn}(I, idim * odim, idim * odim) / sqrt(Tn(idim * odim)) end if d < idim - newstate = (a,c,b, d+1) + newstate = (a, c, b, d+1) elseif d == idim && b < idim - newstate = (a,c,b+1,1) + newstate = (a, c, b+1, 1) elseif d == idim && b == idim && c < odim - newstate = (a,c+1,1,1) + newstate = (a, c+1, 1, 1) else - newstate = (a+1,1,1,1) + newstate = (a+1, 1, 1, 1) end return x, newstate end -length(itr::ChannelBasisIterator) = itr.idim^2*itr.odim^2-itr.idim^2+1 +length(itr::ChannelBasisIterator) = itr.idim^2 * itr.odim^2 - itr.idim^2 + 1 function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<: AbstractMatrix{<:Number} J = convert(DynamicalMatrix{T2}, Φ) @@ -117,6 +123,5 @@ end function combine(basis::ChannelBasis{T}, v::Vector{<:Number}) where T<:AbstractMatrix m = sum(basis.iterator .* v) - @show 1 DynamicalMatrix{T}(m, basis.iterator.idim, basis.iterator.odim) end \ No newline at end of file From 5e180819978e0d6f4f1955222742f1391be0c5b8 Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Fri, 23 Aug 2019 18:34:53 +0200 Subject: [PATCH 4/6] testing matrixbasis without number types --- src/matrixbases.jl | 4 ++-- test/channelbases.jl | 37 ------------------------------------- test/matrixbases.jl | 40 ++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 - 4 files changed, 42 insertions(+), 40 deletions(-) delete mode 100644 test/channelbases.jl diff --git a/src/matrixbases.jl b/src/matrixbases.jl index 948af2f..0c77080 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -99,7 +99,7 @@ function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:Abstrac x = (im * ketbra(T, a, c, odim) ⊗ ketbra(T, b, d, idim) - im * ketbra(T, c, a, odim) ⊗ ketbra(T, d, b, idim)) / sqrt(Tn(2)) elseif a < odim H = iterate(hitr, (b, d))[1] - x = (diagm(0 => vcat(ones(Tn, a), Tn[-a], zeros(Tn, odim-a-1))) ⊗ H) / sqrt(Tn(a+a^2)) + x = (diagm(0 => vcat(ones(Tn, a), Tn[-a], zeros(Tn, odim - a-1))) ⊗ H) / sqrt(Tn(a + a^2)) else x = Matrix{Tn}(I, idim * odim, idim * odim) / sqrt(Tn(idim * odim)) end @@ -116,7 +116,7 @@ function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:Abstrac end length(itr::ChannelBasisIterator) = itr.idim^2 * itr.odim^2 - itr.idim^2 + 1 -function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<: AbstractMatrix{<:Number} +function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<:AbstractMatrix{<:Number} J = convert(DynamicalMatrix{T2}, Φ) represent(basis, J.matrix) end diff --git a/test/channelbases.jl b/test/channelbases.jl deleted file mode 100644 index 8520745..0000000 --- a/test/channelbases.jl +++ /dev/null @@ -1,37 +0,0 @@ -@testset "MatrixBases" begin - -@testset "ChannelBasisIterator" begin - d1=2 - d2=2 - d=d1^2*d^2-d1^2+1 - m = collect(ChannelBasisIterator{Matrix{ComplexF64}}(d1,d2)) - @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) -end - -@testset "represent, combine" begin - d = 4 - A = reshape(collect(1:16), d, d) + reshape(collect(1:16), d, d)' - vA = represent(HermitianBasis{Matrix{ComplexF64}}(d), A) - Ap = combine(HermitianBasis{Matrix{ComplexF64}}(d), vA) - @test A ≈ Ap - B = A*A' - vB = represent(HermitianBasis{Matrix{ComplexF64}}(d), B) - Bp = combine(HermitianBasis{Matrix{ComplexF64}}(d), vB) - @test B ≈ Bp - - vB = represent(HermitianBasis{Matrix{ComplexF32}}(d), B) - @test eltype(vB) == Float32 - - C = Float16[1 2; 3 4] - C += C' - vC = represent(HermitianBasis, C) - @test eltype(vC) == eltype(C) - @test length(vC) == prod(size(C)) -end - -@testset "hermitainbasis" begin - @test hermitianbasis(Matrix{Float32}, 2) == HermitianBasisIterator{Matrix{Float32}}(2) - @test hermitianbasis(2) == HermitianBasisIterator{Matrix{ComplexF64}}(2) -end - -end \ No newline at end of file diff --git a/test/matrixbases.jl b/test/matrixbases.jl index 0329c89..cb8c070 100644 --- a/test/matrixbases.jl +++ b/test/matrixbases.jl @@ -6,6 +6,14 @@ @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) end +@testset "ChannelBasisIterator" begin + d1 = 2 + d2 = 2 + d = d1^2 * d2^2 - d1^2 + 1 + m = collect(ChannelBasisIterator{Matrix{ComplexF64}}(d1,d2)) + @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) +end + @testset "represent, combine" begin d = 4 A = reshape(collect(1:16), d, d) + reshape(collect(1:16), d, d)' @@ -27,9 +35,41 @@ end @test length(vC) == prod(size(C)) end +@testset "represent, combine" begin + d1 = 2 + d2 = 4 + A = reshape(collect(1:16), d1 * d2, d1) * reshape(collect(1:16), d1 * d2, d1)' + B = Matrix{Float64}(I, d2, d2) ⊗ (ptrace(A, [d2, d1], 1))^(-1/2) + A = B * A * B' + vA = represent(ChannelBasis{Matrix{ComplexF64}}(d1, d2), A) + Ap = combine(ChannelBasis{Matrix{ComplexF64}}(d1, d2), vA) + @test A ≈ Ap.matrix + + A = reshape(collect(1:64), d1 * d2, d1 * d2) * reshape(collect(1:64), d1 * d2, d1 * d2)' + Matrix{Float64}(I, d1 * d2, d1 * d2) + B = Matrix{Float64}(I, d1, d1) ⊗ (ptrace(A, [d1, d2], 1))^(-1/2) + B = B * A * B' + vB = represent(ChannelBasis{Matrix{ComplexF64}}(d2, d1), B) + Bp = combine(ChannelBasis{Matrix{ComplexF64}}(d2, d1), vB) + @test B ≈ Bp.matrix + + #vB = represent(ChannelBasis{Matrix{ComplexF32}}(d1,d2), B) + #@test eltype(vB) == Float32 + + # C = Float16[1 2; 3 4] + # C += C' + # vC = represent(ChannelBasis, C) + # @test eltype(vC) == eltype(C) + # @test length(vC) == prod(size(C)) +end + @testset "hermitainbasis" begin @test hermitianbasis(Matrix{Float32}, 2) == HermitianBasisIterator{Matrix{Float32}}(2) @test hermitianbasis(2) == HermitianBasisIterator{Matrix{ComplexF64}}(2) end +# @testset "channelbasis" begin +# @test channelbasis(Matrix{Float32}, 2,2) == ChannelBasisIterator{Matrix{Float32}}(2,2) +# @test channelbasis(2,2) == ChannelBasisIterator{Matrix{ComplexF64}}(2,2) +# end + end \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 8183d73..32b0a90 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,6 @@ using Test my_tests = ["utils.jl", "base.jl", "ptrace.jl", "ptranspose.jl", "reshuffle.jl", "channels.jl", "functionals.jl", "gates.jl", "matrixbases.jl", "permute_systems.jl", "randomqobjects.jl", "convex.jl"] - for my_test in my_tests include(my_test) end From b9d933d024530ecb1a92fc620a6f1fb9603d4b31 Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Wed, 2 Oct 2019 18:23:46 +0200 Subject: [PATCH 5/6] channelbasis corrected --- src/matrixbases.jl | 11 +++++------ test/matrixbases.jl | 44 +++++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/matrixbases.jl b/src/matrixbases.jl index 0c77080..702fbc2 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -64,7 +64,6 @@ end $(SIGNATURES) """ -abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} idim::Int odim::Int @@ -75,7 +74,7 @@ struct ChannelBasisIterator{T} <: AbstractMatrixBasisIterator{T} end abstract type AbstractChannelBasis{T} <: AbstractMatrixBasis{T} end -struct ChannelBasis{T} <: AbstractMatrixBasis{T} +struct ChannelBasis{T} <: AbstractChannelBasis{T} iterator::ChannelBasisIterator{T} function ChannelBasis{T}(idim::Integer, odim::Integer) where T<:AbstractMatrix{<:Number} new(ChannelBasisIterator{T}(idim, odim)) @@ -86,8 +85,7 @@ channelbasis(T::Type{<:AbstractMatrix{<:Number}}, idim::Int, odim::Int=idim) = C channelbasis(idim::Int, odim::Int=idim) = channelbasis(Matrix{ComplexF64}, idim, odim) function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:AbstractMatrix{<:Number} - idim = itr.idim - odim = itr.odim + (idim, odim) = (itr.idim, itr.odim) hitr = itr.hitr (a, c, b, d) = state (a == odim && c == odim && d == 2) && return nothing @@ -114,10 +112,11 @@ function iterate(itr::ChannelBasisIterator{T}, state=(1,1,1,1)) where T<:Abstrac end return x, newstate end + length(itr::ChannelBasisIterator) = itr.idim^2 * itr.odim^2 - itr.idim^2 + 1 -function represent(basis::ChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<:AbstractMatrix{<:Number} - J = convert(DynamicalMatrix{T2}, Φ) +function represent(basis::AbstractChannelBasis{T1}, Φ::AbstractQuantumOperation{T2}) where T1<:AbstractMatrix{<:Number} where T2<:AbstractMatrix{<:Number} + J = convert(DynamicalMatrix{T1}, Φ) represent(basis, J.matrix) end diff --git a/test/matrixbases.jl b/test/matrixbases.jl index cb8c070..7762eed 100644 --- a/test/matrixbases.jl +++ b/test/matrixbases.jl @@ -6,11 +6,11 @@ @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) end -@testset "ChannelBasisIterator" begin - d1 = 2 - d2 = 2 - d = d1^2 * d2^2 - d1^2 + 1 - m = collect(ChannelBasisIterator{Matrix{ComplexF64}}(d1,d2)) +@testset "ChannelBasisIteratorsquare" begin + idim = 2 + odim = idim + d = idim^2 * odim^2 - idim^2 + 1 + m = collect(ChannelBasisIterator{Matrix{ComplexF64}}(idim,odim)) @test [tr(m[i]' * m[j]) for i=1:d, j=1:d] ≈ Matrix{Float64}(I, d, d) end @@ -35,31 +35,25 @@ end @test length(vC) == prod(size(C)) end -@testset "represent, combine" begin - d1 = 2 - d2 = 4 - A = reshape(collect(1:16), d1 * d2, d1) * reshape(collect(1:16), d1 * d2, d1)' - B = Matrix{Float64}(I, d2, d2) ⊗ (ptrace(A, [d2, d1], 1))^(-1/2) +@testset "representchannel, combinechannel" begin + (idim, odim) = (2,4) + A = reshape(collect(1:16), idim * odim, idim) * reshape(collect(1:16), idim * odim, idim)' + B = Matrix{Float64}(I, odim, odim) ⊗ (ptrace(A, [odim, idim], 1))^(-1/2) A = B * A * B' - vA = represent(ChannelBasis{Matrix{ComplexF64}}(d1, d2), A) - Ap = combine(ChannelBasis{Matrix{ComplexF64}}(d1, d2), vA) + vA = represent(ChannelBasis{Matrix{ComplexF64}}(idim, odim), A) + Ap = combine(ChannelBasis{Matrix{ComplexF64}}(idim, odim), vA) @test A ≈ Ap.matrix - A = reshape(collect(1:64), d1 * d2, d1 * d2) * reshape(collect(1:64), d1 * d2, d1 * d2)' + Matrix{Float64}(I, d1 * d2, d1 * d2) - B = Matrix{Float64}(I, d1, d1) ⊗ (ptrace(A, [d1, d2], 1))^(-1/2) + A = reshape(collect(1:64), idim * odim, idim * odim) * reshape(collect(1:64), idim * odim, idim * odim)' + Matrix{Float64}(I, idim * odim, idim * odim) + B = Matrix{Float64}(I, idim, idim) ⊗ (ptrace(A, [idim, odim], 1))^(-1/2) B = B * A * B' - vB = represent(ChannelBasis{Matrix{ComplexF64}}(d2, d1), B) - Bp = combine(ChannelBasis{Matrix{ComplexF64}}(d2, d1), vB) + vB = represent(ChannelBasis{Matrix{ComplexF64}}(odim, idim), B) + Bp = combine(ChannelBasis{Matrix{ComplexF64}}(odim, idim), vB) @test B ≈ Bp.matrix - #vB = represent(ChannelBasis{Matrix{ComplexF32}}(d1,d2), B) - #@test eltype(vB) == Float32 + vB = represent(ChannelBasis{Matrix{ComplexF32}}(idim,odim), B) + @test eltype(vB) == Float64 - # C = Float16[1 2; 3 4] - # C += C' - # vC = represent(ChannelBasis, C) - # @test eltype(vC) == eltype(C) - # @test length(vC) == prod(size(C)) end @testset "hermitainbasis" begin @@ -67,9 +61,5 @@ end @test hermitianbasis(2) == HermitianBasisIterator{Matrix{ComplexF64}}(2) end -# @testset "channelbasis" begin -# @test channelbasis(Matrix{Float32}, 2,2) == ChannelBasisIterator{Matrix{Float32}}(2,2) -# @test channelbasis(2,2) == ChannelBasisIterator{Matrix{ComplexF64}}(2,2) -# end end \ No newline at end of file From b1de6333f6746c7be3f0c7c9e7b9fd2eda21dabd Mon Sep 17 00:00:00 2001 From: Paulina Lewandowska Date: Thu, 3 Oct 2019 14:46:02 +0200 Subject: [PATCH 6/6] answer to lukasz's objection --- src/matrixbases.jl | 2 +- test/matrixbases.jl | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/matrixbases.jl b/src/matrixbases.jl index 702fbc2..a3ebbdb 100644 --- a/src/matrixbases.jl +++ b/src/matrixbases.jl @@ -120,7 +120,7 @@ function represent(basis::AbstractChannelBasis{T1}, Φ::AbstractQuantumOperation represent(basis, J.matrix) end -function combine(basis::ChannelBasis{T}, v::Vector{<:Number}) where T<:AbstractMatrix +function combine(basis::AbstractChannelBasis{T}, v::Vector{<:Number}) where T<:AbstractMatrix m = sum(basis.iterator .* v) DynamicalMatrix{T}(m, basis.iterator.idim, basis.iterator.odim) end \ No newline at end of file diff --git a/test/matrixbases.jl b/test/matrixbases.jl index 7762eed..853b3be 100644 --- a/test/matrixbases.jl +++ b/test/matrixbases.jl @@ -40,18 +40,18 @@ end A = reshape(collect(1:16), idim * odim, idim) * reshape(collect(1:16), idim * odim, idim)' B = Matrix{Float64}(I, odim, odim) ⊗ (ptrace(A, [odim, idim], 1))^(-1/2) A = B * A * B' - vA = represent(ChannelBasis{Matrix{ComplexF64}}(idim, odim), A) - Ap = combine(ChannelBasis{Matrix{ComplexF64}}(idim, odim), vA) + vA = represent(channelbasis(Matrix{ComplexF64}, idim, odim), A) + Ap = combine(channelbasis(Matrix{ComplexF64}, idim, odim), vA) @test A ≈ Ap.matrix A = reshape(collect(1:64), idim * odim, idim * odim) * reshape(collect(1:64), idim * odim, idim * odim)' + Matrix{Float64}(I, idim * odim, idim * odim) B = Matrix{Float64}(I, idim, idim) ⊗ (ptrace(A, [idim, odim], 1))^(-1/2) B = B * A * B' - vB = represent(ChannelBasis{Matrix{ComplexF64}}(odim, idim), B) - Bp = combine(ChannelBasis{Matrix{ComplexF64}}(odim, idim), vB) + vB = represent(channelbasis(Matrix{ComplexF64}, odim, idim), B) + Bp = combine(channelbasis(Matrix{ComplexF64}, odim, idim), vB) @test B ≈ Bp.matrix - vB = represent(ChannelBasis{Matrix{ComplexF32}}(idim,odim), B) + vB = represent(channelbasis(Matrix{ComplexF32}, idim,odim), B) @test eltype(vB) == Float64 end @@ -61,5 +61,9 @@ end @test hermitianbasis(2) == HermitianBasisIterator{Matrix{ComplexF64}}(2) end +@testset "channelbasis" begin + @test channelbasis(Matrix{Float32}, 2) == ChannelBasis{Matrix{Float32}}(2,2) + @test channelbasis(2,3) == ChannelBasis{Matrix{ComplexF64}}(2,3) +end end \ No newline at end of file