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

Minor improvements #225

Merged
merged 10 commits into from
Jan 17, 2025
10 changes: 2 additions & 8 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ version = "0.12.0"
[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
BlockTensorKit = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
FLoops = "cc61a311-1640-44b5-9fba-1b764f453329"
FastClosures = "9aa1b823-49e4-5ca5-8b0f-3971ec8bab6a"
FoldsThreads = "9c68100b-dfe1-47cf-94c8-95104e173443"
HalfIntegers = "f0d1745a-41c9-11e9-1dd9-e5d34d218721"
KrylovKit = "0b1a1467-8014-51b9-945f-bf0ae24f4b77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand All @@ -21,18 +19,15 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
TensorKit = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
TensorKitManifolds = "11fa318c-39cb-4a83-b1ed-cdc7ba1e3684"
Transducers = "28d57a85-8fef-5791-bfe6-a80928e7c999"
TupleTools = "9d95972d-f1c8-5527-a6e0-b4b365fa01f6"
VectorInterface = "409d34a3-91d5-4945-b6ec-7529ddf182d8"

[compat]
Accessors = "0.1"
Aqua = "0.8.9"
BlockTensorKit = "0.1.1"
Compat = "3.47, 4.10"
DocStringExtensions = "0.9.3"
FLoops = "0.1, 0.2"
FastClosures = "0.3"
FoldsThreads = "0.1"
HalfIntegers = "1.6.0"
KrylovKit = "0.8.3"
LinearAlgebra = "1.6"
Expand All @@ -49,7 +44,6 @@ TensorKitManifolds = "0.7"
TensorOperations = "5"
Test = "1"
TestExtras = "0.3"
Transducers = "0.4"
TupleTools = "1.6.0"
VectorInterface = "0.2, 0.3, 0.4, 0.5"
julia = "1.10"
Expand Down
106 changes: 61 additions & 45 deletions src/MPSKit.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,69 @@
module MPSKit

# Public API
# ----------
# utility:
export PeriodicArray, PeriodicVector, PeriodicMatrix
export WindowArray
export left_virtualspace, right_virtualspace, physicalspace
export braille

# states:
export FiniteMPS
export InfiniteMPS
export WindowMPS
export MultilineMPS
export QP, LeftGaugedQP, RightGaugedQP

# operators:
export AbstractMPO
export MPO, FiniteMPO, InfiniteMPO
export MPOHamiltonian, FiniteMPOHamiltonian, InfiniteMPOHamiltonian
export MultilineMPO
export UntimedOperator, TimedOperator, MultipliedOperator, LazySum

# environments:
export environments
export leftenv, rightenv

# algorithms:
export find_groundstate, find_groundstate!
export leading_boundary
export approximate, approximate!
export VUMPS, VOMPS, DMRG, DMRG2, IDMRG1, IDMRG2, GradientGrassmann
export excitations
export FiniteExcited, QuasiparticleAnsatz, ChepigaAnsatz, ChepigaAnsatz2
export time_evolve, timestep, timestep!, make_time_mpo
export TDVP, TDVP2, WI, WII, TaylorCluster
export changebonds, changebonds!
export VUMPSSvdCut, OptimalExpand, SvdCut, RandExpand
export propagator
export DynamicalDMRG, NaiveInvert, Jeckelmann
export exact_diagonalization, fidelity_susceptibility

# toolbox:
export expectation_value, correlator, variance
export correlation_length, marek_gap, transfer_spectrum
export entropy, entanglement_spectrum
export open_boundary_conditions, periodic_boundary_conditions
export entanglementplot, transferplot
export r_LL, l_LL, r_RR, l_RR, r_RL, r_LR, l_RL, l_LR # TODO: rename

# unexported
using Compat: @compat
@compat public DynamicTols
@compat public VERBOSE_NONE, VERBOSE_WARN, VERBOSE_CONV, VERBOSE_ITER, VERBOSE_ALL
@compat public IterLog, loginit!, logiter!, logfinish!, logcancel!

# Imports
# -------
using TensorKit
using TensorKit: BraidingTensor
using BlockTensorKit
using KrylovKit
using KrylovKit: KrylovAlgorithm
using OptimKit, FastClosures
using Base.Threads, FLoops, Transducers, FoldsThreads
using OptimKit
using Base.Threads
using Base.Iterators
using RecipesBase
using VectorInterface
Expand All @@ -22,49 +79,8 @@ using Base: @kwdef
using LoggingExtras
using OhMyThreads

# bells and whistles for mpses
export InfiniteMPS, FiniteMPS, WindowMPS, MultilineMPS
export PeriodicArray, PeriodicVector, PeriodicMatrix, WindowArray
export MPSTensor
export QP, LeftGaugedQP, RightGaugedQP
export r_LL, l_LL, r_RR, l_RR, r_RL, r_LR, l_RL, l_LR # should be properties

# useful utility functions?
export add_util_leg, max_Ds, recalculate!
export left_virtualspace, right_virtualspace, physicalspace
export entanglementplot, transferplot
export braille

# hamiltonian things
export AbstractMPO
export MPO, FiniteMPO, InfiniteMPO
export MPOHamiltonian, FiniteMPOHamiltonian, InfiniteMPOHamiltonian
export SparseMPO, DenseMPO, MultilineMPO
export UntimedOperator, TimedOperator, MultipliedOperator, LazySum

export ∂C, ∂AC, ∂AC2, environments, expectation_value, effective_excitation_hamiltonian
export leftenv, rightenv

# algos
export find_groundstate!, find_groundstate, leading_boundary
export VUMPS, VOMPS, DMRG, DMRG2, IDMRG1, IDMRG2, GradientGrassmann
export excitations, FiniteExcited, QuasiparticleAnsatz, ChepigaAnsatz, ChepigaAnsatz2
export marek_gap, correlation_length, correlator
export time_evolve, timestep!, timestep
export TDVP, TDVP2, make_time_mpo, WI, WII, TaylorCluster
export entanglement_spectrum, transfer_spectrum, variance
export changebonds!, changebonds, VUMPSSvdCut, OptimalExpand, SvdCut, RandExpand
export entropy
export propagator, NaiveInvert, Jeckelmann, DynamicalDMRG
export fidelity_susceptibility
export approximate!, approximate
export periodic_boundary_conditions, open_boundary_conditions
export exact_diagonalization

# transfer matrix
export TransferMatrix
export transfer_left, transfer_right

# Includes
# --------
include("algorithms/algorithm.jl")

# submodules
Expand Down
4 changes: 2 additions & 2 deletions src/algorithms/excitation/quasiparticleexcitation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function excitations(H, alg::QuasiparticleAnsatz, ϕ₀::InfiniteQP, lenvs, renv
num=1, kwargs...)
qp_envs(ϕ) = environments(ϕ, H, lenvs, renvs; kwargs...)
E = effective_excitation_renormalization_energy(H, ϕ₀, lenvs, renvs)
H_eff = @closure(ϕ -> effective_excitation_hamiltonian(H, ϕ, qp_envs(ϕ), E))
H_eff(ϕ) = effective_excitation_hamiltonian(H, ϕ, qp_envs(ϕ), E)

Es, ϕs, convhist = eigsolve(H_eff, ϕ₀, num, :SR, alg.alg)
convhist.converged < num &&
Expand Down Expand Up @@ -134,7 +134,7 @@ function excitations(H, alg::QuasiparticleAnsatz, ϕ₀::FiniteQP,
renvs=ϕ₀.trivial ? lenvs : environments(ϕ₀.right_gs, H); num=1)
qp_envs(ϕ) = environments(ϕ, H, lenvs, renvs)
E = effective_excitation_renormalization_energy(H, ϕ₀, lenvs, renvs)
H_eff = @closure(ϕ -> effective_excitation_hamiltonian(H, ϕ, qp_envs(ϕ), E))
H_eff(ϕ) = effective_excitation_hamiltonian(H, ϕ, qp_envs(ϕ), E)
Es, ϕs, convhist = eigsolve(H_eff, ϕ₀, num, :SR, alg.alg)

convhist.converged < num &&
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/grassmann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ The module exports nothing, and all references to it should be qualified, e.g.
module GrassmannMPS

using ..MPSKit
using ..MPSKit: AbstractMPSEnvironments, InfiniteEnvironments, MultilineEnvironments, ∂∂AC
using ..MPSKit: AbstractMPSEnvironments, InfiniteEnvironments, MultilineEnvironments, ∂∂AC,
recalculate!
using TensorKit
using OhMyThreads
import TensorKitManifolds.Grassmann
Expand Down
2 changes: 1 addition & 1 deletion src/operators/abstractmpo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const SparseMPO{O<:SparseBlockTensorMap} = AbstractMPO{O}
Base.size(mpo::AbstractMPO, args...) = size(parent(mpo), args...)
Base.length(mpo::AbstractMPO) = length(parent(mpo))

@inline Base.getindex(mpo::AbstractMPO, args...) = getindex(parent(mpo), args...)
@inline Base.getindex(mpo::AbstractMPO, i::Int) = getindex(parent(mpo), i)
@inline function Base.setindex!(mpo::AbstractMPO, value::MPOTensor, i::Int)
setindex!(parent(mpo), value, i)
return mpo
Expand Down
4 changes: 2 additions & 2 deletions src/operators/lazysum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ end

# for the AbstractArray interface
Base.size(x::LazySum) = size(x.ops)
Base.getindex(x::LazySum, i) = x.ops[i]
Base.getindex(x::LazySum, i::Int) = x.ops[i]
Base.length(x::LazySum) = prod(size(x))
Base.similar(x::LazySum, ::Type{S}, dims::Dims) where {S} = LazySum(similar(x.ops, S, dims))
Base.setindex!(A::LazySum, X, i) = setindex!(A.ops, X, i)
Base.setindex!(A::LazySum, X, i::Int) = (setindex!(A.ops, X, i); A)

# Holy traits
TimeDependence(x::LazySum) = istimed(x) ? TimeDependent() : NotTimeDependent()
Expand Down
1 change: 1 addition & 0 deletions src/operators/mpohamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ end

Base.:-(H::MPOHamiltonian, λs::AbstractVector{<:Number}) = H + (-λs)
Base.:-(λs::AbstractVector{<:Number}, H::MPOHamiltonian) = λs + (-H)
Base.:-(H1::MPOHamiltonian, H2::MPOHamiltonian) = H1 + (-H2)

function VectorInterface.scale!(H::InfiniteMPOHamiltonian, λ::Number)
foreach(parent(H)) do h
Expand Down
20 changes: 16 additions & 4 deletions src/states/finitemps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@
B<:MPSBondTensor}
return new{A,B}(ALs, ARs, ACs, Cs)
end
function FiniteMPS(ALs::Vector{Union{Missing,A}}, ARs::Vector{Union{Missing,A}},
ACs::Vector{Union{Missing,A}},
Cs::Vector{Union{Missing,B}}) where {A<:GenericMPSTensor,
B<:MPSBondTensor}
function FiniteMPS(ALs::Vector{MA}, ARs::Vector{MA},
ACs::Vector{MA},
Cs::Vector{MB}) where {MA<:Union{GenericMPSTensor,Missing},
MB<:Union{MPSBondTensor,Missing}}
A = _not_missing_type(MA)
B = _not_missing_type(MB)
length(ACs) == length(Cs) - 1 == length(ALs) == length(ARs) ||
throw(DimensionMismatch("length mismatch of tensors"))
sum(ismissing.(ACs)) + sum(ismissing.(Cs)) < length(ACs) + length(Cs) ||
Expand Down Expand Up @@ -126,6 +128,16 @@
end
end

