Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ITensorTDVP.jl v0.4 #74

Merged
merged 29 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7f60c30
v0.4
mtfishman May 12, 2024
adb97e6
Renaming types
mtfishman May 12, 2024
b03fb86
Fix printing
mtfishman May 12, 2024
c796337
Start modernizing solvers
mtfishman May 13, 2024
733db7d
New updater kwarg syntax
mtfishman May 13, 2024
850ff00
Try fixing tests
mtfishman May 13, 2024
6aad4f1
Fix typo
mtfishman May 13, 2024
f1912b3
reduced_operator fallback
mtfishman May 13, 2024
db15aac
Fix namespace issue
mtfishman May 13, 2024
f4f6459
Reorganize tests
mtfishman May 13, 2024
6bcd03a
Revert incorrect change of solver to updater
mtfishman May 13, 2024
40f585e
Update examples/02_dmrg-x.jl
mtfishman May 13, 2024
1e237de
Use `Compat` for `Returns` in older Julia versions
mtfishman May 13, 2024
48c3257
Update test/test_dmrg_x.jl
mtfishman May 13, 2024
803780c
List breaking changes in README
mtfishman May 13, 2024
a6bf54c
Merge branch 'v0.4' of github.com:ITensor/ITensorTDVP.jl into v0.4
mtfishman May 13, 2024
931bb0f
Compat dependency, update observer example syntax
mtfishman May 13, 2024
d53567a
Fix tests
mtfishman May 13, 2024
fc88573
Use Algorithm instead of Backend for backwards compatibility
mtfishman May 14, 2024
d36c829
Small clarification in News
mtfishman May 14, 2024
a97489e
Simplify DMRG-X example a bit
mtfishman May 14, 2024
60e8d0b
Fix typo in README
mtfishman May 14, 2024
8843395
Simplify Observers example
mtfishman May 14, 2024
e17a131
Update README.md
mtfishman May 14, 2024
2fe8d1a
Update README.md
mtfishman May 14, 2024
58baae9
Update README.md
mtfishman May 14, 2024
b6e75e3
Update README.md
mtfishman May 14, 2024
87f8c78
Update README.md
mtfishman May 14, 2024
34be941
Small fixes to upgrade guide
mtfishman May 14, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorTDVP"
uuid = "25707e16-a4db-4a07-99d9-4d67b7af0342"
authors = ["Matthew Fishman <mfishman@flatironinstitute.org> and contributors"]
version = "0.3.1"
version = "0.4.0"

[deps]
ITensors = "9136182c-28ba-11e9-034c-db9fb085ebd5"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ energy, psi = ITensorTDVP.dmrg(H, psi0; nsweeps=10, maxdim=100, cutoff=1e-6)

#### Breaking changes

- ITensorTDVP.jl v0.2.0-v0.2.4: The `applyexp` Krylov exponentiation solver backend was removed, and `solver_backend="applyexp"` option for `tdvp` now just calls `exponentiate` from KrylovKit.jl. `applyexp` is in many ways the same as `exponentiate` bit `exponentiate` has more advanced features like restarts. In these versions, `solver_backend="applyexp"` prints a warning to that effect. As of ITensorTDVP.jl v0.2.5, we have brought back the `applyexp` backend because we received reports that it performed better in certain cases. We plan to investigate that issue and make sure `exponentiate` works as well as `applyexp` in those cases so that we can go back to just having a single `exponentiate` backend.
- ITensorTDVP.jl v0.2.0-v0.2.4: The `applyexp` Krylov exponentiation updater backend was removed, and `updater_backend="applyexp"` option for `tdvp` now just calls `exponentiate` from KrylovKit.jl. `applyexp` is in many ways the same as `exponentiate` bit `exponentiate` has more advanced features like restarts. In these versions, `updater_backend="applyexp"` prints a warning to that effect. As of ITensorTDVP.jl v0.2.5, we have brought back the `applyexp` backend because we received reports that it performed better in certain cases. We plan to investigate that issue and make sure `exponentiate` works as well as `applyexp` in those cases so that we can go back to just having a single `exponentiate` backend.
mtfishman marked this conversation as resolved.
Show resolved Hide resolved

#### Bug fixes

Expand Down
7 changes: 4 additions & 3 deletions examples/01_tdvp.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensorMPS: MPO, OpSum, dmrg, inner, randomMPS, siteinds, tdvp
using ITensors: MPO, OpSum, dmrg, inner, randomMPS, siteinds
using ITensorTDVP: tdvp

function main()
n = 10
Expand All @@ -21,9 +22,9 @@ function main()

