Skip to content

Commit d36f198

Browse files
site_range keyword for truncate! (#971)
1 parent 3221d34 commit d36f198

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/mps/abstractmps.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,20 +1598,25 @@ end
15981598
Perform a truncation of all bonds of an MPS/MPO,
15991599
using the truncation parameters (cutoff,maxdim, etc.)
16001600
provided as keyword arguments.
1601+
1602+
Keyword arguments:
1603+
* `site_range`=1:N - only truncate the MPS bonds between these sites
16011604
"""
16021605
function truncate!(M::AbstractMPS; alg="frobenius", kwargs...)
16031606
return truncate!(Algorithm(alg), M; kwargs...)
16041607
end
16051608

1606-
function truncate!(::Algorithm"frobenius", M::AbstractMPS; kwargs...)
1609+
function truncate!(
1610+
::Algorithm"frobenius", M::AbstractMPS; site_range=1:length(M), kwargs...
1611+
)
16071612
N = length(M)
16081613

16091614
# Left-orthogonalize all tensors to make
16101615
# truncations controlled
1611-
orthogonalize!(M, N)
1616+
orthogonalize!(M, last(site_range))
16121617

16131618
# Perform truncations in a right-to-left sweep
1614-
for j in reverse(2:N)
1619+
for j in reverse((first(site_range) + 1):last(site_range))
16151620
rinds = uniqueinds(M[j], M[j - 1])
16161621
ltags = tags(commonind(M[j], M[j - 1]))
16171622
U, S, V = svd(M[j], rinds; lefttags=ltags, kwargs...)

test/mps.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,12 @@ end
705705

706706
@test inner(M, M0) > 0.1
707707
end
708+
709+
@testset "truncate! with site_range" begin
710+
M = basicRandomMPS(10; dim=10)
711+
truncate!(M; site_range=3:7, maxdim=2)
712+
@test linkdims(M) == [2, 4, 2, 2, 2, 2, 8, 4, 2]
713+
end
708714
end
709715

710716
@testset "Other MPS methods" begin

0 commit comments

Comments
 (0)