_not_missing_type(::Type{Missing}) = throw(ArgumentError("Only missing type present"))

Check warning on line 131 in src/states/finitemps.jl

View check run for this annotation

Codecov / codecov/patch

src/states/finitemps.jl#L131

Added line #L131 was not covered by tests
function _not_missing_type(::Type{T}) where {T}
if T isa Union
return (!(T.a === Missing) && !(T.b === Missing)) ? T :
!(T.a === Missing) ? _not_missing_type(T.b) : _not_missing_type(T.b)
else
return T
end
end

function Base.getproperty(ψ::FiniteMPS, prop::Symbol)
if prop == :AL
return ALView(ψ)
Expand Down
8 changes: 5 additions & 3 deletions src/states/ortho.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
AR_tail = mul!(AC_tail, Q_C', Q_AC)
return _transpose_front(AR_tail)
end
# fix ambiguity + error
regauge!(::MPSBondTensor, ::MPSBondTensor; alg=QRpos()) = error("method ambiguity")

Check warning on line 167 in src/states/ortho.jl

View check run for this annotation

Codecov / codecov/patch

src/states/ortho.jl#L167

Added line #L167 was not covered by tests

# Implementation
# --------------
Expand Down Expand Up @@ -218,7 +220,7 @@
(; AL, C, A_tail, CA_tail) = state
for i in 1:length(AL)
mul!(CA_tail[i], C[i - 1], A_tail[i])
_repartition!(AL[i], CA_tail[i])
repartition!(AL[i], CA_tail[i])
AL[i], C[i] = leftorth!(AL[i]; alg=it.alg_orth)
end
normalize!(C[end])
Expand Down Expand Up @@ -275,9 +277,9 @@
(; A, AR, C, AC_tail) = state
for i in length(AR):-1:1
AC = mul!(AR[i], A[i], C[i]) # use AR as temporary storage for A * C
tmp = _repartition!(AC_tail[i], AC)
tmp = repartition!(AC_tail[i], AC)
C[i - 1], tmp = rightorth!(tmp; alg=it.alg_orth)
_repartition!(AR[i], tmp) # TODO: avoid doing this every iteration
repartition!(AR[i], tmp) # TODO: avoid doing this every iteration
end
normalize!(C[end])
return C[end]
Expand Down
16 changes: 8 additions & 8 deletions src/states/quasiparticle_state.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,41 +216,41 @@
Base.length(v::QP) = length(v.Xs)
Base.eltype(::Type{<:QP{<:Any,<:Any,T}}) where {T} = T

function LinearAlgebra.mul!(w::T, a, v::T) where {T<:QP}
function LinearAlgebra.mul!(w::T, a::Number, v::T) where {T<:QP}
@inbounds for (i, j) in zip(w.Xs, v.Xs)
LinearAlgebra.mul!(i, a, j)
end
return w
end

function LinearAlgebra.mul!(w::T, v::T, a) where {T<:QP}
function LinearAlgebra.mul!(w::T, v::T, a::Number) where {T<:QP}

Check warning on line 226 in src/states/quasiparticle_state.jl

View check run for this annotation

Codecov / codecov/patch

src/states/quasiparticle_state.jl#L226

Added line #L226 was not covered by tests
@inbounds for (i, j) in zip(w.Xs, v.Xs)
LinearAlgebra.mul!(i, j, a)
end
return w
end
function LinearAlgebra.rmul!(v::QP, a)
function LinearAlgebra.rmul!(v::QP, a::Number)
for x in v.Xs
LinearAlgebra.rmul!(x, a)
end
return v
end

function LinearAlgebra.axpy!(a, v::T, w::T) where {T<:QP}
function LinearAlgebra.axpy!(a::Number, v::T, w::T) where {T<:QP}
@inbounds for (i, j) in zip(w.Xs, v.Xs)
LinearAlgebra.axpy!(a, j, i)
end
return w
end
function LinearAlgebra.axpby!(a, v::T, b, w::T) where {T<:QP}
function LinearAlgebra.axpby!(a::Number, v::T, b::Number, w::T) where {T<:QP}

Check warning on line 245 in src/states/quasiparticle_state.jl

View check run for this annotation

Codecov / codecov/patch

src/states/quasiparticle_state.jl#L245

Added line #L245 was not covered by tests
@inbounds for (i, j) in zip(w.Xs, v.Xs)
LinearAlgebra.axpby!(a, j, b, i)
end
return w
end

Base.:*(v::QP, a) = mul!(similar(v), a, v)
Base.:*(a, v::QP) = mul!(similar(v), a, v)
Base.:*(v::QP, a::Number) = mul!(similar(v), a, v)
Base.:*(a::Number, v::QP) = mul!(similar(v), a, v)

Check warning on line 253 in src/states/quasiparticle_state.jl

View check run for this annotation

Codecov / codecov/patch

src/states/quasiparticle_state.jl#L253

Added line #L253 was not covered by tests

Base.zero(v::QP) = v * 0;

Expand All @@ -273,7 +273,7 @@

#step 0 : fuse the utility leg of B with the first leg of B
orig_Bs = map(i -> v[i], 1:length(v))
Bs = @closure map(orig_Bs) do t
Bs = map(orig_Bs) do t
frontmap = isomorphism(storagetype(t), fuse(utl * _firstspace(t)),
utl * _firstspace(t))
@plansor tt[-1 -2; -3] := t[1 -2; 2 -3] * frontmap[-1; 2 1]
Expand Down
Loading
Loading