Skip to content

Commit

Permalink
Rename environments/files and cleanup unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Dec 10, 2024
1 parent ceefc7d commit 0ad721d
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 744 deletions.
9 changes: 5 additions & 4 deletions docs/src/lib/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ MPOHamiltonian

## Environments
```@docs
MPSKit.AbstractInfEnv
MPSKit.AbstractMPSEnvironments
MPSKit.AbstractInfiniteEnvironments
MPSKit.InfiniteMPOEnvironments
MPSKit.MPOHamInfEnv
MPSKit.FinEnv
MPSKit.IDMRGEnvs
MPSKit.InfiniteMPOHamiltonianEnvironments
MPSKit.FiniteEnvironments
MPSKit.IDMRGEnvironments
```

## Generic actions
Expand Down
17 changes: 7 additions & 10 deletions src/MPSKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export entanglementplot, transferplot
export braille

# hamiltonian things
export Cache
export AbstractMPO
export MPO, FiniteMPO, InfiniteMPO
export MPOHamiltonian, FiniteMPOHamiltonian, InfiniteMPOHamiltonian, HMPO
Expand Down Expand Up @@ -69,7 +68,6 @@ export transfer_left, transfer_right

# Abstract type defs
abstract type Algorithm end
abstract type Cache end # cache "manages" environments

# submodules
include("utility/dynamictols.jl")
Expand Down Expand Up @@ -112,14 +110,13 @@ include("operators/lazysum.jl")
include("transfermatrix/transfermatrix.jl")
include("transfermatrix/transfer.jl")

include("environments/abstractenvironments.jl")
include("environments/FinEnv.jl")
include("environments/abstractinfenv.jl")
include("environments/infinitempoenv.jl")
include("environments/mpohaminfenv.jl")
include("environments/qpenv.jl")
include("environments/multipleenv.jl")
include("environments/idmrgenv.jl")
include("environments/abstract_envs.jl")
include("environments/finite_envs.jl")
include("environments/infinitempo_envs.jl")
include("environments/infinitempohamiltonian_envs.jl")
include("environments/qp_envs.jl")
include("environments/idmrg_envs.jl")
include("environments/multiple_envs.jl")
include("environments/lazylincocache.jl")

include("algorithms/fixedpoint.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/approximate/idmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function approximate(ost::MPSMultiline, toapprox::Tuple{<:MPOMultiline,<:MPSMult
alg::IDMRG1, oenvs=environments(ost, toapprox))
ψ = copy(ost)
mpo, above = toapprox
envs = IDMRGEnv(ost, oenvs)
envs = IDMRGEnvironments(ost, oenvs)
log = IterLog("IDMRG")
ϵ::Float64 = 2 * alg.tol

