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

Fix behavior of factorize mindim #1214

Merged
merged 5 commits into from
Oct 19, 2023
Merged
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
17 changes: 11 additions & 6 deletions src/tensor_operations/matrix_decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -661,9 +661,14 @@ function factorize(A::ITensor, Linds...; 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...)
dL, dR = dim(Lis), dim(indices(setdiff(inds(A), Lis)))
maxdim = get(kwargs, :maxdim, min(dL, dR))
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
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)
Expand All @@ -677,13 +682,13 @@ 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
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)
Expand Down
14 changes: 14 additions & 0 deletions test/base/test_decomp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading