Skip to content

Commit

Permalink
Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Dec 13, 2023
1 parent 5de13d3 commit 99ed650
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 122 deletions.
4 changes: 3 additions & 1 deletion src/MPSKit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export entanglementplot, transferplot

# hamiltonian things
export Cache
export SparseMPO, MPOHamiltonian, DenseMPO, MPOMultiline, UntimedOperator, TimedOperator, LazySum
export SparseMPO, MPOHamiltonian, DenseMPO, MPOMultiline
export UntimedOperator, TimedOperator, LazySum

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

Expand Down
27 changes: 17 additions & 10 deletions src/algorithms/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,20 @@ function ∂∂AC2(pos::Int, state, opp::ProjectionOperator, env)
rightenv(env, pos + 1, state))
end


# time dependent derivate operators
(h::UntimedOperator{<:DerivativeOperator})(y, args...) = h.f * h.op(y)
(h::TimedOperator{<:DerivativeOperator})(y, t::Number) = h.f(t) * h.op(y)
(x::LazySum{<:Union{MultipliedOperator{D}, D} where {D<:DerivativeOperator}})(y, t::Number) = sum(O -> O(y,t), x)
(x::LazySum{<:Union{MultipliedOperator{D}, D} where {D<:DerivativeOperator}})(y) = sum(O -> O(y), x)
Base.:*(h::LazySum{<:Union{D,MultipliedOperator{D}} where {D<:DerivativeOperator}}, v) = h(v)
function (x::LazySum{<:Union{MultipliedOperator{D},D} where {D<:DerivativeOperator}})(y,
t::Number)
return sum(O -> O(y, t), x)
end
function (x::LazySum{<:Union{MultipliedOperator{D},D} where {D<:DerivativeOperator}})(y)
return sum(O -> O(y), x)
end
function Base.:*(h::LazySum{<:Union{D,MultipliedOperator{D}} where {D<:DerivativeOperator}},
v)
return h(v)
end

function ∂∂C(pos::Int, mps, opp::MultipliedOperator, cache)
return MultipliedOperator(∂∂C(pos::Int, mps, opp.op, cache), opp.f)
Expand All @@ -301,16 +308,16 @@ function ∂∂AC2(pos::Int, mps, opp::MultipliedOperator, cache)
end

function ∂∂C(pos::Int, mps, opp::LazySum, cache::MultipleEnvironments)
return LazySum{Union{MPO_∂∂C,MultipliedOperator{<:MPO_∂∂C}}}(map((op, openv) -> ∂∂C(pos, mps, op, openv), opp.ops,
cache.envs))
suboperators = map((op, openv) -> ∂∂C(pos, mps, op, openv), opp.ops, cache.envs)
return LazySum{Union{MPO_∂∂C,MultipliedOperator{<:MPO_∂∂C}}}(suboperators)
end

function ∂∂AC(pos::Int, mps, opp::LazySum, cache::MultipleEnvironments)
return LazySum{Union{MPO_∂∂AC,MultipliedOperator{<:MPO_∂∂AC}}}(map((op, openv) -> ∂∂AC(pos, mps, op, openv), opp.ops,
cache.envs))
suboperators = map((op, openv) -> ∂∂AC(pos, mps, op, openv), opp.ops, cache.envs)
return LazySum{Union{MPO_∂∂AC,MultipliedOperator{<:MPO_∂∂AC}}}(suboperators)
end

function ∂∂AC2(pos::Int, mps, opp::LazySum, cache::MultipleEnvironments)
return LazySum{Union{MPO_∂∂AC2,MultipliedOperator{<:MPO_∂∂AC2}}}(map((op, openv) -> ∂∂AC2(pos, mps, op, openv), opp.ops,
cache.envs))
suboperators = map((op, openv) -> ∂∂AC2(pos, mps, op, openv), opp.ops, cache.envs)
return LazySum{Union{MPO_∂∂AC2,MultipliedOperator{<:MPO_∂∂AC2}}}(suboperators)
end
4 changes: 2 additions & 2 deletions src/algorithms/timestep/integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ function integrate end

# default for things that are callable on two arguments
_eval_t(f, t::Number) = Base.Fix2(f, t)
_eval_x(f, x) = Base.Fix1(f, x)
_eval_x(f, x) = Base.Fix1(f, x)

Check warning on line 18 in src/algorithms/timestep/integrators.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/timestep/integrators.jl#L18

Added line #L18 was not covered by tests

