From e8b0dd548c333d606b5f624dcaf8a92e785f9ad2 Mon Sep 17 00:00:00 2001 From: Ryan Levy Date: Wed, 18 Oct 2023 16:22:28 -0400 Subject: [PATCH 1/5] Fix behavior of factorize mindim --- src/tensor_operations/matrix_decomposition.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/tensor_operations/matrix_decomposition.jl b/src/tensor_operations/matrix_decomposition.jl index 03a622b692..ad894efc13 100644 --- a/src/tensor_operations/matrix_decomposition.jl +++ b/src/tensor_operations/matrix_decomposition.jl @@ -662,8 +662,9 @@ function factorize(A::ITensor, Linds...; kwargs...) # so eigen should only be used if a larger cutoff is requested) automatic_cutoff = 1e-12 Lis = indices(Linds...) - dL, dR = dim(Lis), dim(indices(setdiff(inds(A), Lis))) - maxdim = get(kwargs, :maxdim, min(dL, dR)) + dL, dR = dim(commoninds(inds(A), Lis)), dim(indices(setdiff(inds(A), Lis))) + # maxdim is forced to be at most the max given SVD + maxdim = min(get(kwargs, :maxdim, min(dL, dR)), min(dL, dR)) might_truncate = !isnothing(cutoff) || maxdim < min(dL, dR) if isnothing(which_decomp) @@ -683,7 +684,7 @@ function factorize(A::ITensor, Linds...; kwargs...) end L, R, spec = LR elseif which_decomp == "eigen" - L, R, spec = factorize_eigen(A, Linds...; kwargs...) + L, R, spec = factorize_eigen(A, Linds...; kwargs..., maxdim=maxdim) elseif which_decomp == "qr" L, R = factorize_qr(A, Linds...; kwargs...) spec = Spectrum(nothing, 0.0) From 0cacd23bc997e528548ce6610dbc3b5ec72c16a4 Mon Sep 17 00:00:00 2001 From: Ryan Levy Date: Wed, 18 Oct 2023 16:56:58 -0500 Subject: [PATCH 2/5] Update src/tensor_operations/matrix_decomposition.jl Co-authored-by: Matt Fishman --- src/tensor_operations/matrix_decomposition.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tensor_operations/matrix_decomposition.jl b/src/tensor_operations/matrix_decomposition.jl index ad894efc13..b5dccdae6a 100644 --- a/src/tensor_operations/matrix_decomposition.jl +++ b/src/tensor_operations/matrix_decomposition.jl @@ -662,7 +662,9 @@ function factorize(A::ITensor, Linds...; kwargs...) # so eigen should only be used if a larger cutoff is requested) automatic_cutoff = 1e-12 Lis = indices(Linds...) - dL, dR = dim(commoninds(inds(A), Lis)), dim(indices(setdiff(inds(A), Lis))) + Lis = commoninds(A, indices(Linds...)) + Ris = uniqueinds(A, Lis) + dL, dR = dim(Lis), dim(Ris) # maxdim is forced to be at most the max given SVD maxdim = min(get(kwargs, :maxdim, min(dL, dR)), min(dL, dR)) might_truncate = !isnothing(cutoff) || maxdim < min(dL, dR) From 742498c633e0e2d7e60c310962f919e7b28963d9 Mon Sep 17 00:00:00 2001 From: Ryan Levy Date: Wed, 18 Oct 2023 18:34:21 -0400 Subject: [PATCH 3/5] Switch to explicit nothing --- src/tensor_operations/matrix_decomposition.jl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tensor_operations/matrix_decomposition.jl b/src/tensor_operations/matrix_decomposition.jl index b5dccdae6a..63b2df9b70 100644 --- a/src/tensor_operations/matrix_decomposition.jl +++ b/src/tensor_operations/matrix_decomposition.jl @@ -630,7 +630,7 @@ Perform a factorization of `A` into ITensors `L` and `R` such that `A ≈ L * R` For truncation arguments, see: [`svd`](@ref) """ -function factorize(A::ITensor, Linds...; kwargs...) +function factorize(A::ITensor, Linds...; maxdim=nothing, kwargs...) ortho::String = get(kwargs, :ortho, "left") tags::TagSet = get(kwargs, :tags, "Link,fact") plev::Int = get(kwargs, :plev, 0) @@ -666,7 +666,10 @@ function factorize(A::ITensor, Linds...; kwargs...) Ris = uniqueinds(A, Lis) dL, dR = dim(Lis), dim(Ris) # maxdim is forced to be at most the max given SVD - maxdim = min(get(kwargs, :maxdim, min(dL, dR)), min(dL, dR)) + if isnothing(maxdim) + maxdim = min(dL, dR) + end + maxdim = min(maxdim, min(dL, dR)) might_truncate = !isnothing(cutoff) || maxdim < min(dL, dR) if isnothing(which_decomp) @@ -680,7 +683,7 @@ function factorize(A::ITensor, Linds...; kwargs...) end if which_decomp == "svd" - LR = factorize_svd(A, Linds...; kwargs...) + LR = factorize_svd(A, Linds...; kwargs..., maxdim=maxdim) if isnothing(LR) return nothing end From 2dd83f82d3095c2544f8a6b354d828a92a176e51 Mon Sep 17 00:00:00 2001 From: Ryan Levy Date: Thu, 19 Oct 2023 14:23:23 -0400 Subject: [PATCH 4/5] Add test --- test/base/test_decomp.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/base/test_decomp.jl b/test/base/test_decomp.jl index 68601f35a1..1de45f80f3 100644 --- a/test/base/test_decomp.jl +++ b/test/base/test_decomp.jl @@ -465,6 +465,20 @@ end @test blockdim(u, b) == blockdim(i, b) || blockdim(u, b) >= min_blockdim end end + + @testset "factorize with mindim" begin + l = Index(8, "l") + s1 = Index(2, "s1") + s2 = Index(2, "s2") + r = Index(2, "r") + + phi = randomITensor(l, s1, s2, r) + + U, B = factorize(phi, (l, s1); ortho="left", mindim=8, which_decomp="eigen") + + @test norm(U * B - phi) < 1E-5 + @test dim(commonind(U, B)) <= 4 + end end nothing From 182e412e237ee5854d95eff822d36353413857d0 Mon Sep 17 00:00:00 2001 From: Ryan Levy Date: Thu, 19 Oct 2023 14:23:41 -0400 Subject: [PATCH 5/5] Update src/tensor_operations/matrix_decomposition.jl Co-authored-by: Matt Fishman --- src/tensor_operations/matrix_decomposition.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tensor_operations/matrix_decomposition.jl b/src/tensor_operations/matrix_decomposition.jl index 63b2df9b70..9dc9a7c1f1 100644 --- a/src/tensor_operations/matrix_decomposition.jl +++ b/src/tensor_operations/matrix_decomposition.jl @@ -661,7 +661,6 @@ function factorize(A::ITensor, Linds...; maxdim=nothing, kwargs...) # Determines when to use eigen vs. svd (eigen is less precise, # so eigen should only be used if a larger cutoff is requested) automatic_cutoff = 1e-12 - Lis = indices(Linds...) Lis = commoninds(A, indices(Linds...)) Ris = uniqueinds(A, Lis) dL, dR = dim(Lis), dim(Ris)