-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da9e5b7
commit 2883dc1
Showing
9 changed files
with
152 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
""" | ||
integrate(f, y₀, t₀, a, dt, algorithm) | ||
Integrate the differential equation ``dy/dt = a f(y,t)`` over a time step 'dt' starting from ``y(t₀)=y₀``, using the provided algorithm. | ||
For time-independent operators (i.e. not a TimedOperator) t₀ is ingored. | ||
Integrate the differential equation ``dy/dt = a*f(y,t)`` over a time step 'dt' starting from ``y(t₀)=y₀``, using the provided algorithm. | ||
# Arguments | ||
- `f::Function`: driving function | ||
- `y₀`: object to integrate | ||
- `t₀`: time f is evaluated at | ||
- `a` : scalar prefactor | ||
- `dt`: timestep | ||
- `method`: method to integrate TDVP equations | ||
- `algorithm`: integration scheme | ||
""" | ||
function integrate end | ||
|
||
# for backwards compatibility | ||
integrate(f, y₀, a, dt, method) = integrate(f, y₀, 0.0, a, dt, method) | ||
# make time evaluation dispatchable | ||
# user provides f(y,t) that can be called with two arguments | ||
Eval(f,x,t::Number) = f(x) | ||
Eval(f::F,x,t::Number) where {O<:TimedOperator,F<:Union{O,SumOfOperators{O}}} = f(x,t) | ||
|
||
# wrap function into UntimedOperator by default | ||
integrate(f, y₀, t₀, a, dt, method) = integrate(UntimedOperator(f), y₀, t₀, a, dt, method) | ||
|
||
""" | ||
ExpIntegrator | ||
Method that solves ``dy/dt = a*f(y,t)`` by exponentiating the action of f(y,t). | ||
# Fields | ||
- `krylovmethod::Union{Arnoldi,Lanczos}`` KrylovKit method for exponentiation. For options such as tolerance, see Lanczos/Arnoldi in KrylovKit. | ||
""" | ||
@kwdef struct ExpIntegrator | ||
krylovmethod::Union{Arnoldi,Lanczos} = Lanczos(); | ||
end | ||
|
||
#original integrator in iTDVP, namely exponentiation | ||
function integrate( | ||
f::F, y₀, t₀, a, dt, method::Union{Arnoldi,Lanczos} | ||
) where {F<:Union{MultipliedOperator,SumOfOperators}} | ||
sol, convhist = exponentiate(x -> f(x, t₀ + dt / 2), a * dt, y₀, method) | ||
f, y₀, t₀::Number, a::Number, dt::Number, method::ExpIntegrator | ||
) | ||
sol, convhist = exponentiate( x->Eval_t(f,x,t₀), a*dt, y₀, method.krylovmethod) | ||
return sol, convhist.converged, convhist | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#put in defaults | ||
_time_finalize(iter, state, opp, envs) = (state, envs, nothing) | ||
|
||
""" | ||
SimpleScheme{F} <: Algorithm | ||
The simplest numerical scheme to do time evolution by doing consecutive individual timesteps. | ||
# Fields | ||
- `stepalg::S`: algorithm used to do the individual timesteps | ||
- `ts`::AbstractVector: time step points | ||
- `dts`::AbstractVector: dt of each time step | ||
- `time_finalize::F`: user-supplied function which is applied after each iteration which can be used to save objects during the time evolution, with | ||
signature `finalize(iter, Ψ, H, envs) -> Ψ, envs, saved` | ||
""" | ||
struct SimpleScheme{S,F,N} <: MPSKit.Algorithm | ||
stepalg::S | ||
ts::AbstractVector{N} | ||
dts::AbstractVector{N} | ||
time_finalize::F | ||
|
||
function SimpleScheme(stepalg::S,ts::AbstractVector{N},dts::AbstractVector{N},time_finalize::F) where {N<:Number,S,F} | ||
length(ts) == length(dts)+1 || throw(ArgumentError("times and timesteps length need to be compatible")) | ||
all(isapprox.(test.ts[1:end-1] .+ test.dts,test.ts[2:end])) || throw(ArgumentError("times and timesteps need to be compatible")) | ||
return new{S,F,N}(stepalg,ts,dts,time_finalize) | ||
end | ||
end | ||
function SimpleScheme(stepalg,ts::AbstractVector,time_finalize) | ||
return SimpleScheme(stepalg,ts,ts[2:end] .- ts[1:end-1],time_finalize) | ||
end | ||
|
||
function SimpleScheme(stepalg,ts::AbstractRange,time_finalize) | ||
return SimpleScheme(stepalg,ts,repeat([step(ts)],length(ts)-1),time_finalize) | ||
end | ||
|
||
#implement iteration | ||
function Base.iterate(x::SimpleScheme,state=1) | ||
if length(x.ts) == 0 ||state == length(x.ts) | ||
return nothing | ||
else | ||
return ((x.ts[state],x.dts[state]),state+1) | ||
end | ||
end | ||
|
||
""" | ||
time_evolve(Ψ, H, tspan, [environments]; kwargs...) | ||
time_evolve(Ψ, H, scheme, [environments]; kwargs...) | ||
Time evolve the initial state `Ψ` with Hamiltonian `H` over a time span `tspan`. If not specified the | ||
time step `scheme` defaults to `SimpleScheme` which is just a consecutive iteration over `tspan`. | ||
If not specified, an algorithm for the individual time steps will be attempted based on the supplied keywords. | ||
## Arguments | ||
- `Ψ::AbstractMPS`: initial state | ||
- `H::AbstractMPO`: operator that generates the time evolution (can be time-dependent). | ||
- `[environments]`: MPS environment manager | ||
- `tspan::AbstractVector`: time points over which the time evolution is stepped | ||
- `scheme`: time step scheme | ||
## Keywords | ||
- `tol::Float64`: tolerance for convergence criterium | ||
- `maxiter::Int`: maximum amount of iterations | ||
- `verbose::Bool`: display progress information | ||
""" | ||
function time_evolve!(Ψ,H,tspan::AbstractVector{<:Number},envs::Cache=environments(Ψ,H); | ||
integrator= Lanczos(; tol=tol), | ||
tol=Defaults.tol, | ||
tolgauge=Defaults.tolgauge, | ||
gaugemaxiter=Defaults.maxiter, | ||
verbose=Defaults.verbose, | ||
trscheme=nothing, | ||
stepalg = TDVP(;integrator = integrator, tol = tol, tolgauge=tolgauge, gaugemaxiter = gaugemaxiter, verbose=verbose), | ||
time_finalize=Defaults._time_finalize, | ||
saved = [] | ||
) | ||
if !isnothing(trscheme) | ||
stepalg = TDVP2(;integrator = integrator, tol = tol, tolgauge=tolgauge, gaugemaxiter = gaugemaxiter, verbose=verbose, trscheme=trscheme) | ||
end | ||
scheme = SimpleScheme(stepalg,tspan,time_finalize) | ||
return time_evolve!(Ψ,H,scheme,envs;saved=saved) | ||
end | ||
|
||
time_evolve(Ψ,H,tspan,envs::Cache=environments(Ψ,H); kwargs...) = time_evolve(copy(Ψ),H,tspan,envs; kwargs...) | ||
|
||
function time_evolve!(Ψ,H,scheme::SimpleTimeScheme,envs=environments(Ψ,H);saved=[]) | ||
_,_, tobesaved = alg.finalize(1, Ψ, H, envs) | ||
isnothing(tobesaved) || push!(saved,tobesaved) | ||
for (iter,(t,dt)) in enumerate(scheme) | ||
Ψ,envs = timestep!(Ψ,H,t,dt,scheme.stepalg,envs) | ||
|
||
Ψ, envs, tobesaved = scheme.finalize(iter, Ψ, H, envs)::Tuple{typeof(Ψ),typeof(envs),eltype(saved)} | ||
isnothing(tobesaved) || push!(saved,tobesaved) | ||
end | ||
return Ψ, envs, saved | ||
end | ||
time_evolve(Ψ,H,scheme,envs::Cache=environments(Ψ,H); kwargs...) = time_evolve!(copy(Ψ),H,scheme,envs; kwargs...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
struct LinearCombination{O<:Tuple,C<:Tuple} | ||
opps::O | ||
coeffs::C | ||
end | ||
|
||
Base.:*(h::LinearCombination, v) = sum((c * (o * v) for (o, c) in zip(h.opps, h.coeffs))) | ||
(h::LinearCombination)(v) = sum((c * o(v) for (o, c) in zip(h.opps, h.coeffs))) |