function integrate(f, y₀, t::Number, dt::Number, alg::Union{Arnoldi,Lanczos})
y, convhist = exponentiate(_eval_t(f, t), -1im*dt, y₀, alg)
y, convhist = exponentiate(_eval_t(f, t), -1im * dt, y₀, alg)
convhist.converged == 0 && @warn "integration failed $(convhist.normres)"
return y
end
52 changes: 15 additions & 37 deletions src/algorithms/timestep/tdvp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ algorithm for time evolution.
finalize::F = Defaults._finalize
end

function timestep(
Ψ::InfiniteMPS,
H,
t::Number,
dt::Number,
alg::TDVP,
envs::Union{Cache,MultipleEnvironments}=environments(Ψ, H);
leftorthflag=true,
)
function timestep::InfiniteMPS, H, t::Number, dt::Number, alg::TDVP,
envs::Union{Cache,MultipleEnvironments}=environments(Ψ, H);
leftorthflag=true)
temp_ACs = similar.AC)
temp_CRs = similar.CR)
@sync for (loc, (ac, c)) in enumerate(zip.AC, Ψ.CR))
Expand Down Expand Up @@ -64,14 +58,8 @@ function timestep(
return newΨ, envs
end

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

# sweep left to right
for i in 1:(length(Ψ) - 1)
Expand Down Expand Up @@ -124,9 +112,8 @@ algorithm for time evolution.
finalize::F = Defaults._finalize
end

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

# sweep left to right
for i in 1:(length(Ψ) - 1)
Expand All @@ -139,41 +126,32 @@ function timestep!(
Ψ.AC[i + 1] = (complex(nc), _transpose_front(nar))

if i != (length(Ψ) - 1)
Ψ.AC[i + 1] = integrate(
∂∂AC(i + 1, Ψ, H, envs), Ψ.AC[i + 1], t, -dt / 2, alg.integrator
)
Ψ.AC[i + 1] = integrate(∂∂AC(i + 1, Ψ, H, envs), Ψ.AC[i + 1], t, -dt / 2,
alg.integrator)
end
end

# sweep right to left
for i in length(Ψ):-1:2
ac2 = _transpose_front.AL[i - 1]) * _transpose_tail.AC[i])
h_ac2 = ∂∂AC2(i - 1, Ψ, H, envs)
nac2 = integrate(h_ac2, ac2, t + dt / 2, dt / 2, alg.integrator)
nac2 = integrate(h_ac2, ac2, t + dt / 2, dt / 2, alg.integrator)

nal, nc, nar = tsvd!(nac2; trunc=alg.trscheme, alg=TensorKit.SVD())
Ψ.AC[i - 1] = (nal, complex(nc))
Ψ.AC[i] = (complex(nc), _transpose_front(nar))

if i != 2
Ψ.AC[i - 1] = integrate(
∂∂AC(i - 1, Ψ, H, envs), Ψ.AC[i - 1], t + dt / 2, -dt / 2, alg.integrator
)
Ψ.AC[i - 1] = integrate(∂∂AC(i - 1, Ψ, H, envs), Ψ.AC[i - 1], t + dt / 2,
-dt / 2, alg.integrator)
end
end

return Ψ, envs
end

#copying version
function timestep(
Ψ::AbstractFiniteMPS,
H,
time::Number,
timestep::Number,
alg::Union{TDVP,TDVP2},
envs=environments(Ψ, H);
kwargs...,
)
function timestep::AbstractFiniteMPS, H, time::Number, timestep::Number,
alg::Union{TDVP,TDVP2}, envs=environments(Ψ, H); kwargs...)
return timestep!(copy(Ψ), H, time, timestep, alg, envs; kwargs...)
end
end
10 changes: 4 additions & 6 deletions src/algorithms/timestep/time_evolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ through each of the time points obtained by iterating t_span.
function time_evolve end, function time_evolve! end

