Skip to content

Commit

Permalink
Replace isnothing check into truncate_sweep! function
Browse files Browse the repository at this point in the history
  • Loading branch information
jofrevalles committed Dec 4, 2024
1 parent c638e47 commit e5f676d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/Ansatz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,9 @@ Truncate the dimension of the virtual `bond`` of an [`Ansatz`](@ref) Tensor Netw
- Either `threshold` or `maxdim` must be provided. If both are provided, `maxdim` is used.
"""
function truncate!(tn::AbstractAnsatz, bond; threshold=nothing, maxdim=nothing, kwargs...)
if all(isnothing, (threshold, maxdim))
return tn
else
return truncate!(form(tn), tn, bond; threshold, maxdim, kwargs...)
end
all(isnothing, (threshold, maxdim)) && return tn

return truncate!(form(tn), tn, bond; threshold, maxdim, kwargs...)
end

"""
Expand Down
19 changes: 11 additions & 8 deletions src/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,9 @@ function evolve!(::NonCanonical, ψ::AbstractAnsatz, mpo::AbstractMPO; kwargs...
groupinds!(ψ, right_inds[i])
end

if any(!isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing))
truncate_sweep!(form(ψ), ψ; kwargs...)
else
truncate_sweep!(form), ψ; kwargs...)

if all(isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing))
normalize = get(kwargs, :normalize, true)
normalize && normalize!(ψ)
end
Expand All @@ -596,7 +596,7 @@ function evolve!(::MixedCanonical, ψ::AbstractAnsatz, mpo::AbstractMPO; kwargs.
initial_form = form(ψ)
mixed_canonize!(ψ, Site(nsites(ψ))) # We convert all the tensors to left-canonical form

normalize = get(kwargs, :normalize, false)
normalize = get(kwargs, :normalize, true)
evolve!(NonCanonical(), ψ, mpo; normalize, kwargs...)

mixed_canonize!(ψ, initial_form.orthog_center)
Expand All @@ -612,10 +612,9 @@ function evolve!(::Canonical, ψ::AbstractAnsatz, mpo::AbstractMPO; kwargs...)

# set `maxdim` and `threshold` to `nothing` so we later truncate in the `Canonical` form
evolve!(NonCanonical(), ψ, mpo; kwargs..., threshold=nothing, maxdim=nothing, normalize=false)
truncate_sweep!(Canonical(), ψ; kwargs...)

if any(!isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing))
truncate_sweep!(Canonical(), ψ; kwargs...)
else
if all(isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing))
normalize = get(kwargs, :normalize, true)
normalize && canonize!(ψ; normalize)
end
Expand All @@ -629,9 +628,11 @@ end
Do a right-to-left QR sweep on the [`AbstractMPO`](@ref) `ψ` and then left-to-right SVD sweep and truncate the tensors
according to the `threshold` or `maxdim` values. The bond is normalized if `normalize=true`.
"""
function truncate_sweep! end
truncate_sweep!::AbstractMPO; kwargs...) = truncate_sweep!(form(ψ), ψ; kwargs...)

function truncate_sweep!(::NonCanonical, ψ::AbstractMPO; kwargs...)
all(isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing)) && return ψ

for i in nsites(ψ):-1:2
canonize_site!(ψ, Site(i); direction=:left, method=:qr)
end
Expand All @@ -654,6 +655,8 @@ function truncate_sweep!(::MixedCanonical, ψ::AbstractMPO; kwargs...)
end

function truncate_sweep!(::Canonical, ψ::AbstractMPO; kwargs...)
all(isnothing, get.(Ref(kwargs), [:threshold, :maxdim], nothing)) && return ψ

for i in nsites(ψ):-1:2
canonize_site!(ψ, Site(i); direction=:left, method=:qr)
end
Expand Down

0 comments on commit e5f676d

Please sign in to comment.