Expand Down Expand Up @@ -62,7 +62,7 @@ function approximate(ost::MPSMultiline, toapprox::Tuple{<:MPOMultiline,<:MPSMult
length(ost) < 2 && throw(ArgumentError("unit cell should be >= 2"))
mpo, above = toapprox
ψ = copy(ost)
envs = IDMRGEnv(ost, oenvs)
envs = IDMRGEnvironments(ost, oenvs)
ϵ::Float64 = 2 * alg.tol
log = IterLog("IDMRG2")

Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function ∂C(x::Vector, leftenv, rightenv)
end

#downproject for approximate
function c_proj(pos, below, envs::FinEnv)
function c_proj(pos, below, envs::FiniteEnvironments)

Check warning on line 202 in src/algorithms/derivatives.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/derivatives.jl#L202

Added line #L202 was not covered by tests
return ∂C(envs.above.CR[pos], leftenv(envs, pos + 1, below), rightenv(envs, pos, below))
end

Expand Down
63 changes: 6 additions & 57 deletions src/algorithms/expval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ acts, while the operator is either a `AbstractTensorMap` or a `FiniteMPO`.
* `ψ::AbstractMPS` : the state on which to compute the expectation value
* `O::Union{AbstractMPO,Pair}` : the operator to compute the expectation value of.
This can either be an `AbstractMPO`, or a pair of indices and local operator..
* `environments::Cache` : the environments to use for the calculation. If not given, they will be calculated.
* `environments::AbstractMPSEnvironments` : the environments to use for the calculation. If not given, they will be calculated.
# Examples
```jldoctest
Expand Down Expand Up @@ -97,51 +97,21 @@ function contract_mpo_expval(AC::MPSTensor, GL::MPSTensor, O::MPOTensor, GR::MPS
return @plansor GL[1 2; 3] * AC[3 7; 5] * GR[5 8; 6] *
O[2 4; 7 8] * conj(ACbar[1 4; 6])
end
# function expectation_value(ψ::FiniteMPS, H::MPOHamiltonian,
# envs::Cache=environments(ψ, H))
# L = length(ψ) ÷ 2
# GL = leftenv(envs, L, ψ)
# GR = rightenv(envs, L, ψ)
# AC = ψ.AC[L]
# E = sum(keys(H[L])) do (j, k)
# return contract_mpo_expval(AC, GL[j], H[L][j, k], GR[k], AC)
# # return @plansor GL[j][1 2; 3] * AC[3 7; 5] * GR[k][5 8; 6] * conj(AC[1 4; 6]) *
# # H[L][j, k][2 4; 7 8]
# end
# return E / norm(ψ)^2
# end

function expectation_value::FiniteMPS, H::FiniteMPOHamiltonian,
envs::Cache=environments(ψ, H))
envs::AbstractMPSEnvironments=environments(ψ, H))
return dot(ψ, H, ψ, envs) / dot(ψ, ψ)
end
# function expectation_value(ψ::FiniteQP, H::MPOHamiltonian)
# return expectation_value(convert(FiniteMPS, ψ), H)
# end
# function expectation_value(ψ::InfiniteMPS, H::MPOHamiltonian,
# envs::Cache=environments(ψ, H))
# # TODO: this presumably could be done more efficiently
# return sum(1:length(ψ)) do i
# return sum((H.odim):-1:1) do j
# ρ_LL = r_LL(ψ, i)
# util = fill_data!(similar(ψ.AL[1], space(envs.lw[H.odim, i + 1], 2)), one)
# GL = leftenv(envs, i, ψ)
# return @plansor (GL[j] * TransferMatrix(ψ.AL[i], H[i][j, H.odim], ψ.AL[i]))[1 2;
# 3] *
# ρ_LL[3; 1] * conj(util[2])
# end
# end
# end

function expectation_value::InfiniteMPS, H::InfiniteMPOHamiltonian,
envs::Cache=environments(ψ, H))
envs::AbstractMPSEnvironments=environments(ψ, H))
return sum(1:length(ψ)) do i
util = fill_data!(similar.AL[1], right_virtualspace(H, i)[end]), one)
@plansor GR[-1 -2; -3] := r_LL(ψ, i)[-1; -3] * conj(util[-2])
return contract_mpo_expval.AL[i], leftenv(envs, i, ψ), H[i][:, 1, 1, end], GR)
end
end

# no definition for WindowMPS -> not well defined

# DenseMPO
# --------
function expectation_value::FiniteMPS, mpo::FiniteMPO)
Expand Down Expand Up @@ -185,27 +155,6 @@ function expectation_value(ψ, ops::LazySum, envs::MultipleEnvironments)
return sum(((op, env),) -> expectation_value(ψ, op, env), zip(ops.ops, envs))
end

# Transfer matrices
# -----------------
# function expectation_value(ψ::InfiniteMPS, mpo::DenseMPO)
# return expectation_value(convert(MPSMultiline, ψ), convert(MPOMultiline, mpo))
# end
# function expectation_value(ψ::MPSMultiline, mpo::MPOMultiline)
# return expectation_value(ψ, environments(ψ, mpo))
# end
# function expectation_value(ψ::InfiniteMPS, ca::PerMPOInfEnv)
# return expectation_value(convert(MPSMultiline, ψ), ca)
# end
# function expectation_value(ψ::MPSMultiline, O::MPOMultiline, ca::PerMPOInfEnv)
# retval = PeriodicMatrix{scalartype(ψ)}(undef, size(ψ, 1), size(ψ, 2))
# for (i, j) in product(1:size(ψ, 1), 1:size(ψ, 2))
# retval[i, j] = @plansor leftenv(ca, i, j, ψ)[1 2; 3] * O[i, j][2 4; 6 5] *
# ψ.AC[i, j][3 6; 7] * rightenv(ca, i, j, ψ)[7 5; 8] *
# conj(ψ.AC[i + 1, j][1 4; 8])
# end
# return retval
# end

# for now we also have LinearCombination
function expectation_value(ψ, H::LinearCombination, envs::LazyLincoCache=environments(ψ, H))
return sum(((c, op, env),) -> c * expectation_value(ψ, op, env),
Expand All @@ -215,7 +164,7 @@ end
# ProjectionOperator
# ------------------
function expectation_value::FiniteMPS, O::ProjectionOperator,
envs::FinEnv=environments(ψ, O))
envs::FiniteEnvironments=environments(ψ, O))
ens = zeros(scalartype(ψ), length(ψ))
for i in 1:length(ψ)
operator = ∂∂AC(i, ψ, O, envs)
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/groundstate/find_groundstate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ optimization algorithm will be attempted based on the supplied keywords.
- `maxiter::Int`: maximum amount of iterations
- `verbosity::Int`: display progress information
"""
function find_groundstate::AbstractMPS, H, envs::Cache=environments(ψ, H);
function find_groundstate::AbstractMPS, H,
envs::AbstractMPSEnvironments=environments(ψ, H);
tol=Defaults.tol, maxiter=Defaults.maxiter,
verbosity=Defaults.verbosity, trscheme=nothing)
if isa(ψ, InfiniteMPS)
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/groundstate/idmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ end
function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG1, oenvs=environments(ost, H))
ϵ::Float64 = calc_galerkin(ost, oenvs)
ψ = copy(ost)
envs = IDMRGEnv(ost, oenvs)
envs = IDMRGEnvironments(ost, oenvs)
log = IterLog("IDMRG")

LoggingExtras.withlevel(; alg.verbosity) do
Expand Down Expand Up @@ -106,7 +106,7 @@ function find_groundstate(ost::InfiniteMPS, H, alg::IDMRG2, oenvs=environments(o
ϵ::Float64 = calc_galerkin(ost, oenvs)

ψ = copy(ost)
envs = IDMRGEnv(ost, oenvs)
envs = IDMRGEnvironments(ost, oenvs)
log = IterLog("IDMRG2")

LoggingExtras.withlevel(; alg.verbosity) do
Expand Down
9 changes: 5 additions & 4 deletions src/algorithms/timestep/tdvp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ algorithm for time evolution.
end

function timestep::InfiniteMPS, H, t::Number, dt::Number, alg::TDVP,
envs::Union{Cache,MultipleEnvironments}=environments(ψ, H);
envs::AbstractMPSEnvironments=environments(ψ, H);
leftorthflag=true)
temp_ACs = similar.AC)
temp_CRs = similar.CR)
Expand Down Expand Up @@ -59,7 +59,7 @@ function timestep(ψ::InfiniteMPS, H, t::Number, dt::Number, alg::TDVP,
end

function timestep!::AbstractFiniteMPS, H, t::Number, dt::Number, alg::TDVP,
envs::Union{Cache,MultipleEnvironments}=environments(ψ, H))
envs::AbstractMPSEnvironments=environments(ψ, H))

# sweep left to right
for i in 1:(length(ψ) - 1)
Expand Down Expand Up @@ -113,7 +113,7 @@ algorithm for time evolution.
end

function timestep!::AbstractFiniteMPS, H, t::Number, dt::Number, alg::TDVP2,
envs=environments(ψ, H))
envs::AbstractMPSEnvironments=environments(ψ, H))

# sweep left to right
for i in 1:(length(ψ) - 1)
Expand Down Expand Up @@ -152,6 +152,7 @@ end

#copying version
function timestep::AbstractFiniteMPS, H, time::Number, timestep::Number,
alg::Union{TDVP,TDVP2}, envs=environments(ψ, H); kwargs...)
alg::Union{TDVP,TDVP2}, envs::AbstractMPSEnvironments=environments(ψ, H);
kwargs...)
return timestep!(copy(ψ), H, time, timestep, alg, envs; kwargs...)
end
Loading

0 comments on commit 0ad721d

Please sign in to comment.