ϕ = tdvp(
H,
-1.0,
-20.0,
ψ;
nsweeps=20,
time_step=-1.0,
reverse_step=false,
normalize=true,
maxdim=30,
Expand Down
7 changes: 3 additions & 4 deletions examples/02_dmrg-x.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensorMPS: MPO, MPS, OpSum, dmrg_x, inner, siteinds
using ITensors: MPO, MPS, OpSum, inner, siteinds
using ITensorTDVP: dmrg_x
using Random: Random

function main()
Expand Down Expand Up @@ -31,9 +32,7 @@ function main()
initstate = rand(["↑", "↓"], n)
ψ = MPS(s, initstate)

dmrg_x_kwargs = (
nsweeps=10, reverse_step=false, normalize=true, maxdim=20, cutoff=1e-10, outputlevel=1
)
dmrg_x_kwargs = (nsweeps=10, normalize=true, maxdim=20, cutoff=1e-10, outputlevel=1)

mtfishman marked this conversation as resolved.
Show resolved Hide resolved
e, ϕ = dmrg_x(H, ψ; dmrg_x_kwargs...)

Expand Down
2 changes: 1 addition & 1 deletion examples/03_models.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ITensorMPS: OpSum
using ITensors: OpSum

function heisenberg(n; J=1.0, J2=0.0)
ℋ = OpSum()
Expand Down
52 changes: 0 additions & 52 deletions examples/03_solvers.jl

This file was deleted.

80 changes: 43 additions & 37 deletions examples/03_tdvp_time_dependent.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using ITensors: @disable_warn_order
using ITensorMPS: MPO, MPS, inner, randomMPS, siteinds, tdvp
using ITensors: @disable_warn_order, contract
using ITensors: MPO, MPS, inner, randomMPS, siteinds
using ITensorTDVP: tdvp
using LinearAlgebra: norm
using Random: Random

include("03_models.jl")
include("03_solvers.jl")
include("03_updaters.jl")

function main()
Random.seed!(1234)
Expand All @@ -20,7 +21,7 @@ function main()
# How much information to output from TDVP
# Set to 2 to get information about each bond/site
# evolution, and 3 to get information about the
# solver.
# updater.
outputlevel = 3

# Frequency of time dependent terms
Expand Down Expand Up @@ -50,34 +51,24 @@ function main()

tol = 1e-15

# ODE solver parameters
ode_alg = Tsit5()
ode_kwargs = (; reltol=tol, abstol=tol)

# Krylov solver parameters
krylov_kwargs = (; tol=tol, eager=true)

@show n
@show ω₁, ω₂
@show J₁, J₂
@show maxdim, cutoff, nsite
@show start_linkdim
@show time_step, time_stop
@show ode_alg
@show ode_kwargs
@show krylov_kwargs

ω⃗ = [ω₁, ω₂]
f⃗ = [t -> cos(ω * t) for ω in ω⃗]
ω⃗ = (ω₁, ω₂)
f⃗ = map(ω -> (t -> cos(ω * t)), ω⃗)

# H₀ = H(0) = H₁(0) + H₂(0) + …
ℋ₁₀ = heisenberg(n; J=J₁, J2=0.0)
ℋ₂₀ = heisenberg(n; J=0.0, J2=J₂)
ℋ⃗₀ = [ℋ₁₀, ℋ₂₀]
ℋ⃗₀ = (ℋ₁₀, ℋ₂₀)

s = siteinds("S=1/2", n)

H⃗₀ = [MPO(ℋ₀, s) for ℋ₀ in ℋ⃗₀]
H⃗₀ = map(ℋ₀ -> MPO(ℋ₀, s), ℋ⃗₀)

# Initial state, ψ₀ = ψ(0)
# Initialize as complex since that is what OrdinaryDiffEq.jl/DifferentialEquations.jl
Expand All @@ -88,60 +79,75 @@ function main()

println()
println("#"^100)
println("Running TDVP with ODE solver")
println("Running TDVP with ODE updater")
println("#"^100)
println()

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_f⃗, H⃗₀, time_stop, ψ₀; time_step, maxdim, cutoff, nsite, outputlevel
-im * TimeDependentSum(f⃗, H⃗₀),
time_stop,
ψ₀;
updater=ode_updater,
updater_kwargs=(; reltol=tol, abstol=tol),
time_step,
maxdim,
cutoff,
nsite,
outputlevel,
)

println()
println("Finished running TDVP with ODE solver")
println("Finished running TDVP with ODE updater")
println()

println()
println("#"^100)
println("Running TDVP with Krylov solver")
println("Running TDVP with Krylov updater")
println("#"^100)
println()

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

