Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

- Add support of `QobjEvo` for `steadystate` (ODE solver only). ([#536])

## [v0.34.1]
Release date: 2025-08-23

Expand Down Expand Up @@ -301,3 +303,4 @@ Release date: 2024-11-13
[#517]: https://github.com/qutip/QuantumToolbox.jl/issues/517
[#520]: https://github.com/qutip/QuantumToolbox.jl/issues/520
[#531]: https://github.com/qutip/QuantumToolbox.jl/issues/531
[#536]: https://github.com/qutip/QuantumToolbox.jl/issues/536
10 changes: 7 additions & 3 deletions src/steadystate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ end

@doc raw"""
steadystate(
H::QuantumObject{OpType},
H::AbstractQuantumObject{OpType},
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
solver::SteadyStateSolver = SteadyStateDirectSolver(),
kwargs...,
Expand All @@ -111,7 +111,7 @@ Solve the stationary state based on different solvers.
- `kwargs`: The keyword arguments for the solver.
"""
function steadystate(
H::QuantumObject{OpType},
H::AbstractQuantumObject{OpType},
c_ops::Union{Nothing,AbstractVector,Tuple} = nothing;
solver::SteadyStateSolver = SteadyStateDirectSolver(),
kwargs...,
Expand Down Expand Up @@ -199,7 +199,7 @@ function _steadystate(L::QuantumObject{SuperOperator}, solver::SteadyStateDirect
return QuantumObject(ρss, Operator(), L.dimensions)
end

function _steadystate(L::QuantumObject{SuperOperator}, solver::SteadyStateODESolver; kwargs...)
function _steadystate(L::AbstractQuantumObject{SuperOperator}, solver::SteadyStateODESolver; kwargs...)
tmax = solver.tmax

ψ0 = isnothing(solver.ψ0) ? rand_ket(L.dimensions) : solver.ψ0
Expand All @@ -217,12 +217,16 @@ function _steadystate(L::QuantumObject{SuperOperator}, solver::SteadyStateODESol
save_everystep = false,
saveat = ftype[],
callback = cb,
kwargs...,
)

ρss = sol.states[end]
return ρss
end

_steadystate(L::QuantumObjectEvolution{SuperOperator}, solver::SteadyStateSolver; kwargs...) =
throw(ArgumentError("$(get_typename_wrapper(solver)) does not support QobjEvo."))

struct SteadyStateODECondition{CT<:AbstractArray}
cache::CT
end
Expand Down
8 changes: 4 additions & 4 deletions test/core-test/steady_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
a = destroy(N)
a_d = a'
H = a_d * a + 0.1 * (a + a_d)
Ht = QobjEvo(H, (p, t) -> 1) # for test throw
c_ops = [sqrt(0.1) * a]
e_ops = [a_d * a]
psi0 = fock(N, 3)
Expand All @@ -17,18 +18,17 @@
solver = SteadyStateDirectSolver()
ρ_ss = steadystate(H, c_ops, solver = solver)
@test tracedist(rho_me, ρ_ss) < 1e-4
@test_throws ArgumentError steadystate(Ht, c_ops, solver = solver)

solver = SteadyStateLinearSolver()
ρ_ss = steadystate(H, c_ops, solver = solver)
@test tracedist(rho_me, ρ_ss) < 1e-4

solver = SteadyStateLinearSolver()
ρ_ss = steadystate(H, c_ops, solver = solver)
@test tracedist(rho_me, ρ_ss) < 1e-4
Comment on lines -25 to -27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we remove this part because there is exact same thing in Lines 21 - 23

@test_throws ArgumentError steadystate(Ht, c_ops, solver = solver)

solver = SteadyStateEigenSolver()
ρ_ss = steadystate(H, c_ops, solver = solver)
@test tracedist(rho_me, ρ_ss) < 1e-4
@test_throws ArgumentError steadystate(Ht, c_ops, solver = solver)

@testset "Type Inference (steadystate)" begin
L = liouvillian(H, c_ops)
Expand Down
15 changes: 15 additions & 0 deletions test/ext-test/cpu/autodiff/autodiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ function my_f_mesolve(p)
return real(expect(a' * a, sol.states[end]))
end

function my_f_steadystate(p)
ρss = steadystate(
L,
SteadyStateODESolver(ψ0 = ψ0_mesolve, tmax = tlist_mesolve[end]);
params = p,
sensealg = BacksolveAdjoint(autojacvec = EnzymeVJP()),
)

return real(expect(a' * a, ρss))
end

# Analytical solution
n_ss(Δ, F, γ) = abs2(F / (Δ + 1im * γ / 2))

Expand Down Expand Up @@ -113,8 +124,12 @@ n_ss(Δ, F, γ) = abs2(F / (Δ + 1im * γ / 2))

my_f_mesolve_direct(params)
my_f_mesolve(params)
my_f_steadystate(params)

# calculate exact solution and check if steadystate works
grad_exact = Zygote.gradient((p) -> n_ss(p[1], p[2], p[3]), params)[1]
grad_ss = Zygote.gradient(my_f_steadystate, params)[1]
@test grad_ss ≈ grad_exact atol=1e-6

@testset "ForwardDiff.jl" begin
grad_qt = ForwardDiff.gradient(my_f_mesolve_direct, params)
Expand Down
Loading