Skip to content

Commit

Permalink
ElementaryBasis iterator added SuperOperator fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Gawron committed Aug 20, 2019
1 parent 23387c2 commit 83df362
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,10 @@ $(SIGNATURES)
Transforms quntum channel into super-operator matrix.
"""
function SuperOperator{T}(channel::Function, idim::Int, odim::Int) where T<:AbstractMatrix{<:Number}
error("Broken")
odim > 0 ? () : error("Channel dimension has to be nonnegative") # TODO: fix
odim > 0 && idim > 0 ? () : error("Channel dimension has to be nonnegative")

m = zeros(T, idim^2, odim^2)
for (i, e) in enumerate(base_matrices(idim)) # TODO : base_matrices should be not only square
m = zeros(eltype(T), idim^2, odim^2)
for (i, e) in enumerate(ElementaryBasisIterator{Matrix{Int}}(idim, odim))
m[:, i] = res(channel(e))
end
SuperOperator(m, idim, odim)
Expand Down
35 changes: 33 additions & 2 deletions src/matrixbases.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import .Base: length, iterate
export AbstractMatrixBasisIterator, HermitianBasisIterator, AbstractBasis,
AbstractMatrixBasis, HermitianBasis, hermitianbasis, represent, combine
export AbstractMatrixBasisIterator, HermitianBasisIterator, ElementaryBasisIterator, AbstractBasis,
AbstractMatrixBasis, HermitianBasis, ElementaryBasis, hermitianbasis,
represent, combine

abstract type AbstractMatrixBasisIterator{T<:AbstractMatrix} end
struct HermitianBasisIterator{T} <: AbstractMatrixBasisIterator{T}
dim::Int
end

struct ElementaryBasisIterator{T} <: AbstractMatrixBasisIterator{T}
idim::Int
odim::Int
end

abstract type AbstractBasis end
abstract type AbstractMatrixBasis{T} <: AbstractBasis where T<:AbstractMatrix{<:Number} end

Expand All @@ -17,6 +24,18 @@ struct HermitianBasis{T} <: AbstractMatrixBasis{T}
end
end

struct ElementaryBasis{T} <: AbstractMatrixBasis{T}
iterator::ElementaryBasisIterator{T}

function ElementaryBasis{T}(idim::Integer, odim::Integer) where T<:AbstractMatrix{<:Number}
new(ElementaryBasisIterator{T}(idim, odim))
end
end

function ElementaryBasis{T}(dim::Integer) where T<:AbstractMatrix{<:Number}
ElementaryBasisIterator{T}(dim, dim)
end

"""
$(SIGNATURES)
- `dim`: dimensions of the matrix.
Expand Down Expand Up @@ -46,6 +65,18 @@ end

length(itr::HermitianBasisIterator) = itr.dim^2

function iterate(itr::ElementaryBasisIterator{T}, state=(1,1)) where T<:AbstractMatrix{<:Number}
idim, odim = itr.idim, itr.odim
(a, b) = state
a > idim && return nothing

x = zeros(eltype(T), odim, idim)
x[b, a] = one(eltype(T))
return x, b == odim ? (a+1, 1) : (a, b+1)
end

length(itr::ElementaryBasisIterator) = itr.idim * itr.odim

function represent(basis::T, m::Matrix{<:Number}) where T<:AbstractMatrixBasis
real.(tr.([m] .* basis.iterator))
end
Expand Down
4 changes: 2 additions & 2 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ end
@testset "construction from function" begin
ρ = [0.25 0.25im; -0.25im 0.75]
t = hcat([ComplexF64[0.25, 0.25im, -0.25im, 0.75] for i=1:4]...) #stack res ρ
@test_throws ErrorException m = SuperOperator{Matrix{ComplexF64}}(x -> ρ, 2, 2).matrix
@test_broken norm(t-m) 0. atol=1e-15
m = SuperOperator{Matrix{ComplexF64}}(x -> ρ, 2, 2).matrix
@test norm(t-m) 0. atol=1e-15
end

@testset "convert to KrausOperators" begin
Expand Down

0 comments on commit 83df362

Please sign in to comment.