# TODO: is it possible to remove this code-duplication?
function time_evolve(
ψ, H, t_span::AbstractVector{<:Number}, alg, envs=environments(ψ, H); verbose=false
)
function time_evolve(ψ, H, t_span::AbstractVector{<:Number}, alg, envs=environments(ψ, H);
verbose=false)
for (t, dt) in zip(t_span[2:end], diff(t_span))
elapsed = @elapsed ψ, envs = timestep(ψ, H, t, dt, alg, envs)
verbose && @info "Timestep iteration:" t elapsed
ψ, envs = alg.finalize(t, ψ, H, envs)::Tuple{typeof(ψ),typeof(envs)}
end
return ψ, envs
end
function time_evolve!(
ψ, H, t_span::AbstractVector{<:Number}, alg, envs=environments(ψ, H); verbose=false
)
function time_evolve!(ψ, H, t_span::AbstractVector{<:Number}, alg, envs=environments(ψ, H);

Check warning on line 27 in src/algorithms/timestep/time_evolve.jl

View check run for this annotation

Codecov / codecov/patch

src/algorithms/timestep/time_evolve.jl#L27

Added line #L27 was not covered by tests
verbose=false)
for (t, dt) in zip(t_span[2:end], diff(t_span))
elapsed = @elapsed ψ, envs = timestep!(ψ, H, t, dt, alg, envs)
verbose && @info "Timestep iteration:" t elapsed
Expand Down
4 changes: 2 additions & 2 deletions src/operators/lazysum.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end

# For users
# using (t) should return NotTimeDependent LazySum
(x::LazySum)(t::Number) = safe_eval(x, t)
(x::LazySum)(t::Number) = safe_eval(x, t)
Base.sum(x::LazySum) = safe_eval(x) #so it works for untimedoperator

# we define the addition for LazySum and we do the rest with this
Expand All @@ -54,6 +54,6 @@ end

Base.:+(op1::LazySum, op2) = op1 + LazySum(op2)
Base.:+(op1, op2::LazySum) = LazySum(op1) + op2
Base.:+(op1::MultipliedOperator, op2::MultipliedOperator) = LazySum([op1,op2])
Base.:+(op1::MultipliedOperator, op2::MultipliedOperator) = LazySum([op1, op2])

Base.repeat(x::LazySum, args...) = LazySum(repeat.(x, args...))
12 changes: 6 additions & 6 deletions src/operators/multipliedoperator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ const UntimedOperator{O} = MultipliedOperator{O,<:Union{Real,One}}
TimedOperator(x::O, f::F) where {F<:Function,O} = MultipliedOperator(x, f)
UntimedOperator(x::O, c::C) where {C<:Union{Real,One},O} = MultipliedOperator(x, c)

TimedOperator(x) = TimedOperator(x,t->One())
UntimedOperator(x) = UntimedOperator(x,One())
TimedOperator(x) = TimedOperator(x, t -> One())
UntimedOperator(x) = UntimedOperator(x, One())

Check warning on line 30 in src/operators/multipliedoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/multipliedoperator.jl#L29-L30

Added lines #L29 - L30 were not covered by tests

# Holy traits
TimeDependence(x::TimedOperator) = TimeDependent()

# For internal use only
_eval_at(x::UntimedOperator) = x.f * x.op
_eval_at(x::UntimedOperator, ::Number) = x #_eval_at(x)
_eval_at(x::TimedOperator, t::Number) = UntimedOperator(x.op,x.f(t))
_eval_at(x::TimedOperator, t::Number) = UntimedOperator(x.op, x.f(t))

# For users
(x::UntimedOperator)() = _eval_at(x)
(x::TimedOperator)(t::Number) = _eval_at(x,t)
(x::UntimedOperator)() = _eval_at(x)
(x::TimedOperator)(t::Number) = _eval_at(x, t)

# what to do when we multiply by a scalar
Base.:*(op::UntimedOperator, b::Number) = UntimedOperator(op.op, b * op.f)
Base.:*(op::TimedOperator, b::Number) = TimedOperator(op.op, t -> b * op.f(t))
Base.:*(op::TimedOperator, b::Number) = TimedOperator(op.op, t -> b * op.f(t))
Base.:*(b::Number, op::MultipliedOperator) = op * b

Check warning on line 47 in src/operators/multipliedoperator.jl

View check run for this annotation

Codecov / codecov/patch

src/operators/multipliedoperator.jl#L45-L47

Added lines #L45 - L47 were not covered by tests

Base.:*(op::TimedOperator, g::Function) = TimedOperator(op.op, t -> g(t) * op.f(t)) #slightly dangerous
Expand Down
40 changes: 19 additions & 21 deletions test/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,28 @@ end
E₀ = expectation_value(ψ₀, H)

@testset "Finite $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in algs
ψ, envs = timestep(ψ₀, H, 0., dt, alg)
ψ, envs = timestep(ψ₀, H, 0.0, dt, alg)
E = expectation_value(ψ, H, envs)
@test sum(E₀) sum(E) atol = 1e-2
end

Hlazy = LazySum([3 * H, 1.55 * H, -0.1 * H])

