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

implement truncation_error keyword arg for truncate! #99

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/abstractmps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1672,13 +1672,18 @@ provided as keyword arguments.

Keyword arguments:
* `site_range`=1:N - only truncate the MPS bonds between these sites
* `truncation_errors!` - if provided, will store the truncation error from each SVD performed in a single call to `truncate!`. This should be a `Ref` type, for example `truncation_errors! = Ref{Vector{Float64}}()`, which will be overwritten in the function.
"""
function truncate!(M::AbstractMPS; alg="frobenius", kwargs...)
return truncate!(Algorithm(alg), M; kwargs...)
end

function truncate!(
::Algorithm"frobenius", M::AbstractMPS; site_range=1:length(M), kwargs...
::Algorithm"frobenius",
M::AbstractMPS;
site_range=1:length(M),
(truncation_errors!)=nothing,
kwargs...,
)
N = length(M)

Expand All @@ -1687,10 +1692,15 @@ function truncate!(
orthogonalize!(M, last(site_range))

# Perform truncations in a right-to-left sweep
for j in reverse((first(site_range) + 1):last(site_range))
js = reverse((first(site_range) + 1):last(site_range))
for i in eachindex(js)
j = js[i]
rinds = uniqueinds(M[j], M[j - 1])
ltags = tags(commonind(M[j], M[j - 1]))
U, S, V = svd(M[j], rinds; lefttags=ltags, kwargs...)
U, S, V, spec = svd(M[j], rinds; lefttags=ltags, kwargs...)
if !isnothing(truncation_errors)
truncation_errors[][i] = spec.truncerr
end
M[j] = U
M[j - 1] *= (S * V)
setrightlim!(M, j)
Expand Down
12 changes: 12 additions & 0 deletions test/base/test_mps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,18 @@ end
truncate!(M; site_range=3:7, maxdim=2)
@test linkdims(M) == [2, 4, 2, 2, 2, 2, 8, 4, 2]
end

@testset "truncate! with truncation_errors" begin
N = 10
nbonds = N - 1
M = basicRandomMPS(10; dim=10)
truncation_errors = Ref{Vector{Float64}}()
truncation_errors[] = fill(-1.0, nbonds) # set to something other than zero for test.
Copy link
Member

@mtfishman mtfishman Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to design the code so that users don't have to initialize the list ahead of time. We could add a line:

if isnothing(truncation_errors!)
  truncation_errors![] = Vector{real(scalartype(M))}(undef, length(M) - 1)
end

at the top of truncate!(...) (note the change to truncation_errors! based on my comment above).

truncate!(M, maxdim=3, cutoff=1E-3, truncation_errors=truncation_errors)
@test all(truncation_errors[] .>= 0.0)
end


end

@testset "Other MPS methods" begin
Expand Down