|
316 | 316 | } |
317 | 317 | throw(IncompatibleBases()) |
318 | 318 | 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, (l1⊗l2, r1⊗r2))) |
| 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, (l1⊗l2, r1⊗r2))) |
| 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