Skip to content

Commit b6008e0

Browse files
ytdHuangalbertomercurio
authored andcommitted
minor changes
1 parent d63eb5f commit b6008e0

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

src/qobj/superoperators.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ function _sprepost(A, B) # for any other input types
1919
return _spre(A, Id_cache) * _spost(B, Id_cache)
2020
end
2121

22-
_liouvillian(H::AbstractMatrix, Id::AbstractMatrix) = -1im * (_spre(H, Id) - _spost(H, Id))
23-
function _lindblad_dissipator(O::AbstractMatrix, Id::AbstractMatrix)
24-
Od_O = O' * O
25-
return _sprepost(O, O') - (_spre(Od_O, Id) + _spost(Od_O, Id)) / 2
26-
end
27-
28-
## if input is AbstractSciMLOperator
29-
_lazy_tensor_warning(func_name::String, data::AbstractSciMLOperator) =
30-
@warn "The function `$func_name` uses lazy tensor (which can hurt performance) for data type: $(get_typename_wrapper(data))"
22+
## if input is AbstractSciMLOperator
23+
## some of them are optimzed to speed things up
24+
## the rest of the SciMLOperators will just use lazy tensor (and prompt a warning)
3125
_spre(A::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_spre(A.A, Id))
3226
_spre(A::ScaledOperator, Id::AbstractMatrix) = ScaledOperator(A.λ, _spre(A.L, Id))
3327
_spre(A::AddedOperator, Id::AbstractMatrix) = mapreduce(op -> _spre(op, Id), +, A.ops)
@@ -44,10 +38,18 @@ function _spost(B::AbstractSciMLOperator, Id::AbstractMatrix)
4438
return kron(transpose(B), Id)
4539
end
4640

41+
## intrinsic liouvillian
42+
_liouvillian(H::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}} =
43+
-1im * (_spre(H, Id) - _spost(H, Id))
4744
_liouvillian(H::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_liouvillian(H.A, Id))
4845
_liouvillian(H::ScaledOperator, Id::AbstractMatrix) = ScaledOperator(H.λ, _liouvillian(H.L, Id))
4946
_liouvillian(H::AddedOperator, Id::AbstractMatrix) = mapreduce(op -> _liouvillian(op, Id), +, H.ops)
50-
_liouvillian(H::AbstractSciMLOperator, Id::AbstractMatrix) = -1im * (_spre(H, Id) - _spost(H, Id))
47+
48+
# intrinsic lindblad_dissipator
49+
function _lindblad_dissipator(O::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}}
50+
Od_O = O' * O
51+
return _sprepost(O, O') - (_spre(Od_O, Id) + _spost(Od_O, Id)) / 2
52+
end
5153
function _lindblad_dissipator(O::MatrixOperator, Id::AbstractMatrix)
5254
_O = O.A
5355
Od_O = _O' * _O
@@ -57,10 +59,6 @@ function _lindblad_dissipator(O::ScaledOperator, Id::AbstractMatrix)
5759
λc_λ = conj(O.λ) * O.λ
5860
return ScaledOperator(λc_λ, _lindblad_dissipator(O.L, Id))
5961
end
60-
function _lindblad_dissipator(O::AbstractSciMLOperator, Id::AbstractMatrix)
61-
Od_O = O' * O
62-
return _sprepost(O, O') - (_spre(Od_O, Id) + _spost(Od_O, Id)) / 2
63-
end
6462

6563
@doc raw"""
6664
spre(A::AbstractQuantumObject, Id_cache=I(size(A,1)))

src/utilities.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ _non_static_array_warning(argname, arg::AbstractVector{T}) where {T} =
153153
join(arg, ", ") *
154154
")` instead of `$argname = $arg`." maxlog = 1
155155

156+
_lazy_tensor_warning(func_name::String, data::AbstractSciMLOperator) =
157+
@warn "The function `$func_name` uses lazy tensor (which can hurt performance) for data type: $(get_typename_wrapper(data))"
158+
156159
# functions for getting Float or Complex element type
157160
_FType(::AbstractArray{T}) where {T<:Number} = _FType(T)
158161
_FType(::Type{Int32}) = Float32

test/core-test/quantum_objects_evo.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,18 @@
181181
@test isoper(H_td) == true
182182

183183
# SuperOperator
184+
X = a * a'
184185
c_op1 = QobjEvo(((a', coef1),))
185-
c_op2 = QobjEvo(((a, coef2), (a * a', coef3)))
186+
c_op2 = QobjEvo(((a, coef2), (X, coef3)))
186187
c_ops = [c_op1, c_op2]
187188
D1_ti = abs2(coef1(p, t)) * lindblad_dissipator(a')
188-
D2_ti = @test_logs (:warn,) (:warn,) lindblad_dissipator(c_op2)(p, t)
189+
D2_ti =
190+
abs2(coef2(p, t)) * lindblad_dissipator(a) + # normal dissipator for first element in c_op2
191+
abs2(coef3(p, t)) * lindblad_dissipator(X) + # normal dissipator for second element in c_op2
192+
coef2(p, t) * conj(coef3(p, t)) * (spre(a) * spost(X') - 0.5 * spre(X' * a) - 0.5 * spost(X' * a)) + # cross terms
193+
conj(coef2(p, t)) * coef3(p, t) * (spre(X) * spost(a') - 0.5 * spre(a' * X) - 0.5 * spost(a' * X)) # cross terms
189194
L_ti = liouvillian(H_ti) + D1_ti + D2_ti
190-
L_td = @test_logs (:warn,) (:warn,) liouvillian(H_td, c_ops)
195+
L_td = @test_logs (:warn,) (:warn,) liouvillian(H_td, c_ops) # warnings from lazy tensor in `lindblad_dissipator(c_op2)`
191196
ρvec = mat2vec(rand_dm(N))
192197
@test L_td(p, t) L_ti
193198
# TODO: L_td here is ComposedOperator and need to setup cache first for the following test
@@ -199,6 +204,7 @@
199204
@test isconstant(L_td) == false
200205
@test issuper(L_td) == true
201206

207+
@test_logs (:warn,) (:warn,) liouvillian(H_td * H_td) # warnings from lazy tensor
202208
@test_throws MethodError QobjEvo([[a, coef1], a' * a, [a', coef2]])
203209
@test_throws ArgumentError H_td(ρvec, p, t)
204210
@test_throws ArgumentError L_td(ψ, p, t)

test/core-test/time_evolution.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
# @test sol_sse.expect ≈ sol_sse_td2.expect atol = 1e-2 * length(tlist)
230230

231231
@testset "Type Inference mesolve" begin
232+
coef(p, t) = exp(-t)
232233
@inferred mesolveProblem(H, ψ0, tlist, c_ops, e_ops = e_ops, progress_bar = Val(false))
233234
@inferred mesolveProblem(H, ψ0, [0, 10], c_ops, e_ops = e_ops, progress_bar = Val(false))
234235
@inferred mesolveProblem(
@@ -242,7 +243,7 @@
242243
@inferred mesolve(H, ψ0, tlist, c_ops, e_ops = e_ops, progress_bar = Val(false))
243244
@inferred mesolve(H, ψ0, tlist, c_ops, progress_bar = Val(false))
244245
@inferred mesolve(H, ψ0, tlist, c_ops, e_ops = e_ops, saveat = tlist, progress_bar = Val(false))
245-
@inferred mesolve(H, ψ0, tlist, (a, a'), e_ops = (a' * a, a'), progress_bar = Val(false)) # We test the type inference for Tuple
246+
@inferred mesolve(H, ψ0, tlist, (a, QobjEvo(((a', coef),))), e_ops = (a' * a, a'), progress_bar = Val(false)) # We test the type inference for Tuple
246247
@inferred mesolve(H_td, ψ0, tlist, c_ops, e_ops = e_ops, progress_bar = Val(false), params = p)
247248
@inferred mesolve(H_td2, ψ0, tlist, c_ops, e_ops = e_ops, progress_bar = Val(false), params = p)
248249
@inferred mesolve(L_td, ψ0, tlist, c_ops, e_ops = e_ops, progress_bar = Val(false), params = p)

0 commit comments

Comments
 (0)