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

[ITensors] Customisable Arrow directions in SVD #1197

Merged
merged 19 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
02c3bea
Added customisable functionality for operating solely on the diagonal…
JoeyT1994 Sep 19, 2023
c143eea
Customisable arrow dirn on svd(). Correct decomposition for factorize…
JoeyT1994 Sep 19, 2023
598c59b
Formatting. Added tests for custom arrow dirns in SVD. Added test of …
JoeyT1994 Sep 19, 2023
d3bd38d
Merge remote-tracking branch 'upstream/main' into SVD_ArrowDirn_PR
JoeyT1994 Sep 19, 2023
f45c427
Update matrix_decomposition.jl
JoeyT1994 Sep 19, 2023
090ee4f
Added option for directily returning S from factorize_svd()
JoeyT1994 Sep 21, 2023
4d0b6fc
Merge branch 'SVD_ArrowDirn_PR' of github.com:JoeyT1994/ITensors.jl i…
JoeyT1994 Sep 21, 2023
b91033b
Fixed a few bugs
JoeyT1994 Sep 22, 2023
55ba6fe
Removed specific named map_diag! functions
JoeyT1994 Sep 25, 2023
70a1757
Moved Fermion SVD Test to test_fermions. Removed reliance on MPS for …
JoeyT1994 Sep 25, 2023
e5d2a47
Fixed bug causing failing test
JoeyT1994 Sep 25, 2023
384560e
Restricted Arrow Direction on factorize_svd() to just specifying the …
JoeyT1994 Oct 13, 2023
0fe34e7
factorize_svd ind dir kwarg = dir
JoeyT1994 Oct 16, 2023
5c97e30
Commented out test of deprecated kwarg
JoeyT1994 Oct 16, 2023
e8e3534
Merge branch 'main' into SVD_ArrowDirn_PR
mtfishman Oct 17, 2023
a4c6e74
Certain kwargs -> Function signature in factorize_svd
JoeyT1994 Oct 18, 2023
ad28a5c
Fixed bad forwarding of SVD argument
JoeyT1994 Oct 19, 2023
189abac
Dropped type constraint in svd
JoeyT1994 Oct 19, 2023
b242aef
Merge branch 'main' into SVD_ArrowDirn_PR
mtfishman Oct 19, 2023
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
22 changes: 22 additions & 0 deletions src/tensor_operations/matrix_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,25 @@ function exp(A::ITensor; kwargs...)
Lis = Ris'
return exp(A, Lis, Ris; kwargs...)
end

function map_diag!(f::Function, it_destination::ITensor, it_source::ITensor)
return itensor(map_diag!(f, tensor(it_destination), tensor(it_source)))
end
map_diag(f::Function, it::ITensor) = map_diag!(f, copy(it), it)

function map_diag!(f::Function, t_destination::Tensor, t_source::Tensor)
for i in 1:diaglength(t_destination)
NDTensors.setdiagindex!(t_destination, f(NDTensors.getdiagindex(t_source, i)), i)
end
return t_destination
end
map_diag(f::Function, t::Tensor) = map_diag!(f, copy(t), t)

# Convenience functions
sqrt_diag(it::ITensor) = map_diag(sqrt, it)
inv_diag(it::ITensor) = map_diag(inv, it)
invsqrt_diag(it::ITensor) = map_diag(inv ∘ sqrt, it)
pinv_diag(it::ITensor) = map_diag(pinv, it)
pinvsqrt_diag(it::ITensor) = map_diag(pinv ∘ sqrt, it)
sqrtabs_diag(it::ITensor) = map_diag(sqrt ∘ abs, it)
inv_sqrtabs_diag(it::ITensor) = map_diag(inv ∘ sqrt ∘ abs, it)
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 25 additions & 10 deletions src/tensor_operations/matrix_decomposition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Utrunc2, Strunc2, Vtrunc2 = svd(A, i, k; cutoff=1e-10);

