Skip to content

Commit

Permalink
Fix time dependent TDVP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Feb 16, 2024
1 parent 3fde428 commit 0c96235
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 54 deletions.
8 changes: 8 additions & 0 deletions examples/03_solvers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function ode_solver(
return to_itensor(uₜ), nothing
end

function ode_solver(f⃗, H⃗₀, time_step, ψ₀; kwargs...)
return ode_solver(-im * TimeDependentSum(f⃗, H⃗₀), time_step, ψ₀; kwargs...)
end

function krylov_solver(
H::TimeDependentSum, time_step, ψ₀; current_time=zero(time_step), outputlevel=0, kwargs...
)
Expand All @@ -35,3 +39,7 @@ function krylov_solver(
ψₜ, info = exponentiate(H(current_time), time_step, ψ₀; kwargs...)
return ψₜ, info
end

function krylov_solver(f⃗, H⃗₀, time_step, ψ₀; kwargs...)
return krylov_solver(-im * TimeDependentSum(f⃗, H⃗₀), time_step, ψ₀; kwargs...)
end
27 changes: 10 additions & 17 deletions examples/03_tdvp_time_dependent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function main()
# Set to 2 to get information about each bond/site
# evolution, and 3 to get information about the
# solver.
outputlevel = 1
outputlevel = 3

# Frequency of time dependent terms
ω₁ = 0.1
Expand Down Expand Up @@ -92,19 +92,12 @@ function main()
println("#"^100)
println()

function ode_solver(H⃗₀, time_step, ψ₀; kwargs...)
return ode_solver(
-im * TimeDependentSum(f⃗, H⃗₀),
time_step,
ψ₀;
solver_alg=ode_alg,
ode_kwargs...,
kwargs...,
)
function ode_solver_f⃗(H⃗₀, time_step, ψ₀; kwargs...)
return ode_solver(f⃗, H⃗₀, time_step, ψ₀; solver_alg=ode_alg, ode_kwargs..., kwargs...)
end

ψₜ_ode = tdvp(
ode_solver, H⃗₀, time_stop, ψ₀; time_step, maxdim, cutoff, nsite, outputlevel
ode_solver_f⃗, H⃗₀, time_stop, ψ₀; time_step, maxdim, cutoff, nsite, outputlevel
)

println()
Expand All @@ -117,13 +110,13 @@ function main()
println("#"^100)
println()

function krylov_solver(H⃗₀, time_step, ψ₀; kwargs...)
return krylov_solver(
-im * TimeDependentSum(f⃗, H⃗₀), time_step, ψ₀; krylov_kwargs..., kwargs...
)
function krylov_solver_f⃗(H⃗₀, time_step, ψ₀; kwargs...)
return krylov_solver(f⃗, H⃗₀, time_step, ψ₀; krylov_kwargs..., kwargs...)
end

ψₜ_krylov = tdvp(krylov_solver, H⃗₀, time_stop, ψ₀; time_step, cutoff, nsite, outputlevel)
ψₜ_krylov = tdvp(
krylov_solver_f⃗, H⃗₀, time_stop, ψ₀; time_step, cutoff, nsite, outputlevel
)

println()
println("Finished running TDVP with Krylov solver")
Expand All @@ -136,7 +129,7 @@ function main()
println()

@disable_warn_order begin
ψₜ_full, _ = ode_solver(prod.(H⃗₀), time_stop, prod(ψ₀); outputlevel)
ψₜ_full, _ = ode_solver(f⃗, prod.(H⃗₀), time_stop, prod(ψ₀); outputlevel)
end

println()
Expand Down
6 changes: 1 addition & 5 deletions src/tdvp.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using ITensors:
Algorithm,
MPO,
MPS,
@Algorithm_str
using ITensors: Algorithm, MPO, MPS, @Algorithm_str

# Select solver function
solver_function(solver_backend::String) = solver_function(Algorithm(solver_backend))
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Observers = "338f10d5-c7f1-4033-a7d1-f9dec39bcaa0"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
52 changes: 23 additions & 29 deletions test/tdvp_time_dependent.jl
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
@eval module $(gensym())
# These tests take too long to compile, skip for now.
using ITensors: MPO, MPS, siteinds
using ITensors: ITensors, MPO, MPS, siteinds
using ITensorTDVP: ITensorTDVP, tdvp
using LinearAlgebra: norm
using Test: @test, @testset

include(joinpath(pkgdir(ITensorTDVP), "examples", "03_models.jl"))
include(joinpath(pkgdir(ITensorTDVP), "examples", "03_solvers.jl"))

@testset "Time dependent Hamiltonian (eltype=$elt)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
)
function ode_solver(H⃗₀, time_step, ψ₀; kwargs...)
ω₁ = typeof(time_step)(0.1)
ω₂ = typeof(time_step)(0.2)
ω⃗ = [ω₁, ω₂]
@testset "Time dependent Hamiltonian (eltype=$elt, conserve_qns=$conserve_qns)" for elt in
(
Float32, Float64, Complex{Float32}, Complex{Float64}
),
conserve_qns in [false, true]

# Frequencies
ω₁ = 0.1
ω₂ = 0.2

function ode_solver_f⃗(H⃗₀, time_step, ψ₀; kwargs...)
ω⃗ = typeof(time_step)[ω₁, ω₂]
f⃗ = [t -> cos* t) for ω in ω⃗]
ode_alg = Tsit5()
tol = eps(real(time_step))
ode_kwargs = (; reltol=tol, abstol=tol)
return ode_solver(
-im * TimeDependentSum(f⃗, H⃗₀),
time_step,
ψ₀;
solver_alg=ode_alg,
ode_kwargs...,
kwargs...,
f⃗, H⃗₀, time_step, ψ₀; solver_alg=Tsit5(), reltol=tol, abstol=tol, kwargs...
)
end