ψₜ_krylov = tdvp(
krylov_solver_f⃗, H⃗₀, time_stop, ψ₀; time_step, cutoff, nsite, outputlevel
-im * TimeDependentSum(f⃗, H⃗₀),
time_stop,
ψ₀;
updater=krylov_updater,
updater_kwargs=(; tol, eager=true),
time_step,
cutoff,
nsite,
outputlevel,
)

println()
println("Finished running TDVP with Krylov solver")
println("Finished running TDVP with Krylov updater")
println()

println()
println("#"^100)
println("Running full state evolution with ODE solver")
println("Running full state evolution with ODE updater")
println("#"^100)
println()

@disable_warn_order begin
ψₜ_full, _ = ode_solver(f⃗, prod.(H⃗₀), time_stop, prod(ψ₀); outputlevel)
ψₜ_full, _ = ode_updater(
-im * TimeDependentSum(f⃗, contract.(H⃗₀)),
contract(ψ₀);
internal_kwargs=(; time_step=time_stop, outputlevel),
reltol=tol,
abstol=tol,
)
end

println()
println("Finished full state evolution with ODE solver")
println("Finished full state evolution with ODE updater")
println()

@show norm(ψₜ_ode)
@show norm(ψₜ_krylov)
@show norm(ψₜ_full)

@show 1 - abs(inner(prod(ψₜ_ode), ψₜ_full))
@show 1 - abs(inner(prod(ψₜ_krylov), ψₜ_full))
@show 1 - abs(inner(contract(ψₜ_ode), ψₜ_full))
@show 1 - abs(inner(contract(ψₜ_krylov), ψₜ_full))
return nothing
end

Expand Down
22 changes: 22 additions & 0 deletions examples/03_updaters.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using ITensors: ITensor, array, inds, itensor
using ITensorTDVP: TimeDependentSum, to_vec
using KrylovKit: exponentiate
using OrdinaryDiffEq: ODEProblem, Tsit5, solve

function ode_updater(operator, init; internal_kwargs, alg=Tsit5(), kwargs...)
(; current_time, time_step) = (; current_time=zero(Bool), internal_kwargs...)
time_span = typeof(time_step).((current_time, current_time + time_step))
init_vec, to_itensor = to_vec(init)
f(init::ITensor, p, t) = operator(t)(init)
f(init_vec::Vector, p, t) = to_vec(f(to_itensor(init_vec), p, t))[1]
prob = ODEProblem(f, init_vec, time_span)
sol = solve(prob, alg; kwargs...)
state_vec = sol.u[end]
return to_itensor(state_vec), (;)
end

function krylov_updater(operator, init; internal_kwargs, kwargs...)
(; current_time, time_step) = (; current_time=zero(Bool), internal_kwargs...)
state, info = exponentiate(operator(current_time), time_step, init; kwargs...)
return state, (; info)
end
3 changes: 2 additions & 1 deletion examples/04_tdvp_observers.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ITensorMPS: MPO, MPS, OpSum, expect, inner, siteinds, tdvp
using ITensors: MPO, MPS, OpSum, expect, inner, siteinds
using ITensorTDVP: tdvp
using Observers: observer

function main()
Expand Down
13 changes: 7 additions & 6 deletions src/ITensorTDVP.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
module ITensorTDVP
export TimeDependentSum, dmrg_x, linsolve, tdvp, to_vec
include("ITensorsExtensions.jl")
using .ITensorsExtensions: to_vec
include("applyexp.jl")
include("defaults.jl")
include("update_observer.jl")
include("solver_utils.jl")
include("timedependentsum.jl")
include("tdvporder.jl")
include("tdvpinfo.jl")
include("sweep_update.jl")
include("alternating_update.jl")
include("tdvp.jl")
include("dmrg.jl")
include("dmrg_x.jl")
include("projmpo_apply.jl")
include("contract_mpo_mps.jl")
include("projmps2.jl")
include("projmpo_mps2.jl")
include("reducedcontractproblem.jl")
include("contract.jl")
include("reducedconstantterm.jl")
include("reducedlinearproblem.jl")
include("linsolve.jl")
using PackageExtensionCompat: @require_extensions
function __init__()
Expand Down
9 changes: 9 additions & 0 deletions src/ITensorsExtensions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ITensorsExtensions
using ITensors: ITensor, array, inds, itensor
function to_vec(x::ITensor)
function to_itensor(x_vec)
return itensor(x_vec, inds(x))
end
return vec(array(x)), to_itensor
end
end
Loading
Loading