Skip to content

Commit 770983c

Browse files
kshyattKatharine HyattKatharine Hyatt
authored
Support TruncationMultiplets (#602)
* New truncation strategy for degenerate clusters * Fixes and tests * Fix * Drop some collects * Restore collects * Fix for 1.10 --------- Co-authored-by: Katharine Hyatt <katharine.hyatt@ugent.be> Co-authored-by: Katharine Hyatt <katharine.s.hyatt@gmail.com>
1 parent b4b94b4 commit 770983c

9 files changed

Lines changed: 113 additions & 6 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ FiniteDifferences = "0.12"
5757
GPUArrays = "11.4.1"
5858
LRUCache = "1.6"
5959
LinearAlgebra = "1"
60-
MatrixAlgebraKit = "0.6.9"
60+
MatrixAlgebraKit = "0.6.13"
6161
Mooncake = "0.5.27"
6262
OhMyThreads = "0.8.0"
6363
PrecompileTools = "1"

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function MatrixAlgebraKit.findtruncated_svd(values::GPUSectorVector, strategy::S
8585
return Adapt.adapt(Vector, MatrixAlgebraKit.findtruncated(values, strategy))
8686
end
8787

88-
for strat in (:(MatrixAlgebraKit.TruncationByOrder), :(MatrixAlgebraKit.TruncationByError), :(MatrixAlgebraKit.TruncationIntersection), :(TensorKit.Factorizations.TruncationSpace))
88+
for strat in (:(MatrixAlgebraKit.TruncationByOrder), :(MatrixAlgebraKit.TruncationByError), :(MatrixAlgebraKit.TruncationIntersection), :(MatrixAlgebraKit.TruncationMultiplets), :(TensorKit.Factorizations.TruncationSpace))
8989
@eval function MatrixAlgebraKit.findtruncated_svd(values::GPUSectorVector, strategy::$strat)
9090
# returning a GPUSectorVector wrecks things in truncate_{co}domain
9191
# because of scalar indexing

‎src/TensorKit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export @tensor, @tensoropt, @ncon, ncon, @planar, @plansor
9999
export scalar, add!, contract!
100100

101101
# truncation schemes
102-
export notrunc, truncrank, trunctol, truncfilter, truncspace, truncerror
102+
export notrunc, truncrank, trunctol, truncfilter, truncspace, truncerror, truncmultiplets
103103

104104
# cache management
105105
export empty_globalcaches!

‎src/factorizations/factorizations.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ using MatrixAlgebraKit
2323
import MatrixAlgebraKit as MAK
2424
using MatrixAlgebraKit: AbstractAlgorithm, TruncatedAlgorithm, DiagonalAlgorithm
2525
using MatrixAlgebraKit: TruncationStrategy, NoTruncation, TruncationByValue, TruncationByError,
26-
TruncationIntersection, TruncationUnion, TruncationByFilter, TruncationByOrder
26+
TruncationIntersection, TruncationUnion, TruncationByFilter, TruncationByOrder,
27+
TruncationMultiplets
2728
using MatrixAlgebraKit: diagview
2829

2930
include("utility.jl")

‎src/factorizations/truncation.jl‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,37 @@ function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationUnion)
337337
return SectorDict{eltype(sectors), eltype(vals)}(sectors, vals)
338338
end
339339

340+
# Degenerate values can occur in different blocks, for example when the sectors are related by
341+
# a symmetry, so groups of degenerate values are determined across all blocks simultaneously.
342+
function MAK.findtruncated(values::SectorVector, strategy::TruncationMultiplets)
343+
return _findtruncated_multiplets(values, MAK.findtruncated(values, strategy.trunc), strategy)
344+
end
345+
function MAK.findtruncated_svd(values::SectorVector, strategy::TruncationMultiplets)
346+
return _findtruncated_multiplets(values, MAK.findtruncated_svd(values, strategy.trunc), strategy)
347+
end
348+
349+
function _findtruncated_multiplets(values::SectorVector, inds, strategy::TruncationMultiplets)
350+
keep = similar(values, Bool)
351+
to_keep = collect(parent(_ind_to_mask!(keep, inds)))
352+
kept_values = collect(parent(values))
353+
perm = sortperm(kept_values; by = abs, rev = true)
354+
MAK._keep_multiplets!(to_keep, kept_values, perm, strategy)
355+
copyto!(parent(keep), to_keep)
356+
return keep
357+
end
358+
359+
function _ind_to_mask!(keep::SectorVector{Bool}, inds::SectorVector{Bool})
360+
copyto!(parent(keep), parent(inds))
361+
return keep
362+
end
363+
function _ind_to_mask!(keep::SectorVector{Bool}, inds::SectorDict)
364+
fill!(parent(keep), false)
365+
for (c, ind) in pairs(inds)
366+
keep[c][ind] .= true
367+
end
368+
return keep
369+
end
370+
340371
# Truncation error
341372
# ----------------
342373
MAK.truncation_error(values::SectorVector, ind) = MAK.truncation_error!(copy(values), ind)

‎test/amd/factorizations.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,31 @@ for V in spacelist
434434
@test norm(t - U5 * S5 * Vᴴ5) ≈ ϵ5 atol = eps(real(T))^(4 / 5)
435435
@test minimum(diagview(S5)) >= λ
436436
@test abs(dim(domain(S5)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)
437+
438+
U0, S0, Vᴴ0 = svd_compact(t, alg)
439+
Sd = similar(S0)
440+
copyto!(Sd.data, real(T)[3 - mod(i, 3) for i in 0:(length(Sd.data) - 1)])
441+
td = U0 * Sd * Vᴴ0
442+
rtol = sqrt(eps(real(T)))
443+
_, Sr, _, _ = svd_trunc(td; trunc = truncrank(nvals), alg)
444+
for mode in (:down, :up)
445+
trunc = truncmultiplets(truncrank(nvals); rtol, mode)
446+
U6, S6, Vᴴ6, ϵ6 = @constinferred svd_trunc(td; trunc, alg)
447+
@test td * Vᴴ6' ≈ U6 * S6
448+
@test isisometric(U6)
449+
@test isisometric(Vᴴ6; side = :right)
450+
@test norm(td - U6 * S6 * Vᴴ6) ≈ ϵ6 atol = eps(real(T))^(4 / 5)
451+
if mode == :down
452+
@test space(S6, 1) ≾ space(Sr, 1)
453+
else
454+
@test space(Sr, 1) ≾ space(S6, 1)
455+
end
456+
if dim(domain(S6)) > 0
457+
λ6 = minimum(diagview(S6))
458+
_, S7, _, _ = svd_trunc(td; trunc = trunctol(; atol = λ6 - 1 // 2), alg)
459+
@test space(S6, 1) == space(S7, 1)
460+
end
461+
end
437462
end
438463
end
439464

‎test/cuda/factorizations.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,31 @@ for V in spacelist
453453
@test norm(t - U5 * S5 * Vᴴ5) ≈ ϵ5 atol = eps(real(T))^(4 / 5)
454454
@test minimum(diagview(S5)) >= λ
455455
@test abs(dim(domain(S5)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)
456+
457+
U0, S0, Vᴴ0 = svd_compact(t)
458+
Sd = similar(S0)
459+
copyto!(Sd.data, real(T)[3 - mod(i, 3) for i in 0:(length(Sd.data) - 1)])
460+
td = U0 * Sd * Vᴴ0
461+
rtol = sqrt(eps(real(T)))
462+
_, Sr, _, _ = svd_trunc(td; trunc = truncrank(nvals))
463+
for mode in (:down, :up)
464+
trunc = truncmultiplets(truncrank(nvals); rtol, mode)
465+
U6, S6, Vᴴ6, ϵ6 = @constinferred svd_trunc(td; trunc)
466+
@test td * Vᴴ6' ≈ U6 * S6
467+
@test isisometric(U6)
468+
@test isisometric(Vᴴ6; side = :right)
469+
@test norm(td - U6 * S6 * Vᴴ6) ≈ ϵ6 atol = eps(real(T))^(4 / 5)
470+
if mode == :down
471+
@test space(S6, 1) ≾ space(Sr, 1)
472+
else
473+
@test space(Sr, 1) ≾ space(S6, 1)
474+
end
475+
if dim(domain(S6)) > 0
476+
λ6 = minimum(diagview(S6))
477+
_, S7, _, _ = svd_trunc(td; trunc = trunctol(; atol = λ6 - 1 // 2))
478+
@test space(S6, 1) == space(S7, 1)
479+
end
480+
end
456481
end
457482
end
458483

‎test/factorizations/svd.jl‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ for V in spacelist
290290
@test norm(t - U5 * S5 * Vᴴ5) ≈ ϵ5 atol = eps(real(T))^(4 / 5)
291291
@test minimum(diagview(S5)) >= λ
292292
@test abs(dim(domain(S5)) - nvals) ≤ maximum(c -> blockdim(domain(t), c), blocksectors(t); init = 1)
293+
294+
U0, S0, Vᴴ0 = svd_compact(t)
295+
Sd = similar(S0)
296+
copyto!(Sd.data, real(T)[3 - mod(i, 3) for i in 0:(length(Sd.data) - 1)])
297+
td = U0 * Sd * Vᴴ0
298+
rtol = sqrt(eps(real(T)))
299+
_, Sr, _, _ = svd_trunc(td; trunc = truncrank(nvals))
300+
for mode in (:down, :up)
301+
trunc = truncmultiplets(truncrank(nvals); rtol, mode)
302+
U6, S6, Vᴴ6, ϵ6 = @constinferred svd_trunc(td; trunc)
303+
@test td * Vᴴ6' ≈ U6 * S6
304+
@test isisometric(U6)
305+
@test isisometric(Vᴴ6; side = :right)
306+
@test norm(td - U6 * S6 * Vᴴ6) ≈ ϵ6 atol = eps(real(T))^(4 / 5)
307+
if mode == :down
308+
@test space(S6, 1) ≾ space(Sr, 1)
309+
else
310+
@test space(Sr, 1) ≾ space(S6, 1)
311+
end
312+
if dim(domain(S6)) > 0
313+
λ6 = minimum(diagview(S6))
314+
_, S7, _, _ = svd_trunc(td; trunc = trunctol(; atol = λ6 - 1 // 2))
315+
@test space(S6, 1) == space(S7, 1)
316+
end
317+
end
293318
end
294319
end
295320
end

‎test/setup.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ VIBMRepA4 = (
362362
for V in (Vtr, VRepℤ₂, VRepℤ₃, VRepU₁, VfRepℤ₂, VTwistedVecℤ₄, VTwistedVecℤ₃, VRepCU₁, VRepSU₂, VfRepCU₁, VfRepSU₂, VfHubbard, Vfib, VRepA4, VfRepA4, VRepA4Twistedℤ₄, VIBM, VIBMRepA4)
363363
V1, V2, V3, V4, V5 = V
364364
for Vi in (V1, V2, V3, V4, V5)
365-
@test allequal(leftunit, sectors(Vi))
366-
@test allequal(rightunit, sectors(Vi))
365+
@test TensorKit._allequal(leftunit, sectors(Vi))
366+
@test TensorKit._allequal(rightunit, sectors(Vi))
367367
end
368368
@test dim(V1 ⊗ V2 ⊗ V3 ← (V4 ⊗ V5)') > 0
369369
@test V1 ⊗ V2 ≾ (V3 ⊗ V4 ⊗ V5)' # wide blocks for QR

0 commit comments

Comments
 (0)