function krylov_solver(H⃗₀, time_step, ψ₀; kwargs...)
ω₁ = typeof(time_step)(0.1)
ω₂ = typeof(time_step)(0.2)
ω⃗ = [ω₁, ω₂]
function krylov_solver_f⃗(H⃗₀, time_step, ψ₀; kwargs...)
ω⃗ = typeof(time_step)[ω₁, ω₂]
f⃗ = [t -> cos* t) for ω in ω⃗]
tol = eps(real(time_step))
krylov_kwargs = (; tol, eager=true)
return krylov_solver(
-im * TimeDependentSum(f⃗, H⃗₀), time_step, ψ₀; krylov_kwargs..., kwargs...
)
return krylov_solver(f⃗, H⃗₀, time_step, ψ₀; tol, eager=true, kwargs...)
end

n = 4
Expand All @@ -49,14 +43,14 @@ include(joinpath(pkgdir(ITensorTDVP), "examples", "03_solvers.jl"))
maxdim = 100
cutoff = (eps(real(elt)))
s = siteinds("S=1/2", n)
ℋ₁₀ = heisenberg(n; J=J₁, J2=elt(0))
ℋ₂₀ = heisenberg(n; J=elt(0), J2=J₂)
ℋ₁₀ = heisenberg(n; J=J₁, J2=zero(elt))
ℋ₂₀ = heisenberg(n; J=zero(elt), J2=J₂)
ℋ⃗₀ = [ℋ₁₀, ℋ₂₀]
H⃗₀ = [MPO(elt, ℋ₀, s) for ℋ₀ in ℋ⃗₀]
ψ₀ = complex.(MPS(elt, s, j -> isodd(j) ? "" : ""))
ψₜ_ode = tdvp(ode_solver, H⃗₀, time_stop, ψ₀; time_step, maxdim, cutoff, nsite)
ψₜ_krylov = tdvp(krylov_solver, H⃗₀, time_stop, ψ₀; time_step, cutoff, nsite)
ψₜ_full, _ = ode_solver(prod.(H⃗₀), time_stop, prod(ψ₀))
ψₜ_ode = tdvp(ode_solver_f⃗, H⃗₀, time_stop, ψ₀; time_step, maxdim, cutoff, nsite)
ψₜ_krylov = tdvp(krylov_solver_f⃗, H⃗₀, time_stop, ψ₀; time_step, cutoff, nsite)
ψₜ_full, _ = ode_solver_f⃗(prod.(H⃗₀), time_stop, prod(ψ₀))

@test ITensors.scalartype(ψ₀) == complex(elt)
@test ITensors.scalartype(ψₜ_ode) == complex(elt)
Expand Down
2 changes: 1 addition & 1 deletion test/test_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Test: @testset
examples_files = [
"01_tdvp.jl",
"02_dmrg-x.jl",
# "03_tdvp_time_dependent.jl",
"03_tdvp_time_dependent.jl",
"04_tdvp_observers.jl",
"05_tdvp_nonuniform_timesteps.jl",
]
Expand Down
3 changes: 1 addition & 2 deletions test/test_tdvp_time_dependent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ using Test: @test, @test_skip, @testset
@test sum(i -> α * f⃗ₜ[i](t₀) * H⃗ᵣ[i](ψᵣ), eachindex(H⃗))
end
@testset "Time dependent TDVP" begin
# These tests take too long to compile, skip for now.
@test_skip include("tdvp_time_dependent.jl")
include("tdvp_time_dependent.jl")
end
end
end

0 comments on commit 0c96235

Please sign in to comment.