@@ -6,17 +6,17 @@ _mesolve_make_L_QobjEvo(H::Union{QuantumObjectEvolution,Tuple}, c_ops) = liouvil
66_mesolve_make_L_QobjEvo (H:: Nothing , c_ops:: Nothing ) = throw (ArgumentError (" Both H and
77c_ops are Nothing. You are probably running the wrong function." ))
88
9- function _gen_mesolve_solution (sol, times, dimensions, isoperket :: Val )
10- if getVal (isoperket )
11- ρt = map (ϕ -> QuantumObject (ϕ , type = OperatorKet (), dims = dimensions), sol. u)
9+ function _gen_mesolve_solution (sol, prob :: TimeEvolutionProblem{X} ) where {X <: Union{Operator,OperatorKet,SuperOperator} }
10+ if X () == Operator ( )
11+ ρt = map (ϕ -> QuantumObject (vec2mat (ϕ) , type = X (), dims = prob . dimensions), sol. u)
1212 else
13- ρt = map (ϕ -> QuantumObject (vec2mat (ϕ) , type = Operator (), dims = dimensions), sol. u)
13+ ρt = map (ϕ -> QuantumObject (ϕ , type = X (), dims = prob . dimensions), sol. u)
1414 end
1515
1616 kwargs = NamedTuple (sol. prob. kwargs) # Convert to NamedTuple for Zygote.jl compatibility
1717
1818 return TimeEvolutionSol (
19- times,
19+ prob . times,
2020 sol. t,
2121 ρt,
2222 _get_expvals (sol, SaveFuncMESolve),
@@ -86,8 +86,8 @@ function mesolveProblem(
8686 progress_bar:: Union{Val,Bool} = Val (true ),
8787 inplace:: Union{Val,Bool} = Val (true ),
8888 kwargs... ,
89- ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet} }
90- (isoper (H) && isket (ψ0) && isnothing (c_ops)) && return sesolveProblem (
89+ ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet,SuperOperator } }
90+ (isoper (H) && ( isket (ψ0) || isoper (ψ0) ) && isnothing (c_ops)) && return sesolveProblem (
9191 H,
9292 ψ0,
9393 tlist;
@@ -107,11 +107,27 @@ function mesolveProblem(
107107 check_dimensions (L_evo, ψ0)
108108
109109 T = Base. promote_eltype (L_evo, ψ0)
110- ρ0 = if isoperket (ψ0) # Convert it to dense vector with complex element type
111- to_dense (_complex_float_type (T), copy (ψ0. data))
110+ # ρ0 = if isoperket(ψ0) # Convert it to dense vector with complex element type
111+ # to_dense(_complex_float_type(T), copy(ψ0.data))
112+ # else
113+ # to_dense(_complex_float_type(T), mat2vec(ket2dm(ψ0).data))
114+ # end
115+ if isoper (ψ0)
116+ ρ0 = to_dense (_complex_float_type (T), mat2vec (ψ0. data))
117+ state_type = Operator ()
118+ elseif isoperket (ψ0)
119+ ρ0 = to_dense (_complex_float_type (T), copy (ψ0. data))
120+ state_type = OperatorKet ()
121+ elseif isket (ψ0)
122+ ρ0 = to_dense (_complex_float_type (T), mat2vec (ket2dm (ψ0). data))
123+ state_type = Operator ()
124+ elseif issuper (ψ0)
125+ ρ0 = to_dense (_complex_float_type (T), copy (ψ0. data))
126+ state_type = SuperOperator ()
112127 else
113- to_dense ( _complex_float_type (T), mat2vec ( ket2dm (ψ0) . data ))
128+ throw ( ArgumentError ( " Unsupported state type for ψ0 in mesolveProblem. " ))
114129 end
130+
115131 L = cache_operator (L_evo. data, ρ0)
116132
117133 kwargs2 = _merge_saveat (tlist, e_ops, DEFAULT_ODE_SOLVER_OPTIONS; kwargs... )
@@ -122,7 +138,7 @@ function mesolveProblem(
122138
123139 prob = ODEProblem {getVal(inplace),FullSpecialize} (L, ρ0, tspan, params; kwargs4... )
124140
125- return TimeEvolutionProblem (prob, tlist, L_evo. dimensions, (isoperket = Val (isoperket (ψ0)),))
141+ return TimeEvolutionProblem (prob, tlist, state_type, L_evo. dimensions) # , (isoperket = Val(isoperket(ψ0)),))
126142end
127143
128144@doc raw """
@@ -188,8 +204,8 @@ function mesolve(
188204 progress_bar:: Union{Val,Bool} = Val (true ),
189205 inplace:: Union{Val,Bool} = Val (true ),
190206 kwargs... ,
191- ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet} }
192- (isoper (H) && isket (ψ0) && isnothing (c_ops)) && return sesolve (
207+ ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet,SuperOperator } }
208+ (isoper (H) && ( isket (ψ0) || isoper (ψ0) ) && isnothing (c_ops)) && return sesolve (
193209 H,
194210 ψ0,
195211 tlist;
230246function mesolve (prob:: TimeEvolutionProblem , alg:: AbstractODEAlgorithm = DP5 (); kwargs... )
231247 sol = solve (prob. prob, alg; kwargs... )
232248
233- return _gen_mesolve_solution (sol, prob. times, prob . dimensions , prob. kwargs. isoperket)
249+ return _gen_mesolve_solution (sol, prob) # , prob.kwargs.isoperket)
234250end
235251
236252@doc raw """
@@ -298,8 +314,8 @@ function mesolve_map(
298314 params:: Union{NullParameters,Tuple} = NullParameters (),
299315 progress_bar:: Union{Val,Bool} = Val (true ),
300316 kwargs... ,
301- ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet} }
302- (isoper (H) && all (isket, ψ0) && isnothing (c_ops)) && return sesolve_map (
317+ ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet,SuperOperator } }
318+ (isoper (H) && ( all (isket, ψ0) || all (isoper, ψ0) ) && isnothing (c_ops)) && return sesolve_map (
303319 H,
304320 ψ0,
305321 tlist;
@@ -315,10 +331,16 @@ function mesolve_map(
315331 # Convert to appropriate format based on state type
316332 ψ0_iter = map (ψ0) do state
317333 T = _complex_float_type (eltype (state))
318- if isoperket (state)
319- to_dense (T, copy (state. data))
334+ if isoper (state)
335+ to_dense (_complex_float_type (T), mat2vec (state. data))
336+ elseif isoperket (state)
337+ to_dense (_complex_float_type (T), copy (state. data))
338+ elseif isket (state)
339+ to_dense (_complex_float_type (T), mat2vec (ket2dm (state). data))
340+ elseif issuper (state)
341+ to_dense (_complex_float_type (T), copy (state. data))
320342 else
321- to_dense (T, mat2vec ( ket2dm ( state) . data ))
343+ throw ( ArgumentError ( " Unsupported state type for ψ0 in mesolveProblem. " ))
322344 end
323345 end
324346 if params isa NullParameters
@@ -347,7 +369,7 @@ mesolve_map(
347369 tlist:: AbstractVector ,
348370 c_ops:: Union{Nothing,AbstractVector,Tuple} = nothing ;
349371 kwargs... ,
350- ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet} } =
372+ ) where {HOpType<: Union{Operator,SuperOperator} ,StateOpType<: Union{Ket,Operator,OperatorKet,SuperOperator } } =
351373 mesolve_map (H, [ψ0], tlist, c_ops; kwargs... )
352374
353375# this method is for advanced usage
@@ -357,14 +379,14 @@ mesolve_map(
357379#
358380# Return: An array of TimeEvolutionSol objects with the size same as the given iter.
359381function mesolve_map (
360- prob:: TimeEvolutionProblem{<:ODEProblem} ,
382+ prob:: TimeEvolutionProblem{StateOpType, <:ODEProblem} ,
361383 iter:: AbstractArray ,
362384 alg:: AbstractODEAlgorithm = DP5 (),
363385 ensemblealg:: EnsembleAlgorithm = EnsembleThreads ();
364386 prob_func:: Union{Function,Nothing} = nothing ,
365387 output_func:: Union{Tuple,Nothing} = nothing ,
366388 progress_bar:: Union{Val,Bool} = Val (true ),
367- )
389+ ) where {StateOpType <: Union{Ket,Operator,OperatorKet,SuperOperator} }
368390 # generate ensemble problem
369391 ntraj = length (iter)
370392 _prob_func = isnothing (prob_func) ? (prob, i, repeat) -> _se_me_map_prob_func (prob, i, repeat, iter) : prob_func
@@ -380,14 +402,14 @@ function mesolve_map(
380402 ens_prob = TimeEvolutionProblem (
381403 EnsembleProblem (prob. prob, prob_func = _prob_func, output_func = _output_func[1 ], safetycopy = false ),
382404 prob. times,
405+ StateOpType (),
383406 prob. dimensions,
384- (progr = _output_func[2 ], channel = _output_func[3 ], isoperket = prob . kwargs . isoperket ),
407+ (progr = _output_func[2 ], channel = _output_func[3 ]),
385408 )
386409
387410 sol = _ensemble_dispatch_solve (ens_prob, alg, ensemblealg, ntraj)
388411
389412 # handle solution and make it become an Array of TimeEvolutionSol
390- sol_vec =
391- [_gen_mesolve_solution (sol[:, i], prob. times, prob. dimensions, prob. kwargs. isoperket) for i in eachindex (sol)] # map is type unstable
413+ sol_vec = [_gen_mesolve_solution (sol[:, i], prob) for i in eachindex (sol)] # map is type unstable
392414 return reshape (sol_vec, size (iter))
393415end
0 commit comments