Skip to content

Commit cea1d41

Browse files
committed
try to fix autodiff issue
1 parent 205d36f commit cea1d41

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/qobj/superoperators.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@ function _spost(B::AbstractSciMLOperator, Id::AbstractMatrix)
4040
end
4141

4242
## intrinsic liouvillian
43-
_liouvillian(H::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}} =
44-
-1im * (_spre(H, Id) - _spost(H', Id))
43+
function _liouvillian(H::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}}
44+
CType = _complex_float_type(H)
45+
CType(-1.0im) * _spre(H, Id) + CType(1.0im) * _spost(H', Id)
46+
end
4547
_liouvillian(H::MatrixOperator, Id::AbstractMatrix) = MatrixOperator(_liouvillian(H.A, Id))
46-
_liouvillian(H::ScaledOperator, Id::AbstractMatrix) =
47-
-1im * (ScaledOperator(H.λ, _spre(H.L, Id)) - ScaledOperator(conj(H.λ), _spost(H.L', Id)))
48+
function _liouvillian(H::ScaledOperator, Id::AbstractMatrix)
49+
CType = _complex_float_type(H)
50+
CType(-1.0im) * ScaledOperator(H.λ, _spre(H.L, Id)) + CType(1.0im) * ScaledOperator(conj(H.λ), _spost(H.L', Id))
51+
end
4852
_liouvillian(H::AddedOperator, Id::AbstractMatrix) = AddedOperator(map(op -> _liouvillian(op, Id), H.ops))
4953

5054
# intrinsic lindblad_dissipator
5155
function _lindblad_dissipator(O::MT, Id::AbstractMatrix) where {MT<:Union{AbstractMatrix,AbstractSciMLOperator}}
5256
Od_O = O' * O
53-
return _sprepost(O, O') - (_spre(Od_O, Id) + _spost(Od_O, Id)) / 2
57+
return _sprepost(O, O') + _complex_float_type(H)(-0.5 + 0.0im) * (_spre(Od_O, Id) + _spost(Od_O, Id))
5458
end
5559
function _lindblad_dissipator(O::MatrixOperator, Id::AbstractMatrix)
5660
_O = O.A

src/utilities.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ end
175175

176176
# functions for getting Float or Complex element type
177177
_float_type(::AbstractArray{T}) where {T<:Number} = _float_type(T)
178+
_float_type(::AbstractSciMLOperator{T}) where {T<:Number} = _complex_float_type(T)
178179
_float_type(::Type{Int32}) = Float32
179180
_float_type(::Type{Int64}) = Float64
180181
_float_type(::Type{Float32}) = Float32
@@ -186,6 +187,7 @@ _float_type(::Type{Complex{Float64}}) = Float64
186187
_float_type(::Type{Complex{T}}) where {T<:Real} = T
187188
_float_type(T::Type{<:Real}) = T # Allow other untracked Real types, like ForwardDiff.Dual
188189
_complex_float_type(::AbstractArray{T}) where {T<:Number} = _complex_float_type(T)
190+
_complex_float_type(::AbstractSciMLOperator{T}) where {T<:Number} = _complex_float_type(T)
189191
_complex_float_type(::Type{Int32}) = ComplexF32
190192
_complex_float_type(::Type{Int64}) = ComplexF64
191193
_complex_float_type(::Type{Float32}) = ComplexF32

0 commit comments

Comments
 (0)