Skip to content

Commit

Permalink
Add super_to_choi and choi_to_super
Browse files Browse the repository at this point in the history
  • Loading branch information
akirakyle committed Mar 11, 2024
1 parent bb71f52 commit 7e848f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/QuantumOpticsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export Basis, GenericBasis, CompositeBasis, basis,
SuperOperator, DenseSuperOperator, DenseSuperOpType,
SparseSuperOperator, SparseSuperOpType, spre, spost, sprepost, liouvillian,
identitysuperoperator,
super_to_choi, choi_to_super,
#fock
FockBasis, number, destroy, create,
fockstate, coherentstate, coherentstate!,
Expand Down
36 changes: 36 additions & 0 deletions src/superoperators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,39 @@ end
}
throw(IncompatibleBases())
end

# Note the similarity to permutesystems in operators_dense.jl
function super_choi_helper(data::Matrix, (l1, l2), (r1, r2))

Check warning on line 321 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L321

Added line #L321 was not covered by tests
# the reshape swaps within systems due to colum major ordering
# https://docs.qojulia.org/quantumobjects/operators/#tensor_order
# https://forest-benchmarking.readthedocs.io/en/latest/superoperator_representations.html
data = reshape(data, map(length, (l2, l1, r2, r1)))
(l1, l2), (r1, r2) = (r2, l2), (r1, l1)
data = permutedims(data, (1, 3, 2, 4))
data = reshape(data, map(length, (l1l2, r1r2)))
return data, (l1, l2), (r1, r2)

Check warning on line 329 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L325-L329

Added lines #L325 - L329 were not covered by tests
end

function super_choi_helper(data::SparseMatrixCSC, (r2, l2), (r1, l1))
data = _permutedims(data, map(length, (l2, r2, l1, r1)), (1, 3, 2, 4))
data = reshape(data, map(length, (l1l2, r1r2)))

Check warning on line 334 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L332-L334

Added lines #L332 - L334 were not covered by tests
# sparse(data) is necessary since reshape of a sparse array returns a
# ReshapedSparseArray which is not a subtype of AbstractArray and so
# _permutedims fails to acces the ".m" field
# https://github.com/qojulia/QuantumOpticsBase.jl/pull/83
# https://github.com/JuliaSparse/SparseArrays.jl/issues/24
# permutedims in SparseArrays.jl only implements perm (2,1) and so
# _permutedims should be upstreamed
# https://github.com/JuliaLang/julia/issues/26534
return sparse(data), (l1, l2), (r1, r2)

Check warning on line 343 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L343

Added line #L343 was not covered by tests
end

function super_to_choi(op::SuperOperator)::Operator
data, basis_l, basis_r = super_choi_helper(op.data, op.basis_l, op.basis_r)
return Operator(tensor(basis_l...), tensor(basis_r...), data)

Check warning on line 348 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L346-L348

Added lines #L346 - L348 were not covered by tests
end

function choi_to_super(op::Operator)::SuperOperator
data, basis_l, basis_r = super_choi_helper(op.data, op.basis_l.bases, op.basis_r.bases)
SuperOperator(basis_l, basis_r, data)

Check warning on line 353 in src/superoperators.jl

View check run for this annotation

Codecov / codecov/patch

src/superoperators.jl#L351-L353

Added lines #L351 - L353 were not covered by tests
end

0 comments on commit 7e848f4

Please sign in to comment.