Skip to content

Commit 4a196cd

Browse files
Merge branch 'main' into dev/patch-2
2 parents 6d2d0b6 + 9cc0224 commit 4a196cd

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/qutip/QuantumToolbox.jl/tree/main)
99

10-
10+
- Fix Dynamical Fock Dimension states saving due to wrong saving of dimensions. ([#375])
1111
- Add checks for `tlist` in time evolution solvers. The checks are to ensure that `tlist` is not empty, the elements are in increasing order, and the elements are unique. ([#378])
1212

1313
## [v0.25.0]
@@ -86,4 +86,6 @@ Release date: 2024-11-13
8686
[#360]: https://github.com/qutip/QuantumToolbox.jl/issues/360
8787
[#370]: https://github.com/qutip/QuantumToolbox.jl/issues/370
8888
[#371]: https://github.com/qutip/QuantumToolbox.jl/issues/371
89+
[#375]: https://github.com/qutip/QuantumToolbox.jl/issues/375
8990
[#378]: https://github.com/qutip/QuantumToolbox.jl/issues/378
91+

src/time_evolution/time_evolution_dynamical.jl

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ end
5959
_dfd_set_pillow(dim)::Int = min(max(round(Int, 0.02 * dim), 1), 20)
6060

6161
function _DFDIncreaseReduceCondition(u, t, integrator)
62-
internal_params = integrator.p
63-
dim_list = internal_params.dim_list
64-
maxdims = internal_params.maxdims
65-
tol_list = internal_params.tol_list
66-
increase_list = internal_params.increase_list
67-
reduce_list = internal_params.reduce_list
68-
pillow_list = internal_params.pillow_list
69-
dfd_ρt_cache = internal_params.dfd_ρt_cache
62+
params = integrator.p
63+
dim_list = params.dim_list
64+
maxdims = params.maxdims
65+
tol_list = params.tol_list
66+
increase_list = params.increase_list
67+
reduce_list = params.reduce_list
68+
pillow_list = params.pillow_list
69+
dfd_ρt_cache = params.dfd_ρt_cache
7070

7171
# I need this cache because I can't reshape directly the integrator.u
7272
copyto!(dfd_ρt_cache, u)
@@ -89,18 +89,18 @@ function _DFDIncreaseReduceCondition(u, t, integrator)
8989
end
9090

9191
function _DFDIncreaseReduceAffect!(integrator)
92-
internal_params = integrator.p
93-
H = internal_params.H_fun
94-
c_ops = internal_params.c_ops_fun
95-
e_ops = internal_params.e_ops_fun
96-
dim_list = internal_params.dim_list
97-
increase_list = internal_params.increase_list
98-
reduce_list = internal_params.reduce_list
99-
pillow_list = internal_params.pillow_list
100-
dim_list_evo_times = internal_params.dim_list_evo_times
101-
dim_list_evo = internal_params.dim_list_evo
102-
dfd_ρt_cache = internal_params.dfd_ρt_cache
103-
dfd_params = internal_params.dfd_params
92+
params = integrator.p
93+
H = params.H_fun
94+
c_ops = params.c_ops_fun
95+
e_ops = params.e_ops_fun
96+
dim_list = params.dim_list
97+
increase_list = params.increase_list
98+
reduce_list = params.reduce_list
99+
pillow_list = params.pillow_list
100+
dim_list_evo_times = params.dim_list_evo_times
101+
dim_list_evo = params.dim_list_evo
102+
dfd_ρt_cache = params.dfd_ρt_cache
103+
dfd_params = params.dfd_params
104104

105105
ρt = vec2mat(dfd_ρt_cache)
106106

@@ -122,7 +122,7 @@ function _DFDIncreaseReduceAffect!(integrator)
122122
fill!(increase_list, false)
123123
fill!(reduce_list, false)
124124
push!(dim_list_evo_times, integrator.t)
125-
push!(dim_list_evo, dim_list)
125+
push!(dim_list_evo, copy(dim_list))
126126

127127
e_ops2 = map(op -> mat2vec(get_data(op)'), e_ops(dim_list, dfd_params))
128128
L = liouvillian(H(dim_list, dfd_params), c_ops(dim_list, dfd_params)).data
@@ -131,7 +131,7 @@ function _DFDIncreaseReduceAffect!(integrator)
131131
copyto!(integrator.u, mat2vec(ρt))
132132
# By doing this, we are assuming that the system is time-independent and f is a MatrixOperator
133133
integrator.f = ODEFunction{true,FullSpecialize}(MatrixOperator(L))
134-
integrator.p = merge(internal_params, (dfd_ρt_cache = similar(integrator.u),))
134+
integrator.p = merge(params, (dfd_ρt_cache = similar(integrator.u),))
135135
_mesolve_callbacks_new_e_ops!(integrator, e_ops2)
136136

137137
return nothing
@@ -150,15 +150,15 @@ function dfd_mesolveProblem(
150150
kwargs...,
151151
) where {T2<:Integer,StateOpType<:Union{KetQuantumObject,OperatorQuantumObject}}
152152
length(ψ0.dimensions) != length(maxdims) &&
153-
throw(DimensionMismatch("'dim_list' and 'maxdims' do not have the same dimension."))
153+
throw(DimensionMismatch("`dim_list` and `maxdims` do not have the same dimension."))
154154

155155
dim_list = MVector(ψ0.dims)
156156
H₀ = H(dim_list, dfd_params)
157157
c_ops₀ = c_ops(dim_list, dfd_params)
158158
e_ops₀ = e_ops(dim_list, dfd_params)
159159

160160
dim_list_evo_times = [0.0]
161-
dim_list_evo = [dim_list]
161+
dim_list_evo = [copy(dim_list)]
162162
reduce_list = MVector(ntuple(i -> false, Val(length(dim_list))))
163163
increase_list = MVector(ntuple(i -> false, Val(length(dim_list))))
164164
pillow_list = _dfd_set_pillow.(dim_list)
@@ -235,14 +235,12 @@ function dfd_mesolve(
235235

236236
sol = solve(dfd_prob.prob, alg)
237237

238-
ρt = map(
239-
i -> QuantumObject(
240-
vec2mat(sol.u[i]),
241-
type = Operator,
242-
dims = sol.prob.p.dim_list_evo[searchsortedlast(sol.prob.p.dim_list_evo_times, sol.t[i])],
243-
),
244-
eachindex(sol.t),
245-
)
238+
ρt = map(eachindex(sol.t)) do i
239+
idx = findfirst(>=(sol.t[i]), sol.prob.p.dim_list_evo_times)
240+
idx2 = isnothing(idx) ? length(sol.prob.p.dim_list_evo) : (idx == 1 ? 1 : idx - 1)
241+
242+
return QuantumObject(vec2mat(sol.u[i]), type = Operator, dims = sol.prob.p.dim_list_evo[idx2])
243+
end
246244

247245
return TimeEvolutionSol(
248246
dfd_prob.times,

test/core-test/dynamical_fock_dimension_mesolve.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,23 @@
2929
maxdims = [150]
3030
ψ0 = fock(3, 0)
3131
dfd_params == Δ, F = F, κ = κ)
32-
sol = dfd_mesolve(H_dfd0, ψ0, t_l, c_ops_dfd0, maxdims, dfd_params, e_ops = e_ops_dfd0, progress_bar = Val(false))
32+
sol = dfd_mesolve(
33+
H_dfd0,
34+
ψ0,
35+
t_l,
36+
c_ops_dfd0,
37+
maxdims,
38+
dfd_params,
39+
e_ops = e_ops_dfd0,
40+
progress_bar = Val(false),
41+
saveat = t_l,
42+
)
3343

3444
@test sum(abs.((sol.expect[1, :] .- sol0.expect[1, :]) ./ (sol0.expect[1, :] .+ 1e-16))) < 0.01
45+
@test length(sol.states) == length(t_l)
46+
@test all(
47+
diff(getindex.(QuantumToolbox.dimensions_to_dims.(QuantumToolbox.get_dimensions_to.(sol.states)), 1)) .>= 0,
48+
)
3549

3650
######################
3751

0 commit comments

Comments
 (0)