Skip to content

Commit f6830ab

Browse files
kshyattKatharine HyattJutho
authored
Make gauge fixing stable for tied magnitudes (#334)
* Make gauge fixing stable for tied magnitudes * Formatter * Update src/common/gauge.jl Co-authored-by: Jutho <Jutho@users.noreply.github.com> --------- Co-authored-by: Katharine Hyatt <katharine.hyatt@ugent.be> Co-authored-by: Jutho <Jutho@users.noreply.github.com>
1 parent 2433e7c commit f6830ab

5 files changed

Lines changed: 60 additions & 13 deletions

File tree

‎src/common/gauge.jl‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,30 @@
44
55
Fix the residual gauge degrees of freedom in the eigen or singular vectors, that are
66
obtained from the decomposition performed by `f_eig` or `f_svd`.
7-
This is achieved by ensuring that the entry with the largest magnitude in `V` or `U`
8-
is real and positive.
7+
This is achieved by ensuring that, in every column of `V` or `U`, the first entry whose
8+
magnitude is within a relative tolerance of the largest magnitude is real and positive
9+
(see [`gauge_index`](@ref)).
910
""" gaugefix!
1011

1112
# Helper functions
1213
_argmaxabs(x) = reduce(_largest, x; init = zero(eltype(x)))
1314
_largest(x::Real, y::Real) = abs(x) < abs(y) ? y : x
1415
_largest(x::Complex, y::Complex) = abs2(x) < abs2(y) ? y : x
1516

17+
default_gauge_rtol(V) = sqrt(eps(real(float(eltype(V)))))
18+
19+
"""
20+
gauge_index(V; dims = 1, rtol = sqrt(eps(real(eltype(V)))))
21+
22+
Indices of the entries of `V` used to fix the gauge along dimension `dims`: for every slice, the
23+
first entry whose magnitude is at least `(1 - rtol)` times the largest magnitude in that slice.
24+
"""
25+
function gauge_index(V::AbstractArray; dims::Int = 1, rtol::Real = default_gauge_rtol(V))
26+
thr = maximum(abs, V; dims) .* (1 - rtol)
27+
# `findmax` returns the first index among equal maxima (also for GPU arrays)
28+
return last(findmax(abs.(V) .>= thr; dims))
29+
end
30+
1631
function gaugefix!(::typeof(qr_householder!), Q, R, Rd)
1732
ax = Base.OneTo(length(Rd))
1833
if !isempty(Q)
@@ -40,8 +55,8 @@ function gaugefix!(::typeof(lq_householder!), L, Q, Ld)
4055
end
4156

4257
function gaugefix!(::Union{typeof(eig_full!), typeof(eigh_full!), typeof(gen_eig_full!)}, V::AbstractMatrix)
43-
signs = reduce(_largest, V; dims = 1, init = zero(eltype(V)))
44-
@. signs = sign(signs)
58+
isempty(V) && return V
59+
signs = sign.(V[gauge_index(V; dims = 1)])
4560
@. V = V * conj(signs)
4661
return V
4762
end
@@ -52,25 +67,24 @@ end
5267
function gaugefix!(::typeof(svd_full!), U, Vᴴ)
5368
m, n = size(U, 2), size(Vᴴ, 1)
5469
k = min(m, n)
55-
signs = reduce(_largest, U; dims = 1, init = zero(eltype(U)))
56-
@. signs = sign(signs)
70+
isempty(U) && return (U, Vᴴ)
71+
signs = sign.(U[gauge_index(U; dims = 1)])
5772
@. U = U * conj(signs)
5873
Vᴴk = view(Vᴴ, axes(Vᴴ, 1)[1:k], :)
5974
signs_t = transpose(view(signs, :, axes(signs, 2)[1:k]))
6075
@. Vᴴk = signs_t * Vᴴk
6176
if n > m
6277
Vᴴr = view(Vᴴ, axes(Vᴴ, 1)[(k + 1):n], :)
63-
signs_r = reduce(_largest, Vᴴr; dims = 2, init = zero(eltype(Vᴴ)))
64-
@. signs_r = sign(signs_r)
78+
signs_r = sign.(Vᴴr[gauge_index(Vᴴr; dims = 2)])
6579
@. Vᴴr = conj(signs_r) * Vᴴr
6680
end
6781
return (U, Vᴴ)
6882
end
6983

7084
function gaugefix!(::Union{typeof(svd_compact!), typeof(svd_trunc!)}, U, Vᴴ)
7185
@assert axes(U, 2) == axes(Vᴴ, 1)
72-
signs = reduce(_largest, U; dims = 1, init = zero(eltype(U)))
73-
@. signs = sign(signs)
86+
isempty(U) && return (U, Vᴴ)
87+
signs = sign.(U[gauge_index(U; dims = 1)])
7488
signs_t = transpose(signs)
7589
@. U = U * conj(signs)
7690
@. Vᴴ = signs_t * Vᴴ

‎src/pushforwards/eig.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function eig_pushforward!(
1515
if eltype(V) <: Real # fix norm conservation
1616
ΔV .-= V .* real.(sum(conj.(V) .* ΔV; dims = 1))
1717
else # also fix gauge for `gaugefix!` compatibility
18-
_, I = findmax(abs, V; dims = 1)
18+
I = gauge_index(V; dims = 1)
1919
ΔV .-= V .* (real.(sum(conj.(V) .* ΔV; dims = 1)) .+ im .* imag.(ΔV[I] ./ V[I]))
2020
end
2121
end

‎src/pushforwards/eigh.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function eigh_pushforward!(
1313
∂K .*= inv_safe.(transpose(diagview(D)) .- diagview(D), degeneracy_atol)
1414
ΔV = mul!(ΔV, V, ∂K)
1515
if eltype(V) <: Complex # fix gauge for `gaugefix!` compatibility
16-
_, I = findmax(abs, V; dims = 1)
16+
I = gauge_index(V; dims = 1)
1717
infinitesimal_phases = imag.(ΔV[I] ./ V[I])
1818
ΔV .-= im .* V .* infinitesimal_phases
1919
end

‎src/pushforwards/svd.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function svd_pushforward!(
6161
if eltype(U) <: Complex && !iszerotangent(ΔU) && !iszerotangent(ΔVᴴ) # fix gauge for `gaugefix!` compatibility
6262
ΔU₁ = view(ΔU, :, 1:r)
6363
ΔV₁ᴴ = view(ΔVᴴ, 1:r, :)
64-
_, I = findmax(abs, U₁; dims = 1)
64+
I = gauge_index(U₁; dims = 1)
6565
infinitesimal_phases = imag.(ΔU₁[I] .* inv_safe.(U₁[I]))
6666
ΔU₁ .-= im .* U₁ .* infinitesimal_phases
6767
ΔV₁ᴴ .+= im .* transpose(infinitesimal_phases) .* V₁ᴴ

‎test/common/gauge.jl‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using MatrixAlgebraKit
2+
using MatrixAlgebraKit: gauge_index
3+
using Test
4+
using StableRNGs
5+
6+
@testset "gauge_index tie-breaking ($T)" for T in (Float64, ComplexF64)
7+
rng = StableRNG(123)
8+
# the first two entries of every column have (nearly) equal, largest magnitude
9+
V = T[1 -2 3 -4; -1 2 -3 4; 0 0 0 0; 0 0 0 0] .+ randn(rng, T, 4, 4) .* [0, 0, 1.0e-1, 1.0e-1]
10+
V[2, :] .*= 1 + 1.0e-14
11+
I = gauge_index(V; dims = 1)
12+
@test vec(I) == [CartesianIndex(1, j) for j in 1:4]
13+
# rounding-level perturbations of the magnitudes do not change the selected entries
14+
V′ = copy(V)
15+
V′[1, :] .*= 1 - 4eps()
16+
@test gauge_index(V′; dims = 1) == I
17+
@test vec(gauge_index(permutedims(V); dims = 2)) == [CartesianIndex(j, 1) for j in 1:4]
18+
end
19+
20+
@testset "svd gauge stable for exactly tied magnitudes ($T)" for T in (Float64, ComplexF64)
21+
rng = StableRNG(1234)
22+
m, n = 10, 4
23+
B = randn(rng, T, m ÷ 2, n)
24+
# rows of A come in pairs related by a sign flip, so every left singular vector has pairs
25+
# of entries with exactly equal magnitude (in exact arithmetic)
26+
A = vcat(B, -B)
27+
U, S, Vᴴ = svd_compact(A)
28+
for A′ in (A * (1 + eps()), A * (1 - eps()), A .* (1 .+ eps() .* rand(rng, m, n)))
29+
U′, S′, Vᴴ′ = svd_compact(A′)
30+
@test U′ ≈ U
31+
@test Vᴴ′ ≈ Vᴴ
32+
end
33+
end

0 commit comments

Comments
 (0)