Skip to content

Commit 9a7beab

Browse files
committed
fix type instability and reduce memory alloc in liouvillian
1 parent 2f89760 commit 9a7beab

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/qobj/superoperators.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ end
2424
## the rest of the SciMLOperators will just use lazy tensor (and prompt a warning)
2525
_spre(A::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_spre(A.A, Id))
2626
_spre(A::ScaledOperator, Id::AbstractMatrix) = ScaledOperator(A.λ, _spre(A.L, Id))
27-
_spre(A::AddedOperator, Id::AbstractMatrix) = mapreduce(op -> _spre(op, Id), +, A.ops)
27+
_spre(A::AddedOperator, Id::AbstractMatrix) = AddedOperator(map(op -> _spre(op, Id), A.ops))
2828
function _spre(A::AbstractSciMLOperator, Id::AbstractMatrix)
2929
_lazy_tensor_warning("spre", A)
3030
return kron(Id, A)
3131
end
3232

3333
_spost(B::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_spost(B.A, Id))
3434
_spost(B::ScaledOperator, Id::AbstractMatrix) = ScaledOperator(B.λ, _spost(B.L, Id))
35-
_spost(B::AddedOperator, Id::AbstractMatrix) = mapreduce(op -> _spost(op, Id), +, B.ops)
35+
_spost(B::AddedOperator, Id::AbstractMatrix) = AddedOperator(map(op -> _spost(op, Id), B.ops))
3636
function _spost(B::AbstractSciMLOperator, Id::AbstractMatrix)
3737
_lazy_tensor_warning("spost", B)
3838
return kron(transpose(B), Id)
@@ -43,7 +43,7 @@ _liouvillian(H::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,Abstract
4343
-1im * (_spre(H, Id) - _spost(H, Id))
4444
_liouvillian(H::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_liouvillian(H.A, Id))
4545
_liouvillian(H::ScaledOperator, Id::AbstractMatrix) = ScaledOperator(H.λ, _liouvillian(H.L, Id))
46-
_liouvillian(H::AddedOperator, Id::AbstractMatrix) = mapreduce(op -> _liouvillian(op, Id), +, H.ops)
46+
_liouvillian(H::AddedOperator, Id::AbstractMatrix) = AddedOperator(map(op -> _liouvillian(op, Id), H.ops))
4747

4848
# intrinsic lindblad_dissipator
4949
function _lindblad_dissipator(O::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}}
@@ -166,16 +166,18 @@ function liouvillian(
166166
) where {DT,OpType<:Union{OperatorQuantumObject,SuperOperatorQuantumObject}}
167167
L = liouvillian(H, Id_cache)
168168
if !(c_ops isa Nothing)
169-
# sum all the Qobj first
170-
c_ops_ti = filter(op -> isa(op, QuantumObject), c_ops)
169+
c_ops_tuple = Tuple(c_ops) # we must convert it to a tuple, so filter below also return tuple
170+
171+
# sum all the (time-independent) c_ops first
172+
c_ops_ti = filter(op -> isa(op, QuantumObject), c_ops_tuple)
171173
if !isempty(c_ops_ti)
172174
L += mapreduce(op -> lindblad_dissipator(op, Id_cache), +, c_ops_ti)
173175
end
174176

175177
# sum rest of the QobjEvo together
176-
c_ops_td = filter(op -> isa(op, QuantumObjectEvolution), c_ops)
178+
c_ops_td = filter(op -> isa(op, QuantumObjectEvolution), c_ops_tuple)
177179
if !isempty(c_ops_td)
178-
L += mapreduce(op -> lindblad_dissipator(op, Id_cache), +, c_ops_td)
180+
L += AddedOperator(map(op -> lindblad_dissipator(op, Id_cache), c_ops_td)) # it becomes AddedOperator at the end anyway
179181
end
180182
end
181183
return L

test/core-test/quantum_objects_evo.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,11 @@
214214

215215
@testset "Type Inference" begin
216216
H_td2 = H_td + QobjEvo(a + a', coef3)
217-
c_ops1 = (a, a')
218-
c_ops2 = (a, QobjEvo(a', coef1))
217+
218+
# we use destroy and create here because it somehow might cause type instability
219+
c_ops1 = (destroy(N), create(N))
220+
c_ops2 = (destroy(N), QobjEvo(create(N), coef1))
221+
219222
@inferred liouvillian(H_td, c_ops1)
220223
@inferred liouvillian(H_td, c_ops2)
221224
@inferred liouvillian(H_td2, c_ops1)

0 commit comments

Comments
 (0)