-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start moving ITensorTDVP.jl codebase
- Loading branch information
Showing
48 changed files
with
2,497 additions
and
25 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
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,4 +1,4 @@ | ||
using ChainRulesCore: @non_differentiable | ||
using ITensors: Index | ||
using ITensors.ITensorMPS: MPS | ||
using ITensorMPS: MPS | ||
@non_differentiable MPS(::Type{<:Number}, sites::Vector{<:Index}, states_) |
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,9 @@ | ||
module ITensorMPSObserversExt | ||
using Observers: Observers | ||
using Observers.DataFrames: AbstractDataFrame | ||
using ITensorMPS: ITensorMPS | ||
|
||
function ITensorMPS.update_observer!(observer::AbstractDataFrame; kwargs...) | ||
return Observers.update!(observer; kwargs...) | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
ext/ITensorMPSPackageCompilerExt/ITensorMPSPackageCompilerExt.jl
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,3 @@ | ||
module ITensorsPackageCompilerExt | ||
include("compile.jl") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using NDTensors: @Algorithm_str | ||
using ITensors: ITensors | ||
using PackageCompiler: PackageCompiler | ||
|
||
function ITensors.compile( | ||
::Algorithm"PackageCompiler"; | ||
dir::AbstractString=ITensors.default_compile_dir(), | ||
filename::AbstractString=ITensors.default_compile_filename(), | ||
) | ||
if !isdir(dir) | ||
println("""The directory "$dir" doesn't exist yet, creating it now.""") | ||
println() | ||
mkdir(dir) | ||
end | ||
path = joinpath(dir, filename) | ||
println( | ||
"""Creating the system image "$path" containing the compiled version of ITensorMPS. This may take a few minutes.""", | ||
) | ||
PackageCompiler.create_sysimage( | ||
:ITensorMPS; | ||
sysimage_path=path, | ||
precompile_execution_file=joinpath(@__DIR__, "precompile_itensormps.jl"), | ||
) | ||
println(ITensors.compile_note(; dir, filename)) | ||
return path | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using ITensorMPS: MPO, OpSum, dmrg, random_mps, siteinds | ||
|
||
# TODO: This uses all of the tests to make | ||
# precompile statements, but takes a long time | ||
# (e.g. 700 seconds). | ||
# Try again with later versions of PackageCompiler | ||
# | ||
# include(joinpath(joinpath(dirname(dirname(@__DIR__)), | ||
# test"), | ||
# "runtests.jl")) | ||
|
||
function main(; N, dmrg_kwargs) | ||
opsum = OpSum() | ||
for j in 1:(N - 1) | ||
opsum += 0.5, "S+", j, "S-", j + 1 | ||
opsum += 0.5, "S-", j, "S+", j + 1 | ||
opsum += "Sz", j, "Sz", j + 1 | ||
end | ||
for conserve_qns in (false, true) | ||
sites = siteinds("S=1", N; conserve_qns) | ||
H = MPO(opsum, sites) | ||
ψ0 = random_mps(sites, j -> isodd(j) ? "↑" : "↓"; linkdims=2) | ||
dmrg(H, ψ0; outputlevel=0, dmrg_kwargs...) | ||
end | ||
return nothing | ||
end | ||
|
||
main(; N=6, dmrg_kwargs=(; nsweeps=3, maxdim=10, cutoff=1e-13)) |
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,3 +1,3 @@ | ||
module Experimental | ||
using ITensorTDVP: dmrg | ||
include("dmrg.jl") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ..ITensorMPS: | ||
MPS, | ||
alternating_update, | ||
compose_observers, | ||
default_observer, | ||
eigsolve_updater, | ||
values_observer | ||
|
||
function dmrg( | ||
operator, init::MPS; updater=eigsolve_updater, (observer!)=default_observer(), kwargs... | ||
) | ||
info_ref! = Ref{Any}() | ||
info_observer! = values_observer(; info=info_ref!) | ||
observer! = compose_observers(observer!, info_observer!) | ||
state = alternating_update(operator, init; updater, observer!, kwargs...) | ||
return info_ref![].eigval, state | ||
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
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 |
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,117 @@ | ||
using ITensors: ITensors, permute | ||
|
||
function _extend_sweeps_param(param, nsweeps) | ||
if param isa Number | ||
eparam = fill(param, nsweeps) | ||
else | ||
length(param) == nsweeps && return param | ||
eparam = Vector(undef, nsweeps) | ||
eparam[1:length(param)] = param | ||
eparam[(length(param) + 1):end] .= param[end] | ||
end | ||
return eparam | ||
end | ||
|
||
function process_sweeps(; nsweeps, maxdim, mindim, cutoff, noise) | ||
maxdim = _extend_sweeps_param(maxdim, nsweeps) | ||
mindim = _extend_sweeps_param(mindim, nsweeps) | ||
cutoff = _extend_sweeps_param(cutoff, nsweeps) | ||
noise = _extend_sweeps_param(noise, nsweeps) | ||
return (; maxdim, mindim, cutoff, noise) | ||
end | ||
|
||
function alternating_update( | ||
operator, | ||
init::MPS; | ||
updater, | ||
updater_kwargs=(;), | ||
nsweeps=default_nsweeps(), | ||
checkdone=default_checkdone(), | ||
write_when_maxdim_exceeds=default_write_when_maxdim_exceeds(), | ||
nsite=default_nsite(), | ||
reverse_step=default_reverse_step(), | ||
time_start=default_time_start(), | ||
time_step=default_time_step(), | ||
order=default_order(), | ||
(observer!)=default_observer(), | ||
(sweep_observer!)=default_sweep_observer(), | ||
outputlevel=default_outputlevel(), | ||
normalize=default_normalize(), | ||
maxdim=default_maxdim(), | ||
mindim=default_mindim(), | ||
cutoff=default_cutoff(ITensors.scalartype(init)), | ||
noise=default_noise(), | ||
) | ||
reduced_operator = ITensorMPS.reduced_operator(operator) | ||
if isnothing(nsweeps) | ||
return error("Must specify `nsweeps`.") | ||
end | ||
maxdim, mindim, cutoff, noise = process_sweeps(; nsweeps, maxdim, mindim, cutoff, noise) | ||
forward_order = TDVPOrder(order, Base.Forward) | ||
state = copy(init) | ||
# Keep track of the start of the current time step. | ||
# Helpful for tracking the total time, for example | ||
# when using time-dependent updaters. | ||
# This will be passed as a keyword argument to the | ||
# `updater`. | ||
current_time = time_start | ||
info = nothing | ||
for sweep in 1:nsweeps | ||
if !isnothing(write_when_maxdim_exceeds) && maxdim[sweep] > write_when_maxdim_exceeds | ||
if outputlevel >= 2 | ||
println( | ||
"write_when_maxdim_exceeds = $write_when_maxdim_exceeds and maxdim(sweeps, sw) = $(maxdim(sweeps, sweep)), writing environment tensors to disk", | ||
) | ||
end | ||
reduced_operator = disk(reduced_operator) | ||
end | ||
sweep_elapsed_time = @elapsed begin | ||
state, reduced_operator, info = sweep_update( | ||
forward_order, | ||
reduced_operator, | ||
state; | ||
updater, | ||
updater_kwargs, | ||
nsite, | ||
current_time, | ||
time_step, | ||
reverse_step, | ||
sweep, | ||
observer!, | ||
normalize, | ||
outputlevel, | ||
maxdim=maxdim[sweep], | ||
mindim=mindim[sweep], | ||
cutoff=cutoff[sweep], | ||
noise=noise[sweep], | ||
) | ||
end | ||
if !isnothing(time_step) | ||
current_time += time_step | ||
end | ||
update_observer!( | ||
sweep_observer!; state, reduced_operator, sweep, outputlevel, current_time | ||
) | ||
if outputlevel >= 1 | ||
print("After sweep ", sweep, ":") | ||
print(" maxlinkdim=", maxlinkdim(state)) | ||
@printf(" maxerr=%.2E", info.maxtruncerr) | ||
if !isnothing(current_time) | ||
print(" current_time=", round(current_time; digits=3)) | ||
end | ||
print(" time=", round(sweep_elapsed_time; digits=3)) | ||
println() | ||
flush(stdout) | ||
end | ||
isdone = checkdone(; | ||
state, sweep, outputlevel, observer=observer!, sweep_observer=sweep_observer! | ||
) | ||
isdone && break | ||
end | ||
return state | ||
end | ||
|
||
# Assume it is already in a reduced basis. | ||
reduced_operator(operator) = operator | ||
reduced_operator(operators::Vector{MPO}) = ProjMPOSum(operators) | ||
reduced_operator(operator::MPO) = ProjMPO(operator) |
Oops, something went wrong.