@testset "Finite LazySum $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in algs
ψ, envs = timestep(ψ₀, Hlazy, 0., dt, alg)
ψ, envs = timestep(ψ₀, Hlazy, 0.0, dt, alg)
E = expectation_value(ψ, Hlazy, envs)
@test (3 + 1.55 - 0.1) * sum(E₀) sum(E) atol = 1e-2
end

Ht = TimedOperator(H,t->4) + UntimedOperator(H,1.45)

@testset "Finite TimeDependent LazySum $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in algs
ψ, envs = timestep(ψ₀, Ht(1.), 0., dt, alg)
E = expectation_value(ψ, Ht(1.), envs)
Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45)

ψt, envst = timestep(ψ₀, Ht, 1., dt, alg)
Et = expectation_value(ψt, Ht(1.), envst)
@testset "Finite TimeDependent LazySum $(alg isa TDVP ? "TDVP" : "TDVP2")" for alg in
algs
ψ, envs = timestep(ψ₀, Ht(1.0), 0.0, dt, alg)
E = expectation_value(ψ, Ht(1.0), envs)

ψt, envst = timestep(ψ₀, Ht, 1.0, dt, alg)
Et = expectation_value(ψt, Ht(1.0), envst)
@test sum(E) sum(Et) atol = 1e-8
end

Expand All @@ -125,30 +126,29 @@ end
E₀ = expectation_value(ψ₀, H)

@testset "Infinite TDVP" begin
ψ, envs = timestep(ψ₀, H, 0., dt, TDVP())
ψ, envs = timestep(ψ₀, H, 0.0, dt, TDVP())
E = expectation_value(ψ, H, envs)
@test sum(E₀) sum(E) atol = 1e-2
end

Hlazy = LazySum([3 * H, 1.55 * H, -0.1 * H])

@testset "Infinite LazySum TDVP" begin
ψ, envs = timestep(ψ₀, Hlazy, 0., dt, TDVP())
ψ, envs = timestep(ψ₀, Hlazy, 0.0, dt, TDVP())
E = expectation_value(ψ, Hlazy, envs)
@test (3 + 1.55 - 0.1) * sum(E₀) sum(E) atol = 1e-2
end

Ht = TimedOperator(H,t->4) + UntimedOperator(H,1.45)
Ht = TimedOperator(H, t -> 4) + UntimedOperator(H, 1.45)

@testset "Infinite TimeDependent LazySum" begin
ψ, envs = timestep(ψ₀, Ht(1.), 0., dt, TDVP())
E = expectation_value(ψ, Ht(1.), envs)
ψ, envs = timestep(ψ₀, Ht(1.0), 0.0, dt, TDVP())
E = expectation_value(ψ, Ht(1.0), envs)

ψt, envst = timestep(ψ₀, Ht, 1., dt, TDVP())
Et = expectation_value(ψt, Ht(1.), envst)
ψt, envst = timestep(ψ₀, Ht, 1.0, dt, TDVP())
Et = expectation_value(ψt, Ht(1.0), envst)
@test sum(E) sum(Et) atol = 1e-8
end

end

@testset "time_evolve" verbose = true begin
Expand All @@ -174,8 +174,6 @@ end
E = expectation_value(ψ, H, envs)
@test sum(E₀) sum(E) atol = 1e-2
end


end

@testset "leading_boundary" verbose = true begin
Expand Down Expand Up @@ -448,7 +446,7 @@ end
st2, _ = approximate(st, (W2, st), VUMPS(; verbose=false))
st3, _ = approximate(st, (W1, st), IDMRG1(; verbose=false))
st4, _ = approximate(st, (sW2, st), IDMRG2(; trscheme=truncdim(20), verbose=false))
st5, _ = timestep(st, th, 0., dt, TDVP())
st5, _ = timestep(st, th, 0.0, dt, TDVP())
st6 = changebonds(W1 * st, SvdCut(; trscheme=truncdim(10)))

@test abs(dot(st1, st5)) 1.0 atol = dt
Expand Down Expand Up @@ -484,7 +482,7 @@ end
expH = make_time_mpo(H, τ, WI())
ψ₂, = approximate(ψ₂, (expH, ψ₁), alg)
normalize!(ψ₂)
ψ₂′, = timestep(ψ₁, H, 0., τ, TDVP())
ψ₂′, = timestep(ψ₁, H, 0.0, τ, TDVP())
@test abs(dot(ψ₁, ψ₁)) abs(dot(ψ₂, ψ₂′)) atol = 0.001
end

Expand Down
Loading

0 comments on commit 99ed650

Please sign in to comment.