See also: [`factorize`](@ref), [`eigen`](@ref)
"""
function svd(A::ITensor, Linds...; kwargs...)
function svd(A::ITensor, Linds...; leftdir=nothing, rightdir=nothing, kwargs...)
utags::TagSet = get(kwargs, :lefttags, get(kwargs, :utags, "Link,u"))
vtags::TagSet = get(kwargs, :righttags, get(kwargs, :vtags, "Link,v"))

Expand All @@ -133,11 +133,9 @@ function svd(A::ITensor, Linds...; kwargs...)
Ris = [α]
end

CL = combiner(Lis...)
CR = combiner(Ris...)

CL = combiner(Lis...; dir=leftdir)
CR = combiner(Ris...; dir=rightdir)
AC = A * CR * CL

cL = combinedind(CL)
cR = combinedind(CR)
if inds(AC) != (cL, cR)
Expand Down Expand Up @@ -531,7 +529,22 @@ function factorize_qr(A::ITensor, Linds...; kwargs...)
return L, R
end

function factorize_svd(A::ITensor, Linds...; kwargs...)
#
# Generic function implementing a square root decomposition of a diagonal, order 2 tensor with inds u, v
#
function sqrt_decomp(D::ITensor, u::Index, v::Index)
(storagetype(D) <: Diag || storagetype(D) <: DiagBlockSparse) ||
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
error("Must be a diagonal matrix ITensor.")
sqrtDL = diagITensor(u, dag(u)')
sqrtDR = diagITensor(v, dag(v)')
map_diag!(sqrt ∘ abs, sqrtDL, D)
map_diag!(sqrt ∘ abs, sqrtDR, D)
δᵤᵥ = copy(D)
map_diag!(sign, δᵤᵥ, D)
return sqrtDL, prime(δᵤᵥ), sqrtDR
end

function factorize_svd(A::ITensor, Linds...; (singular_values!)=nothing, kwargs...)
ortho::String = get(kwargs, :ortho, "left")
alg::String = get(kwargs, :svd_alg, "divide_and_conquer")
USV = svd(A, Linds...; kwargs..., alg=alg)
Expand All @@ -544,14 +557,16 @@ function factorize_svd(A::ITensor, Linds...; kwargs...)
elseif ortho == "right"
L, R = U * S, V
elseif ortho == "none"
sqrtS = S
sqrtS .= sqrt.(S)
L, R = U * sqrtS, sqrtS * V
replaceind!(L, v, u)
sqrtDL, δᵤᵥ, sqrtDR = sqrt_decomp(S, u, v)
sqrtDR = denseblocks(sqrtDR) * denseblocks(δᵤᵥ)
L, R = noprime(U * sqrtDL), noprime(V * sqrtDR)
else
error("In factorize using svd decomposition, ortho keyword
$ortho not supported. Supported options are left, right, or none.")
end

!isnothing(singular_values!) && (singular_values![] = S)

return L, R, spec
end

Expand Down
45 changes: 45 additions & 0 deletions test/base/test_svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,51 @@ include(joinpath(@__DIR__, "utils", "util.jl"))
@test Base.eltype(V) === eltype
end

@testset "svd arrow directions" begin

#Test Spins
n = 10
χ = 5
s = siteinds("S=1/2", n; conserve_qns=true)
state = [isodd(i) ? "Up" : "Dn" for i in 1:n]
ψ = randomMPS(s, state; linkdims=χ)
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
A = ψ[4] * ψ[5]
linds = [inds(A)[1], inds(A)[2]]

for leftdir in [ITensors.Out, ITensors.In]
for rightdir in [ITensors.Out, ITensors.In]
U, S, V = svd(A, linds...; leftdir, rightdir)
s1, s2 = inds(S)[1], inds(S)[2]
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
@test ITensors.dir(s1) == leftdir
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
@test ITensors.dir(s2) == rightdir
@test norm(U * S * V - A) <= 1e-14

L, R, spec = ITensors.factorize_svd(
A, linds...; leftdir=leftdir, rightdir=rightdir, ortho="none"
)
@test norm(L * R - A) <= 1e-14
end
end

#Test Fermions
#WARNING: CURRENTLY ONLY STABLE FOR BOTH ARROW DIRECTIONS = OUT ON THE SVD.
ITensors.enable_auto_fermion()
n = 5
χ = 2
s = siteinds("Fermion", n; conserve_qns=true)
state = [isodd(i) ? "Occ" : "Emp" for i in 1:n]
ψ = randomMPS(s, state; linkdims=χ)
A = ψ[3] * ψ[4]
linds = [inds(A)[1]]

L, R, spec = ITensors.factorize_svd(
A, linds...; leftdir=ITensors.Out, rightdir=ITensors.Out, ortho="none"
)
@test norm(L * R - A) <= 1e-14

ITensors.disable_auto_fermion()
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
end

# TODO: remove this test, it takes a long time
## @testset "Ill-conditioned matrix" begin
## d = 5000
Expand Down