Skip to content

Commit 7e848f4

Browse files
committed
Add super_to_choi and choi_to_super
1 parent bb71f52 commit 7e848f4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/QuantumOpticsBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export Basis, GenericBasis, CompositeBasis, basis,
3737
SuperOperator, DenseSuperOperator, DenseSuperOpType,
3838
SparseSuperOperator, SparseSuperOpType, spre, spost, sprepost, liouvillian,
3939
identitysuperoperator,
40+
super_to_choi, choi_to_super,
4041
#fock
4142
FockBasis, number, destroy, create,
4243
fockstate, coherentstate, coherentstate!,

src/superoperators.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,39 @@ end
316316
}
317317
throw(IncompatibleBases())
318318
end
319+
320+
# Note the similarity to permutesystems in operators_dense.jl
321+
function super_choi_helper(data::Matrix, (l1, l2), (r1, r2))
322+
# the reshape swaps within systems due to colum major ordering
323+
# https://docs.qojulia.org/quantumobjects/operators/#tensor_order
324+
# https://forest-benchmarking.readthedocs.io/en/latest/superoperator_representations.html
325+
data = reshape(data, map(length, (l2, l1, r2, r1)))
326+
(l1, l2), (r1, r2) = (r2, l2), (r1, l1)
327+
data = permutedims(data, (1, 3, 2, 4))
328+
data = reshape(data, map(length, (l1l2, r1r2)))
329+
return data, (l1, l2), (r1, r2)
330+
end
331+
332+
function super_choi_helper(data::SparseMatrixCSC, (r2, l2), (r1, l1))
333+
data = _permutedims(data, map(length, (l2, r2, l1, r1)), (1, 3, 2, 4))
334+
data = reshape(data, map(length, (l1l2, r1r2)))
335+
# sparse(data) is necessary since reshape of a sparse array returns a
336+
# ReshapedSparseArray which is not a subtype of AbstractArray and so
337+
# _permutedims fails to acces the ".m" field
338+
# https://github.com/qojulia/QuantumOpticsBase.jl/pull/83
339+
# https://github.com/JuliaSparse/SparseArrays.jl/issues/24
340+
# permutedims in SparseArrays.jl only implements perm (2,1) and so
341+
# _permutedims should be upstreamed
342+
# https://github.com/JuliaLang/julia/issues/26534
343+
return sparse(data), (l1, l2), (r1, r2)
344+
end
345+
346+
function super_to_choi(op::SuperOperator)::Operator
347+
data, basis_l, basis_r = super_choi_helper(op.data, op.basis_l, op.basis_r)
348+
return Operator(tensor(basis_l...), tensor(basis_r...), data)
349+
end
350+
351+
function choi_to_super(op::Operator)::SuperOperator
352+
data, basis_l, basis_r = super_choi_helper(op.data, op.basis_l.bases, op.basis_r.bases)
353+
SuperOperator(basis_l, basis_r, data)
354+
end

0 commit comments

Comments
 (0)