Skip to content

Commit

Permalink
Don't tuplify if types are homogenous.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Milsted committed Jul 4, 2024
1 parent 1b634ef commit 0f68f17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/time_dependent_operators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
# Convert storage of heterogeneous stuff to tuples for maximal compilation
# and to avoid runtime dispatch.
_tuplify(o::TimeDependentSum) = TimeDependentSum(Tuple, o)
_tuplify(o::LazySum) = LazySum(eltype(o.factors), o.factors, (o.operators...,))
function _tuplify(o::TimeDependentSum)
if isconcretetype(eltype(o.coefficients)) && isconcretetype(eltype(o.static_op.operators))
# No need to tuplify is types are concrete.
# We will save on compile time this way.
return o
end
return TimeDependentSum(Tuple, o)
end
function _tuplify(o::LazySum)
if isconcretetype(eltype(o.factors)) && isconcretetype(eltype(o.operators))
return o
end
return LazySum(eltype(o.factors), o.factors, (o.operators...,))
end
_tuplify(o::AbstractOperator) = o

"""
Expand All @@ -23,6 +35,7 @@ function _tdopdagger(o::TimeDependentSum)
# that requires that the original operator sticks around and is always
# updated first (though this is checked).
# Copies and conjugates the coefficients from the original op.
# TODO: Make an Adjoint wrapper for TimeDependentSum instead?
o_ls = QuantumOpticsBase.static_operator(o)
facs = o_ls.factors
c1 = (t)->(@assert current_time(o) == t; conj(facs[1]))
Expand Down
14 changes: 14 additions & 0 deletions test/test_timeevolution_tdops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ a = destroy(b)

H0 = number(b)
Hd = (a + a')

# function and op types homogeneous
H = TimeDependentSum(cos=>H0, cos=>Hd)
@test timeevolution._tuplify(H) === H

# op types not homogeneous
H = TimeDependentSum(cos=>H0, cos=>dense(Hd))
@test timeevolution._tuplify(H) !== H

H = TimeDependentSum(1.0=>H0, cos=>Hd)

# function types not homogeneous
@test timeevolution._tuplify(H) !== H

ts = [0.0, 0.4]
ts_half = 0.5 * ts

Expand Down Expand Up @@ -54,6 +66,8 @@ ts_out, rhos = timeevolution.master_dynamic(ts, psi0, H, Js)
ts_out2, rhos2 = timeevolution.master_dynamic(ts, psi0, fman)
@test rhos[end].data rhos2[end].data

set_time!(H, 0.0)
set_time!.(Js, 0.0)
Hnh = H - 0.5im * sum(J' * J for J in Js)

_getf = (H0, Hd, a) -> (t,_) -> (
Expand Down

0 comments on commit 0f68f17

Please sign in to comment.