[WIP] Implement styles in a top-to-bottom approach#360
[WIP] Implement styles in a top-to-bottom approach#360AFeuerpfeil wants to merge 5 commits intoQuantumKitHub:mainfrom
Conversation
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/algorithms/derivatives/hamiltonian_derivatives.jl b/src/algorithms/derivatives/hamiltonian_derivatives.jl
index da6717e..bf1407e 100644
--- a/src/algorithms/derivatives/hamiltonian_derivatives.jl
+++ b/src/algorithms/derivatives/hamiltonian_derivatives.jl
@@ -69,7 +69,8 @@ end
# Constructors
# ------------
-function AC_hamiltonian(::GeometryStyle, ::HamiltonianStyle,
+function AC_hamiltonian(
+ ::GeometryStyle, ::HamiltonianStyle,
site::Int, below, operator, above, envs
)
GL = leftenv(envs, site, below)
@@ -140,7 +141,8 @@ function AC_hamiltonian(::GeometryStyle, ::HamiltonianStyle,
)
end
-function AC2_hamiltonian(::GeometryStyle, ::HamiltonianStyle,
+function AC2_hamiltonian(
+ ::GeometryStyle, ::HamiltonianStyle,
site::Int, below, operator, above, envs
)
GL = leftenv(envs, site, below)
diff --git a/src/algorithms/derivatives/mpo_derivatives.jl b/src/algorithms/derivatives/mpo_derivatives.jl
index 6dc9fbd..d7bae10 100644
--- a/src/algorithms/derivatives/mpo_derivatives.jl
+++ b/src/algorithms/derivatives/mpo_derivatives.jl
@@ -23,8 +23,10 @@ MPO_AC2_Hamiltonian(GL, O1, O2, GR) = MPODerivativeOperator(GL, (O1, O2), GR)
# Constructors
# ------------
function C_hamiltonian(site::Int, below, operator, above, envs)
- return C_hamiltonian(GeometryStyle(below, operator, above), OperatorStyle(operator),
- site, below, operator, above, envs)
+ return C_hamiltonian(
+ GeometryStyle(below, operator, above), OperatorStyle(operator),
+ site, below, operator, above, envs
+ )
end
function C_hamiltonian(::GeometryStyle, ::OperatorStyle, site::Int, below, operator, above, envs)
return MPO_C_Hamiltonian(leftenv(envs, site + 1, below), rightenv(envs, site, below))
diff --git a/src/algorithms/expval.jl b/src/algorithms/expval.jl
index 17d74f9..ce8e424 100644
--- a/src/algorithms/expval.jl
+++ b/src/algorithms/expval.jl
@@ -121,13 +121,15 @@ function contract_mpo_expval2(
τ[3 4; 9 10] * A1[8 1 2; 5] * A2[5 7 13; 14] * O[10 12; 6 7]
end
-function expectation_value(::FiniteChainStyle, ::HamiltonianStyle,
+function expectation_value(
+ ::FiniteChainStyle, ::HamiltonianStyle,
ψ, H, envs = environments(ψ, H)
)
return dot(ψ, H, ψ, envs) / dot(ψ, ψ)
end
-function expectation_value(::InfiniteChainStyle, ::HamiltonianStyle,
+function expectation_value(
+ ::InfiniteChainStyle, ::HamiltonianStyle,
ψ, H, envs = environments(ψ, H)
)
return sum(1:length(ψ)) do i
@@ -148,7 +150,8 @@ end
function expectation_value(::InfiniteChainStyle, ::MPOStyle, ψ, mpo, envs...)
return expectation_value(convert(MultilineMPS, ψ), convert(MultilineMPO, mpo), envs...)
end
-function expectation_value(::InfiniteChainStyle, ::MPOStyle,
+function expectation_value(
+ ::InfiniteChainStyle, ::MPOStyle,
ψ::Multiline, O::Multiline,
envs::MultilineEnvironments = environments(ψ, O)
)
diff --git a/src/algorithms/toolbox.jl b/src/algorithms/toolbox.jl
index ed4a6a1..2e54341 100644
--- a/src/algorithms/toolbox.jl
+++ b/src/algorithms/toolbox.jl
@@ -91,7 +91,7 @@ function transfer_spectrum(above::AbstractMPS; kwargs...)
return transfer_spectrum(GeometryStyle(above), above; kwargs...)
end
function transfer_spectrum(
- ::InfiniteChainStyle, above::AbstractMPS;
+ ::InfiniteChainStyle, above::AbstractMPS;
below = above, tol = Defaults.tol, num_vals = 20,
sector = first(sectors(oneunit(left_virtualspace(above, 1))))
)
@@ -214,7 +214,8 @@ function variance(state::AbstractMPS, H::AbstractMPO, envs = environments(state,
return variance(GeometryStyle(state, H), OperatorStyle(H), state, H, envs)
end
-function variance(::InfiniteChainStyle, ::HamiltonianStyle,
+function variance(
+ ::InfiniteChainStyle, ::HamiltonianStyle,
state::AbstractMPS, H::AbstractMPO, envs = environments(state, H)
)
e_local = map(1:length(state)) do i
@@ -229,7 +230,8 @@ function variance(::InfiniteChainStyle, ::HamiltonianStyle,
return real(expectation_value(state, (H - H_renormalized)^2))
end
-function variance(::FiniteChainStyle, ::HamiltonianStyle,
+function variance(
+ ::FiniteChainStyle, ::HamiltonianStyle,
state::AbstractMPS, H::AbstractMPO, envs = environments(state, H)
)
H2 = H * H
diff --git a/src/environments/abstract_envs.jl b/src/environments/abstract_envs.jl
index 17b9fd9..718e71a 100644
--- a/src/environments/abstract_envs.jl
+++ b/src/environments/abstract_envs.jl
@@ -69,15 +69,17 @@ end
Determine an appropriate algorithm for computing the environments, based on the given `kwargs...`.
"""
function environment_alg(below::AbstractMPS, operator::AbstractMPO, above::AbstractMPS; kwargs...)
- return environment_alg(GeometryStyle(below, operator, above), OperatorStyle(operator),
- below, operator, above; kwargs...)
+ return environment_alg(
+ GeometryStyle(below, operator, above), OperatorStyle(operator),
+ below, operator, above; kwargs...
+ )
end
function environment_alg(
::InfiniteChainStyle, ::MPOStyle, ::AbstractMPS, ::AbstractMPO, ::AbstractMPS;
tol = Defaults.tol, maxiter = Defaults.maxiter, krylovdim = Defaults.krylovdim,
verbosity = Defaults.VERBOSE_NONE, eager = true
- )
+ )
# TODO: Wouldn't it be simpler to use Arnoldi(; eager=eager, verbosity = verbosity, kwargs...)? (Same for the other ones)
return Arnoldi(; tol, maxiter, krylovdim, verbosity, eager)
end
@@ -121,8 +123,9 @@ function leftenv(ca::AbstractMPSEnvironments, ind, state::AbstractMPS)
return leftenv(GeometryStyle(ca, state), ca, ind, state)
end
-function recalculate!(envs::AbstractMPSEnvironments,
- below::AbstractMPS, operator::AbstractMPO, above::AbstractMPS;
+function recalculate!(
+ envs::AbstractMPSEnvironments,
+ below::AbstractMPS, operator::AbstractMPO, above::AbstractMPS;
kwargs...
)
return recalculate!(
diff --git a/src/environments/infinite_envs.jl b/src/environments/infinite_envs.jl
index 532a858..275eaf1 100644
--- a/src/environments/infinite_envs.jl
+++ b/src/environments/infinite_envs.jl
@@ -28,11 +28,12 @@ function environments(
return recalculate!(envs, below, operator, above; kwargs...)
end
-function issamespace(envs::AbstractMPSEnvironments,
+function issamespace(
+ envs::AbstractMPSEnvironments,
below::AbstractMPS, operator::AbstractMPO, above::AbstractMPS
)
return issamespace(
- GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
+ GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
envs, below, operator, above
)
end
@@ -56,7 +57,8 @@ function issamespace(
return true
end
-function recalculate!(::InfiniteChainStyle, ::OperatorStyle,
+function recalculate!(
+ ::InfiniteChainStyle, ::OperatorStyle,
envs::AbstractMPSEnvironments, below::AbstractMPS,
operator::AbstractMPO,
above::AbstractMPS = below;
@@ -86,7 +88,7 @@ function initialize_environments(
below::AbstractMPS, operator::AbstractMPO, above::AbstractMPS = below
)
return initialize_environments(
- GeometryStyle(below, operator, above), OperatorStyle(operator),
+ GeometryStyle(below, operator, above), OperatorStyle(operator),
below, operator, above
)
end
@@ -105,12 +107,13 @@ function compute_leftenvs!(
operator::AbstractMPO, above::AbstractMPS, alg
)
return compute_leftenvs!(
- GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
+ GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
envs, below, operator, above, alg
)
end
-function compute_leftenvs!(::InfiniteChainStyle, ::MPOStyle,
+function compute_leftenvs!(
+ ::InfiniteChainStyle, ::MPOStyle,
envs::AbstractMPSEnvironments, below::AbstractMPS,
operator::AbstractMPO, above::AbstractMPS, alg
)
@@ -130,12 +133,13 @@ function compute_rightenvs!(
operator::AbstractMPO, above::AbstractMPS, alg
)
return compute_rightenvs!(
- GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
+ GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
envs, below, operator, above, alg
)
end
-function compute_rightenvs!(::InfiniteChainStyle, ::MPOStyle,
+function compute_rightenvs!(
+ ::InfiniteChainStyle, ::MPOStyle,
envs::AbstractMPSEnvironments, below::AbstractMPS,
operator::AbstractMPO, above::AbstractMPS, alg
)
@@ -158,7 +162,8 @@ end
# this avoids catastrophic blow-up of norms, while keeping the total normalized
# and does not lead to issues for negative overlaps and real entries.
-function TensorKit.normalize!(::InfiniteChainStyle, ::MPOStyle,
+function TensorKit.normalize!(
+ ::InfiniteChainStyle, ::MPOStyle,
envs::AbstractMPSEnvironments, below, operator, above
)
for i in 1:length(operator)
@@ -172,7 +177,8 @@ end
# InfiniteMPOHamiltonian environments
# -----------------------------------
-function initialize_environments(::InfiniteChainStyle, ::HamiltonianStyle,
+function initialize_environments(
+ ::InfiniteChainStyle, ::HamiltonianStyle,
below, operator, above = below
)
L = check_length(above, operator, below)
@@ -202,7 +208,8 @@ function initialize_environments(::InfiniteChainStyle, ::HamiltonianStyle,
return GLs, GRs
end
-function compute_leftenvs!(::InfiniteChainStyle, ::HamiltonianStyle,
+function compute_leftenvs!(
+ ::InfiniteChainStyle, ::HamiltonianStyle,
envs, below, operator, above, alg
)
L = check_length(below, above, operator)
@@ -260,8 +267,10 @@ function left_cyclethrough!(
index::Int, GL, below::AbstractMPS, operator::AbstractMPO,
above::AbstractMPS = below
)
- return left_cyclethrough!(GeometryStyle(below, operator, above), OperatorStyle(operator), index, GL,
- below, operator, above)
+ return left_cyclethrough!(
+ GeometryStyle(below, operator, above), OperatorStyle(operator), index, GL,
+ below, operator, above
+ )
end
function left_cyclethrough!(
::InfiniteChainStyle, ::HamiltonianStyle,
@@ -339,7 +348,7 @@ function right_cyclethrough!(
above::AbstractMPS = below
)
return right_cyclethrough!(
- GeometryStyle(below, operator, above), OperatorStyle(operator),
+ GeometryStyle(below, operator, above), OperatorStyle(operator),
index, GR, below, operator, above
)
end
@@ -371,7 +380,8 @@ function transfer_leftenv!(
envs::AbstractMPSEnvironments, below::AbstractMPS,
operator::AbstractMPO, above::AbstractMPS, site::Int
)
- return transfer_leftenv!(GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
+ return transfer_leftenv!(
+ GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
envs, below, operator, above, site
)
end
@@ -388,7 +398,8 @@ function transfer_rightenv!(
envs::AbstractMPSEnvironments, below::AbstractMPS,
operator::AbstractMPO, above::AbstractMPS, site::Int
)
- return transfer_rightenv!(GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
+ return transfer_rightenv!(
+ GeometryStyle(envs, below, operator, above), OperatorStyle(operator),
envs, below, operator, above, site
)
end |
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
lkdvos
left a comment
There was a problem hiding this comment.
This looks great, I really like that we can relax the input types.
|
It definitely feels like it is going in the right direction. An example, how the restriction only for the topmost function in the call tree looks like, is function environments(above::AbstractMPS, op::AbstractMPO, below::AbstractMPS)
return environments(GeometryStyle(above, op, below), OperatorStyle(op), above, op, below)
endand then function environments(::InfiniteChainStyle, ::HamiltonianStyle, above, op, below)
...
end |
I am currently working on getting the algorithms to run for user-defined
AbstractMPStypes.To this end, I will go through the algorithms (currently focussing on
IDMRGandVUMPS) and implement theStyleinterface for the highest-level functions, which are called in these algorithms.Examples for these are
calc_galerkin,expectation_value,environments,recalculate!,(A)C(2)_hamiltonian,transfer_leftenv!,transfer_rightenv!.