From 8aefc7c71cc83249cb9c78be4fbf166d9cfdaf76 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Mon, 16 Nov 2026 18:58:20 +0200 Subject: [PATCH 01/13] Add a MINRES local solver to get_green_krylov (solver = :minres) KrylovKit has no MINRES, so every local problem of the linsolve sweeps was solved by restarted GMRES. For Hermitian H each local problem (z - H_eff)x = b is a complex shift of a Hermitian matrix: MINRES solves it by a three-term recurrence without restarts, giving the same G in fewer products at small eta. Opt-in keyword `solver` (default :gmres, unchanged). With :minres the operator is I (x) H^T and z goes into linsolve's coefficients; a non-Hermitian H throws an ArgumentError (from |H - H^dag|/|H|). Tested against exact inverses in test/krylov_minres.jl; golden_dynamics.jl (GMRES path) passes unchanged. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 14 ++++ src/TensorBinding.jl | 3 +- src/solvers/Krylov.jl | 182 +++++++++++++++++++++++++++++++++++++++--- test/krylov_minres.jl | 52 ++++++++++++ test/runtests.jl | 1 + 5 files changed, 238 insertions(+), 14 deletions(-) create mode 100644 test/krylov_minres.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index b6887fe..04c2b5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ reading of semantic versioning: before 1.0, a change in the minor number (0.1 is breaking, and a change in the last number (0.1.0 → 0.1.1) adds features or fixes bugs without breaking the API. +## [Unreleased] + +### Added + +- **MINRES for `get_green_krylov`**: keyword `solver`, `:gmres` (the default, KrylovKit's + GMRES as before) or `:minres`. Each local problem of the sweeps, (z − H_eff)x = b, is + a complex shift of a Hermitian matrix when H is Hermitian; MINRES solves it by a + three-term recurrence without restarts (the iterates of unrestarted GMRES), where + GMRES restarts every `krylovdim` products. The result is the same G in fewer products, + several times fewer at small η. MINRES takes `tol` and a budget of + `maxiter · krylovdim` products per local solve (the GMRES one); a non-Hermitian H + throws an `ArgumentError` (checked from ‖H − H†‖/‖H‖). With `solver=:minres` no + z·I − H is formed: z goes into `linsolve`'s coefficients. + ## [0.2.1] — 2026-11-13 This release keeps the v0.2 API. Every entry under **Changed results** is a bug fix diff --git a/src/TensorBinding.jl b/src/TensorBinding.jl index a70689f..ddda543 100644 --- a/src/TensorBinding.jl +++ b/src/TensorBinding.jl @@ -153,7 +153,8 @@ export chern_marker_gpu, get_bands_gpu, get_dos_stochastic_gpu, # → Utils, TBSystem, AuxDOF, DMRG, kpm/kernels, kpm/recursion # solvers/kpm/exciton.jl exciton LDOS # → Utils, TBSystem, DMRG, kpm/kernels, kpm/recursion -# solvers/Krylov.jl get_green_krylov, Haydock recursion (haydock_cf & co.) +# solvers/Krylov.jl get_green_krylov (GMRES or MINRES local solver), Haydock +# recursion (haydock_cf & co.) # → Utils, MPOTools, TBSystem # solvers/Timeev.jl TDVP/RK4 step and trajectory kernels, propagator MPO, # density-matrix RK4 and observables diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index db331cc..4be8383 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -7,10 +7,11 @@ # # solved by ITensorMPS.linsolve (get_green_krylov), where |M⟩⟩ is the vectorized # (MPS) representation of the matrix M on a 2L-site interleaved quantics chain (odd -# sites = row bits, even sites = column bits; built by _vec_mps_from_mpo); and the -# operator-level Haydock (Lanczos) recursion with its continued fraction and -# resolvent MPO (haydock_cf, eval_haydock_cf, haydock_resolve_mpo) with its -# Hilbert-Schmidt inner product _hs_inner. +# sites = row bits, even sites = column bits; built by _vec_mps_from_mpo), with +# KrylovKit's GMRES or MINRES (_minres_shifted, _minres_updater) as the local solver +# of the sweeps; and the operator-level Haydock (Lanczos) recursion with its +# continued fraction and resolvent MPO (haydock_cf, eval_haydock_cf, +# haydock_resolve_mpo) with its Hilbert-Schmidt inner product _hs_inner. # # Entry points: get_green_krylov, haydock_cf, eval_haydock_cf, haydock_resolve_mpo # Depends on: core/Utils.jl (custom_mpo), core/MPOTools.jl (interleave_mpo), @@ -65,10 +66,107 @@ function _vec_mps_from_mpo(G_mpo::MPO, sites2::Vector{<:Index}; end +""" + _minres_shifted(A, b, x0, a0, a1; tol=1e-10, maxiter=2000) -> (x, info) + +Solve `(a0 + a1·A) x = b` for a Hermitian linear map `A` (a function `v -> A v`) and +complex `a0`, `a1 ≠ 0` by MINRES: the Lanczos three-term recurrence of `A` from the +residual of `x0`, with complex Givens rotations on the shifted tridiagonal +`T − σ`, `σ = −a0/a1`. In exact arithmetic the iterates are those of GMRES without +restarts, kept in three vectors. + +Stops when the residual estimate `|a1|·|t|` is at most `tol` (absolute, as +KrylovKit's `tol`), on a Lanczos breakdown, or after `maxiter` products with `A`. +`info = (; converged, numops, residual)`, with `numops` the products with `A` +(the residual of `x0` included) and `residual` the final estimate. + +Works on any vector type with `norm`, `dot`, `copy` and linear combinations +(ITensors, `Vector`s). A non-Hermitian `A` gives a wrong `x` without an error. +""" +function _minres_shifted(A, b, x0, a0::Number, a1::Number; + tol::Real = 1e-10, maxiter::Int = 2000) + σ = -a0 / a1 + r = b - (a0 * x0 + a1 * A(x0)) + nops = 1 + rhs = r / a1 + β1 = norm(rhs) + x = copy(x0) + abs(a1) * β1 <= tol && return x, (; converged = true, numops = nops, residual = abs(a1) * β1) + v = rhs / β1 + vprev = nothing + βk = 0.0 + m1 = nothing; m2 = nothing # columns k−1, k−2 of V R⁻¹ + p1, q1 = one(ComplexF64), 0.0 # rotation G_{k−1} = [p̄ q; −q p] + p2, q2 = one(ComplexF64), 0.0 # rotation G_{k−2} + t = complex(β1) # current component of the rotated rhs + res = abs(a1) * β1 + for _ in 1:maxiter + w = A(v); nops += 1 + vprev === nothing || (w = w - βk * vprev) + α = real(dot(v, w)) + w = w - α * v + βn = norm(w) + # column k of T − σ: rows k−2, k−1, k, k+1 hold (0, βk, α − σ, βn) + ε = q2 * βk # G_{k−2} on (0, βk) + δ = p2 * βk + δ2 = conj(p1) * δ + q1 * (α - σ) # G_{k−1} on (δ, α − σ) + γ = -q1 * δ + p1 * (α - σ) + ρ = sqrt(abs2(γ) + βn^2) # G_k annihilates βn + p, q = γ / ρ, βn / ρ + τ = conj(p) * t + t = -q * t + m = v + m1 === nothing || (m = m - δ2 * m1) + m2 === nothing || (m = m - ε * m2) + m = m / ρ + x = x + τ * m + res = abs(a1) * abs(t) + m2, m1 = m1, m + p2, q2, p1, q1 = p1, q1, p, q + (res <= tol || βn <= 1e-14 * β1) && + return x, (; converged = res <= tol, numops = nops, residual = res) + vprev = v + v = w / βn + βk = βn + end + return x, (; converged = false, numops = nops, residual = res) +end + + +# Local updater of ITensorMPS.linsolve: MINRES on (a0 + a1·A_eff) x = b, where A_eff is +# the operator projected on the two-site space; Hermitian when the MPO is (orthonormal +# environments). `stats` (local solves, products, unconverged solves) is updated in place. +function _minres_updater(problem, init; internal_kwargs, coefficients, + tol = 1e-10, maxiter = 2000, stats = nothing, kwargs...) + A = ITensorMPS.operator(problem) + b = ITensorMPS.constant_term(problem) + x, info = _minres_shifted(v -> A(v), b, init, coefficients[1], coefficients[2]; + tol, maxiter) + if stats !== nothing + stats[1] += 1 + stats[2] += info.numops + info.converged || (stats[3] += 1) + end + return x, (; info) +end + + +# ‖H − H†‖_F / ‖H‖_F from Hilbert-Schmidt products of the MPOs (exact contractions, no +# MPO sum): ‖H − H†‖² = 2‖H‖² − 2 Re Tr[H H]. The difference cancels to ~√eps, so a +# Hermitian MPO gives ≲ 1e-7. +function _hermiticity_defect(H::MPO) + n2 = real(inner(H, H)) + n2 > 0 || return 0.0 + Hd = swapprime(dag(H), 0 => 1) # links primed too: harmless for inner + d2 = 2n2 - 2real(inner(H, Hd)) + return sqrt(max(d2, 0.0) / n2) +end + + """ get_green_krylov(H_mpo::MPO, sites, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, ishermitian=false, tol=1e-10, - maxiter=600, krylovdim=30, verbose=false) -> MPO + cutoff=1e-8, x0_mpo=nothing, solver=:gmres, ishermitian=false, + tol=1e-10, maxiter=600, krylovdim=30, verbose=false) -> MPO Low-level: compute the retarded Green's function G(ω) = (ω + iη − H)⁻¹ for a raw MPO `H_mpo` defined on `sites`, via the vectorized linear system @@ -85,11 +183,17 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; maxdim::Int = 100, cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, + solver::Symbol = :gmres, ishermitian::Bool = false, tol::Real = 1e-10, maxiter::Int = 600, krylovdim::Int = 30, verbose::Bool = false) + solver in (:gmres, :minres) || + throw(ArgumentError("get_green_krylov: solver must be :gmres or :minres, got :$solver")) + solver === :minres && + return _green_krylov_minres(H_mpo, sites, ω_phys; η, nsweeps, maxdim, cutoff, + x0_mpo, tol, maxiter, krylovdim, verbose) N = length(sites) sites2 = siteinds("Qubit", 2N) @@ -122,10 +226,47 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; end +# get_green_krylov with MINRES as the local solver. The operator is I ⊗ Hᵀ, Hermitian +# when H is, and z goes into linsolve's coefficients: (z − I ⊗ Hᵀ)|G⟩⟩ = |I⟩⟩, so no +# z·I − H is formed. The start is |I⟩⟩ expanded as in the GMRES path, here along the +# Krylov space of I ⊗ Hᵀ, which is that of the shifted operator. +function _green_krylov_minres(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; + η, nsweeps, maxdim, cutoff, x0_mpo, tol, maxiter, + krylovdim, verbose) + defect = _hermiticity_defect(H_mpo) + defect <= 1e-6 || throw(ArgumentError( + "get_green_krylov(solver=:minres) needs a Hermitian H, but ‖H − H†‖/‖H‖ = " * + "$(round(defect; sigdigits=2)); use solver=:gmres")) + N = length(sites) + sites2 = siteinds("Qubit", 2N) + + z = ComplexF64(ω_phys + im * η) + Aop = interleave_mpo(swapprime(H_mpo, 0 => 1), sites2, 0) + rhs = _vec_mps_from_mpo(MPO(sites, "Id"), sites2) + x0 = isnothing(x0_mpo) ? + ITensorMPS.expand(rhs, Aop; alg="global_krylov", krylovdim=2, cutoff=cutoff) : + _vec_mps_from_mpo(x0_mpo, sites2; cutoff=cutoff, maxdim=maxdim) + + verbose && println("Krylov GF: ω = $ω_phys + $(η)i (N=$N, maxdim=$maxdim, nsweeps=$nsweeps, MINRES)", + isnothing(x0_mpo) ? "" : " [KPM warm start]") + + stats = [0, 0, 0] + sol = ITensorMPS.linsolve(Aop, rhs, x0, z, -1.0; + nsweeps = nsweeps, + maxdim = maxdim, + cutoff = cutoff, + updater = _minres_updater, + updater_kwargs = (; tol, maxiter = maxiter * krylovdim, stats)) + verbose && println(" MINRES: $(stats[1]) local solves, $(stats[2]) products, ", + "$(stats[3]) stopped above tol") + return custom_mpo(sol, sites) +end + + """ get_green_krylov(H::TBHamiltonian, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, ishermitian=false, tol=1e-10, - maxiter=600, krylovdim=30, verbose=false) -> MPO + cutoff=1e-8, x0_mpo=nothing, solver=:gmres, ishermitian=false, + tol=1e-10, maxiter=600, krylovdim=30, verbose=false) -> MPO Compute the retarded Green's function @@ -152,12 +293,23 @@ unscaled — no KPM Chebyshev expansion required. `ITensorMPS.expand`). Typical use: pass a low-accuracy KPM Green's function to warm-start the Krylov iteration. Default `nothing`. -- `ishermitian` : Set `true` only when the shifted operator is Hermitian +- `solver` : Local solver of the sweeps. `:gmres` (default): KrylovKit's + GMRES, restarted every `krylovdim` products. `:minres`: MINRES, + for Hermitian H only (an `ArgumentError` otherwise, from + ‖H − H†‖/‖H‖). Each local problem (z − H_eff)x = b is a complex + shift of a Hermitian matrix, which MINRES solves by a three-term + recurrence without restarts: the same G in fewer products, most + at small η, where restarted GMRES slows down. MINRES uses + `tol`, and `maxiter · krylovdim` as its product budget per local + solve (the GMRES one); `ishermitian` does not apply. +- `ishermitian` : GMRES only. Set `true` only when the shifted operator is Hermitian (requires purely imaginary η = 0, not physical for GF). Default `false`. -- `tol` : Krylov solver convergence tolerance. Default `1e-10`. -- `maxiter` : Maximum Krylov iterations per site. Default `600`. +- `tol` : Krylov solver convergence tolerance (absolute, on the local + residual). Default `1e-10`. +- `maxiter` : Maximum Krylov iterations per site (GMRES restarts). Default `600`. - `krylovdim` : Krylov subspace dimension. Default `30`. -- `verbose` : Print progress messages. Default `false`. +- `verbose` : Print progress messages (with MINRES, also the number of local + solves and products). Default `false`. **Usage** ```julia @@ -172,6 +324,9 @@ TensorBinding.KPM_Tn(H, 15; maxdim=50) G_kpm = (1 / H.scale) * TensorBinding.get_Green_retarded_from_Tn(H._tn_cache, 15, (ω - H.center) / H.scale; η=η / H.scale, maxdim=50) G_ws = get_green_krylov(H, ω; x0_mpo=G_kpm, nsweeps=6, maxdim=200) + +# Small η with a Hermitian H: MINRES as the local solver +G_mr = get_green_krylov(H, ω; η=1e-3, nsweeps=20, maxdim=200, solver=:minres) ``` """ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; @@ -180,13 +335,14 @@ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; maxdim::Int = 100, cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, + solver::Symbol = :gmres, ishermitian::Bool = false, tol::Real = 1e-10, maxiter::Int = 600, krylovdim::Int = 30, verbose::Bool = false) return get_green_krylov(H.mpo, H.sites, ω_phys; - η, nsweeps, maxdim, cutoff, x0_mpo, ishermitian, + η, nsweeps, maxdim, cutoff, x0_mpo, solver, ishermitian, tol, maxiter, krylovdim, verbose) end diff --git a/test/krylov_minres.jl b/test/krylov_minres.jl new file mode 100644 index 0000000..d1a5158 --- /dev/null +++ b/test/krylov_minres.jl @@ -0,0 +1,52 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Random, Test +using TensorBinding: get_Hamiltonian, get_green_krylov, _minres_shifted, _hermiticity_defect + +# get_green_krylov(solver=:minres): MINRES as the local solver of the linsolve sweeps, +# for Hermitian H. The default (:gmres) is unchanged and pinned by golden_dynamics.jl. +@testset "get_green_krylov: MINRES local solver" begin + # dense matrix of an MPO; site 1 fastest, rows = primed indices + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + + # The shifted-Hermitian MINRES on dense vectors, against the direct solve. + rng = MersenneTwister(5) + n = 60 + A = randn(rng, ComplexF64, n, n); A = (A + A') / 2 + b = randn(rng, ComplexF64, n) + x0 = zeros(ComplexF64, n) + for (a0, a1) in ((0.3 + 0.05im, -1.0), (0.3 + 1e-3im, -1.0), (1.5, 2.0 + 0.5im)) + x, info = _minres_shifted(v -> A * v, b, x0, a0, a1; tol=1e-11, maxiter=5000) + @test info.converged + @test x ≈ (a0 * I + a1 * A) \ b rtol=1e-8 + end + # from a nonzero start, and the budget: maxiter products after the residual of x0 + xs = randn(rng, ComplexF64, n) + x, info = _minres_shifted(v -> A * v, b, xs, 0.2 + 0.1im, -1.0; tol=1e-11, maxiter=5000) + @test x ≈ ((0.2 + 0.1im) * I - A) \ b rtol=1e-8 + x, info = _minres_shifted(v -> A * v, b, x0, 0.2 + 0.1im, -1.0; maxiter=3) + @test !info.converged && info.numops == 4 + + # The Green's function of a chain, real and complex Hermitian, against the inverse. + z = 0.4 + 0.3im + H = get_Hamiltonian("chain_1d", 1.0; L=4) + s = H.sites + Hc = +(H.mpo, 0.3 * MPO(ComplexF64, s, ["Id", "Y", "Id", "Id"]); cutoff=1e-14) + @test _hermiticity_defect(H.mpo) < 1e-7 + @test _hermiticity_defect(Hc) < 1e-7 + Gm = get_green_krylov(H, 0.4; η=0.3, nsweeps=6, solver=:minres) + @test dmat(Gm, s) ≈ inv(z * I - dmat(H.mpo, s)) rtol=1e-8 + Gc = get_green_krylov(Hc, s, 0.4; η=0.3, nsweeps=6, solver=:minres) + @test dmat(Gc, s) ≈ inv(z * I - dmat(Hc, s)) rtol=1e-8 + # small η, near an eigenvalue of the L = 5 chain (2cos(πk/33)) + H5 = get_Hamiltonian("chain_1d", 1.0; L=5) + ω = 2cos(π * 7 / 33) + 0.02 + G5 = get_green_krylov(H5, ω; η=1e-3, nsweeps=8, maxdim=64, solver=:minres) + @test dmat(G5, H5.sites) ≈ inv((ω + 1e-3im) * I - dmat(H5.mpo, H5.sites)) rtol=1e-6 + + # A non-Hermitian H is refused (GMRES still takes it), and so is an unknown solver. + Hn = +(H.mpo, 0.3im * MPO(s, ["Z", "Id", "Id", "Id"]); cutoff=1e-14) + @test _hermiticity_defect(Hn) > 0.1 + @test_throws ArgumentError get_green_krylov(Hn, s, 0.4; η=0.3, solver=:minres) + @test dmat(get_green_krylov(Hn, s, 0.4; η=0.3, nsweeps=6), s) ≈ + inv(z * I - dmat(Hn, s)) rtol=1e-8 + @test_throws ArgumentError get_green_krylov(H, 0.4; solver=:cg) +end diff --git a/test/runtests.jl b/test/runtests.jl index ed3b284..2b7fd6c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -110,6 +110,7 @@ include("metallic_mean.jl") include("kbonacci.jl") include("kpm_moment_columns.jl") include("gpu_mps_ldos.jl") +include("krylov_minres.jl") # Regression tests for the bugs found in the 2026-09 code audit # (docs/dev/REORGANISATION_TODO.md, "Bugs"). From d61b1c0b89639c2f3c56a77bec1bc6d8e95e41c3 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Mon, 16 Nov 2026 20:41:39 +0200 Subject: [PATCH 02/13] get_green_krylov: random start by default, residual check The start was |I>> with a 2-vector global Krylov expansion, and the two-site sweeps keep the rank their start spans: on a short-ranged G (omega outside the band, or eta = 0.1 on 2^10+ sites) open chains came out 6-20 % off, the G of the bulk without the end or impurity corrections, with nothing to show it. The default start is now |I>> + 0.1 * (random MPS, chi 8) with an 8-vector expansion (keywords `start`, `rng`; `start=:identity` restores the old one; the RNG is a Xoshiro(1) created per call). The same cases reach 1e-11 (clean chain) or the floor of the cutoff (impurity chain). After the sweeps the relative residual is computed from exact inner products; above `residual_warn` (1e-2) a warning says G is not converged, and `return_info=true` returns it. Changed results: dynamics golden cases krylov_green_raw, _tbhamiltonian, _ishermitian_flag (~1e-14) and the five Krylov cases of the RPA golden file, which pin keyword forwarding at unconverged settings. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 31 +++++ src/solvers/Krylov.jl | 193 +++++++++++++++++++----------- test/bugfix_green_krylov_start.jl | 46 +++++++ test/data/dynamics_golden.jl | 12 +- test/data/rpa_golden.jl | 14 +-- test/runtests.jl | 1 + 6 files changed, 212 insertions(+), 85 deletions(-) create mode 100644 test/bugfix_green_krylov_start.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c2b5a..ec8d5ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,37 @@ bugs without breaking the API. `maxiter · krylovdim` products per local solve (the GMRES one); a non-Hermitian H throws an `ArgumentError` (checked from ‖H − H†‖/‖H‖). With `solver=:minres` no z·I − H is formed: z goes into `linsolve`'s coefficients. +- **`get_green_krylov` checks its residual**: the relative residual + ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the solve (from exact inner products) is computed after the + sweeps, and a warning says that G is not converged when it exceeds `residual_warn` + (default `1e-2`; `Inf` skips the check). `return_info=true` returns + `(; G, residual, maxlinkdim)` instead of the MPO. On the chains of the next entry and + on 16×16 square lattices the residual was 1–5 times the relative error of G, down to + its noise floor of ~1e-7. + +### Changed results + +- **`get_green_krylov` starts from a random state** (keywords `start` and `rng`). The + start was |I⟩⟩ with its bonds enlarged by a 2-vector global Krylov expansion, and the + two-site sweeps keep the rank their start spans: on a short-ranged G they stopped on the + G of the bulk, without the corrections near the chain ends or an impurity. At the + default settings, open chains were off by 6–20 % whenever ω lay outside the band (from + 2^6 sites with an impurity, 2^7 without), and inside it at η = 0.1 from 2^10 sites + (at η = 0.03 too at 2^12), with nothing to show it. The start is now + |I⟩⟩ + 0.1·(a random MPS of bond dimension 8) with an 8-vector expansion, which gets + the same cases to 1e-11 (clean chain) and to the floor of the cutoff (impurity chain: + 2e-6 at 2^8 sites, 1e-6–2e-4 at 2^10; `cutoff=1e-12` gives 3e-11–8e-7 there). The + random state comes from `rng`, by default a `Random.Xoshiro(1)` created at each call, + so results do not depend on the global RNG. The first sweep runs at the bond dimension + of the start (up to ~100), which costs the most when G has a small one: on these + chains a solve takes up to 30× longer (30 s instead of 1 s for a clean 2^8-site chain + at η = 0.03). `start=:identity` restores the old start; `x0_mpo` is unchanged. Regenerated golden cases: `krylov_green_raw`, `krylov_green_tbhamiltonian` + and `krylov_green_ishermitian_flag` of `test/data/dynamics_golden.jl` (moved by ~1e-14: + those small chains had converged), and the five Krylov cases of + `test/data/rpa_golden.jl` (`bubble_cplx_krylov` and the four `*_fwd_krylov_mcweeny_tol` + cases), which pin keyword forwarding at deliberately unconverged settings (2 sweeps, + `maxdim` 3–20) and so moved by their whole size. Every other RPA case reproduced + exactly; the file's header now records ITensors 0.9.32 and ITensorMPS 0.3.44. ## [0.2.1] — 2026-11-13 diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index 4be8383..42c2b0b 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -163,10 +163,40 @@ function _hermiticity_defect(H::MPO) end +# ‖(a0 + a1·A)x − b‖ / ‖b‖ from exact inner products, with no MPO–MPS product. The +# terms cancel to ~√eps of the largest one, so values below ~1e-7 are noise. +function _linsolve_residual(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number) + nb2 = real(inner(b, b)) + r2 = abs2(a0) * real(inner(x, x)) + abs2(a1) * real(inner(A, x, A, x)) + nb2 + + 2real(conj(a0) * a1 * inner(x', A, x)) - 2real(conj(a0) * inner(x, b)) - + 2real(conj(a1) * conj(inner(b', A, x))) + return sqrt(max(r2, 0.0) / nb2) +end + + +# Start of the linsolve sweeps of get_green_krylov. :random (from v0.2.2): |I⟩⟩ plus +# 0.1·(a random MPS of bond dimension 8 from `rng`), with its bond bases enlarged by an +# 8-vector global Krylov expansion along A. :identity (up to v0.2.1): |I⟩⟩ with a +# 2-vector expansion. The two-site sweeps keep the rank their start spans: from the +# :identity start they stalled on a short-ranged G (the bulk G without the end +# corrections of an open chain; ω outside the band in 2D), wrong by 1e-3–0.2 with no +# sign of it. The random part fills every bond, the expansion adds the directions of A. +function _green_krylov_start(rhs::MPS, A::MPO, sites2::Vector{<:Index}, start::Symbol, + rng::AbstractRNG, cutoff::Real) + start === :identity && + return ITensorMPS.expand(rhs, A; alg="global_krylov", krylovdim=2, cutoff=cutoff) + r = random_mps(rng, ComplexF64, sites2; linkdims=8) + return ITensorMPS.expand(+(rhs, 0.1 * r; cutoff=1e-14), A; alg="global_krylov", + krylovdim=8, cutoff=cutoff) +end + + """ get_green_krylov(H_mpo::MPO, sites, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, solver=:gmres, ishermitian=false, - tol=1e-10, maxiter=600, krylovdim=30, verbose=false) -> MPO + cutoff=1e-8, x0_mpo=nothing, start=:random, rng=Xoshiro(1), + solver=:gmres, ishermitian=false, tol=1e-10, maxiter=600, + krylovdim=30, return_info=false, residual_warn=1e-2, + verbose=false) -> MPO Low-level: compute the retarded Green's function G(ω) = (ω + iη − H)⁻¹ for a raw MPO `H_mpo` defined on `sites`, via the vectorized linear system @@ -183,90 +213,83 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; maxdim::Int = 100, cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, + start::Symbol = :random, + rng::AbstractRNG = Random.Xoshiro(1), solver::Symbol = :gmres, ishermitian::Bool = false, tol::Real = 1e-10, maxiter::Int = 600, krylovdim::Int = 30, + return_info::Bool = false, + residual_warn::Real = 1e-2, verbose::Bool = false) solver in (:gmres, :minres) || throw(ArgumentError("get_green_krylov: solver must be :gmres or :minres, got :$solver")) - solver === :minres && - return _green_krylov_minres(H_mpo, sites, ω_phys; η, nsweeps, maxdim, cutoff, - x0_mpo, tol, maxiter, krylovdim, verbose) + start in (:random, :identity) || + throw(ArgumentError("get_green_krylov: start must be :random or :identity, got :$start")) N = length(sites) sites2 = siteinds("Qubit", 2N) + z = ComplexF64(ω_phys + im * η) + rhs = _vec_mps_from_mpo(MPO(sites, "Id"), sites2) - z = ComplexF64(ω_phys + im * η) - ω_mpo = z * MPO(sites, "Id") - H_mpo # |G⟩⟩ holds G[r, c] with r on the odd and c on the even qubits (_vec_mps_from_mpo), - # and Lop acts on c: G(z − H) = I, i.e. (z − H)ᵀ on the column register, so the - # operator is embedded transposed (interleave_mpo embeds it as given). - Lop = interleave_mpo(swapprime(ω_mpo, 0 => 1), sites2, 0) - rhs = _vec_mps_from_mpo(MPO(sites, "Id"), sites2) - # Default start: |I⟩⟩ with its bond bases enlarged by a global Krylov expansion - # (ITensorMPS.expand, krylovdim = 2: directions Lop^k|I⟩⟩ folded in). From |I⟩⟩ - # itself (bond dimension 1 between the sites of H) the two-site sweeps stalled on - # real two-register operators such as the bubble's H_eff = I⊗H₂ − H₁⊗I: residual - # ~0.9 and bonds capped below the solution's whatever nsweeps, a ~50 % error in G. - # Converged results elsewhere move by ~1e-14. - x0 = isnothing(x0_mpo) ? - ITensorMPS.expand(rhs, Lop; alg="global_krylov", krylovdim=2, cutoff=cutoff) : - _vec_mps_from_mpo(x0_mpo, sites2; cutoff=cutoff, maxdim=maxdim) - - verbose && println("Krylov GF: ω = $ω_phys + $(η)i (N=$N, maxdim=$maxdim, nsweeps=$nsweeps)", - isnothing(x0_mpo) ? "" : " [KPM warm start]") - - sol = ITensorMPS.linsolve(Lop, rhs, x0; - nsweeps = nsweeps, - maxdim = maxdim, - cutoff = cutoff, - updater_kwargs = (; ishermitian, tol, maxiter, krylovdim)) - return custom_mpo(sol, sites) -end - - -# get_green_krylov with MINRES as the local solver. The operator is I ⊗ Hᵀ, Hermitian -# when H is, and z goes into linsolve's coefficients: (z − I ⊗ Hᵀ)|G⟩⟩ = |I⟩⟩, so no -# z·I − H is formed. The start is |I⟩⟩ expanded as in the GMRES path, here along the -# Krylov space of I ⊗ Hᵀ, which is that of the shifted operator. -function _green_krylov_minres(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; - η, nsweeps, maxdim, cutoff, x0_mpo, tol, maxiter, - krylovdim, verbose) - defect = _hermiticity_defect(H_mpo) - defect <= 1e-6 || throw(ArgumentError( - "get_green_krylov(solver=:minres) needs a Hermitian H, but ‖H − H†‖/‖H‖ = " * - "$(round(defect; sigdigits=2)); use solver=:gmres")) - N = length(sites) - sites2 = siteinds("Qubit", 2N) - - z = ComplexF64(ω_phys + im * η) - Aop = interleave_mpo(swapprime(H_mpo, 0 => 1), sites2, 0) - rhs = _vec_mps_from_mpo(MPO(sites, "Id"), sites2) - x0 = isnothing(x0_mpo) ? - ITensorMPS.expand(rhs, Aop; alg="global_krylov", krylovdim=2, cutoff=cutoff) : - _vec_mps_from_mpo(x0_mpo, sites2; cutoff=cutoff, maxdim=maxdim) + # and the operator acts on c: G(z − H) = I, i.e. (z − H)ᵀ on the column register, so + # it is embedded transposed (interleave_mpo embeds it as given). linsolve solves + # (a0 + a1·A)|G⟩⟩ = |I⟩⟩. + if solver === :minres + # A = I ⊗ Hᵀ, Hermitian when H is, with z in the coefficients: no z·I − H is formed. + defect = _hermiticity_defect(H_mpo) + defect <= 1e-6 || throw(ArgumentError( + "get_green_krylov(solver=:minres) needs a Hermitian H, but ‖H − H†‖/‖H‖ = " * + "$(round(defect; sigdigits=2)); use solver=:gmres")) + A, a0, a1 = interleave_mpo(swapprime(H_mpo, 0 => 1), sites2, 0), z, -1.0 + else + ω_mpo = z * MPO(sites, "Id") - H_mpo + A, a0, a1 = interleave_mpo(swapprime(ω_mpo, 0 => 1), sites2, 0), 0.0, 1.0 + end + x0 = isnothing(x0_mpo) ? _green_krylov_start(rhs, A, sites2, start, rng, cutoff) : + _vec_mps_from_mpo(x0_mpo, sites2; cutoff=cutoff, maxdim=maxdim) - verbose && println("Krylov GF: ω = $ω_phys + $(η)i (N=$N, maxdim=$maxdim, nsweeps=$nsweeps, MINRES)", + verbose && println("Krylov GF: ω = $ω_phys + $(η)i (N=$N, maxdim=$maxdim, nsweeps=$nsweeps", + solver === :minres ? ", MINRES)" : ")", isnothing(x0_mpo) ? "" : " [KPM warm start]") - stats = [0, 0, 0] - sol = ITensorMPS.linsolve(Aop, rhs, x0, z, -1.0; - nsweeps = nsweeps, - maxdim = maxdim, - cutoff = cutoff, - updater = _minres_updater, - updater_kwargs = (; tol, maxiter = maxiter * krylovdim, stats)) - verbose && println(" MINRES: $(stats[1]) local solves, $(stats[2]) products, ", - "$(stats[3]) stopped above tol") - return custom_mpo(sol, sites) + if solver === :minres + stats = [0, 0, 0] + sol = ITensorMPS.linsolve(A, rhs, x0, a0, a1; + nsweeps = nsweeps, + maxdim = maxdim, + cutoff = cutoff, + updater = _minres_updater, + updater_kwargs = (; tol, maxiter = maxiter * krylovdim, stats)) + verbose && println(" MINRES: $(stats[1]) local solves, $(stats[2]) products, ", + "$(stats[3]) stopped above tol") + else + sol = ITensorMPS.linsolve(A, rhs, x0; + nsweeps = nsweeps, + maxdim = maxdim, + cutoff = cutoff, + updater_kwargs = (; ishermitian, tol, maxiter, krylovdim)) + end + G = custom_mpo(sol, sites) + (return_info || isfinite(residual_warn)) || return G + + res = _linsolve_residual(A, rhs, sol, a0, a1) + verbose && println(" relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ = $res") + res > residual_warn && + @warn "get_green_krylov: G is not converged: relative residual " * + "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Raise maxdim " * + "or nsweeps, or lower cutoff (maxlinkdim reached: $(maxlinkdim(sol)))." ω_phys η + return return_info ? (; G, residual = res, maxlinkdim = maxlinkdim(sol)) : G end """ get_green_krylov(H::TBHamiltonian, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, solver=:gmres, ishermitian=false, - tol=1e-10, maxiter=600, krylovdim=30, verbose=false) -> MPO + cutoff=1e-8, x0_mpo=nothing, start=:random, rng=Xoshiro(1), + solver=:gmres, ishermitian=false, tol=1e-10, maxiter=600, + krylovdim=30, return_info=false, residual_warn=1e-2, + verbose=false) -> MPO Compute the retarded Green's function @@ -285,14 +308,26 @@ unscaled — no KPM Chebyshev expansion required. - `η` : Lorentzian broadening. Default `1e-2`. - `nsweeps` : Number of DMRG sweeps for the linear solver. Default `12`. - `maxdim` : Maximum bond dimension of the solution MPS. Default `100`. -- `cutoff` : SVD truncation cutoff. Default `1e-8`. +- `cutoff` : SVD truncation cutoff, relative to ‖|G⟩⟩‖², which grows with the + system. Default `1e-8`. It sets the accuracy floor of G, which the + residual shows: on an open 2^10-site chain with one impurity the + default leaves 1e-6–2e-4, `1e-12` 1e-11–1e-6. - `x0_mpo` : Optional MPO initial guess for G(ω). When provided it is vectorized via `_vec_mps_from_mpo` and passed as `x0` to - `linsolve`, replacing the default guess (the vectorized identity - with its bonds enlarged by a global Krylov expansion, - `ITensorMPS.expand`). + `linsolve`, replacing the start chosen by `start`. Typical use: pass a low-accuracy KPM Green's function to warm-start the Krylov iteration. Default `nothing`. +- `start` : The start of the sweeps when `x0_mpo` is not given. + `:random` (default since v0.2.2): |I⟩⟩ + 0.1·(a random MPS of + bond dimension 8, drawn from `rng`), with its bonds enlarged by + an 8-vector global Krylov expansion (`ITensorMPS.expand`). + `:identity`: |I⟩⟩ with a 2-vector expansion, the start up to + v0.2.1, which stalls on a short-ranged G (an open chain's end + corrections, ω outside the band) and returns a G wrong by + 1e-3–0.2. The sweeps keep the rank their start spans. +- `rng` : Random number generator of the `:random` start. Default + `Random.Xoshiro(1)`, created anew at each call, so the result + does not depend on the global RNG state. - `solver` : Local solver of the sweeps. `:gmres` (default): KrylovKit's GMRES, restarted every `krylovdim` products. `:minres`: MINRES, for Hermitian H only (an `ArgumentError` otherwise, from @@ -308,8 +343,15 @@ unscaled — no KPM Chebyshev expansion required. residual). Default `1e-10`. - `maxiter` : Maximum Krylov iterations per site (GMRES restarts). Default `600`. - `krylovdim` : Krylov subspace dimension. Default `30`. +- `return_info` : Return `(; G, residual, maxlinkdim)` instead of the MPO `G`. + Default `false`. +- `residual_warn`: Warn when the relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the + solve exceeds this value: G is then not converged (`maxdim`, + `nsweeps` or `cutoff` too coarse). The residual comes from exact + inner products; values below ~1e-7 are rounding noise. `Inf` + skips its computation unless `return_info=true`. Default `1e-2`. - `verbose` : Print progress messages (with MINRES, also the number of local - solves and products). Default `false`. + solves and products) and the residual. Default `false`. **Usage** ```julia @@ -335,15 +377,20 @@ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; maxdim::Int = 100, cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, + start::Symbol = :random, + rng::AbstractRNG = Random.Xoshiro(1), solver::Symbol = :gmres, ishermitian::Bool = false, tol::Real = 1e-10, maxiter::Int = 600, krylovdim::Int = 30, + return_info::Bool = false, + residual_warn::Real = 1e-2, verbose::Bool = false) return get_green_krylov(H.mpo, H.sites, ω_phys; - η, nsweeps, maxdim, cutoff, x0_mpo, solver, ishermitian, - tol, maxiter, krylovdim, verbose) + η, nsweeps, maxdim, cutoff, x0_mpo, start, rng, solver, + ishermitian, tol, maxiter, krylovdim, return_info, + residual_warn, verbose) end diff --git a/test/bugfix_green_krylov_start.jl b/test/bugfix_green_krylov_start.jl new file mode 100644 index 0000000..e433044 --- /dev/null +++ b/test/bugfix_green_krylov_start.jl @@ -0,0 +1,46 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Random, Test +using TensorBinding: get_Hamiltonian, add_onsite!, get_green_krylov + +# get_green_krylov started from |I⟩⟩ with a 2-vector global Krylov expansion (up to v0.2.1), +# and the two-site sweeps keep the rank their start spans. On a short-ranged G (ω outside +# the band) of a chain with an impurity, the default settings returned the G without the +# impurity's correction, 9 % off, with nothing to show it. The default start is now +# |I⟩⟩ + 0.1·(random MPS, χ 8) with an 8-vector expansion, and the residual of the solve is +# checked: above `residual_warn` (1e-2) a warning says that G is not converged. +@testset "get_green_krylov: random start and residual check" begin + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + relerr(G, Gx, s) = norm(dmat(G, s) - Gx) / norm(Gx) + + H = get_Hamiltonian("chain_1d", 1.0; L=6) # 64 sites, band [−2, 2] + v = zeros(64); v[64 ÷ 3 + 1] = 2.0 + add_onsite!(H, v; tol=1e-14) + s = H.sites + Hd = dmat(H.mpo, s) + ω = 2.3 + for η in (0.1, 0.01) + Gx = inv((ω + im * η) * I - Hd) + # the start of v0.2.1: wrong, and the residual check now says so + old = @test_logs (:warn, r"not converged") match_mode=:any get_green_krylov( + H, ω; η, start=:identity, return_info=true) + @test relerr(old.G, Gx, s) > 1e-2 + @test old.residual > 1e-2 + # the new default start, with GMRES and with MINRES + for solver in (:gmres, :minres) + new = get_green_krylov(H, ω; η, solver, return_info=true) + @test relerr(new.G, Gx, s) < 1e-5 + @test new.residual < 1e-5 + end + end + + # The random start does not depend on the global RNG; `rng` changes it. + Random.seed!(1); G1 = get_green_krylov(H, 0.5; η=0.1) + Random.seed!(2); G2 = get_green_krylov(H, 0.5; η=0.1) + @test dmat(G1, s) ≈ dmat(G2, s) rtol=1e-12 + G3 = get_green_krylov(H, 0.5; η=0.1, rng=Random.Xoshiro(7)) + @test dmat(G3, s) ≈ inv((0.5 + 0.1im) * I - Hd) rtol=1e-6 + + # residual_warn = Inf: no check, no warning, the plain MPO + G = @test_logs get_green_krylov(H, ω; η=0.1, start=:identity, residual_warn=Inf) + @test G isa MPO + @test_throws ArgumentError get_green_krylov(H, ω; start=:zero) +end diff --git a/test/data/dynamics_golden.jl b/test/data/dynamics_golden.jl index 30bab21..2519eca 100644 --- a/test/data/dynamics_golden.jl +++ b/test/data/dynamics_golden.jl @@ -2,8 +2,10 @@ # # Pinned outputs of the dynamics area, checked by test/golden_dynamics.jl. # Generated from src/ tree 79984c1eef99 -# with Julia 1.12.3. Regenerate only for an intentional behaviour change; -# see the generator's header. +# with Julia 1.12.3. +# Rerun with --only from src/ tree 68e3dc7206aa with Julia 1.12.3: +# krylov_green_ishermitian_flag, krylov_green_raw, krylov_green_tbhamiltonian. +# Regenerate only for an intentional behaviour change; see the generator's header. # # Each entry is (name, group, seed, throws, expected): the case of that name in # DynamicsGoldenRunner.CASES is run after Random.seed!(seed); `throws` is the @@ -149,16 +151,16 @@ push!(DYNAMICS_GOLDEN_CASES, (name = "krylov_vec_mps_from_mpo_wrong_length_throw expected = (message_prefix = "_vec_mps_from_mpo: sites2 length 5 ≠ 2L=6",))) push!(DYNAMICS_GOLDEN_CASES, (name = "krylov_green_raw", group = "Krylov", seed = 303, throws = nothing, - expected = (G = ComplexF64[-0.05973630129647073 - 1.2169359629620449im -0.7745336977965324 - 0.37702804914790183im -0.09721819821291124 + 0.9489208086583698im 0.5555840766009857 + 0.6422606521028371im 0.135441290772635 - 0.6451257977073128im -0.38592652982772846 - 0.808710133260502im -0.08947722306885379 + 0.3253274517636181im 0.2940178725543507 + 0.8884129241758174im; -0.7745336977965304 - 0.37702804914790683im -0.1569544995093775 - 0.26801515430367984im -0.21894962119554326 + 0.2652326029549242im 0.03822309255972779 + 0.30379501095105027im 0.16965754677325534 - 0.1664494811576686im 0.04596406770377663 - 0.31979834594369827im -0.09190865727338368 + 0.07970279091531435im -0.08947722306885816 + 0.3253274517636163im; -0.0972181982129059 + 0.9489208086583597im -0.21894962119554423 + 0.2652326029549337im -0.021513208736740508 - 0.9131409520109932im -0.6048761510232794 - 0.5434775303055759im -0.051254130509129185 + 0.6291224627146659im 0.46367541932760686 + 0.7219634430181474im 0.04596406770378429 - 0.31979834594370016im -0.3859265298277349 - 0.8087101332605033im; 0.5555840766009874 + 0.6422606521028368im 0.03822309255972953 + 0.3037950109510448im -0.6048761510232806 - 0.5434775303055791im -0.11099043180559637 - 0.5878135002473749im -0.3108582784689284 + 0.3449353938702421im -0.05125413050912967 + 0.629122462714666im 0.1696575467732528 - 0.16644948115766578im 0.1354412907726425 - 0.6451257977073174im; 0.13544129077263745 - 0.6451257977073145im 0.16965754677325093 - 0.16644948115766922im -0.05125413050913069 + 0.6291224627146652im -0.3108582784689243 + 0.3449353938702382im -0.11099043180559709 - 0.5878135002473757im -0.6048761510232762 - 0.5434775303055808im 0.03822309255972817 + 0.3037950109510482im 0.5555840766009886 + 0.6422606521028349im; -0.3859265298277327 - 0.8087101332605022im 0.045964067703779754 - 0.3197983459436953im 0.4636754193276105 + 0.7219634430181537im -0.05125413050913218 + 0.6291224627146649im -0.6048761510232789 - 0.5434775303055742im -0.021513208736740674 - 0.913140952010991im -0.2189496211955464 + 0.2652326029549357im -0.0972181982129112 + 0.9489208086583629im; -0.08947722306885666 + 0.325327451763615im -0.09190865727338415 + 0.0797027909153114im 0.045964067703778956 - 0.3197983459437005im 0.16965754677325293 - 0.1664494811576689im 0.03822309255973137 + 0.3037950109510492im -0.21894962119554662 + 0.26523260295492357im -0.15695449950937906 - 0.26801515430368356im -0.7745336977965284 - 0.3770280491479051im; 0.2940178725543504 + 0.8884129241758157im -0.0894772230688548 + 0.32532745176361816im -0.3859265298277298 - 0.8087101332604979im 0.1354412907726392 - 0.6451257977073123im 0.5555840766009827 + 0.6422606521028362im -0.0972181982129093 + 0.9489208086583669im -0.7745336977965336 - 0.37702804914790217im -0.0597363012964684 - 1.216935962962044im],))) + expected = (G = ComplexF64[-0.05973630129646784 - 1.2169359629620429im -0.7745336977965338 - 0.377028049147913im -0.09721819821291011 + 0.9489208086583618im 0.5555840766009895 + 0.6422606521028329im 0.135441290772639 - 0.6451257977073164im -0.3859265298277377 - 0.8087101332605033im -0.08947722306886098 + 0.32532745176361927im 0.2940178725543492 + 0.8884129241758183im; -0.7745336977965378 - 0.3770280491479065im -0.15695449950937984 - 0.268015154303677im -0.21894962119554517 + 0.2652326029549297im 0.03822309255972899 + 0.303795010951048im 0.1696575467732538 - 0.166449481157671im 0.04596406770378003 - 0.31979834594369694im -0.09190865727337531 + 0.07970279091530935im -0.08947722306886058 + 0.3253274517636185im; -0.09721819821290714 + 0.9489208086583653im -0.21894962119554948 + 0.2652326029549281im -0.021513208736740674 - 0.913140952010998im -0.6048761510232812 - 0.5434775303055721im -0.05125413050912971 + 0.6291224627146692im 0.46367541932759704 + 0.7219634430181552im 0.04596406770377888 - 0.31979834594369994im -0.38592652982773823 - 0.8087101332605048im; 0.5555840766009885 + 0.642260652102838im 0.03822309255973548 + 0.30379501095105316im -0.6048761510232739 - 0.5434775303055687im -0.11099043180558901 - 0.5878135002473768im -0.31085827846891506 + 0.3449353938702447im -0.05125413050912434 + 0.6291224627146699im 0.1696575467732582 - 0.16644948115766905im 0.13544129077263883 - 0.6451257977073134im; 0.13544129077263814 - 0.645125797707315im 0.1696575467732533 - 0.16644948115766356im -0.05125413050912937 + 0.6291224627146649im -0.3108582784689343 + 0.3449353938702435im -0.11099043180560036 - 0.58781350024738im -0.6048761510232803 - 0.5434775303055689im 0.038223092559728986 + 0.30379501095105116im 0.5555840766009822 + 0.6422606521028356im; -0.3859265298277325 - 0.8087101332605078im 0.04596406770378112 - 0.31979834594370193im 0.46367541932761136 + 0.7219634430181451im -0.05125413050912537 + 0.6291224627146622im -0.6048761510232703 - 0.543477530305581im -0.02151320873673851 - 0.913140952010997im -0.21894962119554479 + 0.26523260295492im -0.0972181982129095 + 0.9489208086583697im; -0.08947722306885894 + 0.325327451763617im -0.09190865727338324 + 0.07970279091531612im 0.045964067703780226 - 0.3197983459437017im 0.16965754677324962 - 0.16644948115766434im 0.03822309255972669 + 0.30379501095104766im -0.2189496211955479 + 0.2652326029549303im -0.15695449950938012 - 0.2680151543036769im -0.7745336977965356 - 0.37702804914790866im; 0.29401787255435485 + 0.8884129241758117im -0.08947722306885955 + 0.3253274517636131im -0.3859265298277307 - 0.8087101332605038im 0.13544129077263906 - 0.6451257977073197im 0.5555840766009893 + 0.642260652102833im -0.0972181982129117 + 0.9489208086583611im -0.7745336977965314 - 0.37702804914791216im -0.059736301296467675 - 1.2169359629620433im],))) push!(DYNAMICS_GOLDEN_CASES, (name = "krylov_green_warm_start", group = "Krylov", seed = 304, throws = nothing, expected = (G = ComplexF64[-0.059736301296467786 - 1.2169359629620429im -0.7745336977965336 - 0.3770280491479058im -0.09721819821291072 + 0.9489208086583659im 0.555584076600988 + 0.6422606521028386im 0.13544129077263786 - 0.6451257977073159im -0.3859265298277331 - 0.808710133260501im -0.08947722306885816 + 0.3253274517636153im 0.29401787255435174 + 0.8884129241758167im; -0.7745336977965335 - 0.3770280491479079im -0.15695449950937412 - 0.26801515430367845im -0.21894962119554504 + 0.2652326029549301im 0.03822309255972992 + 0.30379501095104844im 0.1696575467732551 - 0.16644948115766733im 0.04596406770378255 - 0.3197983459436947im -0.09190865727338153 + 0.07970279091531274im -0.08947722306885604 + 0.32532745176362177im; -0.09721819821291167 + 0.9489208086583643im -0.21894962119554465 + 0.265232602954928im -0.021513208736741285 - 0.9131409520109949im -0.6048761510232772 - 0.5434775303055774im -0.051254130509129275 + 0.629122462714664im 0.4636754193276053 + 0.7219634430181505im 0.04596406770378164 - 0.3197983459436999im -0.3859265298277324 - 0.8087101332605044im; 0.555584076600986 + 0.642260652102836im 0.03822309255973292 + 0.303795010951048im -0.6048761510232781 - 0.5434775303055716im -0.11099043180559173 - 0.5878135002473783im -0.310858278468928 + 0.3449353938702426im -0.05125413050912941 + 0.6291224627146723im 0.16965754677325323 - 0.16644948115766944im 0.13544129077263975 - 0.6451257977073118im; 0.13544129077263792 - 0.6451257977073128im 0.16965754677325318 - 0.16644948115766994im -0.05125413050913025 + 0.629122462714668im -0.31085827846892566 + 0.34493539387024136im -0.11099043180559617 - 0.5878135002473763im -0.604876151023279 - 0.5434775303055763im 0.03822309255972917 + 0.3037950109510493im 0.5555840766009865 + 0.6422606521028362im; -0.3859265298277324 - 0.8087101332605031im 0.04596406770378174 - 0.31979834594370277im 0.4636754193276073 + 0.7219634430181514im -0.051254130509128144 + 0.6291224627146605im -0.6048761510232759 - 0.5434775303055763im -0.02151320873674306 - 0.9131409520109923im -0.2189496211955465 + 0.2652326029549276im -0.09721819821291261 + 0.9489208086583654im; -0.0894772230688566 + 0.3253274517636166im -0.09190865727338081 + 0.07970279091531662im 0.04596406770378343 - 0.3197983459436976im 0.16965754677325545 - 0.16644948115766584im 0.03822309255972954 + 0.30379501095104666im -0.21894962119554415 + 0.265232602954931im -0.15695449950937773 - 0.26801515430367917im -0.7745336977965329 - 0.3770280491479051im; 0.29401787255434947 + 0.8884129241758152im -0.0894772230688603 + 0.3253274517636118im -0.38592652982773007 - 0.8087101332605018im 0.13544129077263595 - 0.6451257977073195im 0.5555840766009861 + 0.6422606521028372im -0.09721819821291136 + 0.948920808658367im -0.7745336977965318 - 0.3770280491479052im -0.05973630129647034 - 1.2169359629620409im],))) push!(DYNAMICS_GOLDEN_CASES, (name = "krylov_green_tbhamiltonian", group = "Krylov", seed = 305, throws = nothing, - expected = (G = ComplexF64[-1.0502509578958574 - 1.039730937455917im -0.5949081538700902 + 0.3885840989909772im 1.2991320224570198 + 0.7710753687266645im -0.15104227844927057 - 0.6117302805471804im -1.1471445981640072 - 0.48409051325923635im 0.7851258916886594 + 0.7103824624062995im 0.6657838445188744 + 0.2270400185171696im -1.1463978162627533 - 0.7406794911000234im; -0.5949081538700791 + 0.3885840989909735im 0.24888106456116893 - 0.2686555687292499im -0.745950432319338 - 0.2231461815562082im 0.1519874242930265 + 0.2869848554674259im 0.6340836132394053 + 0.09865218185911628im -0.4813607536451201 - 0.2570504947420703im -0.3612719245740823 - 0.030297028693724748im 0.6657838445188786 + 0.22704001851716565im; 1.2991320224570244 + 0.7710753687266669im -0.7459504323193427 - 0.22314618155619986im -0.8982635336028225 - 0.7527460819884824im 0.03917545936931867 + 0.4872362808501022im 0.8177712688119066 + 0.5140248739845997im -0.5123142030233422 - 0.6420273092408939im -0.4813607536451206 - 0.25705049474206276im 0.7851258916886668 + 0.7103824624063038im; -0.15104227844926113 - 0.6117302805471785im 0.151987424293026 + 0.286984855467435im 0.03917545936931684 + 0.4872362808501019im -0.2324796890839549 - 0.525706063471308im -1.107222356893433 - 0.25344321024992034im 0.8177712688119059 + 0.5140248739846034im 0.6340836132394059 + 0.09865218185912539im -1.1471445981639954 - 0.4840905132592359im; -1.1471445981639974 - 0.4840905132592306im 0.6340836132394002 + 0.09865218185911528im 0.8177712688119081 + 0.5140248739846014im -1.1072223568934267 - 0.2534432102499329im -0.2324796890839438 - 0.5257060634713159im 0.03917545936931875 + 0.48723628085008985im 0.1519874242930297 + 0.2869848554674315im -0.15104227844926604 - 0.611730280547183im; 0.7851258916886608 + 0.7103824624062918im -0.48136075364511716 - 0.25705049474206926im -0.5123142030233515 - 0.6420273092409161im 0.8177712688119032 + 0.5140248739845972im 0.0391754593693136 + 0.4872362808500826im -0.8982635336028265 - 0.7527460819884844im -0.7459504323193498 - 0.2231461815562093im 1.2991320224570226 + 0.7710753687266635im; 0.6657838445188745 + 0.22704001851717026im -0.3612719245740868 - 0.030297028693723638im -0.48136075364512515 - 0.2570504947420642im 0.6340836132393918 + 0.09865218185911728im 0.15198742429302367 + 0.2869848554674367im -0.7459504323193574 - 0.22314618155620358im 0.24888106456116305 - 0.26865556872924673im -0.5949081538700871 + 0.38858409899097746im; -1.1463978162627457 - 0.7406794911000212im 0.6657838445188784 + 0.22704001851716948im 0.7851258916886683 + 0.7103824624062962im -1.1471445981639912 - 0.4840905132592287im -0.15104227844925555 - 0.6117302805471806im 1.2991320224570242 + 0.7710753687266698im -0.5949081538700803 + 0.3885840989909737im -1.050250957895855 - 1.039730937455912im],))) + expected = (G = ComplexF64[-1.0502509578958559 - 1.0397309374559218im -0.5949081538700853 + 0.3885840989909734im 1.2991320224570269 + 0.7710753687266612im -0.1510422784492628 - 0.6117302805471887im -1.1471445981639978 - 0.48409051325923547im 0.7851258916886648 + 0.710382462406301im 0.6657838445188775 + 0.22704001851716504im -1.146397816262753 - 0.7406794911000298im; -0.5949081538700836 + 0.3885840989909766im 0.24888106456116868 - 0.2686555687292479im -0.7459504323193449 - 0.22314618155620253im 0.15198742429302564 + 0.28698485546743674im 0.6340836132394001 + 0.09865218185911728im -0.48136075364512365 - 0.25705049474206787im -0.3612719245740844 - 0.03029702869371942im 0.6657838445188724 + 0.22704001851716643im; 1.299132022457025 + 0.7710753687266605im -0.7459504323193418 - 0.2231461815562099im -0.8982635336028237 - 0.7527460819884918im 0.03917545936931795 + 0.4872362808500872im 0.8177712688119058 + 0.5140248739845968im -0.5123142030233471 - 0.6420273092409087im -0.4813607536451229 - 0.2570504947420629im 0.7851258916886654 + 0.7103824624063im; -0.15104227844926127 - 0.6117302805471768im 0.1519874242930279 + 0.2869848554674375im 0.03917545936932185 + 0.48723628085009724im -0.23247968908394695 - 0.5257060634713091im -1.1072223568934287 - 0.2534432102499226im 0.8177712688119066 + 0.5140248739846056im 0.6340836132394034 + 0.09865218185912261im -1.1471445981639936 - 0.48409051325923597im; -1.1471445981639996 - 0.48409051325923397im 0.6340836132394023 + 0.09865218185911828im 0.8177712688119021 + 0.5140248739845976im -1.107222356893427 - 0.25344321024993277im -0.23247968908394845 - 0.5257060634713145im 0.03917545936932171 + 0.48723628085009396im 0.15198742429302906 + 0.28698485546743596im -0.15104227844926107 - 0.6117302805471831im; 0.785125891688661 + 0.7103824624063027im -0.48136075364511727 - 0.2570504947420699im -0.512314203023345 - 0.6420273092409023im 0.8177712688119044 + 0.514024873984603im 0.039175459369316185 + 0.48723628085010084im -0.8982635336028277 - 0.7527460819884778im -0.7459504323193457 - 0.22314618155620258im 1.2991320224570224 + 0.7710753687266665im; 0.6657838445188797 + 0.2270400185171646im -0.361271924574077 - 0.030297028693728856im -0.48136075364511566 - 0.25705049474206837im 0.634083613239407 + 0.09865218185911928im 0.15198742429302992 + 0.2869848554674273im -0.7459504323193423 - 0.22314618155621185im 0.2488810645611699 - 0.2686555687292522im -0.5949081538700791 + 0.38858409899097646im; -1.1463978162627446 - 0.7406794911000121im 0.6657838445188725 + 0.22704001851717176im 0.7851258916886598 + 0.7103824624063013im -1.1471445981639958 - 0.4840905132592343im -0.15104227844926005 - 0.611730280547175im 1.2991320224570222 + 0.7710753687266705im -0.5949081538700831 + 0.38858409899097945im -1.0502509578958523 - 1.0397309374559125im],))) push!(DYNAMICS_GOLDEN_CASES, (name = "krylov_green_ishermitian_flag", group = "Krylov", seed = 306, throws = nothing, - expected = (G = ComplexF64[0.4902242599241317 - 1.233674751247206im -0.28674059267894664 - 0.7165050478958059im -0.47599116043065226 + 0.6460990399164567im -0.24028293159744665 + 1.0259770277081295im; -0.286740592678947 - 0.716505047895805im 0.014233099493479284 - 0.5875757113307479im -0.5270235242763937 + 0.3094719798123266im -0.47599116043065354 + 0.6460990399164586im; -0.4759911604306524 + 0.6460990399164577im -0.5270235242763931 + 0.3094719798123256im 0.014233099493479728 - 0.5875757113307479im -0.28674059267894764 - 0.7165050478958053im; -0.24028293159744565 + 1.0259770277081288im -0.47599116043065104 + 0.6460990399164559im -0.2867405926789461 - 0.7165050478958057im 0.4902242599241305 - 1.233674751247205im],))) + expected = (G = ComplexF64[0.4902242599241298 - 1.2336747512472024im -0.2867405926789469 - 0.7165050478958028im -0.47599116043064993 + 0.6460990399164568im -0.24028293159744787 + 1.0259770277081284im; -0.28674059267894797 - 0.7165050478958047im 0.014233099493479118 - 0.5875757113307476im -0.5270235242763944 + 0.30947197981232266im -0.4759911604306538 + 0.646099039916455im; -0.47599116043065126 + 0.6460990399164546im -0.5270235242763918 + 0.309471979812326im 0.014233099493480839 - 0.5875757113307479im -0.2867405926789484 - 0.716505047895802im; -0.24028293159744735 + 1.025977027708127im -0.47599116043065093 + 0.6460990399164571im -0.2867405926789448 - 0.7165050478958052im 0.4902242599241292 - 1.2336747512472008im],))) push!(DYNAMICS_GOLDEN_CASES, (name = "haydock_identity_seed", group = "Haydock", seed = 310, throws = nothing, expected = (a = [0.899999999999999, 0.9000000000000002, 0.8999999999999985, 0.9000000000000035, 0.8999999999999966], b = [2.8284271247461907, 1.400000000000001, 1.1739302555509699, 1.1602646855992258, 1.0492002025369975], norm0 = 2.8284271247461907, basis = [[0.3535533905932738 0.0 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.3535533905932738 0.0 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.35355339059327373 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.35355339059327373 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.35355339059327373 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.35355339059327373 0.0 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 0.3535533905932737 0.0; 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.3535533905932737], [-0.17677669529663712 0.25253813613805287 -5.606121578003572e-16 2.1856856311971264e-16 -1.5757177929205947e-16 -3.002895540073305e-17 -3.429732701888599e-17 -1.0500493999080774e-16; 0.2525381361380526 -0.1262690680690264 0.25253813613805265 -5.993018064410286e-16 -3.249974874349008e-17 7.815396419352776e-17 -1.9274558851728525e-16 6.697886645248116e-17; 1.9045564674653404e-16 0.2525381361380525 -0.07576144084141534 0.252538136138053 -4.65312248293357e-16 2.584407301627417e-16 -7.333338225876043e-17 6.154320906616307e-17; 3.83543753304003e-16 8.197694574590661e-16 0.2525381361380529 -0.02525381361380491 0.2525381361380526 -5.21129368350348e-16 -8.52298020702773e-17 -7.415929242531029e-17; -1.859883650241373e-16 -2.1703287420016404e-17 4.563709803863861e-16 0.25253813613805254 0.02525381361380524 0.2525381361380528 -5.821218598179571e-16 3.379801001317688e-16; 1.4429450493374202e-17 9.702181422682699e-17 2.2713755291527204e-16 6.14569445478402e-16 0.2525381361380526 0.07576144084141577 0.2525381361380524 -5.82996274851359e-16; 1.0287386967529728e-16 1.665233288754921e-17 -4.1239595890865604e-17 5.783005767894495e-17 3.837323577388086e-16 0.25253813613805226 0.1262690680690267 0.25253813613805276; 2.8966747002257167e-16 -7.292196456499639e-17 -3.2763553023372715e-18 -3.356266970618317e-17 3.6661889810578293e-16 5.65777424807603e-16 0.2525381361380527 0.17677669529663698], [-0.10110730464918265 -0.2581463097425957 0.2151219247854961 -6.512599248717149e-16 -1.7918285845696202e-16 -3.6160955986124903e-16 5.15179705131627e-16 4.446304565113339e-16; -0.2581463097425953 0.0623853581877938 -0.1720975398283963 0.21512192478549585 -5.466575438312858e-16 -3.1556563967298897e-16 -4.92171202811058e-16 6.386952937722296e-16; 0.21512192478549563 -0.17209753982839507 0.027965850222113602 -0.0860487699141983 0.21512192478549672 -5.257538661769664e-16 -6.680690826252884e-16 -2.37377438705078e-16; 2.325425594288819e-16 0.21512192478549458 -0.08604876991419744 0.010756096239274957 1.730171035145478e-16 0.21512192478549644 -9.67975045804219e-16 1.0869652373597887e-16; 8.16147649986939e-16 6.656238796367799e-16 0.2151219247854941 3.013734425915571e-16 0.010756096239273783 0.08604876991419749 0.21512192478549455 -8.818266684909119e-16; 1.1321556868579762e-16 2.101739958762751e-16 3.6962646172006787e-16 0.21512192478549527 0.08604876991419841 0.02796585022211401 0.17209753982839598 0.21512192478549608; -3.452926312282139e-16 5.233394019648193e-16 6.501970698743897e-16 7.611780489183624e-16 0.21512192478549438 0.17209753982839635 0.06238535818779304 0.25814630974259434; -2.895862744960409e-16 -1.3578541117497144e-15 -1.1243274527628678e-16 4.063184106110622e-16 6.00496066581516e-16 0.21512192478549608 0.2581463097425952 -0.10110730464918269], [0.017368721850274865 -0.04600190314138036 -0.27811144403795357 0.18540762935863708 -7.359622538413357e-16 -4.652030707426657e-16 -2.2162179900423633e-15 -6.553471159729785e-16; -0.046001903141380356 -0.26994310281595457 0.028161148602074037 -0.16686686642277263 0.1854076293586372 -3.0871337951719915e-16 5.569364437033469e-16 7.965616987183966e-16; -0.2781114440379544 0.02816114860207271 -0.1530662954803578 -0.03858559796703523 -0.0556222888075912 0.1854076293586362 -1.3149052658358642e-15 6.685798167437965e-16; 0.18540762935863628 -0.16686686642277093 -0.03858559796703715 -0.049538837458583854 -0.06083451349007071 0.05562228880759059 0.18540762935863497 -5.138653786641866e-16; -9.55263209310895e-18 0.18540762935863478 -0.05562228880759071 -0.06083451349007123 0.04953883745858353 -0.03858559796703529 0.16686686642277193 0.1854076293586366; 1.631430149818024e-16 1.1228730028497236e-15 0.18540762935863578 0.05562228880759165 -0.038585597967036374 0.15306629548035763 0.02816114860207456 0.278111444037954; 3.238430715884901e-16 -1.4581658869045182e-15 3.863159321784474e-16 0.1854076293586362 0.16686686642277276 0.028161148602074994 0.26994310281595457 -0.046001903141379634; -5.779387094169626e-16 -6.844499326284106e-16 1.5729243181867197e-15 2.128661007167426e-15 0.18540762935863567 0.2781114440379546 -0.04600190314137943 -0.01736872185027555], [0.056377444896567014 0.05887939798879997 -0.025504391758023885 -0.28274127879169647 0.17671329924481027 -1.1669736473741102e-16 2.152767167587502e-15 -1.2358715250562973e-15; 0.058879397988799015 0.04264893273630199 -0.23406363750610554 -0.018435859788230625 -0.14137063939585104 0.17671329924481108 -3.4043729549684996e-15 -6.021666788216207e-16; -0.025504391758022167 -0.23406363750610623 0.0029047372048728193 -0.1000673420255516 -0.10325824342573989 -2.0773434956016055e-16 0.17671329924480964 -7.523343351660957e-16; -0.2827412787916965 -0.018435859788232613 -0.10006734202555204 -0.10193111483774607 -8.958491152004367e-16 -0.10325824342574112 0.14137063939584532 0.1767132992448087; 0.17671329924481238 -0.14137063939584793 -0.10325824342574096 1.2849684409360465e-15 -0.10193111483774543 0.10006734202555194 -0.018435859788231906 0.28274127879169625; -2.775704117674233e-17 0.1767132992448082 -8.436178910079317e-16 -0.1032582434257413 0.10006734202555184 0.0029047372048734616 0.23406363750610737 -0.025504391758025154; -5.12643376799739e-16 -6.798003721242912e-16 0.17671329924481222 0.14137063939585046 -0.018435859788233002 0.23406363750610848 0.04264893273630356 -0.058879397988798376; 4.34399220971729e-16 -4.37581511886867e-16 1.6010698056248595e-15 0.1767132992448142 0.2827412787916953 -0.02550439175802464 -0.058879397988798585 0.05637744489656738]], cf = ComplexF64[0.9783201853338807 - 2.6186628434526615im, 1.5662231629800385 - 1.9373056305114613im, -7.519330688647888 - 8.862055320073003im], cf_truncated = ComplexF64[-2.5712098020358534 - 1.4681026746283272im, -24.27193138908918 - 15.06777180354717im, 0.6089467451196199 - 0.7341578443359134im], resolve = ComplexF64[0.31301728054746775 - 0.4550363809297271im -0.9426333070782376 + 0.10857418707393457im -0.14946354413619806 + 0.25341346990929225im 0.8183670052177796 - 0.2355535601380048im -0.34675715940521945 + 0.06925088677373303im -1.6375789613221059e-15 + 4.996003610813204e-16im -8.548599488464789e-15 + 1.7887101498549732e-15im -5.251323908167113e-16 + 4.160695063265542e-16im; -0.9426333070782343 + 0.10857418707393368im -0.024972925004379032 - 0.17990807360564834im -0.1840517195149352 - 0.025613985100352288im -0.005200500410755685 + 0.18133222060022525im 0.5409612776936086 - 0.18015285071901882im -0.3467571594052211 + 0.069250886773734im 6.80938251976726e-15 - 1.5836278593984242e-15im 2.522329606974878e-15 - 1.937950671411026e-15im; -0.1494635441362071 + 0.2534134699092928im -0.1840517195149349 - 0.025613985100350956im 0.08247977481808072 - 0.25711211993478866im -0.46353764720341295 + 0.10231961255872435im 0.3193762662054073 + 0.07324051016881475im 0.26355555016942467 - 0.12475214130003026im -0.34675715940522145 + 0.0692508867737335im 2.9282132274488504e-15 - 5.967448757360216e-16im; 0.8183670052177782 - 0.23555356013800424im -0.005200500410752122 + 0.18133222060022472im -0.4635376472034135 + 0.1023196125587246im 0.31434901199356036 - 0.344739907854452im -0.6131928682454222 + 0.18701652604523528im 0.4775095963070658 - 0.0016107746112064003im -0.013850177354744891 - 0.06935143188104434im -0.34675715940521534 + 0.06925088677373435im; -0.3467571594052232 + 0.06925088677373503im 0.5409612776936029 - 0.1801528507190171im 0.31937626620540766 + 0.07324051016881358im -0.6131928682454235 + 0.18701652604523686im 0.34984376844613063 - 0.3821878874254251im -0.6995947572467759 + 0.24177292561974198im 0.4691994898942188 - 0.04322163373983007im -0.29125590487892505 - 0.013950722462057086im; 1.6028844918025698e-15 - 3.3306690738754696e-16im -0.3467571594052134 + 0.0692508867737319im 0.2635555501694292 - 0.12475214130002937im 0.4775095963070659 - 0.0016107746112049293im -0.6995947572467753 + 0.24177292561974206im 0.20161471058392796 - 0.3754441614301019im -0.7893206888132609 + 0.2798849815427945im 0.6412031063720839 - 0.12084295399079986im; 3.0011735800040776e-15 - 1.8048249087515857e-15im 1.2253818600546636e-15 + 1.6656390868091826e-15im -0.3467571594052217 + 0.06925088677373403im -0.013850177354751192 - 0.06935143188104348im 0.46919948989421834 - 0.043221633739831566im -0.7893206888132638 + 0.2798849815427956im 0.2157541892991398 - 0.39708848537250996im -0.24158354138550714 + 0.2454985224085343im; 7.382034405414496e-16 + 1.7683402849769302e-15im -8.303790869144606e-17 + 2.2394679983106707e-16im -7.355227538141662e-16 - 1.214306433183765e-15im -0.34675715940522456 + 0.06925088677373341im -0.2912559048789237 - 0.013950722462056586im 0.6412031063720829 - 0.12084295399079803im -0.24158354138550425 + 0.2454985224085332im -0.4737656253500416 - 0.2271458269000058im]))) diff --git a/test/data/rpa_golden.jl b/test/data/rpa_golden.jl index 416f989..882dc6e 100644 --- a/test/data/rpa_golden.jl +++ b/test/data/rpa_golden.jl @@ -1,8 +1,8 @@ # AUTO-GENERATED by test/data/generate_rpa_golden.jl -- do not edit by hand. # # Pinned outputs of src/physics/rpa/, checked by test/golden_rpa.jl. -# Generated from src/ tree bc6323bd33d4 -# with Julia 1.12.3, ITensors 0.6.23, ITensorMPS 0.2.6. +# Generated from src/ tree 68e3dc7206aa +# with Julia 1.12.3, ITensors 0.9.32, ITensorMPS 0.3.44. # Regenerate only for an intentional behaviour change; see the generator's header. # # Each entry is (name, fn, setup, seed, args, kwargs, throws, expected): `fn` @@ -325,7 +325,7 @@ push!(RPA_GOLDEN_CASES, (name = "bubble_cplx_krylov", fn = :get_bubble_mpo, setu args = (0.3,), kwargs = (P_method = :kpm, Ncheb = 8, maxdim = 20, η = 0.1, GF_method = :krylov, krylov_nsweeps = 2, krylov_maxdim = 20), throws = nothing, - expected = (length = 3, site_dims = [2, 2, 2], eltype = ComplexF64, dense = ComplexF64[0.2668795596623346 + 0.0068400866970211825im -0.012395587909206576 + 0.0321223195307714im -0.08174309766381428 + 0.027831502178395848im -0.03632588452810376 - 0.001612857008306103im -0.12653054324767837 + 0.0239647910925009im -0.03449421436157261 - 0.01849873844055209im -0.014172939116223547 - 0.00015473506655192313im -0.033768948396992675 + 0.014601011038661698im; 0.0316795553820771 - 0.021655959897272453im 0.13653982949576493 - 0.05752123698430536im 0.03129990987523719 - 0.002232368584924723im 0.04839241505949276 - 0.0045828081236232955im -0.035681923021904774 + 0.056012581596164866im -0.013473533587005032 + 0.025100173145117165im -0.016159437694073034 - 0.018515761703768823im -0.09934768501825582 + 0.03787856503511315im; 0.0487306263614902 + 0.002469887227667147im -0.006479139063148758 + 0.009505784496494077im 0.047359960297923644 + 0.07725357826611788im 0.10508929404927464 + 0.013806219203993449im -0.05284054995517033 + 0.024278311629340302im -0.04978119687680739 - 0.016452046609352802im -0.027507593012788257 - 0.07385948265642625im -0.044626310269865244 - 0.04285095392916694im; -0.07381593849766979 - 0.03615500066201862im -0.01922831692168251 + 0.036373197702872596im 0.018246825527001596 + 0.08608625514510125im 0.1970439466548064 + 0.025806352257964335im -0.005732498494239256 - 0.01671063466591098im -0.06496126642939973 + 0.05047366359553257im -0.04420209695456793 - 0.04268862143038114im -0.08836845147534163 - 0.015314428967108624im; -0.1447188444291269 + 0.04592636915905715im 0.020318669339212614 - 0.023160476611197973im 0.049664381558607286 - 0.0068211298945177025im 0.03222811896532448 - 0.02393275101301777im 0.2768484816006025 - 0.016612180613106993im 0.023336997824198343 + 0.016028364759594245im -0.0438766317103838 + 0.01646663246506293im -0.006915809629536197 - 0.022999266726057434im; -0.011068146475789945 + 0.03882553417118895im -0.006871221056823962 + 0.031199491805554327im -0.014993792844817616 - 0.02039807589079673im -0.053961559501019454 - 0.044494232713575645im 0.0027850455575620403 - 0.005616424526929169im 0.1170975175985626 + 0.015723459529118255im 0.007710367521217978 + 0.001112374382605944im 0.01869197951828755 + 0.005757989716873477im; -0.04779954735384249 + 0.06259114215643122im -0.018808901071778107 + 0.005657521503056588im 0.0011065615214569911 - 0.050786054570018106im -0.061395887166551845 + 0.017951610195996587im 0.02382790913685146 - 0.007183070083725647im -0.01850934729825558 - 0.007144440544150089im 0.07271339769913544 + 0.06678342935739515im 0.0648155162754803 - 0.016992511569533505im; 0.03262390529576513 - 0.02785333785902471im -0.04734714576159452 - 0.01138670073088716im -0.013773403087923679 - 0.08085918662663799im -0.06003701345715526 + 0.023328875270423356im -0.0706258679165783 - 0.05055253135457262im -0.006429663946730277 + 0.04128262820078634im 0.0076553184371766165 + 0.081283041306588im 0.18548626066614218 + 0.03406533976807044im], on_ref_sites = true))) + expected = (length = 3, site_dims = [2, 2, 2], eltype = ComplexF64, dense = ComplexF64[0.18943372671593878 + 0.011814505584547656im -0.008923219403889768 + 0.0073820250672568455im -0.07347619841601538 - 0.05424996269175033im -0.06896850086446363 - 0.04430513604651554im -0.04013868793230584 + 0.08217433939092658im -0.018019562645653187 - 0.0028042835926455905im 0.014396032563620133 + 0.07676399777195353im -0.03187242145916015 + 0.04581217741426876im; -0.022063093740314593 - 0.04940667830764208im 0.13519537331068532 + 0.021518892811991073im -0.04430503405556292 - 0.07362881993102448im 0.05665397602999218 + 0.004869211168076902im -0.01212559283539372 + 0.038464062995380785im -0.05079421203115081 - 0.0019072010283453494im -0.04044583742997639 + 0.03165741685640911im -0.09662415258148636 + 0.03750882382213112im; -0.03689317647829173 + 0.07816071059600678im -0.062336915935194116 - 0.06553725420157538im 0.13951534384219927 - 0.004205393358517778im -0.035657461824749 - 0.06074465643558627im 0.017170675356062245 + 0.04457120669753566im 0.017088083528348348 + 0.021174666226179563im -0.04741878581911848 - 0.04209606731821939im 0.03397546391675662 + 0.04194883786712936im; 0.010399378611080966 + 0.04634068595144219im -0.01606017341406458 + 0.03996301662943338im -0.012310413075981314 + 0.08156443446871275im 0.27102857960492216 + 0.07611869605803814im -0.06475319168507582 - 0.08409646870596578im -0.08898641468503628 + 0.00498814742125634im -0.04292344924094994 - 0.05022140373637622im -0.10991131251982564 + 0.01772940597796525im; -0.01444390443038665 + 0.09833973423112434im -0.009258676164694852 - 0.03376787333536283im 0.10494662235914412 + 0.03355194907497539im 0.00174115547444395 + 0.036921462187039726im 0.15605239150371178 - 0.011066347845363004im 0.009836422178332702 + 0.04461945828879571im -0.08903401207170601 - 0.09593320477419436im -0.001806850398785491 - 0.04853406412210606im; -0.023208770121432555 + 0.034640583395766im -0.04039527092258729 + 0.001757813535336663im 0.04104771414735734 - 0.0010930532597679433im -0.09514282816778857 + 0.04241741835618103im -0.00606483031452655 - 0.06199987807369659im 0.1350528547534871 + 0.06957054575968029im -0.04773420784355929 - 0.02756832169427579im 0.12936242744866983 - 0.0074940450455375845im; 0.03475224656929469 + 0.08058915693100059im -0.06712429778529526 + 0.038108114851832874im -0.00255363622819058 + 0.02752864790714945im -0.008621374608169271 + 0.03435843400275722im -0.04411020895535969 - 0.036104820809788156im -0.019589502579978598 - 0.051970720857620264im 0.12116894420197691 + 0.024490030644276812im -0.05040377231047943 - 0.03854188365573731im; 0.0400685122943141 - 0.028159785578451734im 0.0004210415157197138 + 0.01341408293462973im -0.021947479109866042 - 0.07552680660770948im -0.14038233492985494 + 0.0393842521764542im -0.012098386654123517 + 0.0592826004492471im -0.0006927602577392858 + 0.031341976621591335im -0.008543131136465739 + 0.1365394522734282im 0.28066939004818947 + 0.06072184209324634im], on_ref_sites = true))) push!(RPA_GOLDEN_CASES, (name = "bubble_pair_kpm_verbose", fn = :get_bubble_mpo, setup = :chain3_pair, seed = 296696, args = (0.4,), @@ -757,7 +757,7 @@ push!(RPA_GOLDEN_CASES, (name = "rpa_charge_cplx_fwd_krylov_mcweeny_tol", fn = : args = (0.3,), kwargs = (GF_method = :krylov, krylov_nsweeps = 2, krylov_maxdim = 3, krylov_cutoff = 0.01, purify_tol = 0.01, maxdim = 20, η = 0.1, rpa_nsweeps = 2, rpa_maxdim = 20), throws = nothing, - expected = (length = 6, site_dims = [2, 2, 2, 2, 2, 2], eltype = ComplexF64, dense = ComplexF64[0.017984351255096422 + 0.00775671213732014im, -0.037087229420477985 + 0.06039787076966256im, -0.05229677173822273 + 0.0032030618547675884im, -0.02075469899925438 + 0.12342048692011517im, -0.02024169296570986 + 0.016013158713118963im, -0.03711735395256463 - 0.01685421979133269im, -0.002016646076893981 + 0.00022753696819567334im, -0.06531234802158717 + 0.06814348284163238im, 0.03236351952887983 + 0.06850483697915027im, -0.14645550614789238 - 0.07614514549925475im, -0.055416758719357154 + 0.0684685174062474im, 0.15976897331876425 - 0.018848022568311168im, 0.1490847286689692 - 0.12711787825790946im, -0.0001675931504048489 - 0.021884721354958635im, 0.016884906189970038 - 0.04232855671608682im, 0.23530074808776244 - 0.17619056620333878im, 0.018877597320714014 - 0.09996404900536043im, 0.06635520516705898 + 0.017467314540918336im, 0.10718057346329013 - 0.05137238959175698im, 0.04159161257752235 - 0.12017225275288082im, 0.07097826287280111 - 0.0661530082709331im, -0.007476116454865586 + 0.08723448725232244im, -0.02757960703524422 - 0.06453167474493245im, -0.09047226412125689 - 0.11025996684044187im, -0.13585704386484368 + 0.037215294177376124im, 0.1255504828830622 + 0.0025949032924316613im, -0.023890456191872755 - 0.027006445505166062im, -0.23880997865271897 + 0.050305252948012774im, -0.04448258515918937 + 0.06926564325007209im, 0.06701552246076507 - 0.020940054767949422im, -0.048514763179545665 - 0.009728454667506574im, -0.06308564961563547 + 0.07572143572186286im, -0.017973902666112804 + 0.03718147066475952im, -0.04985886369142889 + 0.04148644137758443im, -0.03965752241946641 + 0.022204861451548333im, 0.02088261587657541 + 0.0947387482142384im, -0.04300283862982604 + 0.06355637248961823im, -0.023871611479539624 - 0.014430725755589279im, 0.03872842480207683 - 0.02884573005068146im, 0.00629995745866499 - 0.015333160001820334im, 0.09560550545037728 - 0.11160652557132764im, -0.1643929678672827 + 0.02466264020002849im, 0.10206553165830534 - 0.01985287765766223im, 0.044368040468722586 - 0.13214731549351105im, -0.09198242811487488 + 0.025042873977272243im, -0.07305103030679785 + 0.0485874461553584im, 0.009892449979619744 - 0.052081158367437846im, -0.2235247501560465 + 0.0004256550383066743im, 0.03605661141027118 - 0.004032126752266104im, -0.030446378360461143 - 0.08198084703607028im, 0.0040266151542373944 - 0.01001760219440969im, 0.09886097305099045 - 0.09062897566503811im, 0.05265729589422517 + 0.06258621554744397im, -0.027525103803279227 - 0.04075300385147617im, -0.017233425318275444 + 0.02512157184957401im, 0.027271302489051727 + 0.08052524746340851im, 0.0006389520748295028 - 0.033890199949005814im, 0.049386533682555925 + 0.03820543547834152im, 0.05210891024902833 - 0.027697683621590103im, -0.13682413686406225 + 0.028141683627016842im, -0.016677723473785443 + 0.12890653439218394im, 0.019405025919407702 + 0.07949958996443403im, 0.008439028834586915 + 0.025212385722092452im, -0.07635341004526391 + 0.11145224092540618im], bubble_progress_printed = false))) + expected = (length = 6, site_dims = [2, 2, 2, 2, 2, 2], eltype = ComplexF64, dense = ComplexF64[-0.14669796844130495 + 0.14720826930507194im, 0.13777317122818558 - 0.10694098333358551im, -0.1518391678110335 - 0.039716106822483666im, 0.031124319868145775 + 0.2145746174905691im, -0.10706280720902964 + 0.0006234053843053242im, 0.04332025605510384 - 0.06418621527038534im, -0.013180121242516496 + 0.006837286426918065im, 0.01143998550195749 + 0.10357654321033438im, 0.16860741640194185 + 0.01577929615339176im, -0.13840205691272228 - 0.04314040304136978im, -0.06941142025815436 + 0.04565565408517608im, 0.1101071893710173 + 0.08188423567100396im, 0.1616141469604952 - 0.06106662285933926im, -0.10009753208768862 - 0.04041018263237489im, 0.02740397984223048 + 0.11791939991203462im, 0.07982225873131017 - 0.04315402181824768im, -0.14499107228723765 + 0.2152518256623365im, 0.09956006262222154 - 0.1406333537509555im, -0.1910198393017849 - 0.0693936986839381im, -0.02396482840299677 + 0.2769788671175631im, -0.09948790732823595 + 0.1462514210529106im, 0.03284405084945486 - 0.04014624367883666im, -0.06020626145465535 - 0.14009324521212096im, -0.01102907113768022 + 0.21386550367321705im, 0.0613719013264116 - 0.06598460864227479im, -0.10587780014566414 - 0.09848529877245304im, -0.041568710534526106 + 0.06909490058695941im, 0.08238228820046781 - 0.009967619187638332im, 0.15580057330917052 + 0.2815616364037349im, -0.07337835180085896 - 0.05018088546185511im, -0.08815991850827966 + 0.013160362225550497im, 0.1337952985953578 + 0.1383038189575871im, 0.1847374101908721 + 0.19164927464199935im, 0.0022080519994836026 - 0.3207162282458331im, -0.0665276433457238 + 0.10901218320151161im, 0.20274597648142165 - 0.030898376065330715im, -0.12181436355109425 + 0.16040702044240798im, -0.0016734268914246134 - 0.1571187726430021im, -0.07141002185664908 + 0.07018451187679751im, 0.054850321338850955 + 0.13981373138146524im, -0.019806402863704053 + 0.04792298658735816im, -0.2887308894364298 + 0.1769214971678472im, 0.08158986304807177 - 0.09000076185774489im, -0.21197031864816207 + 0.16541311931595326im, 0.061234541116590935 - 0.35851415220738625im, -0.13582821300034126 + 0.09671066718103505im, 0.1949488163808624 + 0.06995183692221654im, -0.09821908431787381 - 0.22763147368217546im, 0.05041306230614696 + 0.2507702353161074im, 0.0008485182883602588 - 0.22214244177732675im, -0.10154766101608256 + 0.11020559794615im, 0.108547207511655 + 0.11158579155016822im, 0.0025651947049758414 + 0.16331981717927976im, 0.004063774993466888 - 0.052378428921255035im, -0.0852522480633679 + 0.045102627784493476im, 0.13907798147793465 + 0.056331926854240946im, 0.03383158834004098 - 0.07481056010512632im, -0.18040117098039515 + 0.1859161000245081im, 0.13189349831980468 + 0.034493840197607956im, -0.06890572129953593 - 0.11793812014309273im, 0.12881998391739155 - 0.04815279285309608im, -0.06326624295989887 + 0.06352437556391835im, 0.09227479000159731 + 0.08976568479038352im, -0.03516079087342082 - 0.03251395261093185im], bubble_progress_printed = false))) push!(RPA_GOLDEN_CASES, (name = "rpa_charge_cplx_fwd_sp2_maxdim_maxiters", fn = :get_rpa_susceptibility, setup = :chain3_cplx, seed = 737546, args = (0.3,), @@ -775,7 +775,7 @@ push!(RPA_GOLDEN_CASES, (name = "magnon_chi_spin3_z_fwd_krylov_mcweeny_tol", fn args = (0.3,), kwargs = (GF_method = :krylov, krylov_nsweeps = 2, krylov_maxdim = 3, krylov_cutoff = 0.01, purify_tol = 0.01, maxdim = 20, η = 0.1, rpa_nsweeps = 2, rpa_maxdim = 20), throws = nothing, - expected = (length = 6, site_dims = [2, 2, 2, 2, 2, 2], eltype = ComplexF64, dense = ComplexF64[-0.0466457801293958 + 0.04878489492820552im, -0.05008479941604464 - 0.01147357393276805im, 0.026578367943920995 + 0.07235597124623719im, 0.10497437971163426 + 0.017729663765287115im, 0.03015672573691144 + 0.0627692168344086im, -0.11002708456945803 + 0.05004977413574292im, -0.0619651111995977 + 0.0324658267045983im, 0.011217260862709187 + 0.03500787573634705im, -0.005809490490840952 + 0.06338083844532469im, -0.12397454848809297 - 0.022694746181656664im, -0.10009465746862783 - 0.033712962091483806im, -0.009154843933581559 + 0.03481049532141331im, -0.03135039525803293 + 0.07710325445422174im, -0.19355690114953042 - 0.018298918217292834im, -0.15118697676300216 + 0.05987444068794821im, 0.06257045794182717 + 0.07372659955785603im, -0.008117912966021285 - 0.06800427955296796im, 0.0947680566792364 + 0.0007126712433295629im, 0.05028115449563056 + 0.025655711024184676im, 0.018546155122800508 - 0.015522232799721328im, -0.060156208526345734 - 0.07193694853236066im, 0.09669250278641292 - 0.02387769898703581im, -0.030034837203425 - 0.009599998460326067im, 0.036394261268316117 - 0.029525229330727085im, -0.028963577030451437 - 0.10688665760509006im, 0.2092329531834431 - 0.0022245243623011494im, 0.18635689769051317 + 0.006661643779043786im, 0.028839185907270904 - 0.0877490141494113im, -0.04385683866417736 - 0.046631993398387514im, 0.07817902854722392 - 0.08339542601814769im, 0.048009633407968245 - 0.03519280331840206im, -0.03390760964809603 - 0.029169229964642142im, -0.03483821128793135 - 0.02887951579419323im, 0.04773618754357275 - 0.036468577830327635im, 0.0775617755418884 - 0.08372756833823226im, -0.044345760969739856 - 0.04626774538872886im, 0.027226273914517363 - 0.08812554304460474im, 0.18782661868798012 + 0.003121387672867553im, 0.2095916990500265 - 0.0012633229107274022im, -0.028248579960867236 - 0.1070166350613341im, 0.03735184428708933 - 0.02915558855942706im, -0.029492402211317152 - 0.006926161490735709im, 0.09678671674101497 - 0.022537926481791554im, -0.057785388568279666 - 0.0728419585514111im, 0.018154543382783085 - 0.015124774526551563im, 0.04908319900635261 + 0.02561024571626957im, 0.09352730406341132 - 0.0008805025367899753im, -0.008136862518173294 - 0.06743288380047063im, 0.06352428495990844 + 0.0731789313787687im, -0.15113166495331296 + 0.061499972269423116im, -0.19233370409324393 - 0.01934711176943979im, -0.0317236197377827 + 0.07632020864629374im, -0.008179273573632358 + 0.03531084273266073im, -0.1015162023041073 - 0.031328849834185414im, -0.1227464032203428 - 0.0208921769897588im, -0.00563313912572296 + 0.06290410015376802im, 0.010757134809020448 + 0.03514269491248692im, -0.06156938212375522 + 0.03200855645156614im, -0.10953363670061528 + 0.04847213700375769im, 0.028448186690869497 + 0.06286591423543847im, 0.10462867652941008 + 0.017622236619961644im, 0.026913118237044155 + 0.0704483169329673im, -0.049921617104326875 - 0.010151948869432506im, -0.04688910709386431 + 0.048899415058084816im], bubble_progress_printed = false))) + expected = (length = 6, site_dims = [2, 2, 2, 2, 2, 2], eltype = ComplexF64, dense = ComplexF64[-0.012745442892925072 - 0.008563652511976178im, 0.08389173585235431 - 0.03976066155981328im, -0.00882337286671398 + 0.002000589913576928im, 0.11720401306975665 - 0.17007740585436998im, 0.056878266599704554 + 0.007749714363259785im, -0.034374970385462514 - 0.01740144472450865im, 0.010003912422382393 - 0.1269725344792256im, 0.027938007574288378 + 0.04110277996083071im, -0.12266312002667885 + 0.10886635881979534im, 0.01834555718618243 - 0.02787127595908433im, 0.19325379291680167 - 0.10798080523269067im, -0.1617368780682873 + 0.2002776531402526im, -0.005663703344932519 + 0.11016933343445942im, -0.07715708231625754 - 0.050652611523498564im, -0.031816817297345285 - 0.040922130176700586im, -0.030618011345145094 + 0.1389629349038061im, -0.0031105083937864944 - 0.011585991587602523im, -0.03943027729337156 + 0.036899329855401126im, 0.0018660021382533145 - 0.030624802225641904im, -0.02497393090136413 + 0.003293922695291114im, -0.023021881677250677 + 0.015356592055538243im, 0.002874314549523402 + 0.0399658814117101im, -0.049921860727222475 + 0.12287265123543364im, -0.004394903838282518 + 0.13626782317934805im, 0.0008858356970981966 - 0.1518351827662897im, 0.11389867368616716 + 0.07813389721596872im, -0.001601115999894806 + 0.1012210110660523im, 0.0685879437635476 - 0.23519425598129198im, 0.04763781187016325 - 0.011478147696728742im, -0.052341365605771586 - 0.02125089868258983im, -0.02583407191635967 - 0.04391799245196613im, -0.04037645956949439 - 0.06809919153551103im, -0.06086050219802648 + 0.010968002537402731im, 0.015130272034969243 - 0.06411016389285129im, -0.013428216468761273 + 0.021642027012894804im, -0.08287429762220208 + 0.06693156473142228im, -0.05475346135361655 - 0.08056528124718888im, 0.07371022534170692 + 0.037699952504599496im, 0.026042040949564454 + 0.057034836053195974im, 0.0003486946006822393 - 0.11563576673995016im, 0.13827038910947834 - 0.05645509434587284im, -0.12065748996470632 + 0.01280493177868074im, -0.08617025246257515 + 0.0015600207136371473im, 0.0925646853663351 - 0.09792721441727896im, 0.09098314191256421 - 0.09985820295393324im, 0.04027317798861968 + 0.06222108480054771im, -0.1284355813239005 + 0.006658900882728875im, 0.08733492881482528 - 0.09875664067156242im, 0.07279214083139876 + 0.05000126566305597im, -0.10502433439777403 + 0.009032411749573182im, 0.0357613017259837 - 0.050152163199772386im, 0.013520826208235647 + 0.08162302105319395im, -0.03757247114171574 - 0.005628498930616622im, 0.05138721220771168 - 0.012640056305407996im, -0.009200323615228156 + 0.049989825348845884im, -0.014300719837455223 - 0.07229028611249153im, -0.06770279233321161 + 0.03312822868085031im, 0.023680276991674958 - 0.02199405489053951im, 0.007272734066351136 + 0.05606064304955437im, -0.02215710427256587 + 0.03918376749417867im, -0.07263260079773808 - 0.01881467666737066im, 0.10151414113609941 - 0.045404771415643336im, 0.1067425846359856 + 0.021691063122959382im, -0.10974452814508029 + 0.1338629376553656im], bubble_progress_printed = false))) push!(RPA_GOLDEN_CASES, (name = "magnon_chi_spin3_z_fwd_sp2_maxdim_maxiters", fn = :get_magnon_susceptibility, setup = :spin3_z, seed = 948937, args = (0.3,), @@ -793,7 +793,7 @@ push!(RPA_GOLDEN_CASES, (name = "wynn_charge_cplx_fwd_krylov_mcweeny_tol", fn = args = ([0.3],), kwargs = (GF_method = :krylov, krylov_nsweeps = 2, krylov_maxdim = 3, krylov_cutoff = 0.01, purify_tol = 0.01, maxdim = 20, η = 0.1, K_max = 2, maxdim_apply = 20), throws = nothing, - expected = (chi_partial = [-0.0006022839014177481; 0.002709531834590288; 0.002492033354730634;;; -0.0815686530238447; -0.06430435582221443; -0.0644892492904556;;; -0.10016906783790003; -0.09538055633026452; -0.092408087177232;;; 0.04203997968941413; 0.045559714658740036; 0.04612872012437608;;; -0.06668700295976267; -0.0626398021559846; -0.05956459521168826;;; 0.07033314026153167; 0.0910157417671778; 0.0935679289349139;;; 0.14514390861897536; 0.15435700947414438; 0.15617918227310393;;; -0.06823622956817532; -0.056839544289600086; -0.056128874416777254], chi_wynn = [0.0025054369753344827;;; -0.06448729013972793;;; -0.08754279709006259;;; 0.04623844459065427;;; -0.049835215074152485;;; 0.09392719592229527;;; 0.156628424057629;;; -0.056081611555087375], bubble_progress_printed = false))) + expected = (chi_partial = [-0.24174571579335014; -0.21541685860629353; -0.20893036886028266;;; -9.792579617560332e-5; -0.04425275267799907; -0.037345901555909326;;; 0.12945633447370328; 0.09735663546069044; 0.09917180283784914;;; -0.0819668286117943; -0.056625230793859276; -0.053033824842047134;;; 0.0015714399588179495; -0.010531483768567686; -0.015447197572295962;;; -0.041252773056760846; -0.029889518294138034; -0.015014742920385123;;; -0.13083451871565785; -0.13086104629946008; -0.1300255543766374;;; -0.10893841144089474; -0.1758399636137935; -0.19225300407482007], chi_wynn = [-0.20680992888001754;;; -0.03828015585948032;;; 0.09907465273944699;;; -0.05244080953486718;;; -0.018809314785628757;;; -0.07802418154315861;;; -0.1308353350692287;;; -0.1975886316867536], bubble_progress_printed = false))) push!(RPA_GOLDEN_CASES, (name = "wynn_charge_cplx_fwd_sp2_maxdim_maxiters", fn = :get_rpa_susceptibility_wynn, setup = :chain3_cplx, seed = 621589, args = ([0.3],), @@ -811,7 +811,7 @@ push!(RPA_GOLDEN_CASES, (name = "magnon_wynn_spin3_z_fwd_krylov_mcweeny_tol", fn args = ([0.3],), kwargs = (GF_method = :krylov, krylov_nsweeps = 2, krylov_maxdim = 3, krylov_cutoff = 0.01, purify_tol = 0.01, maxdim = 20, η = 0.1, K_max = 2, maxdim_apply = 20), throws = nothing, - expected = (chi_partial = [-1.3296129692474089e-6; 0.00770861611773048; 0.008560149926653236;;; 0.00261458689060572; 0.005924510239497909; 0.005421287297205995;;; -0.032089743783280174; -0.02260999207211977; -0.025860092934973756;;; -0.03094396434977334; -0.05354136563725787; -0.059508170976933256;;; -0.41151889592064583; -0.2464915230820358; -0.28215392085650676;;; -0.032716717091244935; -0.05577037489960874; -0.06180183707142134;;; -0.02989559034970687; -0.02005217923245212; -0.023365846772398797;;; 0.0024806241674444037; 0.005830747823403138; 0.005291042616494243], chi_wynn = [0.008665875543021673;;; 0.005487697896304803;;; -0.025030298908899414;;; -0.061648970144522246;;; -0.27581674377399085;;; -0.06393896066621142;;; -0.022531285176212987;;; 0.005365925527962445], bubble_progress_printed = false))) + expected = (chi_partial = [0.014466125728262585; 0.0137790243254755; 0.013660780604922889;;; -0.023535670953587592; -0.017126072988223186; -0.01859700630957422;;; 0.006703579937857583; 0.01719188733230207; 0.017818511715917213;;; -0.027576881493608704; -0.009256803260674257; -0.011430990525216188;;; -0.0871810805443088; -0.08074300232162868; -0.08040986051168551;;; -0.24405965450018427; -0.2728966846492525; -0.2689784244537422;;; -0.013429540980113614; -0.012203019959611698; -0.012612370646078069;;; 0.03976216176242385; 0.046762123053166724; 0.0414222531406386], chi_wynn = [0.013636202265055518;;; -0.018322450602967465;;; 0.017858328258155476;;; -0.011200336235025676;;; -0.08039168121250384;;; -0.26944713539472953;;; -0.012509937194972641;;; 0.04373299874266634], bubble_progress_printed = false))) push!(RPA_GOLDEN_CASES, (name = "magnon_wynn_spin3_z_fwd_sp2_maxdim_maxiters", fn = :get_magnon_susceptibility_wynn, setup = :spin3_z, seed = 537650, args = ([0.3],), diff --git a/test/runtests.jl b/test/runtests.jl index 2b7fd6c..6a2e3a0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -147,6 +147,7 @@ include("bugfix_build_K.jl") include("bugfix_qtci_profiles.jl") include("bugfix_large_L_cutoffs.jl") include("bugfix_onsite_aux.jl") +include("bugfix_green_krylov_start.jl") # The 0.1 names and keywords renamed in 0.2: deprecated aliases with a one-time warning. include("deprecations.jl") From d594505cf98646cb18a623a434ccee25ad027c92 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Mon, 16 Nov 2026 20:46:01 +0200 Subject: [PATCH 03/13] RPA Dyson solve: residual check rpa_from_bubble_diag computes the relative residual ||(I - Pi0 V)chi - Pi0|| / ||Pi0|| of its solve from exact inner products and warns above `residual_warn` (1e-2); `return_info=true` returns (; chi, residual, maxlinkdim). get_rpa_susceptibility and get_magnon_susceptibility pass `rpa_residual_warn` and `return_info` through. Near the instability the default cutoff limits chi to about kappa * 1e-5: at U = 0.999 U_c on a 32-site chain (kappa = 1e3) chi is off by 5e-3 with a residual of 5e-2, which now warns; rpa_cutoff = 1e-12 gives 1e-7. The default cutoff and every returned value are unchanged. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 9 +++++ src/physics/rpa/dyson.jl | 58 ++++++++++++++++++++++++------- test/bugfix_rpa_dyson_residual.jl | 48 +++++++++++++++++++++++++ test/runtests.jl | 1 + 4 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 test/bugfix_rpa_dyson_residual.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index ec8d5ab..25d72c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,15 @@ bugs without breaking the API. `(; G, residual, maxlinkdim)` instead of the MPO. On the chains of the next entry and on 16×16 square lattices the residual was 1–5 times the relative error of G, down to its noise floor of ~1e-7. +- **The RPA Dyson solve checks its residual**: `rpa_from_bubble_diag` computes the + relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ and warns above `residual_warn` (default + `1e-2`); `return_info=true` returns `(; chi, residual, maxlinkdim)`. + `get_rpa_susceptibility` and `get_magnon_susceptibility` pass `rpa_residual_warn` and + `return_info` to it. Near the RPA instability the truncation limits χ to about + κ·1e-5 at the default `cutoff = 1e-8` (κ the condition number of I − Π₀V): on a + 32-site half-filled chain at U = 0.999 U_c (κ = 10³) χ is off by 5e-3 with a residual + of 5e-2, so the check warns; `rpa_cutoff = 1e-12` gives 1e-7. The default cutoff is + unchanged. ### Changed results diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index 8198312..78365e0 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -28,7 +28,8 @@ """ rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; - nsweeps=20, maxdim=400, cutoff=1e-8) -> MPS + nsweeps=20, maxdim=400, cutoff=1e-8, return_info=false, + residual_warn=1e-2) -> MPS Solve the RPA Dyson equation (I − Π₀V) χ = Π₀ for the interacting susceptibility χ using DMRG-style linear solve. @@ -41,9 +42,18 @@ the row index i on the odd sites, the column index j on the even ones (site n of `finalfinalsites` on sites 2n−1 and 2n), so that `custom_mpo(χ, finalfinalsites)` is χ as an MPO. The solve is [(I − Π₀V) ⊗ I] vec(χ) = vec(Π₀), with I − Π₀V on the odd sites, started from vec(Π₀). + +Near the RPA instability I − Π₀V is nearly singular and the truncation at `cutoff` +limits the accuracy: on a 32-site half-filled chain with V = U·1 at U = 0.999 U_c +(condition number 10³) the default `cutoff = 1e-8` gives χ to 5e-3, `1e-12` to 1e-7. +The relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ of the solve (exact inner products) is +checked: above `residual_warn` (default `1e-2`; `Inf` skips the check) a warning +suggests a smaller `cutoff`. `return_info=true` returns `(; chi, residual, +maxlinkdim)` instead of vec(χ). """ function rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; - nsweeps=20, maxdim=400, cutoff=1e-8) + nsweeps=20, maxdim=400, cutoff=1e-8, + return_info::Bool=false, residual_warn::Real=1e-2) Id = MPO(finalfinalsites, "Id") ΠV = apply(Π, MPOV; maxdim=maxdim, cutoff=cutoff) A = Id - ΠV @@ -57,8 +67,17 @@ function rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; b = _vec_mps_from_mpo(Π, finalsites) x0 = deepcopy(b) - return ITensorMPS.linsolve(Aop, b, x0; - nsweeps=nsweeps, maxdim=maxdim, cutoff=cutoff) + χ = ITensorMPS.linsolve(Aop, b, x0; + nsweeps=nsweeps, maxdim=maxdim, cutoff=cutoff) + (return_info || isfinite(residual_warn)) || return χ + + res = _linsolve_residual(Aop, b, χ, 0.0, 1.0) + res > residual_warn && + @warn "rpa_from_bubble_diag: the Dyson solve is not converged: relative residual " * + "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Close to the RPA " * + "instability lower `cutoff` (`rpa_cutoff`), e.g. to 1e-12; else raise " * + "`nsweeps` or `maxdim` (maxlinkdim reached: $(maxlinkdim(χ)))." + return return_info ? (; chi = χ, residual = res, maxlinkdim = maxlinkdim(χ)) : χ end @@ -82,7 +101,8 @@ end """ get_rpa_susceptibility(H::TBHamiltonian, MPOV, ω; mode=:charge, rpa_nsweeps=20, - rpa_maxdim=400, rpa_cutoff=1e-8, ) -> MPS + rpa_maxdim=400, rpa_cutoff=1e-8, rpa_residual_warn=1e-2, + return_info=false, ) -> MPS Compute the RPA susceptibility χ(ω) for a system described by `H` with interaction MPO `MPOV`. Returns χ = (I − Π₀V)⁻¹Π₀, the full response matrix @@ -105,13 +125,20 @@ All `get_bubble_mpo` keyword arguments are accepted and forwarded. **Additional keywords (Dyson solve)** - `rpa_nsweeps` : sweeps for the RPA linsolve. Default `20`. - `rpa_maxdim` : max bond dim for the RPA linsolve. Default `400`. -- `rpa_cutoff` : cutoff for the RPA linsolve. Default `1e-8`. +- `rpa_cutoff` : cutoff for the RPA linsolve. Default `1e-8`. Close to the RPA + instability use a smaller one (see `rpa_from_bubble_diag`). +- `rpa_residual_warn` : warn when the relative residual of the Dyson solve exceeds + this value (`Inf`: no check). Default `1e-2`. +- `return_info` : return `(; chi, residual, maxlinkdim)` of the Dyson solve instead of + vec(χ). Default `false`. """ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; mode::Symbol = :charge, rpa_nsweeps::Int = 20, rpa_maxdim::Int = 400, rpa_cutoff::Real = 1e-8, + rpa_residual_warn::Real = 1e-2, + return_info::Bool = false, ϵF::Real = 0.0, P_method::Symbol = :purification, GF_method::Symbol = :kpm, @@ -150,7 +177,8 @@ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; # one fewer than the spinful H.sites. finalsites = _rpa_pair_sites(out_sites) return rpa_from_bubble_diag(Π, MPOV, finalsites, out_sites; - nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, cutoff=rpa_cutoff) + nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, cutoff=rpa_cutoff, + residual_warn=rpa_residual_warn, return_info) end # ============================================================ @@ -384,7 +412,8 @@ end """ get_magnon_susceptibility(H, MPOV, ω; rpa_nsweeps=20, rpa_maxdim=400, - rpa_cutoff=1e-8, kwargs...) -> MPS + rpa_cutoff=1e-8, rpa_residual_warn=1e-2, + return_info=false, kwargs...) -> MPS RPA transverse spin susceptibility χ^{+−}_RPA(ω) for a spinful `TBHamiltonian`. @@ -397,12 +426,16 @@ on the spin-projected sites (`H.sites` without the spin index), vectorized as in **Keyword arguments** - `rpa_nsweeps`, `rpa_maxdim`, `rpa_cutoff` : Dyson linsolve parameters. +- `rpa_residual_warn`, `return_info` : the residual check of the Dyson solve, as + `residual_warn` and `return_info` of `rpa_from_bubble_diag`. - All `get_bubble_mpo` keywords forwarded via `kwargs...`. """ function get_magnon_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; - rpa_nsweeps::Int = 20, - rpa_maxdim::Int = 400, - rpa_cutoff::Real = 1e-8, + rpa_nsweeps::Int = 20, + rpa_maxdim::Int = 400, + rpa_cutoff::Real = 1e-8, + rpa_residual_warn::Real = 1e-2, + return_info::Bool = false, kwargs...) H.spin_s === nothing && error("get_magnon_susceptibility: H is not spinful — call add_spin!(H) first") @@ -412,7 +445,8 @@ function get_magnon_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; # H.L counts position qubits only; H_up.sites also keeps any sublattice/layer index. finalsites = _rpa_pair_sites(H_up.sites) return rpa_from_bubble_diag(Π, MPOV, finalsites, H_up.sites; - nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, cutoff=rpa_cutoff) + nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, cutoff=rpa_cutoff, + residual_warn=rpa_residual_warn, return_info) end diff --git a/test/bugfix_rpa_dyson_residual.jl b/test/bugfix_rpa_dyson_residual.jl new file mode 100644 index 0000000..259ef26 --- /dev/null +++ b/test/bugfix_rpa_dyson_residual.jl @@ -0,0 +1,48 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Test +using TensorBinding: get_Hamiltonian, rpa_from_bubble_diag, custom_mpo, _rpa_pair_sites + +# The Dyson solve (I − Π₀V)χ = Π₀ near the RPA instability: at the default cutoff 1e-8 the +# truncation limits χ to about κ·1e-5 (κ the condition number of I − Π₀V), with nothing to +# show it. The solve now checks its relative residual and warns above `residual_warn`. +# Π₀ is the exact Lindhard bubble of a half-filled 32-site chain, V = U·1 with U = α U_c. +@testset "rpa_from_bubble_diag: residual check near the instability" begin + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + function mpo_of(A, s) # exact MPO of the dense matrix A on `s` + d = dim.(s) + return MPO(ITensor(reshape(A, d..., d...), prime.(s)..., s...), s; cutoff=1e-15) + end + H = get_Hamiltonian("chain_1d", 1.0; L=5) + s = H.sites + E, ψ = eigen(Hermitian(dmat(H.mpo, s))) + D = length(E) + f = Float64.(E .< 0) + W = reshape([ψ[i, a] * conj(ψ[i, b]) for i in 1:D, a in 1:D, b in 1:D], D, D * D) + bubble(ω, η) = W * Diagonal(vec([(f[b] - f[a]) / (ω + im * η - E[b] + E[a]) + for a in 1:D, b in 1:D])) * W' + η = 0.03 + μ = eigvals(bubble(0.0, η)) + Uc = 1 / real(μ[argmax(abs.(real.(μ)))]) + Πd = bubble(0.0, η) + Π = mpo_of(Πd, s) + fs = _rpa_pair_sites(s) + relerr(r, χx) = norm(dmat(custom_mpo(r.chi, s), s) - χx) / norm(χx) + + # far from the instability: converged, no warning + V = 0.3Uc * MPO(s, "Id") + χx = (I - 0.3Uc * Πd) \ Πd + r = @test_logs rpa_from_bubble_diag(Π, V, fs, s; return_info=true) + @test r.residual < 1e-3 && relerr(r, χx) < 1e-3 + + # κ = 10³: the default cutoff leaves 5e-3, and the residual check warns + V = 0.999Uc * MPO(s, "Id") + χx = (I - 0.999Uc * Πd) \ Πd + r = @test_logs (:warn, r"not converged") match_mode=:any rpa_from_bubble_diag( + Π, V, fs, s; return_info=true) + @test r.residual > 1e-2 && relerr(r, χx) > 1e-3 + # a tight cutoff converges, without a warning + r = @test_logs rpa_from_bubble_diag(Π, V, fs, s; cutoff=1e-12, nsweeps=40, + return_info=true) + @test r.residual < 1e-3 && relerr(r, χx) < 1e-5 + # the default return is vec(χ) as before; residual_warn = Inf skips the check + @test (@test_logs rpa_from_bubble_diag(Π, V, fs, s; residual_warn=Inf)) isa MPS +end diff --git a/test/runtests.jl b/test/runtests.jl index 6a2e3a0..7a5f8bb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -148,6 +148,7 @@ include("bugfix_qtci_profiles.jl") include("bugfix_large_L_cutoffs.jl") include("bugfix_onsite_aux.jl") include("bugfix_green_krylov_start.jl") +include("bugfix_rpa_dyson_residual.jl") # The 0.1 names and keywords renamed in 0.2: deprecated aliases with a one-time warning. include("deprecations.jl") From e07a8cb9d881f18bee011ccf1702df38134c3a03 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Mon, 16 Nov 2026 20:55:31 +0200 Subject: [PATCH 04/13] Add correction vectors: get_correction_vector, get_ldos_cv, get_exciton_ldos_cv, refine_bound_state The resolvent of one probe state, x = (omega + i eta - H)^-1 |b>, by ITensorMPS.linsolve with the safeguards worked out in the resolvent and inversion studies: a Krylov seed b + sum_k H^k b (the plain probe start can stall on two-particle registers), sweeps until G = stops moving, the residual from exact inner products, and up to `rounds` residual expansions when it is above `rtol`. Warm starts (`x0`), continuation in eta (`eta_path`) and the MINRES updater are available. get_ldos_cv / get_exciton_ldos_cv scan a frequency grid with warm starts and return the LDOS per unit physical energy with a residual per point; refine_bound_state gives the energy and probe weight of an isolated level by Rayleigh-quotient iteration (to ~eta^2 of its last step). Tested against dense resolvents (impurity chain, exciton contact probe), a forced failure that must be flagged, and the impurity bound level. The probe-start stall needs registers too large for CI (1D, L = 20 in the study), so it is not reproduced in the test. Docs: a "Correction Vectors" section on the Solvers page. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 18 ++ docs/src/api/solvers.md | 22 +++ docs/src/index.md | 8 +- src/TensorBinding.jl | 12 +- src/solvers/CorrectionVector.jl | 330 ++++++++++++++++++++++++++++++++ test/correction_vector.jl | 73 +++++++ test/runtests.jl | 1 + 7 files changed, 458 insertions(+), 6 deletions(-) create mode 100644 src/solvers/CorrectionVector.jl create mode 100644 test/correction_vector.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d72c0..bd03abb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,24 @@ bugs without breaking the API. 32-site half-filled chain at U = 0.999 U_c (κ = 10³) χ is off by 5e-3 with a residual of 5e-2, so the check warns; `rpa_cutoff = 1e-12` gives 1e-7. The default cutoff is unchanged. +- **Correction vectors** (`src/solvers/CorrectionVector.jl`, a new section of the + Solvers page): the resolvent of one probe state, x = (ω + iη − H)⁻¹|b⟩ and + G = ⟨b|x⟩, at a resolution set by η rather than by a number of KPM moments. + - `get_correction_vector(H, b, ω; eta, …)` returns `(; G, x, residual, converged, + maxlinkdim, sweeps, rounds)`. It starts from a Krylov seed b + Σₖ Hᵏb (from the + probe alone the two-site sweeps can stall on two-particle registers), sweeps until + G stops moving, checks the residual and, above `rtol` (0.02), expands x along the + residual for up to `rounds` rounds. `x0` warm-starts it, `eta_path` continues in η, + and `solver=:minres` uses the MINRES updater. + - `get_ldos_cv(H, X, ω_vals; eta, …)` and `get_exciton_ldos_cv(H, X, ω_vals; eta, …)`: + the LDOS of a site or of the exciton contact probe |X, X⟩ along a frequency grid, + each point warm-started from the previous one, as `(; ldos, G, residual, converged, + maxlinkdim)` in physical units (`ldos = −Im G/π`, one state integrates to 1; + `get_ldos_online(…) · π/H.scale` is in the same units). `get_ldos_cv` takes the + auxiliary projections of `get_ldos_online`. + - `refine_bound_state(H, b, ω0; eta_steps, …)`: the energy and probe weight of an + isolated level by Rayleigh-quotient iteration on the correction vector, to ~η² of + the last step (default `eta_steps = (0.05, 0.01, 1e-3, 1e-4)`). ### Changed results diff --git a/docs/src/api/solvers.md b/docs/src/api/solvers.md index 8b4ab2f..725a637 100644 --- a/docs/src/api/solvers.md +++ b/docs/src/api/solvers.md @@ -36,6 +36,28 @@ Modules = [TensorBinding] Pages = ["solvers/Krylov.jl"] ``` +## Correction Vectors + +The resolvent of one probe state, `(ω + iη − H)⁻¹|b⟩`, by a DMRG-like linear solve: +site and exciton LDOS at a resolution set by η instead of a number of moments, and the +energy and weight of an isolated level. When to use which route: + +- **One site, one probe, a few frequencies, small η:** correction vectors. They cost + far less than the full Green's function and resolve isolated peaks and gaps that KPM + needs thousands of moments for. Check `converged` (the residual). +- **Many frequencies, a continuum, or a spectrum over a whole window:** KPM, whose one + recursion serves every ω and η. +- **The whole G(ω) as an MPO:** `get_green_krylov`, cheap when G has a low rank (clean + and impurity lattices, ω outside the band). + +The correction-vector LDOS is per unit physical energy (one state integrates to 1); +`get_ldos_online(…) · π/H.scale` is in the same units. + +```@autodocs +Modules = [TensorBinding] +Pages = ["solvers/CorrectionVector.jl"] +``` + ## DMRG ```@autodocs diff --git a/docs/src/index.md b/docs/src/index.md index 0196257..3a65592 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -49,8 +49,9 @@ functions need CUDA.jl to be loaded (see the README). - Model construction and mutation: `add_hopping!`, `add_hopping_2D!`, `add_interaction!`, `add_onsite!`, `add_soc!`, `add_spin!`, `add_superconductivity!`, `add_tjunction!`, `add_zeeman!`, `get_Hamiltonian` -- Spectral functions: `get_dos_stochastic`, `get_dos_trace`, `get_green_krylov`, - `get_ldos_online`, `get_ldos_spatial`, `get_ldos_spectrum`, `KPM_Tn`, `KPM_Tn_mps` +- Spectral functions: `get_correction_vector`, `get_dos_stochastic`, `get_dos_trace`, + `get_green_krylov`, `get_ldos_cv`, `get_ldos_online`, `get_ldos_spatial`, + `get_ldos_spectrum`, `KPM_Tn`, `KPM_Tn_mps`, `refine_bound_state` - Band structure and quasiparticle interference: `get_bands`, `get_qpi` - Density-matrix purification: `mcweeny_purify`, `sp2_purify` - Mean-field self-consistency: `get_scf` @@ -59,7 +60,8 @@ functions need CUDA.jl to be loaded (see the README). - RPA response: `get_bubble_mpo`, `get_rpa_susceptibility`, `get_rpa_susceptibility_wynn` - Non-Hermitian models and spectra: `add_nh_nonreciprocal_hopping!`, `add_nh_onsite!`, `add_nh_skin_hopping!`, `nh_spectral_function`, `nh_spectrum_grid` -- Excitons: `get_exciton_bands`, `get_exciton_continuum`, `get_exciton_ldos_spatial` +- Excitons: `get_exciton_bands`, `get_exciton_continuum`, `get_exciton_ldos_cv`, + `get_exciton_ldos_spatial` - Time evolution: `build_tdvp_propagator_mpo`, `evolve_rk4_dm_nh`, `evolve_rk4_dm_timedep`, `evolve_with_propagator`, `evolve_with_tdvp`, `evolve_with_tdvp_timedep` - Sampling plans: `fibonacci_ldos_sampling_plan`, `kspace_sampling_plan`, diff --git a/src/TensorBinding.jl b/src/TensorBinding.jl index ddda543..94da50f 100644 --- a/src/TensorBinding.jl +++ b/src/TensorBinding.jl @@ -42,8 +42,9 @@ export expect, inner, MPO, MPS, OpSum, siteinds export add_hopping!, add_hopping_2D!, add_interaction!, add_onsite!, add_soc!, add_spin!, add_superconductivity!, add_tjunction!, add_zeeman!, get_Hamiltonian # Spectral functions: KPM moments, LDOS, DOS, Krylov Green's function -export get_dos_stochastic, get_dos_trace, get_green_krylov, get_ldos_online, - get_ldos_spatial, get_ldos_spectrum, KPM_Tn, KPM_Tn_mps +export get_correction_vector, get_dos_stochastic, get_dos_trace, get_green_krylov, + get_ldos_cv, get_ldos_online, get_ldos_spatial, get_ldos_spectrum, KPM_Tn, + KPM_Tn_mps, refine_bound_state # Band structure and quasiparticle interference export get_bands, get_qpi # Density-matrix purification @@ -59,7 +60,8 @@ export get_bubble_mpo, get_rpa_susceptibility, get_rpa_susceptibility_wynn export add_nh_nonreciprocal_hopping!, add_nh_onsite!, add_nh_skin_hopping!, nh_spectral_function, nh_spectrum_grid # Excitons -export get_exciton_bands, get_exciton_continuum, get_exciton_ldos_spatial +export get_exciton_bands, get_exciton_continuum, get_exciton_ldos_cv, + get_exciton_ldos_spatial # Time evolution export build_tdvp_propagator_mpo, evolve_rk4_dm_nh, evolve_rk4_dm_timedep, evolve_with_propagator, evolve_with_tdvp, evolve_with_tdvp_timedep @@ -156,6 +158,9 @@ export chern_marker_gpu, get_bands_gpu, get_dos_stochastic_gpu, # solvers/Krylov.jl get_green_krylov (GMRES or MINRES local solver), Haydock # recursion (haydock_cf & co.) # → Utils, MPOTools, TBSystem +# solvers/CorrectionVector.jl correction vectors (ω + iη − H)⁻¹|b⟩ of a probe: site and +# exciton LDOS, refinement of an isolated level +# → Krylov, AuxDOF, Utils, TBSystem # solvers/Timeev.jl TDVP/RK4 step and trajectory kernels, propagator MPO, # density-matrix RK4 and observables # → Utils, Hamiltonian, TBSystem @@ -252,6 +257,7 @@ include("solvers/kpm/ldos.jl") include("solvers/kpm/dos.jl") include("solvers/kpm/exciton.jl") include("solvers/Krylov.jl") +include("solvers/CorrectionVector.jl") include("solvers/Timeev.jl") include("physics/Purification.jl") include("physics/Supercond.jl") diff --git a/src/solvers/CorrectionVector.jl b/src/solvers/CorrectionVector.jl new file mode 100644 index 0000000..ce91eb7 --- /dev/null +++ b/src/solvers/CorrectionVector.jl @@ -0,0 +1,330 @@ +# solvers/CorrectionVector.jl — correction vectors of a probe state +# +# Contents: the correction vector x = (ω + iη − H)⁻¹|b⟩ of one probe |b⟩ and its +# projection G = ⟨b|x⟩ (get_correction_vector), by ITensorMPS.linsolve with three +# safeguards: a Krylov seed, sweeps until G stops moving, and a check of the residual +# with residual-expansion rounds; scans over ω with warm starts and the LDOS built on +# them (get_ldos_cv for a site, get_exciton_ldos_cv for the exciton contact |X, X⟩); and +# the refinement of an isolated peak into its energy and weight by Rayleigh-quotient +# iteration on the correction vector (refine_bound_state). +# +# Entry points: get_correction_vector, get_ldos_cv, get_exciton_ldos_cv, +# refine_bound_state +# Depends on: solvers/Krylov.jl (_minres_updater, _linsolve_residual, +# _hermiticity_defect), core/AuxDOF.jl (probe_state, _aux_projection, +# _probe_sectors), physics/TwoParticle.jl (mpsexciton), core/TBSystem.jl. + + +# ============================================================ +# 1. One frequency +# ============================================================ + +_cv_mpo(H::TBHamiltonian) = H.mpo +_cv_mpo(H::MPO) = H + +# b + Σₖ Hᵏb/‖Hᵏb‖ (k = 1…K), compressed to `maxdim`. From the probe alone, a product +# state, the two-site sweeps stall on interleaved two-particle registers: a hop flips +# carry bits two sites apart, outside the tangent space of the probe (residual ~1–30, +# χ ≈ 7, G wrong). The seed spans those directions. +function _cv_krylov_seed(Hm::MPO, b::MPS, K::Int; maxdim::Int, cutoff::Real) + acc = (1.0 + 0.0im) * b + v = b + for _ in 1:K + v = apply(Hm, v; maxdim, cutoff) + v = v / norm(v) + acc = +(acc, v; maxdim, cutoff) + end + return acc +end + +# `nsweeps` sweeps of linsolve on (z − H)x = b, from x. +function _cv_sweeps(Hm::MPO, b::MPS, x::MPS, z::Complex; nsweeps::Int, solver::Symbol, + maxdim::Int, cutoff::Real, tol::Real, krylovdim::Int, maxiter::Int) + if solver === :minres + return ITensorMPS.linsolve(Hm, b, x, z, -1.0; nsweeps, maxdim, cutoff, + outputlevel = 0, updater = _minres_updater, + updater_kwargs = (; tol, maxiter = maxiter * krylovdim)) + end + return ITensorMPS.linsolve(Hm, b, x, z, -1.0; nsweeps, maxdim, cutoff, outputlevel = 0, + updater_kwargs = (; ishermitian = false, tol, krylovdim, + maxiter, verbosity = 0)) +end + +# One frequency from the start x: sweeps one at a time until |ΔG| ≤ gtol·|G| (at least +# 2, at most nsweeps), then the residual; above rtol, up to `rounds` residual expansions +# x ← x + 0.3‖x‖/‖r‖·r, r = (z − H)x − b (as an MPS), with 3 sweeps each, keeping the +# best x. A two-site sweep can stop on a wrong vector that looks converged in G; the +# residual shows it, and the expansion moves x out of the tangent space it is stuck in. +function _cv_point(Hm::MPO, b::MPS, x::MPS, z::Complex; nsweeps::Int, gtol::Real, + rtol::Real, rounds::Int, kw...) + G = complex(NaN) + n = 0 + while n < nsweeps + x = _cv_sweeps(Hm, b, x, z; nsweeps = 1, kw...) + n += 1 + Gn = inner(b, x) + (n >= 2 && abs(Gn - G) <= gtol * abs(Gn)) && (G = Gn; break) + G = Gn + end + res = _linsolve_residual(Hm, b, x, z, -1.0) + k = 0 + while k < rounds && res > rtol + k += 1 + md = 2 * kw[:maxdim] + r = +(z * x, -1.0 * apply(Hm, x; cutoff = kw[:cutoff], maxdim = md), -1.0 * b; + cutoff = kw[:cutoff], maxdim = md) + x1 = +(x, (0.3 * norm(x) / norm(r)) * r; cutoff = kw[:cutoff], maxdim = md) + x1 = _cv_sweeps(Hm, b, x1, z; nsweeps = 3, kw...) + n += 3 + r1 = _linsolve_residual(Hm, b, x1, z, -1.0) + r1 < res && ((x, res) = (x1, r1)) + end + return (; G = inner(b, x), x, residual = res, converged = res <= rtol, + maxlinkdim = maxlinkdim(x), sweeps = n, rounds = k) +end + +function _cv_check_solver(Hm::MPO, solver::Symbol, fname::String) + solver in (:gmres, :minres) || + throw(ArgumentError("$fname: solver must be :gmres or :minres, got :$solver")) + if solver === :minres + defect = _hermiticity_defect(Hm) + defect <= 1e-6 || throw(ArgumentError( + "$fname(solver=:minres) needs a Hermitian H, but ‖H − H†‖/‖H‖ = " * + "$(round(defect; sigdigits=2)); use solver=:gmres")) + end + return nothing +end + +""" + get_correction_vector(H, b::MPS, ω; eta=1e-2, x0=nothing, eta_path=(), + solver=:gmres, maxdim=400, cutoff=1e-12, nsweeps=12, + gtol=1e-6, rtol=0.02, rounds=3, seed_krylov=40, + seed_maxdim=64, tol=1e-10, krylovdim=30, maxiter=30) + -> (; G, x, residual, converged, maxlinkdim, sweeps, rounds) + +The correction vector `x = (ω + iη − H)⁻¹|b⟩` of the probe `|b⟩` and +`G = ⟨b|x⟩ = ⟨b|(ω + iη − H)⁻¹|b⟩`, the resolvent projected on the probe, with η = +`eta`. `H` is a `TBHamiltonian` or an MPO on the sites of `b`; the energies are +physical (no KPM rescaling). The local density of the probe is `−imag(G)/π`, per +unit physical energy for a normalised `b`. + +For one probe a correction vector costs far less than the full Green's function +(`get_green_krylov`), and it resolves what KPM needs many moments for: an isolated +peak at small η, a gap in a band. Its bond dimension stays small where `x` is +dominated by a bound state, and grows where `ω` lies in a continuum: there a KPM run +that serves every ω is usually cheaper. + +**Algorithm.** `ITensorMPS.linsolve` sweeps on `(ω + iη − H)x = b`, one at a time, +until `|ΔG| ≤ gtol·|G|` (at least 2, at most `nsweeps`), from +- `x0` when given (a warm start, e.g. the `x` of a neighbouring ω); +- otherwise the Krylov seed `b + Σₖ Hᵏb/‖Hᵏb‖`, `k = 1…seed_krylov`, compressed to + `seed_maxdim`. From the probe alone, a product state, the two-site sweeps stall on + two-particle (interleaved) registers and return a wrong `x` that looks converged. + +Then the relative residual `‖(ω + iη − H)x − b‖/‖b‖` (exact inner products). Above +`rtol`, up to `rounds` residual expansions `x ← x + 0.3‖x‖/‖r‖·r` with 3 sweeps each, +keeping the best `x`. `converged = residual ≤ rtol`: when `false`, `G` is not to be +trusted (raise `maxdim`, or warm-start). + +`eta_path`: broadenings solved first, in order, each warm-starting the next and the +last one `eta` (continuation in η at fixed ω). At small η a cold start can fail where +a path from a larger η converges, provided its first step converges. + +**Keywords** +- `solver`: local solver of the sweeps, `:gmres` (KrylovKit) or `:minres` (Hermitian + H only, see `get_green_krylov`). +- `maxdim`, `cutoff`: truncation of `x`. The default cutoff `1e-12` is tighter than + elsewhere: the bound-state part of `x` grows as 1/η and the rest is small next to it. +- `tol`, `krylovdim`, `maxiter`: of the local solver (GMRES restarts `maxiter`; MINRES + takes `maxiter·krylovdim` products). + +Returns `x` and `G`, the residual, `converged`, the bond dimension of `x`, and the +numbers of sweeps and expansion rounds. + +```julia +b = TensorBinding.probe_state(H, 17) # site 17 +r = get_correction_vector(H, b, 0.3; eta=1e-3) +r.converged || @warn "not converged" r.residual +ρ = -imag(r.G) / π +``` +""" +function get_correction_vector(H::Union{TBHamiltonian,MPO}, b::MPS, ω::Real; + eta::Real = 1e-2, + x0::Union{MPS,Nothing} = nothing, + eta_path = (), + solver::Symbol = :gmres, + maxdim::Int = 400, + cutoff::Real = 1e-12, + nsweeps::Int = 12, + gtol::Real = 1e-6, + rtol::Real = 0.02, + rounds::Int = 3, + seed_krylov::Int = 40, + seed_maxdim::Int = 64, + tol::Real = 1e-10, + krylovdim::Int = 30, + maxiter::Int = 30) + Hm = _cv_mpo(H) + _cv_check_solver(Hm, solver, "get_correction_vector") + eta > 0 || throw(ArgumentError("get_correction_vector: eta must be positive, got $eta")) + x = x0 === nothing ? _cv_krylov_seed(Hm, b, seed_krylov; maxdim = seed_maxdim, cutoff) : x0 + kw = (; nsweeps, gtol, rtol, rounds, solver, maxdim, cutoff, tol, krylovdim, maxiter) + sweeps = 0 + for e in eta_path + r = _cv_point(Hm, b, x, ComplexF64(ω + im * e); kw...) + x = r.x + sweeps += r.sweeps + end + r = _cv_point(Hm, b, x, ComplexF64(ω + im * eta); kw...) + return merge(r, (; sweeps = r.sweeps + sweeps)) +end + + +# ============================================================ +# 2. Scans: site and exciton LDOS +# ============================================================ + +# Correction vectors of one probe along ω_vals, each warm-started from the previous x. +function _cv_scan(Hm::MPO, b::MPS, ω_vals, eta::Real; kwargs...) + n = length(ω_vals) + G = zeros(ComplexF64, n) + res = zeros(Float64, n) + χ = zeros(Int, n) + x = nothing + for (i, ω) in enumerate(ω_vals) + r = get_correction_vector(Hm, b, ω; eta, x0 = x, kwargs...) + x = r.x + G[i], res[i], χ[i] = r.G, r.residual, r.maxlinkdim + end + return G, res, χ +end + +function _cv_ldos_result(Gs, ress, χs, rtol, fname) + G = sum(Gs) + res = reduce((a, b) -> max.(a, b), ress) + χ = reduce((a, b) -> max.(a, b), χs) + bad = count(>(rtol), res) + bad > 0 && @warn "$fname: $bad of $(length(res)) frequencies not converged " * + "(residual > rtol = $rtol): raise maxdim or nsweeps, or use eta_path" + return (; ldos = -imag.(G) ./ π, G, residual = res, converged = res .<= rtol, + maxlinkdim = χ) +end + +""" + get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, , + ) + -> (; ldos, G, residual, converged, maxlinkdim) + +The local density of states at position `X` (1-indexed in `1:H.N`) from correction +vectors: `ldos[i] = −Im⟨X|(ω_i + iη − H)⁻¹|X⟩/π`, a Lorentzian of width `eta` around +each level, per unit **physical** energy (one state integrates to 1). The KPM +functions return the density per unit rescaled energy times `1/π` (Solvers page, +Conventions): `get_ldos_online(…) · π/H.scale` is in the units of `ldos` here. + +The frequencies are solved in the given order, each correction vector warm-starting +the next (only the first is seeded), so a fine grid is cheaper per point than a coarse +one. Each point keeps its residual; a warning counts the points above `rtol`. + +On an exciton Hamiltonian (two-particle register) the probe is the contact state +|X, X⟩, as in `get_exciton_ldos_cv`. Auxiliary degrees of freedom are summed or +selected with the keywords of `get_ldos_online` (`spin_proj`, `proj_s`, `nambu_proj`, +`proj_nambu`, `layer_proj`, `proj_layer`, `sublat_proj`, `proj_sl`), one scan per +sector. Every other keyword goes to `get_correction_vector`. +""" +function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; + eta::Real = 1e-2, + rtol::Real = 0.02, + nambu_proj::Bool = false, + proj_nambu = nothing, + spin_proj::Bool = false, + proj_s = nothing, + layer_proj::Bool = false, + proj_layer = nothing, + sublat_proj::Bool = false, + proj_sl = nothing, + kwargs...) + 1 <= X <= H.N || throw(BoundsError(1:H.N, X)) + aux = _aux_projection(H; nambu_proj, proj_nambu, spin_proj, proj_s, + layer_proj, proj_layer, sublat_proj, proj_sl) + Gs = Vector{ComplexF64}[]; ress = Vector{Float64}[]; χs = Vector{Int}[] + for σ in _probe_sectors(aux) + G, res, χ = _cv_scan(H.mpo, probe_state(H, X, σ), ω_vals, eta; rtol, kwargs...) + push!(Gs, G); push!(ress, res); push!(χs, χ) + end + return _cv_ldos_result(Gs, ress, χs, rtol, "get_ldos_cv") +end + +""" + get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, + ) + -> (; ldos, G, residual, converged, maxlinkdim) + +The exciton LDOS at the contact probe |X, X⟩ (electron and hole on position `X`, +1-indexed in `1:H.N`; `mpsexciton`, the probe of `get_exciton_ldos`) from correction +vectors, in the units and with the scan of `get_ldos_cv`: per unit physical energy, +`ldos = −Im G/π`; `get_exciton_ldos(…) · π/H.scale` is in the same units. + +The bound-exciton peaks of a contact probe are where correction vectors pay most: `x` +stays at a small bond dimension down to small η, where KPM-MPS needs moments beyond +its truncation floor. In the two-particle continuum `x` spreads and its bond +dimension grows; check `converged`. +""" +function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; + eta::Real = 1e-2, + rtol::Real = 0.02, + kwargs...) + 1 <= X <= H.N || throw(BoundsError(1:H.N, X)) + length(H.sites) == 2 * H.L || + throw(ArgumentError("get_exciton_ldos_cv: H is not an exciton Hamiltonian " * + "(2L interleaved electron-hole sites); use get_ldos_cv")) + G, res, χ = _cv_scan(H.mpo, mpsexciton(X, H.sites), ω_vals, eta; rtol, kwargs...) + return _cv_ldos_result([G], [res], [χ], rtol, "get_exciton_ldos_cv") +end + + +# ============================================================ +# 3. Isolated peaks: energy and weight +# ============================================================ + +""" + refine_bound_state(H, b::MPS, ω0; eta_steps=(0.05, 0.01, 1e-3, 1e-4), x0=nothing, + ) + -> (; energy, weight, x, residual, converged, history) + +The energy `E` and the probe weight `w = |⟨ψ|b⟩|²/⟨b|b⟩` of an isolated eigenstate +`ψ` near `ω0`, by Rayleigh-quotient iteration on the correction vector: for each η of +`eta_steps`, `x = (ω + iη − H)⁻¹|b⟩` (warm-started), then +`ω ← ⟨x|H|x⟩/⟨x|x⟩` and `w = |⟨b|x⟩|²/(⟨x|x⟩⟨b|b⟩)`. As η → 0 at ω = E, x is +dominated by ψ, so the quotients converge to E and w: the content of an isolated +peak, which a spectrum at finite η only broadens. The other levels enter at order +η²/Δ (Δ the distance to the next level the probe sees), so the last η sets the +accuracy: with the default 1e-4 and Δ of order one, E to ~1e-8. + +Start `ω0` within the peak's width at the first η (a coarse scan with `get_ldos_cv` +at η ≈ 0.1–0.2 finds it). A level that is not isolated at the last η (another state +within it) gives a mixed quotient; the history shows it as an energy that keeps +moving. `history` holds `(; eta, omega, energy, weight, residual)` per step. +""" +function refine_bound_state(H::Union{TBHamiltonian,MPO}, b::MPS, ω0::Real; + eta_steps = (0.05, 0.01, 1e-3, 1e-4), + x0::Union{MPS,Nothing} = nothing, + kwargs...) + Hm = _cv_mpo(H) + nb = real(inner(b, b)) + ω = Float64(ω0) + x = x0 + history = NamedTuple[] + r = nothing + for η in eta_steps + r = get_correction_vector(Hm, b, ω; eta = η, x0 = x, kwargs...) + x = r.x + nx = real(inner(x, x)) + E = real(inner(x', Hm, x)) / nx + w = abs2(inner(b, x)) / (nx * nb) + push!(history, (; eta = η, omega = ω, energy = E, weight = w, residual = r.residual)) + ω = E + end + last = history[end] + return (; energy = last.energy, weight = last.weight, x, residual = last.residual, + converged = r.converged, history) +end diff --git a/test/correction_vector.jl b/test/correction_vector.jl new file mode 100644 index 0000000..334d16e --- /dev/null +++ b/test/correction_vector.jl @@ -0,0 +1,73 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Test +using TensorBinding: get_Hamiltonian, add_onsite!, exciton_hamiltonian, probe_state, + mpsexciton, get_correction_vector, get_ldos_cv, get_exciton_ldos_cv, + refine_bound_state + +# Correction vectors x = (ω + iη − H)⁻¹|b⟩ against dense resolvents: a site of a chain, the +# contact probe of an exciton, the residual check, and the Rayleigh-quotient refinement +# of an isolated level. +@testset "correction vectors: get_correction_vector, get_ldos_cv, refine_bound_state" begin + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + dvec(ψ, s) = vec(Array(prod(ψ), s...)) + gref(Hd, bv, z) = dot(bv, (z * I - Hd) \ bv) + + # A 64-site chain with an attractive impurity (a bound level below the band). + H = get_Hamiltonian("chain_1d", 1.0; L=6) + v = zeros(64); v[20] = -2.0 + add_onsite!(H, v; tol=1e-14) + s, Hd = H.sites, dmat(H.mpo, H.sites) + b = probe_state(H, 20); bv = dvec(b, s) + + # one frequency, GMRES and MINRES, TBHamiltonian and raw MPO + for (ω, η) in ((0.3, 0.05), (-2.8, 1e-3)) + for solver in (:gmres, :minres) + r = get_correction_vector(H, b, ω; eta=η, solver) + @test r.converged && r.residual < 1e-4 # rounding floor ~1e-8·‖x‖/‖b‖ + @test r.G ≈ gref(Hd, bv, ω + im * η) rtol=1e-6 + end + end + @test get_correction_vector(H.mpo, b, 0.3; eta=0.05).G ≈ gref(Hd, bv, 0.3 + 0.05im) rtol=1e-6 + # continuation in η reaches the same answer + r = get_correction_vector(H, b, -2.8; eta=1e-3, eta_path=(0.1, 0.01)) + @test r.G ≈ gref(Hd, bv, -2.8 + 1e-3im) rtol=1e-6 + + # the LDOS scan: −Im G/π per unit physical energy + ωs = range(-3.0, 3.0; length=7) + res = get_ldos_cv(H, 20, ωs; eta=0.05) + @test all(res.converged) + @test res.ldos ≈ [-imag(gref(Hd, bv, ω + 0.05im)) / π for ω in ωs] rtol=1e-6 + + # a forced failure (maxdim 2) is flagged, and the scan warns + bad = get_correction_vector(H, b, 0.3; eta=0.05, maxdim=2, seed_maxdim=2, rounds=1) + @test !bad.converged && bad.residual > 0.02 + @test_logs (:warn, r"not converged") match_mode=:any get_ldos_cv( + H, 20, [0.3]; eta=0.05, maxdim=2, seed_maxdim=2, rounds=1) + + # the isolated impurity level: energy and probe weight against dense eigenstates + F = eigen(Hermitian(Hd)) + k = argmin(F.values) + rb = refine_bound_state(H, b, -2.7) + @test rb.energy ≈ F.values[k] atol=1e-8 + @test rb.weight ≈ abs2(dot(F.vectors[:, k], bv)) rtol=1e-6 + + # Exciton: contact probe |X, X⟩ on a 16-site chain (4 qubits per particle, 8 + # interleaved), attractive contact U = 2: a bound exciton below the continuum. + He = exciton_hamiltonian("chain_1d", 1.0, x -> 2.0; L=4) + se = He.sites + Hed = ITensors.@disable_warn_order dmat(He.mpo, se) + X = 7 + be = mpsexciton(X, se); bev = dvec(be, se) + ωe = range(-6.0, 1.0; length=6) + re = get_exciton_ldos_cv(He, X, ωe; eta=0.05) + @test all(re.converged) + @test re.ldos ≈ [-imag(gref(Hed, bev, ω + 0.05im)) / π for ω in ωe] rtol=1e-6 + # get_ldos_cv on an exciton Hamiltonian uses the same contact probe + @test get_ldos_cv(He, X, ωe; eta=0.05).ldos ≈ re.ldos rtol=1e-8 + @test_throws ArgumentError get_exciton_ldos_cv(H, 3, [0.0]) + + # argument checks + @test_throws ArgumentError get_correction_vector(H, b, 0.3; eta=0.0) + @test_throws ArgumentError get_correction_vector(H, b, 0.3; solver=:cg) + Hn = +(H.mpo, 0.3im * MPO(s, ["Z", "Id", "Id", "Id", "Id", "Id"]); cutoff=1e-14) + @test_throws ArgumentError get_correction_vector(Hn, b, 0.3; solver=:minres) +end diff --git a/test/runtests.jl b/test/runtests.jl index 7a5f8bb..595a99b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -111,6 +111,7 @@ include("kbonacci.jl") include("kpm_moment_columns.jl") include("gpu_mps_ldos.jl") include("krylov_minres.jl") +include("correction_vector.jl") # Regression tests for the bugs found in the 2026-09 code audit # (docs/dev/REORGANISATION_TODO.md, "Bugs"). From 778349582756a6bffdb5898f52407805917a9ac3 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Mon, 16 Nov 2026 21:03:19 +0200 Subject: [PATCH 05/13] RPA: get_rpa_susceptibility_column; Wynn series warns near the instability get_rpa_susceptibility_column(H, MPOV, omega, j) solves (I - Pi0 V)x = Pi0|j> on the L sites of Pi0 for one column of chi, with the bubble keywords and defaults of get_rpa_susceptibility and tighter solve defaults (40 sweeps, cutoff 1e-12), plus the residual check of the Dyson solve. In the inversion study one column gave chi_jj to 1e-7 - 2e-6 at condition number 1e3 in about a second, against minutes for the whole chi. The Wynn drivers estimate the spectral radius of V Pi0 from the norms of the Neumann terms and warn above 0.9, where the K_max = 6 estimate was 1-90 % off. Returned values are unchanged (RPA goldens pass as they are). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 +++++ docs/src/index.md | 3 +- src/TensorBinding.jl | 3 +- src/physics/rpa/dyson.jl | 100 +++++++++++++++++++++++++++++++++++++++ test/rpa_column.jl | 64 +++++++++++++++++++++++++ test/runtests.jl | 1 + 6 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 test/rpa_column.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index bd03abb..0d8510f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,19 @@ bugs without breaking the API. - `refine_bound_state(H, b, ω0; eta_steps, …)`: the energy and probe weight of an isolated level by Rayleigh-quotient iteration on the correction vector, to ~η² of the last step (default `eta_steps = (0.05, 0.01, 1e-3, 1e-4)`). +- **`get_rpa_susceptibility_column(H, MPOV, ω, j; …)`**: one column x = χ|j⟩ of the RPA + susceptibility, from (I − Π₀V)x = Π₀|j⟩ on the L sites of Π₀ instead of the whole χ + on the 2L-site vectorized register; `j` is a basis state (a position for position-only + sites) or any MPS. The bubble keywords and defaults are those of + `get_rpa_susceptibility`, whose column it returns; the solve defaults are tighter + (`rpa_nsweeps = 40`, `rpa_cutoff = 1e-12`), which costs little on L sites. On a + 256-site half-filled chain up to U = 0.999 U_c (condition number 10³) it gave χ_jj to + 1e-7–2e-6 and the column to 3e-6 in about a second, against minutes for the whole χ. +- **The Wynn-accelerated RPA warns near the instability**: the three Wynn drivers + estimate the spectral radius ρ of VΠ₀ from ‖Tₙ‖/‖Tₙ₋₁‖ of the Neumann terms and warn + when it exceeds 0.9. There the series converges slowly or diverges, and the default + `K_max = 6` was 1–90 % off at ρ ≈ 1 on a half-filled chain (with ρ > 1 from U ≈ 0.6 U_c + at finite ω and η). The returned values are unchanged. ### Changed results diff --git a/docs/src/index.md b/docs/src/index.md index 3a65592..57184c5 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -57,7 +57,8 @@ functions need CUDA.jl to be loaded (see the README). - Mean-field self-consistency: `get_scf` - Topology: `chern_marker`, `get_thouless_pump`, `get_valley_operator`, `valley_chern_marker`, `winding_marker` -- RPA response: `get_bubble_mpo`, `get_rpa_susceptibility`, `get_rpa_susceptibility_wynn` +- RPA response: `get_bubble_mpo`, `get_rpa_susceptibility`, + `get_rpa_susceptibility_column`, `get_rpa_susceptibility_wynn` - Non-Hermitian models and spectra: `add_nh_nonreciprocal_hopping!`, `add_nh_onsite!`, `add_nh_skin_hopping!`, `nh_spectral_function`, `nh_spectrum_grid` - Excitons: `get_exciton_bands`, `get_exciton_continuum`, `get_exciton_ldos_cv`, diff --git a/src/TensorBinding.jl b/src/TensorBinding.jl index 94da50f..7084532 100644 --- a/src/TensorBinding.jl +++ b/src/TensorBinding.jl @@ -55,7 +55,8 @@ export get_scf export chern_marker, get_thouless_pump, get_valley_operator, valley_chern_marker, winding_marker # RPA response -export get_bubble_mpo, get_rpa_susceptibility, get_rpa_susceptibility_wynn +export get_bubble_mpo, get_rpa_susceptibility, get_rpa_susceptibility_column, + get_rpa_susceptibility_wynn # Non-Hermitian models and spectra export add_nh_nonreciprocal_hopping!, add_nh_onsite!, add_nh_skin_hopping!, nh_spectral_function, nh_spectrum_grid diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index 78365e0..0431a5f 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -181,6 +181,86 @@ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; residual_warn=rpa_residual_warn, return_info) end + +# The get_bubble_mpo keywords of get_rpa_susceptibility and their defaults there, for +# get_rpa_susceptibility_column (which must give a column of the same χ). +const _RPA_BUBBLE_DEFAULTS = (; ϵF = 0.0, P_method = :purification, GF_method = :kpm, + Ncheb = 150, maxdim = 200, cutoff = 1e-8, purify_method = :mcweeny, purify_maxdim = 40, + purify_maxiters = 30, purify_tol = 1e-5, η = 1e-3, krylov_nsweeps = 12, + krylov_maxdim = 100, krylov_cutoff = 1e-8, verbose = false) + +# One column of the Dyson solution: (I − Π₀V)x = Π₀|b⟩ on the L sites of Π₀, from x = Π₀|b⟩. +function _rpa_column_solve(Π::MPO, MPOV::MPO, sites, b::MPS; nsweeps::Int, maxdim::Int, + cutoff::Real, residual_warn::Real, return_info::Bool) + A = MPO(sites, "Id") - apply(Π, MPOV; maxdim=maxdim, cutoff=cutoff) + rhs = apply(Π, b; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) + x = ITensorMPS.linsolve(A, rhs, deepcopy(rhs); nsweeps, maxdim, cutoff) + (return_info || isfinite(residual_warn)) || return x + res = _linsolve_residual(A, rhs, x, 0.0, 1.0) + res > residual_warn && + @warn "get_rpa_susceptibility_column: the Dyson solve is not converged: relative " * + "residual $(round(res; sigdigits=2)) > rpa_residual_warn = $residual_warn. " * + "Lower `rpa_cutoff` or raise `rpa_nsweeps` / `rpa_maxdim` (maxlinkdim " * + "reached: $(maxlinkdim(x)))." + return return_info ? (; chi = x, residual = res, maxlinkdim = maxlinkdim(x)) : x +end + +""" + get_rpa_susceptibility_column(H::TBHamiltonian, MPOV, ω, j; mode=:charge, + rpa_nsweeps=40, rpa_maxdim=400, rpa_cutoff=1e-12, + rpa_residual_warn=1e-2, return_info=false, + ) -> MPS + +One column of the RPA susceptibility χ = (I − Π₀V)⁻¹Π₀: the L-site MPS x = χ|j⟩, with +χ_ij = ⟨i|x⟩, from the Dyson equation (I − Π₀V)x = Π₀|j⟩ on the sites of Π₀. `j` is +the 1-indexed basis state of those sites (`_basis_state_mps(j − 1, sites)`: the +position `j` when they are position qubits only), or any MPS on them (then x = χ|j⟩). + +Where one site's response is enough (χ_jj, a local probe, one column of a real-space +response) this is much cheaper than `get_rpa_susceptibility`, which solves for the +whole χ on the 2L-site vectorized register, and it holds its accuracy close to the +instability: on a 256-site half-filled chain up to U = 0.999 U_c (condition number +10³), χ_jj to 1e-7–2e-6 and the column to 3e-6 in about a second, against minutes for +the whole χ. The tighter defaults (`rpa_nsweeps = 40`, `rpa_cutoff = 1e-12`) cost +little on L sites. + +The bubble is that of `get_rpa_susceptibility` with the same keywords and defaults +(`mode`, `ϵF`, `P_method`, `GF_method`, `Ncheb`, `maxdim`, `cutoff`, `η`, …), so the +result is column `j` of its χ. The residual check and `return_info` (returning +`(; chi, residual, maxlinkdim)`) are those of `rpa_from_bubble_diag`. +""" +function get_rpa_susceptibility_column(H::TBHamiltonian, MPOV::MPO, ω::Real, j; + mode::Symbol = :charge, + rpa_nsweeps::Int = 40, + rpa_maxdim::Int = 400, + rpa_cutoff::Real = 1e-12, + rpa_residual_warn::Real = 1e-2, + return_info::Bool = false, + kwargs...) + bubble_kw = merge(_RPA_BUBBLE_DEFAULTS, kwargs) + if mode == :charge + Π = get_bubble_mpo(H, H, ω; bubble_kw...) + out_sites = H.sites + elseif mode == :magnetic + H.spin_s === nothing && + error("get_rpa_susceptibility_column: mode=:magnetic requires a spinful H (call add_spin!(H) first)") + H_up = _project_spin_sector(H, 1) + H_dn = _project_spin_sector(H, 2) + Π = get_bubble_mpo(H_up, H_dn, ω; bubble_kw...) + out_sites = H_up.sites + else + error("get_rpa_susceptibility_column: unknown mode=$mode. Choose :charge or :magnetic") + end + b = j isa MPS ? j : begin + D = prod(dim.(out_sites)) + 1 <= j <= D || throw(BoundsError(1:D, j)) + _basis_state_mps(j - 1, out_sites) + end + return _rpa_column_solve(Π, MPOV, out_sites, b; nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, + cutoff=rpa_cutoff, residual_warn=rpa_residual_warn, + return_info) +end + # ============================================================ # 3. Wynn ε-algorithm accelerated RPA # ============================================================ @@ -232,6 +312,7 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In chi_partial = nothing chi_wynn = nothing + slow = Tuple{Int,Float64}[] # (item, ρ estimate) of slow series for (i, x) in enumerate(items) verbose && println(label(i, x)) @@ -249,11 +330,19 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In # Individual term contributions: spect_terms[n+1, q] spect_terms = zeros(Float64, K_max+1, nq) spect_terms[1, :] = s0 + tnorm = zeros(Float64, K_max+1) # ‖Tₙ‖_F + tnorm[1] = norm(term) for n in 1:K_max term = apply(term, MPOV; maxdim=maxdim_apply, cutoff=cutoff_apply) term = apply(term, Π0; maxdim=maxdim_apply, cutoff=cutoff_apply) spect_terms[n+1, :] = -imag.(get_spect_k(term)) + tnorm[n+1] = norm(term) + end + # ‖Tₙ‖/‖Tₙ₋₁‖ tends to the spectral radius ρ of VΠ₀; over the last two steps: + if K_max >= 2 && tnorm[K_max-1] > 0 + ρ = sqrt(tnorm[K_max+1] / tnorm[K_max-1]) + ρ > 0.9 && push!(slow, (i, ρ)) end # Partial sums (Wynn input) @@ -271,6 +360,17 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In verbose && println(" done ($(K_max+1) terms, $n_wynn Wynn estimates)") end + # Near ρ(VΠ₀) = 1 (the RPA instability, and at finite ω, η already below it) the + # Neumann series converges slowly or diverges, and a few Wynn steps do not fix it: + # with K_max = 6 the estimate was 1–90 % off at ρ ≈ 1 on a half-filled chain. + if !isempty(slow) + ρmax = maximum(last, slow) + @warn "RPA Wynn series: ‖Tₙ‖/‖Tₙ₋₁‖ ≈ $(round(ρmax; sigdigits=3)) > 0.9 at " * + "$(length(slow)) of $nω frequencies (the spectral radius of VΠ₀): the Neumann " * + "series converges slowly or diverges there, and the Wynn estimates with " * + "K_max = $K_max can be off by tens of %. Use a larger K_max or the Dyson " * + "solve (get_rpa_susceptibility, get_rpa_susceptibility_column)." + end return chi_partial, chi_wynn end diff --git a/test/rpa_column.jl b/test/rpa_column.jl new file mode 100644 index 0000000..3e5b1da --- /dev/null +++ b/test/rpa_column.jl @@ -0,0 +1,64 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Random, Test +using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, + get_rpa_susceptibility_column, rpa_wynn_from_bubbles, custom_mpo, + _rpa_column_solve, _basis_state_mps + +# One column of the RPA susceptibility, (I − Π₀V)x = Π₀|j⟩ on the L sites of Π₀, and the +# warning of the Wynn series near ρ(VΠ₀) = 1. +@testset "RPA: one column of χ; Wynn series near the instability" begin + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + dvec(ψ, s) = vec(Array(prod(ψ), s...)) + function mpo_of(A, s) + d = dim.(s) + return MPO(ITensor(reshape(A, d..., d...), prime.(s)..., s...), s; cutoff=1e-15) + end + + # Exact Lindhard bubble of a half-filled 32-site chain, V = U·1, U = α U_c. + H = get_Hamiltonian("chain_1d", 1.0; L=5) + s = H.sites + E, ψ = eigen(Hermitian(dmat(H.mpo, s))) + D = length(E) + f = Float64.(E .< 0) + W = reshape([ψ[i, a] * conj(ψ[i, b]) for i in 1:D, a in 1:D, b in 1:D], D, D * D) + bubble(ω, η) = W * Diagonal(vec([(f[b] - f[a]) / (ω + im * η - E[b] + E[a]) + for a in 1:D, b in 1:D])) * W' + μ = eigvals(bubble(0.0, 0.03)) + Uc = 1 / real(μ[argmax(abs.(real.(μ)))]) + Πd = bubble(0.0, 0.03) + Π = mpo_of(Πd, s) + j = 17 + bj = _basis_state_mps(j - 1, s) + for α in (0.3, 0.999) # condition number 1.4 and 10³ + V = α * Uc * MPO(s, "Id") + χx = (I - α * Uc * Πd) \ Πd + col = χx * dvec(bj, s) + r = @test_logs _rpa_column_solve(Π, V, s, bj; nsweeps=40, maxdim=400, cutoff=1e-12, + residual_warn=1e-2, return_info=true) + @test norm(dvec(r.chi, s) - col) / norm(col) < 1e-5 + @test r.residual < 1e-3 + end + + # The Wynn series warns where it converges slowly (‖Tₙ‖/‖Tₙ₋₁‖ ≈ ρ(VΠ₀) > 0.9). + @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=6) + @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( + [Π], 0.999Uc * MPO(s, "Id"); K_max=6) + + # Through the bubble: column j of get_rpa_susceptibility's χ, on a 4-site chain with + # the exact occupied-state projector cached (so the bubble itself is exact). + Hc = get_Hamiltonian("chain_1d", 1.0; L=2, scale=2.2) + F = eigen(Hermitian(dmat(Hc.mpo, Hc.sites))) + Hc._density_cache = mpo_of(F.vectors * Diagonal(F.values .< Hc.center) * F.vectors', + Hc.sites) + sc = Hc.sites + kw = (; GF_method=:krylov, η=0.2, krylov_nsweeps=10, krylov_maxdim=64, + krylov_cutoff=1e-12, cutoff=1e-12, maxdim=64) + V = +(0.5 * MPO(sc, "Id"), 0.2 * MPO(sc, ["Z", "Id"]); cutoff=1e-14) + χ = dmat(custom_mpo(get_rpa_susceptibility(Hc, V, 0.3; kw..., rpa_nsweeps=6, + rpa_maxdim=64, rpa_cutoff=1e-12), sc), sc) + for jj in (1, 3) + x = get_rpa_susceptibility_column(Hc, V, 0.3, jj; kw...) + @test dvec(x, sc) ≈ χ * dvec(_basis_state_mps(jj - 1, sc), sc) rtol=1e-8 + end + @test_throws BoundsError get_rpa_susceptibility_column(Hc, V, 0.3, 5; kw...) + @test_throws ErrorException get_rpa_susceptibility_column(Hc, V, 0.3, 1; mode=:spin) +end diff --git a/test/runtests.jl b/test/runtests.jl index 595a99b..301f45f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -112,6 +112,7 @@ include("kpm_moment_columns.jl") include("gpu_mps_ldos.jl") include("krylov_minres.jl") include("correction_vector.jl") +include("rpa_column.jl") # Regression tests for the bugs found in the 2026-09 code audit # (docs/dev/REORGANISATION_TODO.md, "Bugs"). From 4c134cc175377bb221f82b498f464a024af12159 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Tue, 17 Nov 2026 11:21:06 +0200 Subject: [PATCH 06/13] Comment why the 1e-8 cutoffs of get_green_krylov and the Dyson solve stay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The relative cutoff is size-blind and sets the accuracy floor of G (and of the RPA χ near the instability); the residual checks report it. A comment at each default says so, to keep it from being "fixed" silently. Co-Authored-By: Claude Opus 5.5 --- src/physics/rpa/dyson.jl | 6 ++++++ src/solvers/Krylov.jl | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index 0431a5f..ec9f860 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -53,6 +53,10 @@ maxlinkdim)` instead of vec(χ). """ function rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; nsweeps=20, maxdim=400, cutoff=1e-8, + # The default cutoff stays 1e-8 on purpose (0.2.2): its error + # grows like κ·1e-5 near the instability (κ the condition number + # of I − Π₀V) and the residual check warns there. A tighter default + # would cost ~5× on every call and move every RPA result. return_info::Bool=false, residual_warn::Real=1e-2) Id = MPO(finalfinalsites, "Id") ΠV = apply(Π, MPOV; maxdim=maxdim, cutoff=cutoff) @@ -136,6 +140,7 @@ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; mode::Symbol = :charge, rpa_nsweeps::Int = 20, rpa_maxdim::Int = 400, + # 1e-8 on purpose, an accuracy floor: see rpa_from_bubble_diag. rpa_cutoff::Real = 1e-8, rpa_residual_warn::Real = 1e-2, return_info::Bool = false, @@ -533,6 +538,7 @@ on the spin-projected sites (`H.sites` without the spin index), vectorized as in function get_magnon_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; rpa_nsweeps::Int = 20, rpa_maxdim::Int = 400, + # 1e-8 on purpose, an accuracy floor: see rpa_from_bubble_diag. rpa_cutoff::Real = 1e-8, rpa_residual_warn::Real = 1e-2, return_info::Bool = false, diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index 42c2b0b..875d748 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -211,6 +211,12 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; η::Real = 1e-2, nsweeps::Int = 12, maxdim::Int = 100, + # The default cutoff stays 1e-8 on purpose (0.2.2). It is relative to + # ‖|G⟩⟩‖², so it is size-blind and sets the accuracy floor of G: 1e-6–2e-4 + # on an open impurity chain of 2^10 sites, up to 5e-4 at 2^12, which the + # residual check reports. Pass cutoff = 1e-12 when G must be better + # (1e-11–1e-6 there, at up to 2.5× the time) and read the residual with + # return_info = true. A tighter default would move every result. cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, start::Symbol = :random, @@ -375,6 +381,7 @@ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; η::Real = 1e-2, nsweeps::Int = 12, maxdim::Int = 100, + # 1e-8 on purpose, an accuracy floor: see the MPO method above. cutoff::Real = 1e-8, x0_mpo::Union{MPO,Nothing} = nothing, start::Symbol = :random, From e633d20b11ec89ef4b20d84fcb1366ba161f29e6 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Tue, 17 Nov 2026 11:23:37 +0200 Subject: [PATCH 07/13] Raise the ITensorMPS lower bound to 0.3.26 The MINRES solver and the correction vectors pass their own local solver to ITensorMPS's linsolve, and random_mps(rng, ...) seeds the new start. The suite passes at 0.3.26 (CI mode, Julia 1.12) and 0.3.44; nothing below was tested. The cap below 0.3.45 stays. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +++++++ Project.toml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d8510f..6725dca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,13 @@ bugs without breaking the API. `K_max = 6` was 1–90 % off at ρ ≈ 1 on a half-filled chain (with ρ > 1 from U ≈ 0.6 U_c at finite ω and η). The returned values are unchanged. +### Changed + +- **Dependencies:** ITensorMPS 0.3.26 to 0.3.44 (was 0.3.16 to 0.3.44). The MINRES + solver and the correction vectors pass their own local solver to ITensorMPS's + `linsolve`, and the suite was run at 0.3.26 and 0.3.44 only. The cap below 0.3.45 + stays (see 0.2.0). + ### Changed results - **`get_green_krylov` starts from a random state** (keywords `start` and `rng`). The diff --git a/Project.toml b/Project.toml index 446ff81..a501d05 100644 --- a/Project.toml +++ b/Project.toml @@ -17,7 +17,7 @@ TensorCrossInterpolation = "b261b2ec-6378-4871-b32e-9173bb050604" [compat] FFTW = "1" -ITensorMPS = "0.3.16 - 0.3.44" +ITensorMPS = "0.3.26 - 0.3.44" ITensors = "0.9" NDTensors = "0.4" Quantics = "0.4" From 3add85c4789e15a72118cb5a06bf75a2fff4f864 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 00:43:49 +0200 Subject: [PATCH 08/13] Residual checks: rounding floor, exact evaluation, cheaper contraction, size guard The residual behind the checks of get_green_krylov, the correction vectors and the RPA Dyson solves came from inner products whose terms cancel to ~sqrt(eps) of the largest, with the result clipped at zero. That floor grows with |x| (as 1/eta for a resolvent, with an energy offset, with the condition number of a Dyson solve), so a wrong vector could read as residual 0 and the exact one as 0.04 (0.2.2 review: numerics 2, API F7). _linsolve_residual now returns (; residual, floor, method): - the residual is an upper bound that includes the rounding floor of the evaluation, sqrt(32 eps S), S = (|a0| + |a1| sigma)^2 |x|^2 + |b|^2 with sigma the rms singular value of A (measured: at most 5.8 eps S over correction vectors at eta = 1e-2..1e-8, Green's functions and Dyson systems up to condition number 1e5); - where the floor leaves the comparison with the threshold open, the residual is read off the orthogonality centre of the direct sum of its terms (exact to ~eps); - is contracted one tensor at a time, chi^3 w^2 + chi^2 w^3 per site instead of the chi^3 w^3 of inner(A, x, A, x) (API F1), and the check is skipped with one @info when it would hold more than 2^27 numbers. The return_info tuples and get_correction_vector's result gain residual_floor. Also: comparisons that a NaN residual cannot pass silently (numerics 7); a zero right-hand side of the Dyson solves returns zero with residual 0 instead of NaN or a false warning (API F12); the Krylov seed of the correction vectors stops when H^k b = 0 instead of dividing by zero, and the expansion rounds no longer overflow at maxdim = typemax(Int) (numerics 5). CHANGELOG: the two residual checks move from Added to Changed (they are on by default and have a cost), with the measured residual 3e-2 of the U = 0.999 U_c case. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 57 +++++++----- src/physics/rpa/dyson.jl | 64 +++++++++---- src/solvers/CorrectionVector.jl | 50 ++++++---- src/solvers/Krylov.jl | 146 ++++++++++++++++++++++++++---- test/bugfix_rpa_dyson_residual.jl | 3 + test/linsolve_residual.jl | 80 ++++++++++++++++ test/rpa_column.jl | 6 ++ test/runtests.jl | 1 + 8 files changed, 332 insertions(+), 75 deletions(-) create mode 100644 test/linsolve_residual.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index 6725dca..b0e2ae0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,31 +18,15 @@ bugs without breaking the API. `maxiter · krylovdim` products per local solve (the GMRES one); a non-Hermitian H throws an `ArgumentError` (checked from ‖H − H†‖/‖H‖). With `solver=:minres` no z·I − H is formed: z goes into `linsolve`'s coefficients. -- **`get_green_krylov` checks its residual**: the relative residual - ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the solve (from exact inner products) is computed after the - sweeps, and a warning says that G is not converged when it exceeds `residual_warn` - (default `1e-2`; `Inf` skips the check). `return_info=true` returns - `(; G, residual, maxlinkdim)` instead of the MPO. On the chains of the next entry and - on 16×16 square lattices the residual was 1–5 times the relative error of G, down to - its noise floor of ~1e-7. -- **The RPA Dyson solve checks its residual**: `rpa_from_bubble_diag` computes the - relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ and warns above `residual_warn` (default - `1e-2`); `return_info=true` returns `(; chi, residual, maxlinkdim)`. - `get_rpa_susceptibility` and `get_magnon_susceptibility` pass `rpa_residual_warn` and - `return_info` to it. Near the RPA instability the truncation limits χ to about - κ·1e-5 at the default `cutoff = 1e-8` (κ the condition number of I − Π₀V): on a - 32-site half-filled chain at U = 0.999 U_c (κ = 10³) χ is off by 5e-3 with a residual - of 5e-2, so the check warns; `rpa_cutoff = 1e-12` gives 1e-7. The default cutoff is - unchanged. - **Correction vectors** (`src/solvers/CorrectionVector.jl`, a new section of the Solvers page): the resolvent of one probe state, x = (ω + iη − H)⁻¹|b⟩ and G = ⟨b|x⟩, at a resolution set by η rather than by a number of KPM moments. - - `get_correction_vector(H, b, ω; eta, …)` returns `(; G, x, residual, converged, - maxlinkdim, sweeps, rounds)`. It starts from a Krylov seed b + Σₖ Hᵏb (from the - probe alone the two-site sweeps can stall on two-particle registers), sweeps until - G stops moving, checks the residual and, above `rtol` (0.02), expands x along the - residual for up to `rounds` rounds. `x0` warm-starts it, `eta_path` continues in η, - and `solver=:minres` uses the MINRES updater. + - `get_correction_vector(H, b, ω; eta, …)` returns `(; G, x, residual, + residual_floor, converged, maxlinkdim, sweeps, rounds)`. It starts from a Krylov + seed b + Σₖ Hᵏb (from the probe alone the two-site sweeps can stall on two-particle + registers), sweeps until G stops moving, checks the residual and, above `rtol` + (0.02), expands x along the residual for up to `rounds` rounds. `x0` warm-starts + it, `eta_path` continues in η, and `solver=:minres` uses the MINRES updater. - `get_ldos_cv(H, X, ω_vals; eta, …)` and `get_exciton_ldos_cv(H, X, ω_vals; eta, …)`: the LDOS of a site or of the exciton contact probe |X, X⟩ along a frequency grid, each point warm-started from the previous one, as `(; ldos, G, residual, converged, @@ -68,6 +52,35 @@ bugs without breaking the API. ### Changed +- **`get_green_krylov` checks its residual**, on by default: the relative residual + ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the solve is computed after the sweeps, and a warning says + that G is not converged when it exceeds `residual_warn` (default `1e-2`). + `return_info=true` returns `(; G, residual, residual_floor, maxlinkdim)` instead of + the MPO. The check costs about one sweep or less; `residual_warn = Inf` skips it. On + the chains of the "random state" entry below and on 16×16 square lattices the residual + was 1–5 times the relative error of G. +- **The RPA Dyson solve checks its residual**, on by default: `rpa_from_bubble_diag` + computes the relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ and warns above `residual_warn` + (default `1e-2`); `return_info=true` returns `(; chi, residual, residual_floor, + maxlinkdim)`. `get_rpa_susceptibility` and `get_magnon_susceptibility` pass + `rpa_residual_warn` and `return_info` to it. Near the RPA instability the truncation + limits χ to about κ·1e-5 at the default `cutoff = 1e-8` (κ the condition number of + I − Π₀V): on a 32-site half-filled chain at U = 0.999 U_c (κ = 10³) χ is off by 5e-3 + with a residual of 3e-2, so the check warns; `rpa_cutoff = 1e-12` gives 1e-7. The + default cutoff is unchanged. The check contracts ⟨Aχ|Aχ⟩ once, χ³w² + χ²w³ operations + per site for bond dimensions χ of vec(χ) and w of I − Π₀V: less than a sweep for a + low-rank Π₀V, more for a high-rank one; `rpa_residual_warn = Inf` skips it. A bubble + Π₀ = 0 returns χ = 0 with residual 0. +- **How these residuals are evaluated** (and those of the correction vectors): from + inner products, with ⟨Ax|Ax⟩ contracted one tensor at a time. Their terms cancel, so + the value is an upper bound that includes the rounding floor of the evaluation, about + 1e-7·‖A‖‖x‖/‖b‖ (returned as `residual_floor`): a converged solve reads as its floor, + not as 0, and rounding noise cannot pass for a small residual. Where the floor leaves + the comparison with the threshold open (a correction vector at η ≲ 1e-5 on a level, a + constant energy offset of 10³ in H) the residual is evaluated exactly, from the direct + sum of its terms. A check whose contraction would hold more than 2^27 numbers (bond + dimensions χ of the solution and w of the operator with χ²w² > 6.7e7) is skipped with + one `@info` and reports `residual = NaN`. - **Dependencies:** ITensorMPS 0.3.26 to 0.3.44 (was 0.3.16 to 0.3.44). The MINRES solver and the correction vectors pass their own local solver to ITensorMPS's `linsolve`, and the suite was run at 0.3.26 and 0.3.44 only. The cap below 0.3.45 diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index ec9f860..4288a50 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -46,10 +46,15 @@ sites, started from vec(Π₀). Near the RPA instability I − Π₀V is nearly singular and the truncation at `cutoff` limits the accuracy: on a 32-site half-filled chain with V = U·1 at U = 0.999 U_c (condition number 10³) the default `cutoff = 1e-8` gives χ to 5e-3, `1e-12` to 1e-7. -The relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ of the solve (exact inner products) is -checked: above `residual_warn` (default `1e-2`; `Inf` skips the check) a warning -suggests a smaller `cutoff`. `return_info=true` returns `(; chi, residual, -maxlinkdim)` instead of vec(χ). +The relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ of the solve is checked, for Π₀V as +truncated at `cutoff`: above `residual_warn` (default `1e-2`) a warning suggests a +smaller `cutoff`. `return_info=true` returns `(; chi, residual, residual_floor, +maxlinkdim)` instead of vec(χ); `residual` is an upper bound that includes the +rounding floor of its evaluation (`residual_floor`, about 1e-7 times the condition +number). The check contracts ⟨Aχ|Aχ⟩ once, at χ³w² + χ²w³ operations per site for bond +dimensions χ of vec(χ) and w of I − Π₀V: less than a sweep while w is small, more for a +high-rank Π₀V. It is skipped, with a message and `residual = NaN`, when it would hold +more than 2^27 numbers (χ²w² > 6.7e7); `residual_warn = Inf` skips it altogether. """ function rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; nsweeps=20, maxdim=400, cutoff=1e-8, @@ -70,18 +75,30 @@ function rpa_from_bubble_diag(Π, MPOV, finalsites, finalfinalsites; Aop = interleave_mpo(A, finalsites, 1) b = _vec_mps_from_mpo(Π, finalsites) + # Π₀ = 0: χ = 0, and a residual relative to ‖Π₀‖ is undefined. + iszero(norm(b)) && + return return_info ? (; chi = b, residual = 0.0, residual_floor = 0.0, + maxlinkdim = maxlinkdim(b)) : b + x0 = deepcopy(b) χ = ITensorMPS.linsolve(Aop, b, x0; nsweeps=nsweeps, maxdim=maxdim, cutoff=cutoff) (return_info || isfinite(residual_warn)) || return χ - res = _linsolve_residual(Aop, b, χ, 0.0, 1.0) - res > residual_warn && - @warn "rpa_from_bubble_diag: the Dyson solve is not converged: relative residual " * - "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Close to the RPA " * - "instability lower `cutoff` (`rpa_cutoff`), e.g. to 1e-12; else raise " * - "`nsweeps` or `maxdim` (maxlinkdim reached: $(maxlinkdim(χ)))." - return return_info ? (; chi = χ, residual = res, maxlinkdim = maxlinkdim(χ)) : χ + info = _linsolve_residual(Aop, b, χ, 0.0, 1.0; threshold = residual_warn) + res = info.residual + if info.method === :skipped + isfinite(residual_warn) && _residual_skipped_info("rpa_from_bubble_diag", χ, Aop, + "χ is returned unchecked; residual_warn = Inf (rpa_residual_warn) silences this.") + else + res <= residual_warn || + @warn "rpa_from_bubble_diag: the Dyson solve is not converged: relative residual " * + "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Close to the RPA " * + "instability lower `cutoff` (`rpa_cutoff`), e.g. to 1e-12; else raise " * + "`nsweeps` or `maxdim` (maxlinkdim reached: $(maxlinkdim(χ)))." + end + return return_info ? (; chi = χ, residual = res, residual_floor = info.floor, + maxlinkdim = maxlinkdim(χ)) : χ end @@ -199,15 +216,26 @@ function _rpa_column_solve(Π::MPO, MPOV::MPO, sites, b::MPS; nsweeps::Int, maxd cutoff::Real, residual_warn::Real, return_info::Bool) A = MPO(sites, "Id") - apply(Π, MPOV; maxdim=maxdim, cutoff=cutoff) rhs = apply(Π, b; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) + # Π₀|b⟩ = 0: x = 0, and a residual relative to ‖Π₀|b⟩‖ is undefined. + iszero(norm(rhs)) && + return return_info ? (; chi = rhs, residual = 0.0, residual_floor = 0.0, + maxlinkdim = maxlinkdim(rhs)) : rhs x = ITensorMPS.linsolve(A, rhs, deepcopy(rhs); nsweeps, maxdim, cutoff) (return_info || isfinite(residual_warn)) || return x - res = _linsolve_residual(A, rhs, x, 0.0, 1.0) - res > residual_warn && - @warn "get_rpa_susceptibility_column: the Dyson solve is not converged: relative " * - "residual $(round(res; sigdigits=2)) > rpa_residual_warn = $residual_warn. " * - "Lower `rpa_cutoff` or raise `rpa_nsweeps` / `rpa_maxdim` (maxlinkdim " * - "reached: $(maxlinkdim(x)))." - return return_info ? (; chi = x, residual = res, maxlinkdim = maxlinkdim(x)) : x + info = _linsolve_residual(A, rhs, x, 0.0, 1.0; threshold = residual_warn) + res = info.residual + if info.method === :skipped + isfinite(residual_warn) && _residual_skipped_info("get_rpa_susceptibility_column", x, A, + "The column is returned unchecked; rpa_residual_warn = Inf silences this.") + else + res <= residual_warn || + @warn "get_rpa_susceptibility_column: the Dyson solve is not converged: relative " * + "residual $(round(res; sigdigits=2)) > rpa_residual_warn = $residual_warn. " * + "Lower `rpa_cutoff` or raise `rpa_nsweeps` / `rpa_maxdim` (maxlinkdim " * + "reached: $(maxlinkdim(x)))." + end + return return_info ? (; chi = x, residual = res, residual_floor = info.floor, + maxlinkdim = maxlinkdim(x)) : x end """ diff --git a/src/solvers/CorrectionVector.jl b/src/solvers/CorrectionVector.jl index ce91eb7..56f2b68 100644 --- a/src/solvers/CorrectionVector.jl +++ b/src/solvers/CorrectionVector.jl @@ -30,8 +30,10 @@ function _cv_krylov_seed(Hm::MPO, b::MPS, K::Int; maxdim::Int, cutoff::Real) acc = (1.0 + 0.0im) * b v = b for _ in 1:K - v = apply(Hm, v; maxdim, cutoff) - v = v / norm(v) + v = apply(Hm, v; maxdim, cutoff) + nv = norm(v) + nv > 0 || break # Hᵏb = 0 (a decoupled site): nothing to add + v = v / nv acc = +(acc, v; maxdim, cutoff) end return acc @@ -55,6 +57,9 @@ end # x ← x + 0.3‖x‖/‖r‖·r, r = (z − H)x − b (as an MPS), with 3 sweeps each, keeping the # best x. A two-site sweep can stop on a wrong vector that looks converged in G; the # residual shows it, and the expansion moves x out of the tangent space it is stuck in. +# The residual is the upper bound of _linsolve_residual (never below its rounding floor, +# exact where the floor would leave the comparison with rtol open); NaN, with +# converged = false, when its contraction is too large to run. function _cv_point(Hm::MPO, b::MPS, x::MPS, z::Complex; nsweeps::Int, gtol::Real, rtol::Real, rounds::Int, kw...) G = complex(NaN) @@ -66,21 +71,24 @@ function _cv_point(Hm::MPO, b::MPS, x::MPS, z::Complex; nsweeps::Int, gtol::Real (n >= 2 && abs(Gn - G) <= gtol * abs(Gn)) && (G = Gn; break) G = Gn end - res = _linsolve_residual(Hm, b, x, z, -1.0) + info = _linsolve_residual(Hm, b, x, z, -1.0; threshold = rtol) + res = info.residual + info.method === :skipped && _residual_skipped_info("get_correction_vector", x, Hm, + "It is reported as residual = NaN, converged = false.") k = 0 while k < rounds && res > rtol k += 1 - md = 2 * kw[:maxdim] + md = kw[:maxdim] > typemax(Int) ÷ 2 ? typemax(Int) : 2 * kw[:maxdim] r = +(z * x, -1.0 * apply(Hm, x; cutoff = kw[:cutoff], maxdim = md), -1.0 * b; cutoff = kw[:cutoff], maxdim = md) x1 = +(x, (0.3 * norm(x) / norm(r)) * r; cutoff = kw[:cutoff], maxdim = md) x1 = _cv_sweeps(Hm, b, x1, z; nsweeps = 3, kw...) n += 3 - r1 = _linsolve_residual(Hm, b, x1, z, -1.0) - r1 < res && ((x, res) = (x1, r1)) + i1 = _linsolve_residual(Hm, b, x1, z, -1.0; threshold = rtol) + i1.residual < res && ((x, info, res) = (x1, i1, i1.residual)) end - return (; G = inner(b, x), x, residual = res, converged = res <= rtol, - maxlinkdim = maxlinkdim(x), sweeps = n, rounds = k) + return (; G = inner(b, x), x, residual = res, residual_floor = info.floor, + converged = res <= rtol, maxlinkdim = maxlinkdim(x), sweeps = n, rounds = k) end function _cv_check_solver(Hm::MPO, solver::Symbol, fname::String) @@ -100,7 +108,7 @@ end solver=:gmres, maxdim=400, cutoff=1e-12, nsweeps=12, gtol=1e-6, rtol=0.02, rounds=3, seed_krylov=40, seed_maxdim=64, tol=1e-10, krylovdim=30, maxiter=30) - -> (; G, x, residual, converged, maxlinkdim, sweeps, rounds) + -> (; G, x, residual, residual_floor, converged, maxlinkdim, sweeps, rounds) The correction vector `x = (ω + iη − H)⁻¹|b⟩` of the probe `|b⟩` and `G = ⟨b|x⟩ = ⟨b|(ω + iη − H)⁻¹|b⟩`, the resolvent projected on the probe, with η = @@ -121,10 +129,20 @@ until `|ΔG| ≤ gtol·|G|` (at least 2, at most `nsweeps`), from `seed_maxdim`. From the probe alone, a product state, the two-site sweeps stall on two-particle (interleaved) registers and return a wrong `x` that looks converged. -Then the relative residual `‖(ω + iη − H)x − b‖/‖b‖` (exact inner products). Above -`rtol`, up to `rounds` residual expansions `x ← x + 0.3‖x‖/‖r‖·r` with 3 sweeps each, -keeping the best `x`. `converged = residual ≤ rtol`: when `false`, `G` is not to be -trusted (raise `maxdim`, or warm-start). +Then the relative residual `‖(ω + iη − H)x − b‖/‖b‖`. Above `rtol`, up to `rounds` +residual expansions `x ← x + 0.3‖x‖/‖r‖·r` with 3 sweeps each, keeping the best `x`. +`converged = residual ≤ rtol`: when `false`, `G` is not to be trusted (raise `maxdim`, +or warm-start). + +The residual comes from inner products whose terms cancel to about +`1e-7·(|ω| + ‖H‖)·‖x‖/‖b‖`, a rounding floor that grows as 1/η (and with a constant +energy offset in `H`). `residual` is an upper bound that includes this floor +(`residual_floor`); where the floor would leave the comparison with `rtol` open +(η ≲ 1e-5 on a level, or an offset of 10³ at η = 1e-4) the residual is evaluated +exactly instead, from the direct sum of its terms. The truncation sets a second +floor, about `√cutoff·‖x‖/‖b‖`: at small η, `rtol` is reachable only with +`cutoff ≲ (rtol·η)²`. Where the contraction of the check would hold more than 2^27 +numbers it is skipped with a message: `residual = NaN`, `converged = false`. `eta_path`: broadenings solved first, in order, each warm-starting the next and the last one `eta` (continuation in η at fixed ω). At small η a cold start can fail where @@ -138,8 +156,8 @@ a path from a larger η converges, provided its first step converges. - `tol`, `krylovdim`, `maxiter`: of the local solver (GMRES restarts `maxiter`; MINRES takes `maxiter·krylovdim` products). -Returns `x` and `G`, the residual, `converged`, the bond dimension of `x`, and the -numbers of sweeps and expansion rounds. +Returns `x` and `G`, the residual and its rounding floor, `converged`, the bond +dimension of `x`, and the numbers of sweeps and expansion rounds. ```julia b = TensorBinding.probe_state(H, 17) # site 17 @@ -203,7 +221,7 @@ function _cv_ldos_result(Gs, ress, χs, rtol, fname) G = sum(Gs) res = reduce((a, b) -> max.(a, b), ress) χ = reduce((a, b) -> max.(a, b), χs) - bad = count(>(rtol), res) + bad = count(r -> !(r <= rtol), res) # a NaN residual counts bad > 0 && @warn "$fname: $bad of $(length(res)) frequencies not converged " * "(residual > rtol = $rtol): raise maxdim or nsweeps, or use eta_path" return (; ldos = -imag.(G) ./ π, G, residual = res, converged = res .<= rtol, diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index 875d748..bcd5330 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -163,14 +163,109 @@ function _hermiticity_defect(H::MPO) end -# ‖(a0 + a1·A)x − b‖ / ‖b‖ from exact inner products, with no MPO–MPS product. The -# terms cancel to ~√eps of the largest one, so values below ~1e-7 are noise. -function _linsolve_residual(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number) +# The residual check holds the environment of ⟨Ax|Ax⟩ times one site: χ²w²d numbers +# (χ, w the bond dimensions of x and A). Above this many it is skipped: 2^27 ComplexF64 +# are 2 GiB, and the contraction holds two or three such arrays at a time. (A Ref, so +# that the tests can reach the skipped branch on small systems.) +const _RESIDUAL_MAX_ELEMENTS = Ref(2.0^27) +# Largest bond dimension, (w + 1)χ + χ_b, of the direct sum the exact evaluation +# orthogonalizes. +const _RESIDUAL_EXACT_MAXDIM = 2000 +# Rounding of the two evaluations, in units of eps and of the scale +# S = (|a0| + |a1|σ)²‖x‖² + ‖b‖², σ = ‖A‖_F/√D the rms singular value of A: ‖r‖² from the +# inner products is known to ± FLOOR_FACTOR·eps·S (measured: up to 5.8·eps·S), ‖r‖ from +# the direct sum to ± EXACT_FLOOR_FACTOR·eps·√S (measured: up to 37·eps·√S), over +# correction vectors at η = 1e-2…1e-8, Green's functions and Dyson systems of condition +# number up to 1e5 (examples/nontracked/fixes_checks/review_022/residual_floor.jl). +const _RESIDUAL_FLOOR_FACTOR = 32.0 +const _RESIDUAL_EXACT_FLOOR_FACTOR = 64.0 + +# ⟨Ax|Ax⟩ contracted one tensor at a time (environment · x · A · A† · x†): χ³w²d + χ²w³d² +# per site, where ITensorMPS's inner(A, x, A, x) joins A[j]·x[j] and its conjugate first, +# χ³w³d. The environment holds χ²w² numbers either way. The bra copies get their own +# link indices and their own copy of the site index A acts on. +function _norm2_Ax(A::MPO, x::MPS) + xb = sim(linkinds, dag(x)) + Ab = sim(linkinds, dag(A)) + E = ITensor(1.0) + for j in eachindex(x) + s = commonind(A[j], x[j]) + sb = sim(s) + E = ((E * x[j]) * A[j]) * replaceind(Ab[j], s, sb) + E = E * replaceind(xb[j], s, sb) + end + return real(scalar(E)) +end + +# ‖(a0 + a1·A)x − b‖ read off the orthogonality centre of the direct sum +# a0·x ⊕ a1·Ax ⊕ (−b), with Ax the untruncated product: accurate to ~eps of the largest +# term, where the inner products cancel to ~√eps of it. `nothing` when the sum cannot be +# formed (a bond of x or b without a link index; A may lack some, as interleave_mpo's +# output does) or would exceed _RESIDUAL_EXACT_MAXDIM. +function _linsolve_residual_exact(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number) + n = length(x) + n >= 2 || return nothing + haslinks(ψ) = all(j -> linkind(ψ, j) !== nothing, 1:n-1) + (haslinks(x) && haslinks(b)) || return nothing + χ, w = maxlinkdim(x), maxlinkdim(A) + (iszero(a0) ? w * χ : (w + 1) * χ) + maxlinkdim(b) <= _RESIDUAL_EXACT_MAXDIM || + return nothing + Ax = apply(A, x; alg = "naive", truncate = false) + r = iszero(a0) ? +(a1 * Ax, -1 * b; alg = "directsum") : + +(a0 * x, a1 * Ax, -1 * b; alg = "directsum") + orthogonalize!(r, 1) + return norm(r[1]) +end + +# The relative residual ‖(a0 + a1·A)x − b‖/‖b‖ of a linsolve result, as +# (; residual, floor, method). +# +# method = :inner: from exact inner products, with no MPO–MPS product, +# ‖r‖² = |a0|²⟨x|x⟩ + |a1|²⟨Ax|Ax⟩ + ⟨b|b⟩ + 2Re(ā0 a1⟨x|A|x⟩) − 2Re(ā0⟨x|b⟩) − 2Re(a1⟨b|A|x⟩). +# The terms cancel, and so do the products inside ⟨Ax|Ax⟩ where Ax is small next to +# ‖A‖‖x‖ (an ill-conditioned A): ‖r‖² is known to ± f² = _RESIDUAL_FLOOR_FACTOR·eps·S +# only, S = (|a0| + |a1|σ)²‖x‖² + ‖b‖² with σ = ‖A‖_F/√D standing for ‖A‖. That is a +# rounding floor f/‖b‖ ≈ 1e-7·(|a0| + |a1|σ)‖x‖/‖b‖, which grows with ‖x‖: as 1/η for a +# resolvent, with a constant energy offset in A, with the condition number of a Dyson +# solve. `residual` is the upper bound √(max(‖r‖², 0) + f²)/‖b‖, never below the floor: +# a converged solve reads as its floor, not as 0, and noise cannot pass for a small +# residual. +# method = :exact: when the floor leaves the comparison with `threshold` open (the +# threshold lies between the lower and the upper bound), from the direct sum +# (_linsolve_residual_exact), where it is affordable; `floor` is then ~eps-sized. +# method = :skipped: the contraction would hold more than _RESIDUAL_MAX_ELEMENTS numbers; +# `residual` and `floor` are NaN. +function _linsolve_residual(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number; + threshold::Real = Inf) + Float64(maxlinkdim(x))^2 * Float64(maxlinkdim(A))^2 * maximum(dim, siteinds(x)) > + _RESIDUAL_MAX_ELEMENTS[] && return (; residual = NaN, floor = NaN, method = :skipped) nb2 = real(inner(b, b)) - r2 = abs2(a0) * real(inner(x, x)) + abs2(a1) * real(inner(A, x, A, x)) + nb2 + - 2real(conj(a0) * a1 * inner(x', A, x)) - 2real(conj(a0) * inner(x, b)) - - 2real(conj(a1) * conj(inner(b', A, x))) - return sqrt(max(r2, 0.0) / nb2) + nx2 = real(inner(x, x)) + bAx = inner(b', A, x) + r2 = abs2(a1) * _norm2_Ax(A, x) + nb2 - 2real(a1 * bAx) + iszero(a0) || (r2 += abs2(a0) * nx2 + 2real(conj(a0) * a1 * inner(x', A, x)) - + 2real(conj(a0) * inner(x, b))) + r2 = max(r2, 0.0) + σ = sqrt(real(inner(A, A)) / _site_space_dim(siteinds(x))) + S = (abs(a0) + abs(a1) * σ)^2 * nx2 + nb2 + f2 = _RESIDUAL_FLOOR_FACTOR * eps(Float64) * S + if isfinite(threshold) && r2 - f2 <= threshold^2 * nb2 < r2 + f2 + rx = _linsolve_residual_exact(A, b, x, a0, a1) + rx === nothing || + return (; residual = rx / sqrt(nb2), method = :exact, + floor = _RESIDUAL_EXACT_FLOOR_FACTOR * eps(Float64) * sqrt(S / nb2)) + end + return (; residual = sqrt((r2 + f2) / nb2), floor = sqrt(f2 / nb2), method = :inner) +end + +# One line, once per session and function, when the residual check was skipped; `then` +# says what the caller does about it. +function _residual_skipped_info(fname::AbstractString, x::MPS, A::MPO, then::AbstractString) + @info "$fname: the residual check was skipped: at bond dimensions " * + "$(maxlinkdim(x)) (solution) and $(maxlinkdim(A)) (operator) its contraction " * + "would hold more than 2^27 numbers (2 GiB) at a time. $then" maxlog=1 _id=Symbol( + fname, :_residual_skipped) + return nothing end @@ -280,13 +375,21 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; G = custom_mpo(sol, sites) (return_info || isfinite(residual_warn)) || return G - res = _linsolve_residual(A, rhs, sol, a0, a1) - verbose && println(" relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ = $res") - res > residual_warn && - @warn "get_green_krylov: G is not converged: relative residual " * - "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Raise maxdim " * - "or nsweeps, or lower cutoff (maxlinkdim reached: $(maxlinkdim(sol)))." ω_phys η - return return_info ? (; G, residual = res, maxlinkdim = maxlinkdim(sol)) : G + info = _linsolve_residual(A, rhs, sol, a0, a1; threshold = residual_warn) + res = info.residual + if info.method === :skipped + isfinite(residual_warn) && + _residual_skipped_info("get_green_krylov", sol, A, + "G is returned unchecked; residual_warn = Inf silences this.") + else + verbose && println(" relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ = $res") + res <= residual_warn || + @warn "get_green_krylov: G is not converged: relative residual " * + "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Raise maxdim " * + "or nsweeps, or lower cutoff (maxlinkdim reached: $(maxlinkdim(sol)))." ω_phys η + end + return return_info ? (; G, residual = res, residual_floor = info.floor, + maxlinkdim = maxlinkdim(sol)) : G end @@ -349,13 +452,18 @@ unscaled — no KPM Chebyshev expansion required. residual). Default `1e-10`. - `maxiter` : Maximum Krylov iterations per site (GMRES restarts). Default `600`. - `krylovdim` : Krylov subspace dimension. Default `30`. -- `return_info` : Return `(; G, residual, maxlinkdim)` instead of the MPO `G`. - Default `false`. +- `return_info` : Return `(; G, residual, residual_floor, maxlinkdim)` instead of + the MPO `G`. Default `false`. - `residual_warn`: Warn when the relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the solve exceeds this value: G is then not converged (`maxdim`, - `nsweeps` or `cutoff` too coarse). The residual comes from exact - inner products; values below ~1e-7 are rounding noise. `Inf` - skips its computation unless `return_info=true`. Default `1e-2`. + `nsweeps` or `cutoff` too coarse). The residual comes from inner + products, whose terms cancel: `residual` is an upper bound that + includes the rounding floor of that evaluation (`residual_floor`, + a few 1e-7 here), so a converged solve reads as its floor, not 0. + The check costs about one sweep or less, and is skipped with a + message when its contraction would hold more than 2^27 numbers + (`residual` is then `NaN`). `Inf` skips its computation unless + `return_info=true`. Default `1e-2`. - `verbose` : Print progress messages (with MINRES, also the number of local solves and products) and the residual. Default `false`. diff --git a/test/bugfix_rpa_dyson_residual.jl b/test/bugfix_rpa_dyson_residual.jl index 259ef26..b6bc847 100644 --- a/test/bugfix_rpa_dyson_residual.jl +++ b/test/bugfix_rpa_dyson_residual.jl @@ -45,4 +45,7 @@ using TensorBinding: get_Hamiltonian, rpa_from_bubble_diag, custom_mpo, _rpa_pai @test r.residual < 1e-3 && relerr(r, χx) < 1e-5 # the default return is vec(χ) as before; residual_warn = Inf skips the check @test (@test_logs rpa_from_bubble_diag(Π, V, fs, s; residual_warn=Inf)) isa MPS + # Π₀ = 0: χ = 0 with residual 0 (it was NaN), and no warning + r0 = @test_logs rpa_from_bubble_diag(0.0 * Π, V, fs, s; return_info=true) + @test r0.residual == 0 && norm(r0.chi) == 0 end diff --git a/test/linsolve_residual.jl b/test/linsolve_residual.jl new file mode 100644 index 0000000..c9c8153 --- /dev/null +++ b/test/linsolve_residual.jl @@ -0,0 +1,80 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Random, Test +using TensorBinding: get_Hamiltonian, add_onsite!, probe_state, get_correction_vector, + get_green_krylov, _linsolve_residual, _norm2_Ax, _RESIDUAL_MAX_ELEMENTS + +# The residual ‖(a0 + a1·A)x − b‖/‖b‖ behind the checks of get_green_krylov, the +# correction vectors and the RPA Dyson solves. From inner products its terms cancel to +# ~√eps of the largest, a floor that grows with ‖x‖ (as 1/η for a resolvent): a wrong x +# could read as residual 0, and the exact x as 0.04. The value is now an upper bound that +# never goes below that floor, and where the floor leaves the comparison with the +# threshold open the residual is evaluated exactly, from a direct sum. +@testset "linsolve residual: rounding floor, exact evaluation, size guard" begin + dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) + dvec(ψ, s) = vec(Array(prod(ψ), s...)) + tomps(v, s) = MPS(reshape(v, dim.(s)...), s; cutoff=1e-30) + + # A 64-site chain with a bound level below the band; ω on the level. + H = get_Hamiltonian("chain_1d", 1.0; L=6) + v = zeros(64); v[20] = -2.0 + add_onsite!(H, v; tol=1e-14) + s, Hd = H.sites, dmat(H.mpo, H.sites) + b = probe_state(H, 20); bv = dvec(b, s) + E0 = eigvals(Hermitian(Hd))[1] + rng = Xoshiro(3) + truth(x, z) = norm((z * I - Hd) * dvec(x, s) - bv) / norm(bv) + function perturbed(z, δ) # the exact solution plus δ‖x‖·(unit noise) + xv = (z * I - Hd) \ bv + p = randn(rng, ComplexF64, 64) + return tomps(xv + δ * norm(xv) * p / norm(p), s) + end + + # ⟨Ax|Ax⟩ one tensor at a time against ITensorMPS's contraction + y = perturbed(complex(0.3, 0.05), 0.1) + @test _norm2_Ax(H.mpo, y) ≈ real(inner(H.mpo, y, H.mpo, y)) rtol=1e-12 + + # Well above the floor (η = 0.05) the inner products give the dense residual. + z = complex(E0, 0.05) + x = perturbed(z, 1e-3) + r = _linsolve_residual(H.mpo, b, x, z, -1.0; threshold=0.02) + @test r.method === :inner && r.floor < 1e-5 + @test r.residual ≈ truth(x, z) rtol=1e-6 + + # η = 1e-6: the floor (≈ 0.1) is above rtol = 0.02. + z = complex(E0, 1e-6) + x = perturbed(z, 0.0) # the exact x: true residual ~1e-9 + r = _linsolve_residual(H.mpo, b, x, z, -1.0) + @test r.method === :inner && r.floor > 0.02 + @test r.residual >= r.floor # an upper bound, not noise below the floor + r = _linsolve_residual(H.mpo, b, x, z, -1.0; threshold=0.02) + @test r.method === :exact && r.residual < 1e-6 + for _ in 1:5 # wrong vectors, true residual ≈ 0.05–0.3 + x = perturbed(z, 5e-8) + r = _linsolve_residual(H.mpo, b, x, z, -1.0; threshold=0.02) + @test r.method === :exact + @test r.residual ≈ truth(x, z) rtol=1e-4 + @test r.residual > 0.02 + end + + # The vectorized system of get_green_krylov (a0 = 0), whose operator has a bond + # without a link index: the exact path still runs. + Hg = get_Hamiltonian("chain_1d", 1.0; L=4) + res = get_green_krylov(Hg, 2.3; η=0.1, return_info=true) + @test res.residual >= res.residual_floor > 0 + + # Above the size bound the check is skipped with one message, never a warning: the + # correction vector reports residual NaN and converged = false. + old = _RESIDUAL_MAX_ELEMENTS[] + try + _RESIDUAL_MAX_ELEMENTS[] = 10.0 + r = _linsolve_residual(H.mpo, b, x, z, -1.0; threshold=0.02) + @test r.method === :skipped && isnan(r.residual) + cv = @test_logs (:info, r"residual check was skipped") match_mode=:any get_correction_vector( + H, b, 0.3; eta=0.05) + @test isnan(cv.residual) && !cv.converged + G = @test_logs (:info, r"residual check was skipped") match_mode=:any get_green_krylov( + Hg, 2.3; η=0.1) + @test G isa MPO + finally + _RESIDUAL_MAX_ELEMENTS[] = old + end +end diff --git a/test/rpa_column.jl b/test/rpa_column.jl index 3e5b1da..9cd1261 100644 --- a/test/rpa_column.jl +++ b/test/rpa_column.jl @@ -38,6 +38,12 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, @test r.residual < 1e-3 end + # Π₀|j⟩ = 0: x = 0 with residual 0, and no warning (it was 0.89, "not converged") + r0 = @test_logs _rpa_column_solve(0.0 * Π, 0.3Uc * MPO(s, "Id"), s, bj; nsweeps=40, + maxdim=400, cutoff=1e-12, residual_warn=1e-2, + return_info=true) + @test r0.residual == 0 && norm(r0.chi) == 0 + # The Wynn series warns where it converges slowly (‖Tₙ‖/‖Tₙ₋₁‖ ≈ ρ(VΠ₀) > 0.9). @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=6) @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( diff --git a/test/runtests.jl b/test/runtests.jl index 301f45f..6fdab99 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -111,6 +111,7 @@ include("kbonacci.jl") include("kpm_moment_columns.jl") include("gpu_mps_ldos.jl") include("krylov_minres.jl") +include("linsolve_residual.jl") include("correction_vector.jl") include("rpa_column.jl") From a6d4d2f568ecc31ef6490e4fc7d3b2294fb4f426 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 01:09:47 +0200 Subject: [PATCH 09/13] get_green_krylov: size-aware default cutoff, warning on the absolute residual The cutoff of the sweeps is relative to the squared norm of vec(G), which grows as the number of sites D, so the fixed default 1e-8 weighed D times more next to one row of G and truncated away what sets that row apart: an impurity site 2.6 % off from 2^14 sites, and from 2^20 the end rows of a clean open chain returned as bulk rows (G_11 50 % off). The residual check added in this release could not see it: it was relative to the norm of vec(1) = sqrt(D), so one wrong row cost it 1/sqrt(D), 8e-4 at 2^20 sites against a threshold of 1e-2 (0.2.2 review, numerics 1). - cutoff defaults to nothing: 1e-8 up to 2^10 sites (unchanged), 1e-8 2^10/D beyond, never below 1e-14 (under which the expansion of the start truncates nothing and its memory grows without bound, seen at 2^36 sites). - The warning is on the absolute residual |G(z - H) - 1|_F, which bounds every row's, with residual_warn = 0.1; return_info adds residual_abs. Against dense inverses at 2^10 and 2^12 sites it was 1-8 times the relative error of the worst row. Beyond ~2^32 sites, where the rounding floor of the inner products nears the threshold, the residual is evaluated exactly. - The random part of the start keeps its absolute weight 0.1 (a tenth of one row at every size). Scaling it with sqrt(D), as the review suggested, was tried and made the chain ends bulk rows from 2^20 sites: it outweighs the rows it is there for. What the review saw (the part dropped by the truncated sum beyond ~2^33 sites) is fixed by a direct sum beyond 2^30 sites; the start is unchanged below. Measured at the default settings on open chains, clean and with an impurity, at four (omega, eta) in and outside the band, against analytic elements: chain ends, middle bond and impurity site to 4e-6 or better from 2^14 to 2^30 sites, no warning; to 1e-4 at 2^34-2^36 (0.3-2 % at a band edge, with the warning); chain ends lost at 2^40, with the warning (single rows are below double precision there). Scripts and logs: examples/nontracked/fixes_checks/green_krylov_sizeaware_cutoff*. The dynamics and RPA golden suites are unchanged (718 tests). New test at 2^20 sites; docstring: x0_mpo gets no basis expansion (numerics 5), the Hermiticity test of solver=:minres is size-blind (numerics 6). Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 37 +++++-- src/solvers/Krylov.jl | 175 ++++++++++++++++++++++-------- test/bugfix_green_krylov_size.jl | 50 +++++++++ test/bugfix_green_krylov_start.jl | 8 +- test/runtests.jl | 1 + 5 files changed, 214 insertions(+), 57 deletions(-) create mode 100644 test/bugfix_green_krylov_size.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e2ae0..5a85611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,13 +52,18 @@ bugs without breaking the API. ### Changed -- **`get_green_krylov` checks its residual**, on by default: the relative residual - ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the solve is computed after the sweeps, and a warning says - that G is not converged when it exceeds `residual_warn` (default `1e-2`). - `return_info=true` returns `(; G, residual, residual_floor, maxlinkdim)` instead of - the MPO. The check costs about one sweep or less; `residual_warn = Inf` skips it. On - the chains of the "random state" entry below and on 16×16 square lattices the residual - was 1–5 times the relative error of G. +- **`get_green_krylov` checks its residual**, on by default: after the sweeps the + residual ‖G(z − H) − 1‖_F is computed, and a warning says that G is not converged when + it exceeds `residual_warn` (default `0.1`). `return_info=true` returns + `(; G, residual, residual_abs, residual_floor, maxlinkdim)` instead of the MPO: + `residual_abs` is that residual and `residual` = `residual_abs`/√D the one relative + to ‖|I⟩⟩‖ = √D, for D sites. The warning is on the absolute one because it bounds the + residual of every row of G, where the relative one is the root mean square over the + D rows and cannot show a wrong row on a large system (8e-4 for a 2^20-site chain whose + two end rows are bulk rows, 50 % off). Against dense inverses at 2^10 and 2^12 sites + the absolute residual was 1–8 times the relative error of the worst row, so the + threshold flags rows that are off by a few % or more. The check costs about one sweep + or less; `residual_warn = Inf` skips it. - **The RPA Dyson solve checks its residual**, on by default: `rpa_from_bubble_diag` computes the relative residual ‖(I − Π₀V)χ − Π₀‖/‖Π₀‖ and warns above `residual_warn` (default `1e-2`); `return_info=true` returns `(; chi, residual, residual_floor, @@ -88,6 +93,24 @@ bugs without breaking the API. ### Changed results +- **`get_green_krylov`: the default cutoff scales with the system beyond 2^10 sites.** + `cutoff` now defaults to `nothing`: `1e-8` up to 2^10 sites, as before, and + `1e-8·2^10/D` beyond for D sites, never below `1e-14`. The cutoff of the sweeps is + relative to ‖|G⟩⟩‖², which grows as D, so a fixed `1e-8` weighed D times more next to + one row of G and truncated away what sets that row apart from the others: an impurity + site was 2.6 % off from 2^14 sites, and from 2^20 the end rows of a clean open chain + came back as bulk rows (G_11 50 % off), with a relative residual of 8e-4. With the + new default, open chains of 2^14–2^30 sites, clean and with an impurity, gave the + chain ends, the middle bond and the impurity site to 4e-6 or better, for ω in and + outside the band, in the same time. Beyond 2^30 sites the floor binds: the same + elements to 1e-4 at 2^34–2^36 (0.3–2 % at a band edge, with the warning), and from + about 2^38 sites a single row is below double precision next to ‖|G⟩⟩‖², so the chain + ends are lost again, with the warning: local quantities on such systems are for the + correction vectors. Results up to 2^10 sites are unchanged, and `cutoff = 1e-8` + restores the old behaviour at any size; `get_bubble_mpo` and the RPA drivers pass + their own `krylov_cutoff = 1e-8`, unchanged. The random part of the start (next + entry), which the truncated sum dropped beyond ~2^33 sites, is added by a direct sum + beyond 2^30. - **`get_green_krylov` starts from a random state** (keywords `start` and `rng`). The start was |I⟩⟩ with its bonds enlarged by a 2-vector global Krylov expansion, and the two-site sweeps keep the rank their start spans: on a short-ranged G they stopped on the diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index bcd5330..9335b5b 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -276,21 +276,62 @@ end # :identity start they stalled on a short-ranged G (the bulk G without the end # corrections of an open chain; ω outside the band in 2D), wrong by 1e-3–0.2 with no # sign of it. The random part fills every bond, the expansion adds the directions of A. +# +# The weight of the random part is absolute: the random MPS has norm 1, a row of G a norm +# of order 1, and |I⟩⟩ the norm √D (D the dimension of the site space). At 0.1 it is a +# tenth of one row at every size, below what sets a row apart from the others. Scaled up +# with √D (0.1·√D/64 was tried) it outweighs those rows: the ends of an open chain came +# back as bulk rows from 2^20 sites. It must survive the truncations, though: next to +# ‖|I⟩⟩‖² = D its weight is 0.01/D, above the default cutoff of the sweeps (1e-5/D beyond +# 2^10 sites), but the sum |I⟩⟩ + 0.1·r at cutoff 1e-14 drops it from about 2^33 sites +# (pure |I⟩⟩ at 2^36). Beyond 2^30 sites the sum is therefore a direct sum, which keeps +# it; up to 2^30 it is the truncated sum, as before. From about 2^39 sites 0.01/D is +# below the floor 1e-14 of the cutoff and the expansion drops it anyway: the start is +# then |I⟩⟩, the sweeps stall, and the residual check says so (see _green_krylov_cutoff). function _green_krylov_start(rhs::MPS, A::MPO, sites2::Vector{<:Index}, start::Symbol, - rng::AbstractRNG, cutoff::Real) + rng::AbstractRNG, cutoff::Real, D::Real) start === :identity && return ITensorMPS.expand(rhs, A; alg="global_krylov", krylovdim=2, cutoff=cutoff) - r = random_mps(rng, ComplexF64, sites2; linkdims=8) - return ITensorMPS.expand(+(rhs, 0.1 * r; cutoff=1e-14), A; alg="global_krylov", - krylovdim=8, cutoff=cutoff) + r = random_mps(rng, ComplexF64, sites2; linkdims=8) + x0 = D > 2.0^30 ? +(rhs, 0.1 * r; alg="directsum") : +(rhs, 0.1 * r; cutoff=1e-14) + return ITensorMPS.expand(x0, A; alg="global_krylov", krylovdim=8, cutoff=cutoff) end +# The default cutoff of get_green_krylov on a site space of dimension D: 1e-8 up to 2^10 +# sites, as before, 1e-8·2^10/D beyond, and never below 1e-14. A cutoff is relative to +# ‖|G⟩⟩‖² ∝ D, so a fixed one weighs D times more next to a single row of G as the system +# grows, and what sets that row apart from the others (a chain end, an impurity) is +# truncated away: at 1e-8 the end rows of a clean open chain came back as bulk rows from +# 2^20 sites (G_11 50 % off) and an impurity site was 2.6 % off from 2^14. Scaled with +# 1/D, the cutoff keeps the weight it has next to one row at 2^10 sites. +# +# The floor 1e-14 is that of double precision: below it the truncations of the start +# (ITensorMPS.expand) cut nothing, and its bond dimensions and memory grew without bound +# at 2^36 sites. It is reached at 2^30 sites; beyond, the cutoff weighs more and more +# next to one row again (a row is 1/D of ‖|G⟩⟩‖²: 1.5e-11 at 2^36, 9e-13 at 2^40), which +# the residual check reports. +# +# Measured at the default settings on open chains, clean and with one impurity (V = 2), +# at ω = 2.3, 0.5 (η = 0.1), −2.05 (η = 0.05) and 1.0 (η = 0.03), against analytic +# elements (examples/nontracked/fixes_checks/green_krylov_sizeaware_cutoff*.jl): +# 2^14–2^30 sites: chain ends, middle bond (both directions) and impurity site to 4e-6 +# or better; ‖G(z − H) − 1‖_F ≤ 4e-2; no warning. +# 2^34, 2^36: the same elements to 1e-4, except the impurity chain at the band edge +# (0.3–2 %, ‖G(z − H) − 1‖_F = 0.10 and 0.19, with the warning). +# 2^40: the chain ends are bulk rows (‖G(z − H) − 1‖_F = 0.9, with the warning). +# Against the dense inverse at 2^10 and 2^12 sites, ‖G(z − H) − 1‖_F was 1–8 times the +# relative error of the worst row. A looser 1e-8·2^13/D left rows 3 % off and diagonal +# elements 1 % off at the band edge; tighter ones (down to 1e-8·2^3/D) did not lower +# the worst rows reliably. +_green_krylov_cutoff(D::Real) = max(1e-8 * min(1.0, 1024 / Float64(D)), 1e-14) + + """ get_green_krylov(H_mpo::MPO, sites, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, start=:random, rng=Xoshiro(1), + cutoff=nothing, x0_mpo=nothing, start=:random, rng=Xoshiro(1), solver=:gmres, ishermitian=false, tol=1e-10, maxiter=600, - krylovdim=30, return_info=false, residual_warn=1e-2, + krylovdim=30, return_info=false, residual_warn=0.1, verbose=false) -> MPO Low-level: compute the retarded Green's function G(ω) = (ω + iη − H)⁻¹ for a @@ -306,13 +347,7 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; η::Real = 1e-2, nsweeps::Int = 12, maxdim::Int = 100, - # The default cutoff stays 1e-8 on purpose (0.2.2). It is relative to - # ‖|G⟩⟩‖², so it is size-blind and sets the accuracy floor of G: 1e-6–2e-4 - # on an open impurity chain of 2^10 sites, up to 5e-4 at 2^12, which the - # residual check reports. Pass cutoff = 1e-12 when G must be better - # (1e-11–1e-6 there, at up to 2.5× the time) and read the residual with - # return_info = true. A tighter default would move every result. - cutoff::Real = 1e-8, + cutoff::Union{Real,Nothing} = nothing, # _green_krylov_cutoff x0_mpo::Union{MPO,Nothing} = nothing, start::Symbol = :random, rng::AbstractRNG = Random.Xoshiro(1), @@ -322,12 +357,14 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; maxiter::Int = 600, krylovdim::Int = 30, return_info::Bool = false, - residual_warn::Real = 1e-2, + residual_warn::Real = 0.1, verbose::Bool = false) solver in (:gmres, :minres) || throw(ArgumentError("get_green_krylov: solver must be :gmres or :minres, got :$solver")) start in (:random, :identity) || throw(ArgumentError("get_green_krylov: start must be :random or :identity, got :$start")) + D = _site_space_dim(sites) + cutoff = something(cutoff, _green_krylov_cutoff(D)) N = length(sites) sites2 = siteinds("Qubit", 2N) z = ComplexF64(ω_phys + im * η) @@ -348,7 +385,7 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; ω_mpo = z * MPO(sites, "Id") - H_mpo A, a0, a1 = interleave_mpo(swapprime(ω_mpo, 0 => 1), sites2, 0), 0.0, 1.0 end - x0 = isnothing(x0_mpo) ? _green_krylov_start(rhs, A, sites2, start, rng, cutoff) : + x0 = isnothing(x0_mpo) ? _green_krylov_start(rhs, A, sites2, start, rng, cutoff, D) : _vec_mps_from_mpo(x0_mpo, sites2; cutoff=cutoff, maxdim=maxdim) verbose && println("Krylov GF: ω = $ω_phys + $(η)i (N=$N, maxdim=$maxdim, nsweeps=$nsweeps", @@ -375,29 +412,39 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; G = custom_mpo(sol, sites) (return_info || isfinite(residual_warn)) || return G - info = _linsolve_residual(A, rhs, sol, a0, a1; threshold = residual_warn) - res = info.residual + # ‖|I⟩⟩‖ = √D: the relative residual of the solve is the root mean square of the D row + # residuals ‖G_r(z − H) − e_r‖, so a wrong row costs it 1/√D and it cannot show one on + # a large system (a chain end returned as a bulk row: 8e-4 at 2^20 sites). The warning + # is on the absolute residual ‖G(z − H) − 1‖_F = √D × the relative one, which bounds + # the residual of every row. + nrhs = sqrt(D) + info = _linsolve_residual(A, rhs, sol, a0, a1; threshold = residual_warn / nrhs) + res, absres = info.residual, info.residual * nrhs if info.method === :skipped isfinite(residual_warn) && _residual_skipped_info("get_green_krylov", sol, A, "G is returned unchecked; residual_warn = Inf silences this.") else - verbose && println(" relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ = $res") - res <= residual_warn || - @warn "get_green_krylov: G is not converged: relative residual " * - "$(round(res; sigdigits=2)) > residual_warn = $residual_warn. Raise maxdim " * - "or nsweeps, or lower cutoff (maxlinkdim reached: $(maxlinkdim(sol)))." ω_phys η + verbose && println(" residual ‖G(z − H) − 1‖_F = $absres (relative to ‖1‖_F: $res)") + absres <= residual_warn || + @warn "get_green_krylov: G is not converged: ‖G(z − H) − 1‖_F = " * + "$(round(absres; sigdigits=2)) > residual_warn = $residual_warn" * + (info.floor * nrhs > residual_warn / 2 ? + " (rounding floor of this residual: $(round(info.floor * nrhs; sigdigits=2)))" : "") * + ". Some rows of G are off by about that much, relative to themselves: " * + "lower cutoff, or raise maxdim or nsweeps (cutoff = $cutoff, maxlinkdim " * + "reached: $(maxlinkdim(sol)))." ω_phys η end - return return_info ? (; G, residual = res, residual_floor = info.floor, - maxlinkdim = maxlinkdim(sol)) : G + return return_info ? (; G, residual = res, residual_abs = absres, + residual_floor = info.floor, maxlinkdim = maxlinkdim(sol)) : G end """ get_green_krylov(H::TBHamiltonian, ω_phys; η=1e-2, nsweeps=12, maxdim=100, - cutoff=1e-8, x0_mpo=nothing, start=:random, rng=Xoshiro(1), + cutoff=nothing, x0_mpo=nothing, start=:random, rng=Xoshiro(1), solver=:gmres, ishermitian=false, tol=1e-10, maxiter=600, - krylovdim=30, return_info=false, residual_warn=1e-2, + krylovdim=30, return_info=false, residual_warn=0.1, verbose=false) -> MPO Compute the retarded Green's function @@ -417,19 +464,39 @@ unscaled — no KPM Chebyshev expansion required. - `η` : Lorentzian broadening. Default `1e-2`. - `nsweeps` : Number of DMRG sweeps for the linear solver. Default `12`. - `maxdim` : Maximum bond dimension of the solution MPS. Default `100`. -- `cutoff` : SVD truncation cutoff, relative to ‖|G⟩⟩‖², which grows with the - system. Default `1e-8`. It sets the accuracy floor of G, which the - residual shows: on an open 2^10-site chain with one impurity the - default leaves 1e-6–2e-4, `1e-12` 1e-11–1e-6. +- `cutoff` : SVD truncation cutoff of the sweeps, relative to ‖|G⟩⟩‖², which + grows as the dimension D of the site space (the number of sites). + Default `nothing`: `1e-8` up to 2^10 sites and `1e-8·2^10/D` + beyond (never below `1e-14`), so that it weighs the same next to + one row of G at every size. A fixed `1e-8` (the default up to + v0.2.1, still available by passing it) truncates away what sets + a row apart from the others on a large system: the end rows of + a clean open chain came back as bulk rows from 2^20 sites. + The cutoff sets the accuracy of G row by row. With the default, + on open chains of 2^10–2^30 sites with one impurity, the chain + ends, the middle bond and the impurity site were right to 4e-6 + or better and the rows with the largest error were off by a few + 1e-3 of their norm (‖G(z − H) − 1‖_F up to 4e-2). Beyond 2^30 + sites the floor `1e-14` binds and rows lose accuracy again: + at 2^34–2^36 the same elements were right to 1e-4, or to + 0.3–2 % at a band edge, with the warning; from about 2^38 + sites a single row is below double precision next to + ‖|G⟩⟩‖², and the ends of a chain come back as bulk rows, with + the warning. For local quantities on such systems use the + correction vectors (`get_ldos_cv`), which solve for one row. - `x0_mpo` : Optional MPO initial guess for G(ω). When provided it is vectorized via `_vec_mps_from_mpo` and passed as `x0` to `linsolve`, replacing the start chosen by `start`. Typical use: pass a low-accuracy KPM Green's function to - warm-start the Krylov iteration. Default `nothing`. + warm-start the Krylov iteration. Default `nothing`. It is used + as given, without the basis expansion of the two starts below: + from a low-rank guess (a multiple of the identity) the sweeps + stall as they do from `:identity`. - `start` : The start of the sweeps when `x0_mpo` is not given. `:random` (default since v0.2.2): |I⟩⟩ + 0.1·(a random MPS of - bond dimension 8, drawn from `rng`), with its bonds enlarged by - an 8-vector global Krylov expansion (`ITensorMPS.expand`). + bond dimension 8 and norm 1, drawn from `rng`: a tenth of one + row of G at every size), with its bonds enlarged by an + 8-vector global Krylov expansion (`ITensorMPS.expand`). `:identity`: |I⟩⟩ with a 2-vector expansion, the start up to v0.2.1, which stalls on a short-ranged G (an open chain's end corrections, ω outside the band) and returns a G wrong by @@ -440,7 +507,9 @@ unscaled — no KPM Chebyshev expansion required. - `solver` : Local solver of the sweeps. `:gmres` (default): KrylovKit's GMRES, restarted every `krylovdim` products. `:minres`: MINRES, for Hermitian H only (an `ArgumentError` otherwise, from - ‖H − H†‖/‖H‖). Each local problem (z − H_eff)x = b is a complex + ‖H − H†‖/‖H‖ > 1e-6 in the Frobenius norm, which a non-Hermitian + term on a single site passes beyond ~2^40 sites; the residual + still shows a wrong G). Each local problem (z − H_eff)x = b is a complex shift of a Hermitian matrix, which MINRES solves by a three-term recurrence without restarts: the same G in fewer products, most at small η, where restarted GMRES slows down. MINRES uses @@ -452,18 +521,29 @@ unscaled — no KPM Chebyshev expansion required. residual). Default `1e-10`. - `maxiter` : Maximum Krylov iterations per site (GMRES restarts). Default `600`. - `krylovdim` : Krylov subspace dimension. Default `30`. -- `return_info` : Return `(; G, residual, residual_floor, maxlinkdim)` instead of - the MPO `G`. Default `false`. -- `residual_warn`: Warn when the relative residual ‖A|G⟩⟩ − |I⟩⟩‖/‖|I⟩⟩‖ of the - solve exceeds this value: G is then not converged (`maxdim`, - `nsweeps` or `cutoff` too coarse). The residual comes from inner - products, whose terms cancel: `residual` is an upper bound that - includes the rounding floor of that evaluation (`residual_floor`, - a few 1e-7 here), so a converged solve reads as its floor, not 0. - The check costs about one sweep or less, and is skipped with a +- `return_info` : Return `(; G, residual, residual_abs, residual_floor, maxlinkdim)` + instead of the MPO `G`: `residual_abs` = ‖G(z − H) − 1‖_F, and + `residual` = `residual_abs`/√D, the residual of the solve + relative to ‖|I⟩⟩‖ = √D. Default `false`. +- `residual_warn`: Warn when the residual ‖G(z − H) − 1‖_F exceeds this value: G is + then not converged (`cutoff`, `maxdim` or `nsweeps` too coarse). + This residual bounds that of every row, ‖G_r(z − H) − e_r‖, and + on open chains with an impurity it was 1–8 times the relative + error of the worst row of G: the default `0.1` flags rows that + are off by a few % or more (the bulk rows returned for the ends + of a long chain give 0.9), where the default settings leave up + to 4e-2. The relative residual cannot do that: it is the root + mean square over the D rows, 8e-4 for those wrong end rows at + 2^20 sites. The residual comes from inner products whose terms + cancel: it is an upper bound that includes the rounding floor + of that evaluation (`residual_floor`, a few 1e-7 relative, i.e. + times √D for the absolute one), so a converged solve reads as + its floor, not 0; beyond ~2^32 sites, where that floor nears + the threshold, the residual is evaluated exactly instead. The + check costs about one sweep or less, and is skipped with a message when its contraction would hold more than 2^27 numbers - (`residual` is then `NaN`). `Inf` skips its computation unless - `return_info=true`. Default `1e-2`. + (the residuals are then `NaN`). `Inf` skips its computation + unless `return_info=true`. Default `0.1`. - `verbose` : Print progress messages (with MINRES, also the number of local solves and products) and the residual. Default `false`. @@ -489,8 +569,7 @@ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; η::Real = 1e-2, nsweeps::Int = 12, maxdim::Int = 100, - # 1e-8 on purpose, an accuracy floor: see the MPO method above. - cutoff::Real = 1e-8, + cutoff::Union{Real,Nothing} = nothing, x0_mpo::Union{MPO,Nothing} = nothing, start::Symbol = :random, rng::AbstractRNG = Random.Xoshiro(1), @@ -500,7 +579,7 @@ function get_green_krylov(H::TBHamiltonian, ω_phys::Real; maxiter::Int = 600, krylovdim::Int = 30, return_info::Bool = false, - residual_warn::Real = 1e-2, + residual_warn::Real = 0.1, verbose::Bool = false) return get_green_krylov(H.mpo, H.sites, ω_phys; η, nsweeps, maxdim, cutoff, x0_mpo, start, rng, solver, diff --git a/test/bugfix_green_krylov_size.jl b/test/bugfix_green_krylov_size.jl new file mode 100644 index 0000000..07c2f96 --- /dev/null +++ b/test/bugfix_green_krylov_size.jl @@ -0,0 +1,50 @@ +using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Test +using TensorBinding: get_Hamiltonian, get_green_krylov, _green_krylov_cutoff + +# get_green_krylov on a large system. The cutoff of the sweeps is relative to ‖|G⟩⟩‖² ∝ D +# (D sites), so a fixed 1e-8 truncated away what sets one row of G apart from the others: +# from 2^20 sites the end rows of a clean open chain came back as bulk rows, G_11 50 % off. +# The residual check could not see it either: relative to ‖|I⟩⟩‖ = √D, one wrong row costs +# 1/√D (8e-4 here, under the threshold 1e-2). The default cutoff now scales as 1/D beyond +# 2^10 sites, and the warning is on the absolute residual ‖G(z − H) − 1‖_F, which bounds +# every row's. +@testset "get_green_krylov: size-aware cutoff, absolute residual" begin + bstate(s, p) = MPS(s, [string(b) for b in reverse(digits(p - 1; base=2, pad=length(s)))]) + el(G, s, i, j) = inner(bstate(s, i)', G, bstate(s, j)) + + @test _green_krylov_cutoff(2.0^6) == 1e-8 # unchanged up to 2^10 sites + @test _green_krylov_cutoff(2.0^10) == 1e-8 + @test _green_krylov_cutoff(2.0^20) == 1e-8 / 2^10 + + # A clean open chain of 2^20 sites, ω outside the band: analytic elements. + L = 20; N = 2^L + H = get_Hamiltonian("chain_1d", 1.0; L) + s = H.sites + m = N ÷ 2 # the bond m ↔ m+1 flips every bit + h = real(el(H.mpo, s, m, m + 1)) + ω, η = 2.3, 0.1 + z = ω + im * η + sq = sqrt(z - 2) * sqrt(z + 2) + gs = (z - sq) / 2 # end site of a semi-infinite chain + g0 = 1 / sq # bulk diagonal + g1 = g0 * (z - sq) / 2h # bulk nearest neighbour + + # the default call: chain ends and middle bond right, and no warning + r = @test_logs get_green_krylov(H, ω; η, return_info=true) + @test el(r.G, s, 1, 1) ≈ gs rtol=1e-6 + @test el(r.G, s, N, N) ≈ gs rtol=1e-6 + @test el(r.G, s, m, m) ≈ g0 rtol=1e-6 + @test el(r.G, s, m, m + 1) ≈ g1 rtol=1e-6 + @test el(r.G, s, m + 1, m) ≈ g1 rtol=1e-6 + @test r.residual_abs ≈ r.residual * sqrt(N) + @test r.residual_abs < 0.01 + + # the fixed cutoff of v0.2.1: bulk rows at the chain ends. The relative residual is + # under the old threshold, the absolute one is not, and the call warns. + old = @test_logs (:warn, r"not converged") match_mode=:any get_green_krylov( + H, ω; η, cutoff=1e-8, return_info=true) + @test abs(el(old.G, s, 1, 1) - gs) / abs(gs) > 0.1 + @test el(old.G, s, 1, 1) ≈ g0 rtol=1e-3 + @test old.residual < 1e-2 + @test old.residual_abs > 0.1 +end diff --git a/test/bugfix_green_krylov_start.jl b/test/bugfix_green_krylov_start.jl index e433044..e35e32b 100644 --- a/test/bugfix_green_krylov_start.jl +++ b/test/bugfix_green_krylov_start.jl @@ -6,7 +6,8 @@ using TensorBinding: get_Hamiltonian, add_onsite!, get_green_krylov # the band) of a chain with an impurity, the default settings returned the G without the # impurity's correction, 9 % off, with nothing to show it. The default start is now # |I⟩⟩ + 0.1·(random MPS, χ 8) with an 8-vector expansion, and the residual of the solve is -# checked: above `residual_warn` (1e-2) a warning says that G is not converged. +# checked: when ‖G(z − H) − 1‖_F exceeds `residual_warn` (0.1) a warning says that G is not +# converged (bugfix_green_krylov_size.jl has the large-system side of the check). @testset "get_green_krylov: random start and residual check" begin dmat(M, s) = (D = prod(dim.(s)); reshape(Array(prod(M), prime.(s)..., s...), D, D)) relerr(G, Gx, s) = norm(dmat(G, s) - Gx) / norm(Gx) @@ -24,6 +25,7 @@ using TensorBinding: get_Hamiltonian, add_onsite!, get_green_krylov H, ω; η, start=:identity, return_info=true) @test relerr(old.G, Gx, s) > 1e-2 @test old.residual > 1e-2 + @test old.residual_abs ≈ 8 * old.residual # ‖|I⟩⟩‖ = √64 # the new default start, with GMRES and with MINRES for solver in (:gmres, :minres) new = get_green_krylov(H, ω; η, solver, return_info=true) @@ -32,11 +34,13 @@ using TensorBinding: get_Hamiltonian, add_onsite!, get_green_krylov end end - # The random start does not depend on the global RNG; `rng` changes it. + # The random start does not depend on the global RNG; `rng` changes it (another start, + # so another G within the accuracy of the solve). Random.seed!(1); G1 = get_green_krylov(H, 0.5; η=0.1) Random.seed!(2); G2 = get_green_krylov(H, 0.5; η=0.1) @test dmat(G1, s) ≈ dmat(G2, s) rtol=1e-12 G3 = get_green_krylov(H, 0.5; η=0.1, rng=Random.Xoshiro(7)) + @test dmat(G3, s) != dmat(G1, s) @test dmat(G3, s) ≈ inv((0.5 + 0.1im) * I - Hd) rtol=1e-6 # residual_warn = Inf: no check, no warning, the plain MPO diff --git a/test/runtests.jl b/test/runtests.jl index 6fdab99..6cd7b18 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -151,6 +151,7 @@ include("bugfix_qtci_profiles.jl") include("bugfix_large_L_cutoffs.jl") include("bugfix_onsite_aux.jl") include("bugfix_green_krylov_start.jl") +include("bugfix_green_krylov_size.jl") include("bugfix_rpa_dyson_residual.jl") # The 0.1 names and keywords renamed in 0.2: deprecated aliases with a one-time warning. From 01fa7dcddebbacfc5bcbc184ef2d0bc56c9a075c Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 01:10:10 +0200 Subject: [PATCH 10/13] Correction vectors: converged flag of refine_bound_state, scan results, stray keywords From the 0.2.2 review (numerics 3; API F4, F8, F9, F10, F14), before the names become permanent: - refine_bound_state: `converged` was the residual flag of the last linear solve only, so a start in a continuum returned converged = true on an energy still moving by 4e-3 per step. It now also asks that the last move of the energy (returned as energy_step) be within the eta before the last, which holds once the level is resolved at that eta. A stray `eta` keyword, which overrode every step of eta_steps while the history reported the scheduled values, is an ArgumentError, and so is an empty eta_steps (it was a BoundsError). The docstring no longer claims that the history shows an unresolved pair of levels: it does not; the result is their mean energy and summed weight. - get_ldos_cv, get_exciton_ldos_cv: the per-frequency bond dimensions are `maxlinkdims`, the package's name for such a vector; a single frequency omega::Real returns scalars (it returned 1-element vectors, where the KPM siblings return a scalar); `x0`, which replaced the warm start at every frequency, is an ArgumentError. - Docstrings: the conversion to the KPM units holds for the default kernel (1/H.scale with :hodc); why maxiter is 30 here and 600 in get_green_krylov; mpsexciton lives in core/Utils.jl. Tests: the continuum case, the refused keywords, the scalar methods, the sum rules of the CV and KPM LDOS on one grid (units), and get_ldos_cv on a spinful chain. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 13 +++-- docs/src/api/solvers.md | 3 +- src/solvers/CorrectionVector.jl | 85 +++++++++++++++++++++++++-------- test/correction_vector.jl | 44 +++++++++++++++-- 4 files changed, 117 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a85611..096bab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,12 +30,17 @@ bugs without breaking the API. - `get_ldos_cv(H, X, ω_vals; eta, …)` and `get_exciton_ldos_cv(H, X, ω_vals; eta, …)`: the LDOS of a site or of the exciton contact probe |X, X⟩ along a frequency grid, each point warm-started from the previous one, as `(; ldos, G, residual, converged, - maxlinkdim)` in physical units (`ldos = −Im G/π`, one state integrates to 1; - `get_ldos_online(…) · π/H.scale` is in the same units). `get_ldos_cv` takes the - auxiliary projections of `get_ldos_online`. + maxlinkdims)` with one entry per frequency, in physical units (`ldos = −Im G/π`, + one state integrates to 1; with the default KPM kernel, + `get_ldos_online(…) · π/H.scale` is in the same units). A single frequency + `ω::Real` returns scalars (`maxlinkdim` for the bond dimension). `get_ldos_cv` + takes the auxiliary projections of `get_ldos_online`. - `refine_bound_state(H, b, ω0; eta_steps, …)`: the energy and probe weight of an isolated level by Rayleigh-quotient iteration on the correction vector, to ~η² of - the last step (default `eta_steps = (0.05, 0.01, 1e-3, 1e-4)`). + the last step (default `eta_steps = (0.05, 0.01, 1e-3, 1e-4)`). `converged` asks for + a converged last solve and an energy that has stopped moving (`energy_step`, the + last move, within the η before the last): in a continuum it is `false`. Two levels + closer than the last η are not told apart (their mean energy, their summed weight). - **`get_rpa_susceptibility_column(H, MPOV, ω, j; …)`**: one column x = χ|j⟩ of the RPA susceptibility, from (I − Π₀V)x = Π₀|j⟩ on the L sites of Π₀ instead of the whole χ on the 2L-site vectorized register; `j` is a basis state (a position for position-only diff --git a/docs/src/api/solvers.md b/docs/src/api/solvers.md index 725a637..1864e26 100644 --- a/docs/src/api/solvers.md +++ b/docs/src/api/solvers.md @@ -51,7 +51,8 @@ energy and weight of an isolated level. When to use which route: and impurity lattices, ω outside the band). The correction-vector LDOS is per unit physical energy (one state integrates to 1); -`get_ldos_online(…) · π/H.scale` is in the same units. +with the default KPM kernel, `get_ldos_online(…) · π/H.scale` is in the same units +(`1/H.scale` with `kernel = :hodc`, which is normalised to 1). ```@autodocs Modules = [TensorBinding] diff --git a/src/solvers/CorrectionVector.jl b/src/solvers/CorrectionVector.jl index 56f2b68..33c17e6 100644 --- a/src/solvers/CorrectionVector.jl +++ b/src/solvers/CorrectionVector.jl @@ -12,7 +12,7 @@ # refine_bound_state # Depends on: solvers/Krylov.jl (_minres_updater, _linsolve_residual, # _hermiticity_defect), core/AuxDOF.jl (probe_state, _aux_projection, -# _probe_sectors), physics/TwoParticle.jl (mpsexciton), core/TBSystem.jl. +# _probe_sectors), core/Utils.jl (mpsexciton), core/TBSystem.jl. # ============================================================ @@ -154,7 +154,9 @@ a path from a larger η converges, provided its first step converges. - `maxdim`, `cutoff`: truncation of `x`. The default cutoff `1e-12` is tighter than elsewhere: the bound-state part of `x` grows as 1/η and the rest is small next to it. - `tol`, `krylovdim`, `maxiter`: of the local solver (GMRES restarts `maxiter`; MINRES - takes `maxiter·krylovdim` products). + takes `maxiter·krylovdim` products). `maxiter` is 30 here, 900 products per local + solve, where `get_green_krylov` has 600: a local solve that stops short is taken up + by the next sweep, and the residual shows what is left. Returns `x` and `G`, the residual and its rounding floor, `converged`, the bond dimension of `x`, and the numbers of sweeps and expansion rounds. @@ -203,7 +205,11 @@ end # ============================================================ # Correction vectors of one probe along ω_vals, each warm-started from the previous x. -function _cv_scan(Hm::MPO, b::MPS, ω_vals, eta::Real; kwargs...) +function _cv_scan(Hm::MPO, b::MPS, ω_vals, eta::Real, fname::String; kwargs...) + # a splatted x0 would replace the warm start at every frequency + haskey(kwargs, :x0) && throw(ArgumentError( + "$fname: `x0` is not a keyword of the scan, which warm-starts each frequency " * + "from the one before; use get_correction_vector for a single start")) n = length(ω_vals) G = zeros(ComplexF64, n) res = zeros(Float64, n) @@ -225,23 +231,33 @@ function _cv_ldos_result(Gs, ress, χs, rtol, fname) bad > 0 && @warn "$fname: $bad of $(length(res)) frequencies not converged " * "(residual > rtol = $rtol): raise maxdim or nsweeps, or use eta_path" return (; ldos = -imag.(G) ./ π, G, residual = res, converged = res .<= rtol, - maxlinkdim = χ) + maxlinkdims = χ) end +# The result of a one-frequency scan as scalars. +_cv_ldos_scalar(r) = (; ldos = r.ldos[1], G = r.G[1], residual = r.residual[1], + converged = r.converged[1], maxlinkdim = r.maxlinkdims[1]) + """ - get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, , - ) + get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector; eta=1e-2, + , ) + -> (; ldos, G, residual, converged, maxlinkdims) + get_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; …) -> (; ldos, G, residual, converged, maxlinkdim) The local density of states at position `X` (1-indexed in `1:H.N`) from correction vectors: `ldos[i] = −Im⟨X|(ω_i + iη − H)⁻¹|X⟩/π`, a Lorentzian of width `eta` around each level, per unit **physical** energy (one state integrates to 1). The KPM functions return the density per unit rescaled energy times `1/π` (Solvers page, -Conventions): `get_ldos_online(…) · π/H.scale` is in the units of `ldos` here. +Conventions): with their default kernel, `get_ldos_online(…) · π/H.scale` is in the +units of `ldos` here (with `kernel = :hodc`, which is normalised to 1, the factor is +`1/H.scale`). The frequencies are solved in the given order, each correction vector warm-starting the next (only the first is seeded), so a fine grid is cheaper per point than a coarse -one. Each point keeps its residual; a warning counts the points above `rtol`. +one. Each point keeps its residual; a warning counts the points above `rtol`. The +result holds one entry per frequency (`maxlinkdims`: the bond dimension of each +correction vector); a single frequency `ω::Real` returns scalars. On an exciton Hamiltonian (two-particle register) the probe is the contact state |X, X⟩, as in `get_exciton_ldos_cv`. Auxiliary degrees of freedom are summed or @@ -249,7 +265,7 @@ selected with the keywords of `get_ldos_online` (`spin_proj`, `proj_s`, `nambu_p `proj_nambu`, `layer_proj`, `proj_layer`, `sublat_proj`, `proj_sl`), one scan per sector. Every other keyword goes to `get_correction_vector`. """ -function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; +function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector{<:Real}; eta::Real = 1e-2, rtol::Real = 0.02, nambu_proj::Bool = false, @@ -266,28 +282,35 @@ function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; layer_proj, proj_layer, sublat_proj, proj_sl) Gs = Vector{ComplexF64}[]; ress = Vector{Float64}[]; χs = Vector{Int}[] for σ in _probe_sectors(aux) - G, res, χ = _cv_scan(H.mpo, probe_state(H, X, σ), ω_vals, eta; rtol, kwargs...) + G, res, χ = _cv_scan(H.mpo, probe_state(H, X, σ), ω_vals, eta, "get_ldos_cv"; + rtol, kwargs...) push!(Gs, G); push!(ress, res); push!(χs, χ) end return _cv_ldos_result(Gs, ress, χs, rtol, "get_ldos_cv") end +get_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; kwargs...) = + _cv_ldos_scalar(get_ldos_cv(H, X, [ω]; kwargs...)) + """ - get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, + get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector; eta=1e-2, ) + -> (; ldos, G, residual, converged, maxlinkdims) + get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; …) -> (; ldos, G, residual, converged, maxlinkdim) The exciton LDOS at the contact probe |X, X⟩ (electron and hole on position `X`, 1-indexed in `1:H.N`; `mpsexciton`, the probe of `get_exciton_ldos`) from correction vectors, in the units and with the scan of `get_ldos_cv`: per unit physical energy, -`ldos = −Im G/π`; `get_exciton_ldos(…) · π/H.scale` is in the same units. +`ldos = −Im G/π`; with its default kernel, `get_exciton_ldos(…) · π/H.scale` is in the +same units. A single frequency `ω::Real` returns scalars. The bound-exciton peaks of a contact probe are where correction vectors pay most: `x` stays at a small bond dimension down to small η, where KPM-MPS needs moments beyond its truncation floor. In the two-particle continuum `x` spreads and its bond dimension grows; check `converged`. """ -function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; +function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector{<:Real}; eta::Real = 1e-2, rtol::Real = 0.02, kwargs...) @@ -295,10 +318,14 @@ function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; length(H.sites) == 2 * H.L || throw(ArgumentError("get_exciton_ldos_cv: H is not an exciton Hamiltonian " * "(2L interleaved electron-hole sites); use get_ldos_cv")) - G, res, χ = _cv_scan(H.mpo, mpsexciton(X, H.sites), ω_vals, eta; rtol, kwargs...) + G, res, χ = _cv_scan(H.mpo, mpsexciton(X, H.sites), ω_vals, eta, "get_exciton_ldos_cv"; + rtol, kwargs...) return _cv_ldos_result([G], [res], [χ], rtol, "get_exciton_ldos_cv") end +get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; kwargs...) = + _cv_ldos_scalar(get_exciton_ldos_cv(H, X, [ω]; kwargs...)) + # ============================================================ # 3. Isolated peaks: energy and weight @@ -307,7 +334,7 @@ end """ refine_bound_state(H, b::MPS, ω0; eta_steps=(0.05, 0.01, 1e-3, 1e-4), x0=nothing, ) - -> (; energy, weight, x, residual, converged, history) + -> (; energy, weight, x, residual, energy_step, converged, history) The energy `E` and the probe weight `w = |⟨ψ|b⟩|²/⟨b|b⟩` of an isolated eigenstate `ψ` near `ω0`, by Rayleigh-quotient iteration on the correction vector: for each η of @@ -319,14 +346,30 @@ peak, which a spectrum at finite η only broadens. The other levels enter at ord accuracy: with the default 1e-4 and Δ of order one, E to ~1e-8. Start `ω0` within the peak's width at the first η (a coarse scan with `get_ldos_cv` -at η ≈ 0.1–0.2 finds it). A level that is not isolated at the last η (another state -within it) gives a mixed quotient; the history shows it as an energy that keeps -moving. `history` holds `(; eta, omega, energy, weight, residual)` per step. +at η ≈ 0.1–0.2 finds it). `history` holds `(; eta, omega, energy, weight, residual)` +per step, and `energy_step` is the last move of the energy, `|E − ω|` of the last +step. + +`converged` is `true` when the last correction vector is converged (its residual) and +the energy has stopped moving: `energy_step` is at most the η of the step before the +last, which holds once the level is resolved at that η. In a continuum the quotient +keeps drifting from step to step and `converged` is `false`. Two levels closer than +the last η are not told apart, and nothing shows it: the history looks like that of +one level, `energy` is their weighted mean and `weight` the sum of their weights. +Lower the last η below the splitting you need to resolve. + +The broadenings are those of `eta_steps` (`eta` is not a keyword here); every other +keyword goes to `get_correction_vector`. """ function refine_bound_state(H::Union{TBHamiltonian,MPO}, b::MPS, ω0::Real; eta_steps = (0.05, 0.01, 1e-3, 1e-4), x0::Union{MPS,Nothing} = nothing, kwargs...) + isempty(eta_steps) && + throw(ArgumentError("refine_bound_state: eta_steps is empty")) + # a splatted eta would win over the one of each step, at every step + haskey(kwargs, :eta) && throw(ArgumentError( + "refine_bound_state: `eta` is not a keyword here; the broadenings are eta_steps")) Hm = _cv_mpo(H) nb = real(inner(b, b)) ω = Float64(ω0) @@ -343,6 +386,10 @@ function refine_bound_state(H::Union{TBHamiltonian,MPO}, b::MPS, ω0::Real; ω = E end last = history[end] + # The energy has stopped moving when its last step is within the η before the last + # (the only η for a single step): for an isolated level the step is ~η²/Δ. + energy_step = abs(last.energy - last.omega) + η_before = history[max(end - 1, 1)].eta return (; energy = last.energy, weight = last.weight, x, residual = last.residual, - converged = r.converged, history) + energy_step, converged = r.converged && energy_step <= η_before, history) end diff --git a/test/correction_vector.jl b/test/correction_vector.jl index 334d16e..e83d688 100644 --- a/test/correction_vector.jl +++ b/test/correction_vector.jl @@ -1,7 +1,7 @@ using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Test -using TensorBinding: get_Hamiltonian, add_onsite!, exciton_hamiltonian, probe_state, - mpsexciton, get_correction_vector, get_ldos_cv, get_exciton_ldos_cv, - refine_bound_state +using TensorBinding: get_Hamiltonian, add_onsite!, add_spin!, exciton_hamiltonian, + probe_state, mpsexciton, get_correction_vector, get_ldos_cv, + get_exciton_ldos_cv, get_ldos_online, refine_bound_state # Correction vectors x = (ω + iη − H)⁻¹|b⟩ against dense resolvents: a site of a chain, the # contact probe of an exciton, the residual check, and the Rayleigh-quotient refinement @@ -31,11 +31,18 @@ using TensorBinding: get_Hamiltonian, add_onsite!, exciton_hamiltonian, probe_st r = get_correction_vector(H, b, -2.8; eta=1e-3, eta_path=(0.1, 0.01)) @test r.G ≈ gref(Hd, bv, -2.8 + 1e-3im) rtol=1e-6 - # the LDOS scan: −Im G/π per unit physical energy + # the LDOS scan: −Im G/π per unit physical energy, one entry per frequency ωs = range(-3.0, 3.0; length=7) res = get_ldos_cv(H, 20, ωs; eta=0.05) @test all(res.converged) @test res.ldos ≈ [-imag(gref(Hd, bv, ω + 0.05im)) / π for ω in ωs] rtol=1e-6 + @test res.maxlinkdims isa Vector{Int} && length(res.maxlinkdims) == 7 + # a single frequency returns scalars + one = get_ldos_cv(H, 20, 0.3; eta=0.05) + @test one.ldos isa Float64 && one.converged isa Bool && one.maxlinkdim isa Int + @test one.ldos ≈ -imag(gref(Hd, bv, 0.3 + 0.05im)) / π rtol=1e-6 + # the scan warm-starts each frequency itself: x0 is refused, not silently reused + @test_throws ArgumentError get_ldos_cv(H, 20, ωs; eta=0.05, x0=b) # a forced failure (maxdim 2) is flagged, and the scan warns bad = get_correction_vector(H, b, 0.3; eta=0.05, maxdim=2, seed_maxdim=2, rounds=1) @@ -49,6 +56,34 @@ using TensorBinding: get_Hamiltonian, add_onsite!, exciton_hamiltonian, probe_st rb = refine_bound_state(H, b, -2.7) @test rb.energy ≈ F.values[k] atol=1e-8 @test rb.weight ≈ abs2(dot(F.vectors[:, k], bv)) rtol=1e-6 + @test rb.converged && rb.energy_step < 1e-5 + # in the continuum the quotient keeps moving, and `converged` says so although the + # last linear solve is converged + rc = refine_bound_state(H, b, 0.3) + @test !rc.converged && rc.residual < 0.02 + @test rc.energy_step > 1e-3 + # the broadenings are eta_steps: a stray `eta` would override every step + @test_throws ArgumentError refine_bound_state(H, b, -2.7; eta=1e-2) + @test_throws ArgumentError refine_bound_state(H, b, -2.7; eta_steps=()) + + # Units: per unit physical energy, one state integrates to 1; the KPM LDOS times + # π/H.scale is in the same units (16-site chain, both sum rules on one grid). + H4 = get_Hamiltonian("chain_1d", 1.0; L=4) + ωg = range(-6.0, 6.0; length=121) + dω = step(ωg) + cv = get_ldos_cv(H4, 5, ωg; eta=0.3) + @test all(cv.converged) + @test sum(cv.ldos) * dω ≈ 1 atol=0.06 # Lorentzian tails beyond ±6 + kpm = get_ldos_online(H4, 60, 5, ωg) + @test sum(kpm) * dω * π / H4.scale ≈ 1 atol=0.02 + + # Auxiliary sites: on a spinful chain the default sums the two spin sectors, and a + # projection selects one. + Hs = get_Hamiltonian("chain_1d", 1.0; L=4) + add_spin!(Hs) + ref = get_ldos_cv(H4, 5, 0.4; eta=0.2).ldos + @test get_ldos_cv(Hs, 5, 0.4; eta=0.2).ldos ≈ 2ref rtol=1e-6 + @test get_ldos_cv(Hs, 5, 0.4; eta=0.2, spin_proj=true, proj_s=1).ldos ≈ ref rtol=1e-6 # Exciton: contact probe |X, X⟩ on a 16-site chain (4 qubits per particle, 8 # interleaved), attractive contact U = 2: a bound exciton below the continuum. @@ -61,6 +96,7 @@ using TensorBinding: get_Hamiltonian, add_onsite!, exciton_hamiltonian, probe_st re = get_exciton_ldos_cv(He, X, ωe; eta=0.05) @test all(re.converged) @test re.ldos ≈ [-imag(gref(Hed, bev, ω + 0.05im)) / π for ω in ωe] rtol=1e-6 + @test get_exciton_ldos_cv(He, X, ωe[2]; eta=0.05).ldos ≈ re.ldos[2] rtol=1e-6 # get_ldos_cv on an exciton Hamiltonian uses the same contact probe @test get_ldos_cv(He, X, ωe; eta=0.05).ldos ≈ re.ldos rtol=1e-8 @test_throws ArgumentError get_exciton_ldos_cv(H, 3, [0.0]) From e41090d96049b16b18da820a3fe92e3279297f47 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 01:18:15 +0200 Subject: [PATCH 11/13] RPA: Krylov-bubble start and residual keywords, column operator and names, Wynn estimate From the 0.2.2 review (API F2, F3, F5, F10, F11): - get_bubble_mpo, get_rpa_susceptibility and get_rpa_susceptibility_wynn take krylov_start and krylov_residual_warn and pass them to get_green_krylov. Every driver on the Krylov bubble had changed results with the new start and gained the residual check of that solve, with no keyword to restore the first or silence the second. krylov_cutoff also accepts nothing (the size-aware default of get_green_krylov); its default stays 1e-8. - get_rpa_susceptibility_column builds the operator at min(rpa_cutoff, 1e-14), as a direct sum: at the solve cutoff it was off by sqrt(cutoff), which set the accuracy of the column. On a 256-site chain up to U = 0.999 U_c: chi_jj to 1e-7 or better, the column to 1e-6 (examples/nontracked/fixes_checks/rpa_column_accuracy.jl). The new function takes the ASCII names `eta` and `fermi` (the Greek names of the bubble are accepted too, not both), checks the column index before computing the bubble, and its docstring says that the bubble is recomputed at each call. - Wynn series: the estimate sqrt(|T_K|/|T_{K-2}|) of the spectral radius is from below (1-6 % low at K = 6, 25-40 % at K = 2), so K_max = 2 and 3 could not warn at all. The thresholds are 0.85 for K_max >= 4 and 0.6 below, and the message calls it a lower bound. The returned values are unchanged. Tests: a non-symmetric V, so that a column is told from a row (with the diagonal V of the old test a transposed solve passed); j as an MPS, return_info, the ASCII keywords, K_max = 1 and 2, the two Krylov-bubble keywords. The column tolerance is 1e-9 where it was 1e-5. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 33 ++++++++---- src/physics/rpa/bubble.jl | 15 ++++-- src/physics/rpa/dyson.jl | 109 +++++++++++++++++++++++++------------- test/rpa_column.jl | 45 ++++++++++++++-- 4 files changed, 148 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 096bab1..e570c59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,15 +45,21 @@ bugs without breaking the API. susceptibility, from (I − Π₀V)x = Π₀|j⟩ on the L sites of Π₀ instead of the whole χ on the 2L-site vectorized register; `j` is a basis state (a position for position-only sites) or any MPS. The bubble keywords and defaults are those of - `get_rpa_susceptibility`, whose column it returns; the solve defaults are tighter - (`rpa_nsweeps = 40`, `rpa_cutoff = 1e-12`), which costs little on L sites. On a - 256-site half-filled chain up to U = 0.999 U_c (condition number 10³) it gave χ_jj to - 1e-7–2e-6 and the column to 3e-6 in about a second, against minutes for the whole χ. + `get_rpa_susceptibility`, whose column it returns, with the broadening and the Fermi + energy also under their ASCII names `eta` and `fermi`; the solve defaults are tighter + (`rpa_nsweeps = 40`, `rpa_cutoff = 1e-12`), which costs little on L sites, and Π₀V is + built at `min(rpa_cutoff, 1e-14)`. On a 256-site half-filled chain up to + U = 0.999 U_c (condition number 10³) the Dyson solve gave χ_jj to 1e-7 or better and + the column to 1e-6 in 1–4 s, against minutes for the whole χ. The bubble is computed + at each call, as in `get_rpa_susceptibility`. - **The Wynn-accelerated RPA warns near the instability**: the three Wynn drivers - estimate the spectral radius ρ of VΠ₀ from ‖Tₙ‖/‖Tₙ₋₁‖ of the Neumann terms and warn - when it exceeds 0.9. There the series converges slowly or diverges, and the default - `K_max = 6` was 1–90 % off at ρ ≈ 1 on a half-filled chain (with ρ > 1 from U ≈ 0.6 U_c - at finite ω and η). The returned values are unchanged. + estimate the spectral radius ρ of VΠ₀ from (‖T_K‖/‖T_{K−2}‖)^½ of the last Neumann + terms and warn when the series converges slowly or diverges (ρ ≳ 0.9), where the + default `K_max = 6` was 1–90 % off at ρ ≈ 1 on a half-filled chain (with ρ > 1 from + U ≈ 0.6 U_c at finite ω and η). The estimate is from below (1–6 % low at K = 6, + 25–40 % at K = 2), so the threshold is 0.85 for `K_max ≥ 4` and 0.6 for 2 and 3; + `K_max < 2` has nothing to estimate from, and no check. The returned values are + unchanged. ### Changed @@ -113,7 +119,8 @@ bugs without breaking the API. ends are lost again, with the warning: local quantities on such systems are for the correction vectors. Results up to 2^10 sites are unchanged, and `cutoff = 1e-8` restores the old behaviour at any size; `get_bubble_mpo` and the RPA drivers pass - their own `krylov_cutoff = 1e-8`, unchanged. The random part of the start (next + their own `krylov_cutoff = 1e-8`, unchanged (`krylov_cutoff = nothing` selects the + new default, on the doubled register of D² states). The random part of the start (next entry), which the truncated sum dropped beyond ~2^33 sites, is added by a direct sum beyond 2^30. - **`get_green_krylov` starts from a random state** (keywords `start` and `rng`). The @@ -130,7 +137,13 @@ bugs without breaking the API. so results do not depend on the global RNG. The first sweep runs at the bond dimension of the start (up to ~100), which costs the most when G has a small one: on these chains a solve takes up to 30× longer (30 s instead of 1 s for a clean 2^8-site chain - at η = 0.03). `start=:identity` restores the old start; `x0_mpo` is unchanged. Regenerated golden cases: `krylov_green_raw`, `krylov_green_tbhamiltonian` + at η = 0.03). `start=:identity` restores the old start; `x0_mpo` is unchanged. The + bubble with `GF_method = :krylov` (`get_bubble_mpo` and every RPA driver built on it) + solves for its Green's function this way, so its results moved too and its solves + cost more; `krylov_start = :identity` restores the old start there, and + `krylov_residual_warn` (default `0.1`, `Inf` to skip) is the threshold of the + residual check of that solve. Regenerated golden cases: `krylov_green_raw`, + `krylov_green_tbhamiltonian` and `krylov_green_ishermitian_flag` of `test/data/dynamics_golden.jl` (moved by ~1e-14: those small chains had converged), and the five Krylov cases of `test/data/rpa_golden.jl` (`bubble_cplx_krylov` and the four `*_fwd_krylov_mcweeny_tol` diff --git a/src/physics/rpa/bubble.jl b/src/physics/rpa/bubble.jl index 4b1bbc9..5d43fb7 100644 --- a/src/physics/rpa/bubble.jl +++ b/src/physics/rpa/bubble.jl @@ -111,7 +111,13 @@ Compute the non-interacting polarization bubble Π₀(ω) on `H1.sites`. - `η` : Lorentzian broadening for the GF. Default `1e-3`. - `krylov_nsweeps` : DMRG sweeps for Krylov solver. Default `12`. - `krylov_maxdim` : Max bond dim for Krylov solver. Default `100`. -- `krylov_cutoff` : SVD cutoff for Krylov solver. Default `1e-8`. +- `krylov_cutoff` : SVD cutoff for Krylov solver. Default `1e-8`, at every size; + `nothing` takes the size-aware default of `get_green_krylov` (the solve is on the + doubled register, of dimension D² for D sites). +- `krylov_start` : start of the Krylov solve, `start` of `get_green_krylov`. Default + `:random` (since v0.2.2); `:identity` is the start up to v0.2.1. +- `krylov_residual_warn` : `residual_warn` of `get_green_krylov`, the threshold of the + warning on the residual of G_eff. Default `0.1`; `Inf` skips the check and its cost. - `verbose` : Print progress. Default `false`. """ function get_bubble_mpo(H1::TBHamiltonian, H2::TBHamiltonian, ω::Real; @@ -128,7 +134,9 @@ function get_bubble_mpo(H1::TBHamiltonian, H2::TBHamiltonian, ω::Real; η::Real = 1e-3, krylov_nsweeps::Int = 12, krylov_maxdim::Int = 100, - krylov_cutoff::Real = 1e-8, + krylov_cutoff::Union{Real,Nothing} = 1e-8, + krylov_start::Symbol = :random, + krylov_residual_warn::Real = 0.1, verbose::Bool = false) L1 = H1.L; L2 = H2.L @@ -188,7 +196,8 @@ function get_bubble_mpo(H1::TBHamiltonian, H2::TBHamiltonian, ω::Real; GF_mpo = get_green_krylov(Heff, sites_combined, ω; η=η, nsweeps=krylov_nsweeps, maxdim=krylov_maxdim, cutoff=krylov_cutoff, - verbose=verbose) + start=krylov_start, + residual_warn=krylov_residual_warn, verbose=verbose) else error("Unknown GF_method: $GF_method. Choose :kpm or :krylov") end diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index 4288a50..946592e 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -15,12 +15,13 @@ # drivers are thin wrappers over one series kernel, _rpa_wynn_series. # Split from the former physics/RPA_tk.jl. # -# Entry points: get_rpa_susceptibility, get_rpa_susceptibility_wynn, -# rpa_wynn_from_bubbles, rpa_from_bubble_diag, get_magnon_bubble, -# get_magnon_susceptibility, get_magnon_susceptibility_wynn. +# Entry points: get_rpa_susceptibility, get_rpa_susceptibility_column, +# get_rpa_susceptibility_wynn, rpa_wynn_from_bubbles, rpa_from_bubble_diag, +# get_magnon_bubble, get_magnon_susceptibility, get_magnon_susceptibility_wynn. # Depends on: core/MPOTools.jl, core/TBSystem.jl, core/AuxDOF.jl, -# solvers/Krylov.jl (_vec_mps_from_mpo), physics/rpa/bubble.jl, -# physics/qft/conjugation.jl (see the source map in src/TensorBinding.jl). +# core/Utils.jl (_basis_state_mps, custom_mpo), solvers/Krylov.jl (_vec_mps_from_mpo, +# _linsolve_residual), physics/rpa/bubble.jl, physics/qft/conjugation.jl (see the +# source map in src/TensorBinding.jl). # ============================================================ # 1. Dyson solve @@ -174,12 +175,15 @@ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; η::Real = 1e-3, krylov_nsweeps::Int = 12, krylov_maxdim::Int = 100, - krylov_cutoff::Real = 1e-8, + krylov_cutoff::Union{Real,Nothing} = 1e-8, + krylov_start::Symbol = :random, + krylov_residual_warn::Real = 0.1, verbose::Bool = false) bubble_kw = (; ϵF, P_method, GF_method, Ncheb, maxdim, cutoff, purify_method, purify_maxdim, purify_maxiters, purify_tol, - η, krylov_nsweeps, krylov_maxdim, krylov_cutoff, verbose) + η, krylov_nsweeps, krylov_maxdim, krylov_cutoff, krylov_start, + krylov_residual_warn, verbose) if mode == :charge Π = get_bubble_mpo(H, H, ω; bubble_kw...) @@ -209,12 +213,18 @@ end const _RPA_BUBBLE_DEFAULTS = (; ϵF = 0.0, P_method = :purification, GF_method = :kpm, Ncheb = 150, maxdim = 200, cutoff = 1e-8, purify_method = :mcweeny, purify_maxdim = 40, purify_maxiters = 30, purify_tol = 1e-5, η = 1e-3, krylov_nsweeps = 12, - krylov_maxdim = 100, krylov_cutoff = 1e-8, verbose = false) + krylov_maxdim = 100, krylov_cutoff = 1e-8, krylov_start = :random, + krylov_residual_warn = 0.1, verbose = false) # One column of the Dyson solution: (I − Π₀V)x = Π₀|b⟩ on the L sites of Π₀, from x = Π₀|b⟩. +# The operator and the right-hand side are built at min(cutoff, 1e-14), and I − Π₀V as a +# direct sum (the default MPO `+` has a floor of ~√eps): at the solve cutoff the operator +# was off by ~√cutoff, which set the accuracy of the column (4e-7 at 1e-12, κ = 10³). function _rpa_column_solve(Π::MPO, MPOV::MPO, sites, b::MPS; nsweeps::Int, maxdim::Int, cutoff::Real, residual_warn::Real, return_info::Bool) - A = MPO(sites, "Id") - apply(Π, MPOV; maxdim=maxdim, cutoff=cutoff) + ΠV = apply(Π, MPOV; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) + A = length(sites) == 1 ? MPO(sites, "Id") - ΠV : + +(MPO(sites, "Id"), -1 * ΠV; alg="directsum") rhs = apply(Π, b; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) # Π₀|b⟩ = 0: x = 0, and a residual relative to ‖Π₀|b⟩‖ is undefined. iszero(norm(rhs)) && @@ -240,9 +250,9 @@ end """ get_rpa_susceptibility_column(H::TBHamiltonian, MPOV, ω, j; mode=:charge, - rpa_nsweeps=40, rpa_maxdim=400, rpa_cutoff=1e-12, - rpa_residual_warn=1e-2, return_info=false, - ) -> MPS + eta=1e-3, fermi=0.0, rpa_nsweeps=40, rpa_maxdim=400, + rpa_cutoff=1e-12, rpa_residual_warn=1e-2, + return_info=false, ) -> MPS One column of the RPA susceptibility χ = (I − Π₀V)⁻¹Π₀: the L-site MPS x = χ|j⟩, with χ_ij = ⟨i|x⟩, from the Dyson equation (I − Π₀V)x = Π₀|j⟩ on the sites of Π₀. `j` is @@ -250,27 +260,52 @@ the 1-indexed basis state of those sites (`_basis_state_mps(j − 1, sites)`: th position `j` when they are position qubits only), or any MPS on them (then x = χ|j⟩). Where one site's response is enough (χ_jj, a local probe, one column of a real-space -response) this is much cheaper than `get_rpa_susceptibility`, which solves for the -whole χ on the 2L-site vectorized register, and it holds its accuracy close to the -instability: on a 256-site half-filled chain up to U = 0.999 U_c (condition number -10³), χ_jj to 1e-7–2e-6 and the column to 3e-6 in about a second, against minutes for -the whole χ. The tighter defaults (`rpa_nsweeps = 40`, `rpa_cutoff = 1e-12`) cost -little on L sites. +response) the Dyson solve is much cheaper than that of `get_rpa_susceptibility`, which +solves for the whole χ on the 2L-site vectorized register, and it holds its accuracy +close to the instability: on a 256-site half-filled chain up to U = 0.999 U_c +(condition number 10³), χ_jj to 1e-7 or better and the column to 1e-6 in 1–4 s, +against minutes for the whole χ. The tighter defaults (`rpa_nsweeps = 40`, +`rpa_cutoff = 1e-12`) cost little on L sites. The bubble Π₀(ω) is computed at each +call, as in `get_rpa_susceptibility`, and usually costs more than the column solve: +several columns at one ω pay for it each time. The bubble is that of `get_rpa_susceptibility` with the same keywords and defaults -(`mode`, `ϵF`, `P_method`, `GF_method`, `Ncheb`, `maxdim`, `cutoff`, `η`, …), so the -result is column `j` of its χ. The residual check and `return_info` (returning -`(; chi, residual, maxlinkdim)`) are those of `rpa_from_bubble_diag`. +(`mode`, `P_method`, `GF_method`, `Ncheb`, `maxdim`, `cutoff`, …), so the result is +column `j` of its χ. The broadening and the Fermi energy are `eta` and `fermi` here +(the `η` and `ϵF` of `get_rpa_susceptibility` and `get_bubble_mpo`, which are accepted +too). The residual check and `return_info` (returning `(; chi, residual, +residual_floor, maxlinkdim)`) are those of `rpa_from_bubble_diag`, with Π₀V built at +`min(rpa_cutoff, 1e-14)`. """ -function get_rpa_susceptibility_column(H::TBHamiltonian, MPOV::MPO, ω::Real, j; +function get_rpa_susceptibility_column(H::TBHamiltonian, MPOV::MPO, ω::Real, + j::Union{Integer,MPS}; mode::Symbol = :charge, + eta::Union{Real,Nothing} = nothing, + fermi::Union{Real,Nothing} = nothing, rpa_nsweeps::Int = 40, rpa_maxdim::Int = 400, rpa_cutoff::Real = 1e-12, rpa_residual_warn::Real = 1e-2, return_info::Bool = false, kwargs...) - bubble_kw = merge(_RPA_BUBBLE_DEFAULTS, kwargs) + mode in (:charge, :magnetic) || + error("get_rpa_susceptibility_column: unknown mode=$mode. Choose :charge or :magnetic") + # `eta` and `fermi` are the ASCII names of the bubble keywords η and ϵF + (eta === nothing || !haskey(kwargs, :η)) || throw(ArgumentError( + "get_rpa_susceptibility_column: pass `eta` or `η`, not both")) + (fermi === nothing || !haskey(kwargs, :ϵF)) || throw(ArgumentError( + "get_rpa_susceptibility_column: pass `fermi` or `ϵF`, not both")) + # the column index, before any bubble work (Π₀ lives on H.sites without the spin + # index for :magnetic) + if j isa Integer + n_out = mode == :magnetic && H.spin_s !== nothing ? + round(Int, _site_space_dim(H.sites) / dim(H.spin_s)) : + round(Int, _site_space_dim(H.sites)) + 1 <= j <= n_out || throw(BoundsError(1:n_out, j)) + end + bubble_kw = merge(_RPA_BUBBLE_DEFAULTS, values(kwargs), + eta === nothing ? (;) : (; η = eta), + fermi === nothing ? (;) : (; ϵF = fermi)) if mode == :charge Π = get_bubble_mpo(H, H, ω; bubble_kw...) out_sites = H.sites @@ -281,14 +316,8 @@ function get_rpa_susceptibility_column(H::TBHamiltonian, MPOV::MPO, ω::Real, j; H_dn = _project_spin_sector(H, 2) Π = get_bubble_mpo(H_up, H_dn, ω; bubble_kw...) out_sites = H_up.sites - else - error("get_rpa_susceptibility_column: unknown mode=$mode. Choose :charge or :magnetic") - end - b = j isa MPS ? j : begin - D = prod(dim.(out_sites)) - 1 <= j <= D || throw(BoundsError(1:D, j)) - _basis_state_mps(j - 1, out_sites) end + b = j isa MPS ? j : _basis_state_mps(Int(j) - 1, out_sites) return _rpa_column_solve(Π, MPOV, out_sites, b; nsweeps=rpa_nsweeps, maxdim=rpa_maxdim, cutoff=rpa_cutoff, residual_warn=rpa_residual_warn, return_info) @@ -372,10 +401,14 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In spect_terms[n+1, :] = -imag.(get_spect_k(term)) tnorm[n+1] = norm(term) end - # ‖Tₙ‖/‖Tₙ₋₁‖ tends to the spectral radius ρ of VΠ₀; over the last two steps: + # (‖T_K‖/‖T_{K−2}‖)^½ tends to the spectral radius ρ of VΠ₀ from below: a Frobenius + # norm weighs every eigenvalue of VΠ₀, so at finite K the ratio is a power mean + # of them. On half-filled chains of 2^5–2^9 sites it read 1–6 % low at K = 6 and + # 25–40 % low at K = 2, hence the two thresholds (ρ ≳ 0.9 in both cases). With + # K_max < 2 there is no ratio to take, and no check. if K_max >= 2 && tnorm[K_max-1] > 0 ρ = sqrt(tnorm[K_max+1] / tnorm[K_max-1]) - ρ > 0.9 && push!(slow, (i, ρ)) + ρ > (K_max >= 4 ? 0.85 : 0.6) && push!(slow, (i, ρ)) end # Partial sums (Wynn input) @@ -398,8 +431,9 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In # with K_max = 6 the estimate was 1–90 % off at ρ ≈ 1 on a half-filled chain. if !isempty(slow) ρmax = maximum(last, slow) - @warn "RPA Wynn series: ‖Tₙ‖/‖Tₙ₋₁‖ ≈ $(round(ρmax; sigdigits=3)) > 0.9 at " * - "$(length(slow)) of $nω frequencies (the spectral radius of VΠ₀): the Neumann " * + @warn "RPA Wynn series: the spectral radius of VΠ₀ is at least " * + "$(round(ρmax; sigdigits=3)) at $(length(slow)) of $nω frequencies (an " * + "estimate from below, from ‖T_K‖/‖T_{K−2}‖ at K = $K_max): the Neumann " * "series converges slowly or diverges there, and the Wynn estimates with " * "K_max = $K_max can be off by tens of %. Use a larger K_max or the Dyson " * "solve (get_rpa_susceptibility, get_rpa_susceptibility_column)." @@ -491,7 +525,9 @@ function get_rpa_susceptibility_wynn(H::TBHamiltonian, MPOV::MPO, η::Real = 1e-3, krylov_nsweeps::Int = 12, krylov_maxdim::Int = 100, - krylov_cutoff::Real = 1e-8, + krylov_cutoff::Union{Real,Nothing} = 1e-8, + krylov_start::Symbol = :random, + krylov_residual_warn::Real = 0.1, verbose::Bool = false) mode ∈ (:charge, :magnetic) || @@ -501,7 +537,8 @@ function get_rpa_susceptibility_wynn(H::TBHamiltonian, MPOV::MPO, bubble_kw = (; ϵF, P_method, GF_method, Ncheb, maxdim, cutoff, purify_method, purify_maxdim, purify_maxiters, purify_tol, - η, krylov_nsweeps, krylov_maxdim, krylov_cutoff, verbose) + η, krylov_nsweeps, krylov_maxdim, krylov_cutoff, krylov_start, + krylov_residual_warn, verbose) # For :magnetic, project spin sectors once before the ω loop H_up = mode == :magnetic ? _project_spin_sector(H, 1) : nothing diff --git a/test/rpa_column.jl b/test/rpa_column.jl index 9cd1261..9e8ff11 100644 --- a/test/rpa_column.jl +++ b/test/rpa_column.jl @@ -34,7 +34,7 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, col = χx * dvec(bj, s) r = @test_logs _rpa_column_solve(Π, V, s, bj; nsweeps=40, maxdim=400, cutoff=1e-12, residual_warn=1e-2, return_info=true) - @test norm(dvec(r.chi, s) - col) / norm(col) < 1e-5 + @test norm(dvec(r.chi, s) - col) / norm(col) < 1e-9 @test r.residual < 1e-3 end @@ -44,10 +44,16 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, return_info=true) @test r0.residual == 0 && norm(r0.chi) == 0 - # The Wynn series warns where it converges slowly (‖Tₙ‖/‖Tₙ₋₁‖ ≈ ρ(VΠ₀) > 0.9). + # The Wynn series warns where it converges slowly: (‖T_K‖/‖T_{K−2}‖)^½ estimates + # ρ(VΠ₀) from below, 1–6 % low at K = 6 and 25–40 % low at K = 2, with a threshold for + # each. K_max < 2 has no ratio to take, and no check. @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=6) @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( [Π], 0.999Uc * MPO(s, "Id"); K_max=6) + @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=2) + @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( + [Π], 0.999Uc * MPO(s, "Id"); K_max=2) + @test_logs rpa_wynn_from_bubbles([Π], 0.999Uc * MPO(s, "Id"); K_max=1) # Through the bubble: column j of get_rpa_susceptibility's χ, on a 4-site chain with # the exact occupied-state projector cached (so the bubble itself is exact). @@ -58,13 +64,42 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, sc = Hc.sites kw = (; GF_method=:krylov, η=0.2, krylov_nsweeps=10, krylov_maxdim=64, krylov_cutoff=1e-12, cutoff=1e-12, maxdim=64) - V = +(0.5 * MPO(sc, "Id"), 0.2 * MPO(sc, ["Z", "Id"]); cutoff=1e-14) + # A non-symmetric V, so that χ ≠ χᵀ and a column is told from a row (with a diagonal V + # and the symmetric Π₀ of a real chain, χ is symmetric and a transposed solve passes). + V = mpo_of(0.5I + 0.2 * [0 1 0 0; 0 0 0 0; 0.3 0 0 1; 0 0 0 0], sc) χ = dmat(custom_mpo(get_rpa_susceptibility(Hc, V, 0.3; kw..., rpa_nsweeps=6, rpa_maxdim=64, rpa_cutoff=1e-12), sc), sc) + @test norm(χ - transpose(χ)) / norm(χ) > 0.01 for jj in (1, 3) - x = get_rpa_susceptibility_column(Hc, V, 0.3, jj; kw...) - @test dvec(x, sc) ≈ χ * dvec(_basis_state_mps(jj - 1, sc), sc) rtol=1e-8 + ej = dvec(_basis_state_mps(jj - 1, sc), sc) + x = get_rpa_susceptibility_column(Hc, V, 0.3, jj; kw...) + @test dvec(x, sc) ≈ χ * ej rtol=1e-8 + @test !isapprox(dvec(x, sc), transpose(χ) * ej; rtol=1e-3) end + # `j` as an MPS, return_info, and the ASCII names of the broadening and Fermi energy + b3 = _basis_state_mps(2, sc) + r3 = get_rpa_susceptibility_column(Hc, V, 0.3, b3; kw..., return_info=true) + @test dvec(r3.chi, sc) ≈ χ * dvec(b3, sc) rtol=1e-8 + @test r3.residual < 1e-3 && r3.residual >= r3.residual_floor + kwa = Base.structdiff(kw, (; η=0)) + xa = get_rpa_susceptibility_column(Hc, V, 0.3, 3; kwa..., eta=0.2, fermi=0.0) + @test dvec(xa, sc) ≈ χ * dvec(b3, sc) rtol=1e-8 + @test_throws ArgumentError get_rpa_susceptibility_column(Hc, V, 0.3, 3; kw..., eta=0.2) + @test_throws ArgumentError get_rpa_susceptibility_column(Hc, V, 0.3, 3; kw..., fermi=0.0, + ϵF=0.0) @test_throws BoundsError get_rpa_susceptibility_column(Hc, V, 0.3, 5; kw...) + @test_throws BoundsError get_rpa_susceptibility_column(Hc, V, 0.3, 0; kw...) @test_throws ErrorException get_rpa_susceptibility_column(Hc, V, 0.3, 1; mode=:spin) + + # The Krylov bubble takes the start and the residual threshold of get_green_krylov: + # the start of v0.2.1 is reachable, and the check can be silenced. + kwk = (; GF_method=:krylov, η=0.2, krylov_nsweeps=2, krylov_maxdim=4, cutoff=1e-12, + maxdim=64) + @test_logs (:warn, r"get_green_krylov: G is not converged") match_mode=:any get_bubble_mpo( + Hc, Hc, 0.3; kwk...) + Π1 = @test_logs get_bubble_mpo(Hc, Hc, 0.3; kwk..., krylov_residual_warn=Inf) + Π2 = @test_logs get_bubble_mpo(Hc, Hc, 0.3; kwk..., krylov_residual_warn=Inf, + krylov_start=:identity) + @test Π1 isa MPO && Π2 isa MPO + @test dmat(Π1, sc) != dmat(Π2, sc) end From e9ed479ae84c38f75b5f8da62c4631f9c35749e7 Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 02:16:14 +0200 Subject: [PATCH 12/13] Follow-up to the review fixes: level test of refine_bound_state, floor scale, Wynn thresholds A second review, of the four fix commits (e633d20..e41090d), found that three of them did not do what they claimed. Report and scripts: examples/nontracked/fixes_checks/review_022/rereview/. - refine_bound_state: for x = (w + i eta - H)^-1 b the Rayleigh quotient gives E - w = eta Re G / Im G exactly, about eta in a continuum whatever w, so the new rule "the last energy step is within the eta before the last" passed at any start: converged = true at 2^20 sites in the middle of the band. `converged` now also asks for a stationary weight over the last step: it is independent of eta on a level and proportional to eta in a continuum, and the measured change must be within a tenth of the continuum's. A single step, or two equal eta at the end, is never reported as converged. - Rounding floor of the residual: its scale used the rms singular value of A, which averages a strong local term away (one bond of 1e3-1e5 on a chain: rounding up to 2e4 eps S against the 32 assumed; an upper bound 15 % low; a converged vector reported at 2.5e-2). The scale is now the larger of the rms value and a power-iteration estimate of the norm of A (6-8 products on a bond-16 MPS, fixed seed), and a negative sum of the inner products beyond the floor raises the floor to it. The reviewer's cases now all resolve (b1_after_fix.log). - The exact evaluation had a hard bond limit of 2000 and no memory bound (13 GB for a 60-site register near it). It is now bounded by the same 2^27 numbers as the inner-product check, and the docstrings state the condition. - Wynn warning: the thresholds were calibrated at K = 2 and 6 on 2^5-2^9 sites; on 1024-4096 sites the estimate reads lower, and K_max = 4 warned only from rho = 0.99. One threshold per K_max (0.55, 0.70, 0.77, 0.81, then 0.83), from the large-system ratios, with the calibration range stated. - Column solve: a bubble that annihilates |j> leaves a right-hand side of ~1e-15, not exactly zero, and still gave residual 0.2 and a warning. The test is now relative to |Pi| |b|. A Bool column index is refused, and a spinless H in the magnetic channel is refused as such before the index is checked. - get_ldos_cv, get_exciton_ldos_cv take any collection of frequencies again (the AbstractVector annotation refused tuples); the scalar method stays. The warning of get_green_krylov names the krylov_* keywords of the RPA drivers. Tests that pin these: refine_bound_state in a continuum of 2^16 sites; the exact path on the vectorized operator (asserted, where the old test only said so); the norm estimate on one strong site among 2^20; a default get_green_krylov at 2^36 sites (direct-sum start, exact residual); the column at 128 sites, where the old operator gave 4e-7 (the 32-site case passed on the old code); a bubble that annihilates |j>; K_max = 2..5; krylov_cutoff = nothing. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 34 ++++++++------ src/physics/rpa/dyson.jl | 42 +++++++++++------ src/solvers/CorrectionVector.jl | 52 +++++++++++++++------ src/solvers/Krylov.jl | 79 ++++++++++++++++++++++---------- test/bugfix_green_krylov_size.jl | 17 +++++++ test/correction_vector.jl | 19 +++++++- test/linsolve_residual.jl | 27 ++++++++++- test/rpa_column.jl | 50 ++++++++++++++++---- 8 files changed, 242 insertions(+), 78 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e570c59..43a5e7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,9 +38,12 @@ bugs without breaking the API. - `refine_bound_state(H, b, ω0; eta_steps, …)`: the energy and probe weight of an isolated level by Rayleigh-quotient iteration on the correction vector, to ~η² of the last step (default `eta_steps = (0.05, 0.01, 1e-3, 1e-4)`). `converged` asks for - a converged last solve and an energy that has stopped moving (`energy_step`, the - last move, within the η before the last): in a continuum it is `false`. Two levels - closer than the last η are not told apart (their mean energy, their summed weight). + a converged last solve and for the last two steps to agree that there is a level: + a weight that does not change with η (in a continuum it is proportional to η) and + an energy that has stopped moving (`energy_step`, the last move, within the η + before the last). A single step cannot show that and is never reported as + converged. Two levels closer than the last η are not told apart (their mean energy, + their summed weight). - **`get_rpa_susceptibility_column(H, MPOV, ω, j; …)`**: one column x = χ|j⟩ of the RPA susceptibility, from (I − Π₀V)x = Π₀|j⟩ on the L sites of Π₀ instead of the whole χ on the 2L-site vectorized register; `j` is a basis state (a position for position-only @@ -56,10 +59,12 @@ bugs without breaking the API. estimate the spectral radius ρ of VΠ₀ from (‖T_K‖/‖T_{K−2}‖)^½ of the last Neumann terms and warn when the series converges slowly or diverges (ρ ≳ 0.9), where the default `K_max = 6` was 1–90 % off at ρ ≈ 1 on a half-filled chain (with ρ > 1 from - U ≈ 0.6 U_c at finite ω and η). The estimate is from below (1–6 % low at K = 6, - 25–40 % at K = 2), so the threshold is 0.85 for `K_max ≥ 4` and 0.6 for 2 and 3; - `K_max < 2` has nothing to estimate from, and no check. The returned values are - unchanged. + U ≈ 0.6 U_c at finite ω and η). The estimate is from below, further at small K and + on large systems (8 % low at K = 6 and 39 % at K = 2 on half-filled chains of + 1024–4096 sites, 1 % and 25 % at 32 sites), so the threshold depends on `K_max`: + 0.55, 0.70, 0.77, 0.81 for 2 to 5 and 0.83 from 6, calibrated on those chains + (another band structure or dimension shifts it). `K_max < 2` has nothing to estimate + from, and no check. The returned values are unchanged. ### Changed @@ -90,13 +95,16 @@ bugs without breaking the API. - **How these residuals are evaluated** (and those of the correction vectors): from inner products, with ⟨Ax|Ax⟩ contracted one tensor at a time. Their terms cancel, so the value is an upper bound that includes the rounding floor of the evaluation, about - 1e-7·‖A‖‖x‖/‖b‖ (returned as `residual_floor`): a converged solve reads as its floor, - not as 0, and rounding noise cannot pass for a small residual. Where the floor leaves - the comparison with the threshold open (a correction vector at η ≲ 1e-5 on a level, a + 1e-7·‖A‖‖x‖/‖b‖ (returned as `residual_floor`; ‖A‖ from a few power iterations, so + that a strong local term counts): a converged solve reads as its floor, not as 0, and + rounding noise cannot pass for a small residual. Where the floor leaves the + comparison with the threshold open (a correction vector at η ≲ 1e-5 on a level, a constant energy offset of 10³ in H) the residual is evaluated exactly, from the direct - sum of its terms. A check whose contraction would hold more than 2^27 numbers (bond - dimensions χ of the solution and w of the operator with χ²w² > 6.7e7) is skipped with - one `@info` and reports `residual = NaN`. + sum of its terms, as long as that sum fits in memory (bond dimensions of the solution + up to about 100–300); beyond, the bound with its floor is what is reported. A check + whose contraction would hold more than 2^27 numbers (bond dimensions χ of the + solution and w of the operator with χ²w² > 6.7e7) is skipped with one `@info` and + reports `residual = NaN`. - **Dependencies:** ITensorMPS 0.3.26 to 0.3.44 (was 0.3.16 to 0.3.44). The MINRES solver and the correction vectors pass their own local solver to ITensorMPS's `linsolve`, and the suite was run at 0.3.26 and 0.3.44 only. The cap below 0.3.45 diff --git a/src/physics/rpa/dyson.jl b/src/physics/rpa/dyson.jl index 946592e..c3e16df 100644 --- a/src/physics/rpa/dyson.jl +++ b/src/physics/rpa/dyson.jl @@ -151,8 +151,8 @@ All `get_bubble_mpo` keyword arguments are accepted and forwarded. instability use a smaller one (see `rpa_from_bubble_diag`). - `rpa_residual_warn` : warn when the relative residual of the Dyson solve exceeds this value (`Inf`: no check). Default `1e-2`. -- `return_info` : return `(; chi, residual, maxlinkdim)` of the Dyson solve instead of - vec(χ). Default `false`. +- `return_info` : return `(; chi, residual, residual_floor, maxlinkdim)` of the Dyson + solve instead of vec(χ). Default `false`. """ function get_rpa_susceptibility(H::TBHamiltonian, MPOV::MPO, ω::Real; mode::Symbol = :charge, @@ -223,13 +223,17 @@ const _RPA_BUBBLE_DEFAULTS = (; ϵF = 0.0, P_method = :purification, GF_method = function _rpa_column_solve(Π::MPO, MPOV::MPO, sites, b::MPS; nsweeps::Int, maxdim::Int, cutoff::Real, residual_warn::Real, return_info::Bool) ΠV = apply(Π, MPOV; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) + # (one site has no bond to sum over; linsolve refuses it below, with its own message) A = length(sites) == 1 ? MPO(sites, "Id") - ΠV : +(MPO(sites, "Id"), -1 * ΠV; alg="directsum") rhs = apply(Π, b; maxdim=maxdim, cutoff=min(cutoff, 1e-14)) - # Π₀|b⟩ = 0: x = 0, and a residual relative to ‖Π₀|b⟩‖ is undefined. - iszero(norm(rhs)) && - return return_info ? (; chi = rhs, residual = 0.0, residual_floor = 0.0, - maxlinkdim = maxlinkdim(rhs)) : rhs + # Π₀|b⟩ = 0, to rounding (a Π₀ that annihilates |b⟩ leaves ~1e-15·‖Π₀‖‖b‖): x = 0, and + # a residual relative to ‖Π₀|b⟩‖ is undefined. + if norm(rhs) <= 100 * eps(Float64) * norm(Π) * norm(b) + x0 = 0.0 * rhs + return return_info ? (; chi = x0, residual = 0.0, residual_floor = 0.0, + maxlinkdim = maxlinkdim(x0)) : x0 + end x = ITensorMPS.linsolve(A, rhs, deepcopy(rhs); nsweeps, maxdim, cutoff) (return_info || isfinite(residual_warn)) || return x info = _linsolve_residual(A, rhs, x, 0.0, 1.0; threshold = residual_warn) @@ -295,12 +299,15 @@ function get_rpa_susceptibility_column(H::TBHamiltonian, MPOV::MPO, ω::Real, "get_rpa_susceptibility_column: pass `eta` or `η`, not both")) (fermi === nothing || !haskey(kwargs, :ϵF)) || throw(ArgumentError( "get_rpa_susceptibility_column: pass `fermi` or `ϵF`, not both")) + mode == :magnetic && H.spin_s === nothing && + error("get_rpa_susceptibility_column: mode=:magnetic requires a spinful H (call add_spin!(H) first)") # the column index, before any bubble work (Π₀ lives on H.sites without the spin # index for :magnetic) if j isa Integer - n_out = mode == :magnetic && H.spin_s !== nothing ? - round(Int, _site_space_dim(H.sites) / dim(H.spin_s)) : - round(Int, _site_space_dim(H.sites)) + j isa Bool && throw(ArgumentError( + "get_rpa_susceptibility_column: the column index j is an integer or an MPS, got a Bool")) + n_out = mode == :magnetic ? round(Int, _site_space_dim(H.sites) / dim(H.spin_s)) : + round(Int, _site_space_dim(H.sites)) 1 <= j <= n_out || throw(BoundsError(1:n_out, j)) end bubble_kw = merge(_RPA_BUBBLE_DEFAULTS, values(kwargs), @@ -359,6 +366,16 @@ function wynn_epsilon(s::AbstractVector{<:Number}) end +# Thresholds of the Wynn warning on the estimate (‖T_K‖/‖T_{K−2}‖)^½ of ρ(VΠ₀), for +# K_max = 2, 3, 4, 5 and ≥ 6: 0.9 times the ratio estimate/ρ, so that the warning starts +# at ρ ≈ 0.9. The ratios, 0.61, 0.77, 0.86, 0.90 and 0.92, are those of the dense +# Lindhard bubble of half-filled chains and rings of 1024–4096 sites at ω = 0, η = 0.03, +# V = U·1 (they fall with the size: 0.75, 0.90, 0.96, 0.98, 0.99 at 32 sites, where the +# warning then starts at ρ ≈ 0.75–0.85). Another band structure, η or dimension shifts +# them; only this family was measured +# (examples/nontracked/fixes_checks/review_022/rereview/d1_wynn_estimate.*). +const _WYNN_RHO_THRESHOLDS = (0.55, 0.70, 0.77, 0.81, 0.83) + # The Wynn-accelerated Neumann series behind rpa_wynn_from_bubbles, # get_rpa_susceptibility_wynn and get_magnon_susceptibility_wynn. For the i-th entry x # of `items` (a bubble MPO or a frequency) it prints `label(i, x)` when verbose, takes @@ -403,12 +420,11 @@ function _rpa_wynn_series(bubble, items, MPOV::MPO; K_max::Int, maxdim_apply::In end # (‖T_K‖/‖T_{K−2}‖)^½ tends to the spectral radius ρ of VΠ₀ from below: a Frobenius # norm weighs every eigenvalue of VΠ₀, so at finite K the ratio is a power mean - # of them. On half-filled chains of 2^5–2^9 sites it read 1–6 % low at K = 6 and - # 25–40 % low at K = 2, hence the two thresholds (ρ ≳ 0.9 in both cases). With - # K_max < 2 there is no ratio to take, and no check. + # of them (_WYNN_RHO_THRESHOLDS). With K_max < 2 there is no ratio to take, and + # no check. if K_max >= 2 && tnorm[K_max-1] > 0 ρ = sqrt(tnorm[K_max+1] / tnorm[K_max-1]) - ρ > (K_max >= 4 ? 0.85 : 0.6) && push!(slow, (i, ρ)) + ρ > _WYNN_RHO_THRESHOLDS[min(K_max, 6) - 1] && push!(slow, (i, ρ)) end # Partial sums (Wynn input) diff --git a/src/solvers/CorrectionVector.jl b/src/solvers/CorrectionVector.jl index 33c17e6..edc9459 100644 --- a/src/solvers/CorrectionVector.jl +++ b/src/solvers/CorrectionVector.jl @@ -139,7 +139,9 @@ The residual comes from inner products whose terms cancel to about energy offset in `H`). `residual` is an upper bound that includes this floor (`residual_floor`); where the floor would leave the comparison with `rtol` open (η ≲ 1e-5 on a level, or an offset of 10³ at η = 1e-4) the residual is evaluated -exactly instead, from the direct sum of its terms. The truncation sets a second +exactly instead, from the direct sum of its terms, as long as that sum fits in memory +(a bond dimension of `x` up to about 100–300 for the usual Hamiltonians; beyond, a +converged `x` reads as its floor and `converged = false`). The truncation sets a second floor, about `√cutoff·‖x‖/‖b‖`: at small η, `rtol` is reachable only with `cutoff ≲ (rtol·η)²`. Where the contraction of the check would hold more than 2^27 numbers it is skipped with a message: `residual = NaN`, `converged = false`. @@ -239,7 +241,7 @@ _cv_ldos_scalar(r) = (; ldos = r.ldos[1], G = r.G[1], residual = r.residual[1], converged = r.converged[1], maxlinkdim = r.maxlinkdims[1]) """ - get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector; eta=1e-2, + get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, , ) -> (; ldos, G, residual, converged, maxlinkdims) get_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; …) @@ -265,7 +267,7 @@ selected with the keywords of `get_ldos_online` (`spin_proj`, `proj_s`, `nambu_p `proj_nambu`, `layer_proj`, `proj_layer`, `sublat_proj`, `proj_sl`), one scan per sector. Every other keyword goes to `get_correction_vector`. """ -function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector{<:Real}; +function get_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta::Real = 1e-2, rtol::Real = 0.02, nambu_proj::Bool = false, @@ -293,7 +295,7 @@ get_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; kwargs...) = _cv_ldos_scalar(get_ldos_cv(H, X, [ω]; kwargs...)) """ - get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector; eta=1e-2, + get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta=1e-2, ) -> (; ldos, G, residual, converged, maxlinkdims) get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω::Real; …) @@ -310,7 +312,7 @@ stays at a small bond dimension down to small η, where KPM-MPS needs moments be its truncation floor. In the two-particle continuum `x` spreads and its bond dimension grows; check `converged`. """ -function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals::AbstractVector{<:Real}; +function get_exciton_ldos_cv(H::TBHamiltonian, X::Int, ω_vals; eta::Real = 1e-2, rtol::Real = 0.02, kwargs...) @@ -351,12 +353,21 @@ per step, and `energy_step` is the last move of the energy, `|E − ω|` of the step. `converged` is `true` when the last correction vector is converged (its residual) and -the energy has stopped moving: `energy_step` is at most the η of the step before the -last, which holds once the level is resolved at that η. In a continuum the quotient -keeps drifting from step to step and `converged` is `false`. Two levels closer than -the last η are not told apart, and nothing shows it: the history looks like that of -one level, `energy` is their weighted mean and `weight` the sum of their weights. -Lower the last η below the splitting you need to resolve. +the last two steps agree that there is a level: +- the weight is stationary. On a level `w` does not depend on η; in a continuum + `x` spreads over the states within η and `w` is proportional to η. The change of `w` + over the last step must be within a tenth of the change a continuum would show, + `|1 − w_last/w_before| ≤ 0.1·|1 − η_last/η_before|`; +- the energy has stopped moving: `energy_step` is at most the η of the step before the + last. (In a continuum the step is about η_last whatever ω, so this alone shows + nothing; it catches a quotient crawling between the levels of a small system.) +So `eta_steps` needs two different broadenings at its end: a single step, or two equal +ones, is never reported as converged. A level that is only resolved at the last η +(its weight below about η_before times the background of the other states) is +reported as not converged. Two levels closer than the last η are not told apart, and +nothing shows it: the history looks like that of one level, `energy` is their weighted +mean and `weight` the sum of their weights. Lower the last η below the splitting you +need to resolve. The broadenings are those of `eta_steps` (`eta` is not a keyword here); every other keyword goes to `get_correction_vector`. @@ -386,10 +397,21 @@ function refine_bound_state(H::Union{TBHamiltonian,MPO}, b::MPS, ω0::Real; ω = E end last = history[end] - # The energy has stopped moving when its last step is within the η before the last - # (the only η for a single step): for an isolated level the step is ~η²/Δ. + # A level, or a continuum? For x = (ω + iη − H)⁻¹b the quotient gives + # E − ω = η·Re G/Im G exactly, about η in a continuum whatever ω: the size of the + # energy step alone shows nothing there. The weight does: w = η|G|²/|Im G| is + # independent of η on a level and proportional to η in a continuum, so over the last + # step w_last/w_before is 1 or η_last/η_before. `drift` is the measured change in + # units of the continuum's (0 on a level, 1 in a continuum; NaN or Inf, hence not + # converged, for a single step or two equal η). The energy step still catches a + # quotient crawling between the levels of a small system. energy_step = abs(last.energy - last.omega) - η_before = history[max(end - 1, 1)].eta + stationary = false + if length(history) >= 2 + before = history[end-1] + drift = (1 - last.weight / before.weight) / (1 - last.eta / before.eta) + stationary = abs(drift) <= 0.1 && energy_step <= before.eta + end return (; energy = last.energy, weight = last.weight, x, residual = last.residual, - energy_step, converged = r.converged && energy_step <= η_before, history) + energy_step, converged = r.converged && stationary, history) end diff --git a/src/solvers/Krylov.jl b/src/solvers/Krylov.jl index 9335b5b..abd2333 100644 --- a/src/solvers/Krylov.jl +++ b/src/solvers/Krylov.jl @@ -168,18 +168,37 @@ end # are 2 GiB, and the contraction holds two or three such arrays at a time. (A Ref, so # that the tests can reach the skipped branch on small systems.) const _RESIDUAL_MAX_ELEMENTS = Ref(2.0^27) -# Largest bond dimension, (w + 1)χ + χ_b, of the direct sum the exact evaluation -# orthogonalizes. -const _RESIDUAL_EXACT_MAXDIM = 2000 # Rounding of the two evaluations, in units of eps and of the scale -# S = (|a0| + |a1|σ)²‖x‖² + ‖b‖², σ = ‖A‖_F/√D the rms singular value of A: ‖r‖² from the -# inner products is known to ± FLOOR_FACTOR·eps·S (measured: up to 5.8·eps·S), ‖r‖ from -# the direct sum to ± EXACT_FLOOR_FACTOR·eps·√S (measured: up to 37·eps·√S), over -# correction vectors at η = 1e-2…1e-8, Green's functions and Dyson systems of condition -# number up to 1e5 (examples/nontracked/fixes_checks/review_022/residual_floor.jl). +# S = (|a0| + |a1|σ)²‖x‖² + ‖b‖², with σ standing for ‖A‖: the larger of its rms singular +# value ‖A‖_F/√D and a power-iteration estimate of its largest one (_opnorm_estimate). +# ‖r‖² from the inner products is known to ± FLOOR_FACTOR·eps·S (measured: up to +# 5.8·eps·S with the rms value alone), ‖r‖ from the direct sum to +# ± EXACT_FLOOR_FACTOR·eps·√S (measured: up to 37·eps·√S), over correction vectors at +# η = 1e-2…1e-8, Green's functions and Dyson systems of condition number up to 1e5 +# (examples/nontracked/fixes_checks/review_022/residual_floor.jl). The rms value alone +# is too small a scale when A is large on a few states only: with one bond of strength +# 1e3–1e5 on a chain the rounding reached 2e4·eps·S (review_022/rereview/b3_*). const _RESIDUAL_FLOOR_FACTOR = 32.0 const _RESIDUAL_EXACT_FLOOR_FACTOR = 64.0 +# A lower bound of the operator norm ‖A‖₂: the largest ‖Av‖/‖v‖ over a few power +# iterations on a small MPS (bond dimension ≤ 16), from a fixed random state, so that it +# is the same at every call and leaves the global RNG alone. It finds a term that is +# large on a few states only (a strong local potential or bond: 100 on one site of 2^30 +# in six iterations), which the rms singular value averages away over the D states. +function _opnorm_estimate(A::MPO, sites; iters::Int = 8) + v = random_mps(Random.Xoshiro(0), ComplexF64, sites; linkdims = 4) + est = 0.0 + for _ in 1:iters + v = apply(A, v; maxdim = 16, cutoff = 1e-12) + nv = norm(v) + (isfinite(nv) && nv > 0) || break + est = max(est, nv) + v = v / nv + end + return est +end + # ⟨Ax|Ax⟩ contracted one tensor at a time (environment · x · A · A† · x†): χ³w²d + χ²w³d² # per site, where ITensorMPS's inner(A, x, A, x) joins A[j]·x[j] and its conjugate first, # χ³w³d. The environment holds χ²w² numbers either way. The bra copies get their own @@ -201,15 +220,17 @@ end # a0·x ⊕ a1·Ax ⊕ (−b), with Ax the untruncated product: accurate to ~eps of the largest # term, where the inner products cancel to ~√eps of it. `nothing` when the sum cannot be # formed (a bond of x or b without a link index; A may lack some, as interleave_mpo's -# output does) or would exceed _RESIDUAL_EXACT_MAXDIM. +# output does) or is too large: Ax and the sum are held for all n sites at the bond +# dimension m = (w + 1)χ + χ_b, 2·n·m²·d numbers, under the bound of the inner-product +# check (_RESIDUAL_MAX_ELEMENTS: m up to about 1300 on 20 qubits, 750 on 60). function _linsolve_residual_exact(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number) n = length(x) n >= 2 || return nothing haslinks(ψ) = all(j -> linkind(ψ, j) !== nothing, 1:n-1) (haslinks(x) && haslinks(b)) || return nothing χ, w = maxlinkdim(x), maxlinkdim(A) - (iszero(a0) ? w * χ : (w + 1) * χ) + maxlinkdim(b) <= _RESIDUAL_EXACT_MAXDIM || - return nothing + m = Float64((iszero(a0) ? w * χ : (w + 1) * χ) + maxlinkdim(b)) + 2 * n * m^2 * maximum(dim, siteinds(x)) <= _RESIDUAL_MAX_ELEMENTS[] || return nothing Ax = apply(A, x; alg = "naive", truncate = false) r = iszero(a0) ? +(a1 * Ax, -1 * b; alg = "directsum") : +(a0 * x, a1 * Ax, -1 * b; alg = "directsum") @@ -224,15 +245,19 @@ end # ‖r‖² = |a0|²⟨x|x⟩ + |a1|²⟨Ax|Ax⟩ + ⟨b|b⟩ + 2Re(ā0 a1⟨x|A|x⟩) − 2Re(ā0⟨x|b⟩) − 2Re(a1⟨b|A|x⟩). # The terms cancel, and so do the products inside ⟨Ax|Ax⟩ where Ax is small next to # ‖A‖‖x‖ (an ill-conditioned A): ‖r‖² is known to ± f² = _RESIDUAL_FLOOR_FACTOR·eps·S -# only, S = (|a0| + |a1|σ)²‖x‖² + ‖b‖² with σ = ‖A‖_F/√D standing for ‖A‖. That is a -# rounding floor f/‖b‖ ≈ 1e-7·(|a0| + |a1|σ)‖x‖/‖b‖, which grows with ‖x‖: as 1/η for a -# resolvent, with a constant energy offset in A, with the condition number of a Dyson -# solve. `residual` is the upper bound √(max(‖r‖², 0) + f²)/‖b‖, never below the floor: -# a converged solve reads as its floor, not as 0, and noise cannot pass for a small -# residual. +# only, S = (|a0| + |a1|σ)²‖x‖² + ‖b‖² with σ standing for ‖A‖ (the larger of +# ‖A‖_F/√D and _opnorm_estimate). That is a rounding floor +# f/‖b‖ ≈ 1e-7·(|a0| + |a1|σ)‖x‖/‖b‖, which grows with ‖x‖: as 1/η for a resolvent, +# with a constant energy offset or a strong local term in A, with the condition number +# of a Dyson solve. A sum below −f² shows the estimate too small, and f² is then +# raised to it. `residual` is the upper bound √(max(‖r‖², 0) + f²)/‖b‖, never below +# the floor: a converged solve reads as its floor, not as 0, and noise cannot pass for +# a small residual. # method = :exact: when the floor leaves the comparison with `threshold` open (the # threshold lies between the lower and the upper bound), from the direct sum -# (_linsolve_residual_exact), where it is affordable; `floor` is then ~eps-sized. +# (_linsolve_residual_exact), where it fits in memory; `floor` is then ~eps-sized. +# Where it does not fit, the :inner bound is returned: a converged x then reads as its +# floor, above the threshold. # method = :skipped: the contraction would hold more than _RESIDUAL_MAX_ELEMENTS numbers; # `residual` and `floor` are NaN. function _linsolve_residual(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number; @@ -245,10 +270,11 @@ function _linsolve_residual(A::MPO, b::MPS, x::MPS, a0::Number, a1::Number; r2 = abs2(a1) * _norm2_Ax(A, x) + nb2 - 2real(a1 * bAx) iszero(a0) || (r2 += abs2(a0) * nx2 + 2real(conj(a0) * a1 * inner(x', A, x)) - 2real(conj(a0) * inner(x, b))) - r2 = max(r2, 0.0) - σ = sqrt(real(inner(A, A)) / _site_space_dim(siteinds(x))) + σ = max(sqrt(real(inner(A, A)) / _site_space_dim(siteinds(x))), + _opnorm_estimate(A, siteinds(x))) S = (abs(a0) + abs(a1) * σ)^2 * nx2 + nb2 - f2 = _RESIDUAL_FLOOR_FACTOR * eps(Float64) * S + f2 = max(_RESIDUAL_FLOOR_FACTOR * eps(Float64) * S, -r2) # r2 < −f²: f² was too small + r2 = max(r2, 0.0) if isfinite(threshold) && r2 - f2 <= threshold^2 * nb2 < r2 + f2 rx = _linsolve_residual_exact(A, b, x, a0, a1) rx === nothing || @@ -318,8 +344,10 @@ end # 2^14–2^30 sites: chain ends, middle bond (both directions) and impurity site to 4e-6 # or better; ‖G(z − H) − 1‖_F ≤ 4e-2; no warning. # 2^34, 2^36: the same elements to 1e-4, except the impurity chain at the band edge -# (0.3–2 %, ‖G(z − H) − 1‖_F = 0.10 and 0.19, with the warning). -# 2^40: the chain ends are bulk rows (‖G(z − H) − 1‖_F = 0.9, with the warning). +# (0.3–2 %, ‖G(z − H) − 1‖_F = 0.10 and 0.19, with the warning); at 2^36 the impurity +# chain at η = 0.03 warns too (0.36), with these elements right to 1e-5. +# 2^40: the chain ends are bulk rows in all eight cases (‖G(z − H) − 1‖_F = 0.9–6, with +# the warning). # Against the dense inverse at 2^10 and 2^12 sites, ‖G(z − H) − 1‖_F was 1–8 times the # relative error of the worst row. A looser 1e-8·2^13/D left rows 3 % off and diagonal # elements 1 % off at the band edge; tighter ones (down to 1e-8·2^3/D) did not lower @@ -432,8 +460,9 @@ function get_green_krylov(H_mpo::MPO, sites::Vector{<:Index}, ω_phys::Real; (info.floor * nrhs > residual_warn / 2 ? " (rounding floor of this residual: $(round(info.floor * nrhs; sigdigits=2)))" : "") * ". Some rows of G are off by about that much, relative to themselves: " * - "lower cutoff, or raise maxdim or nsweeps (cutoff = $cutoff, maxlinkdim " * - "reached: $(maxlinkdim(sol)))." ω_phys η + "lower cutoff, or raise maxdim or nsweeps (krylov_cutoff, krylov_maxdim, " * + "krylov_nsweeps through get_bubble_mpo and the RPA drivers; cutoff = " * + "$cutoff, maxlinkdim reached: $(maxlinkdim(sol)))." ω_phys η end return return_info ? (; G, residual = res, residual_abs = absres, residual_floor = info.floor, maxlinkdim = maxlinkdim(sol)) : G diff --git a/test/bugfix_green_krylov_size.jl b/test/bugfix_green_krylov_size.jl index 07c2f96..8720856 100644 --- a/test/bugfix_green_krylov_size.jl +++ b/test/bugfix_green_krylov_size.jl @@ -47,4 +47,21 @@ using TensorBinding: get_Hamiltonian, get_green_krylov, _green_krylov_cutoff @test el(old.G, s, 1, 1) ≈ g0 rtol=1e-3 @test old.residual < 1e-2 @test old.residual_abs > 0.1 + + # 2^36 sites. The start: the truncated sum |I⟩⟩ + 0.1·r drops the random part there, + # so it is a direct sum beyond 2^30 sites. The check: the rounding floor of the inner + # products (0.1 in ‖G(z − H) − 1‖_F) is at the threshold, so the residual is + # evaluated exactly, from a direct sum on an operator with a bond without link index. + # (KrylovKit may warn about a local solve that stops at 1.1e-10: only the warning of + # the check is looked for.) + L = 36; N = 2^L + H = get_Hamiltonian("chain_1d", 1.0; L) + s = H.sites + io = IOBuffer() + r = with_logger(() -> get_green_krylov(H, ω; η, return_info=true), SimpleLogger(io)) + @test !occursin("G is not converged", String(take!(io))) + @test el(r.G, s, 1, 1) ≈ gs rtol=1e-6 + @test el(r.G, s, N, N) ≈ gs rtol=1e-6 + @test r.residual_abs < 0.01 # the floor of the inner products: 0.1 + @test r.residual_floor * sqrt(N) < 1e-4 # the exact evaluation's end diff --git a/test/correction_vector.jl b/test/correction_vector.jl index e83d688..434227f 100644 --- a/test/correction_vector.jl +++ b/test/correction_vector.jl @@ -43,6 +43,8 @@ using TensorBinding: get_Hamiltonian, add_onsite!, add_spin!, exciton_hamiltonia @test one.ldos ≈ -imag(gref(Hd, bv, 0.3 + 0.05im)) / π rtol=1e-6 # the scan warm-starts each frequency itself: x0 is refused, not silently reused @test_throws ArgumentError get_ldos_cv(H, 20, ωs; eta=0.05, x0=b) + # any collection of frequencies + @test get_ldos_cv(H, 20, (0.3, 0.5); eta=0.05).ldos[1] ≈ one.ldos rtol=1e-8 # a forced failure (maxdim 2) is flagged, and the scan warns bad = get_correction_vector(H, b, 0.3; eta=0.05, maxdim=2, seed_maxdim=2, rounds=1) @@ -57,11 +59,24 @@ using TensorBinding: get_Hamiltonian, add_onsite!, add_spin!, exciton_hamiltonia @test rb.energy ≈ F.values[k] atol=1e-8 @test rb.weight ≈ abs2(dot(F.vectors[:, k], bv)) rtol=1e-6 @test rb.converged && rb.energy_step < 1e-5 - # in the continuum the quotient keeps moving, and `converged` says so although the - # last linear solve is converged + @test rb.weight ≈ rb.history[end-1].weight rtol=1e-3 # stationary on a level + # between the levels of this small chain (spacing ~0.1) the quotient crawls from step + # to step, and `converged` says so although the last linear solve is converged rc = refine_bound_state(H, b, 0.3) @test !rc.converged && rc.residual < 0.02 @test rc.energy_step > 1e-3 + # In a real continuum (2^16 sites, level spacing 1e-4) the energy step is about the + # last η whatever ω0, and shows nothing: E − ω = η·Re G/Im G. The weight does: it is + # proportional to η there, and stationary on a level. + H16 = get_Hamiltonian("chain_1d", 1.0; L=16) + rc16 = refine_bound_state(H16, probe_state(H16, 2^15 + 3), 0.3; + eta_steps=(0.05, 0.01, 1e-3)) + @test rc16.residual < 0.02 && rc16.energy_step < 0.01 + @test rc16.weight / rc16.history[end-1].weight ≈ 0.1 rtol=0.05 + @test !rc16.converged + # a single step, or two equal broadenings at the end, cannot show a level + @test !refine_bound_state(H, b, F.values[k]; eta_steps=(1e-3,)).converged + @test !refine_bound_state(H, b, -2.7; eta_steps=(0.01, 1e-3, 1e-3)).converged # the broadenings are eta_steps: a stray `eta` would override every step @test_throws ArgumentError refine_bound_state(H, b, -2.7; eta=1e-2) @test_throws ArgumentError refine_bound_state(H, b, -2.7; eta_steps=()) diff --git a/test/linsolve_residual.jl b/test/linsolve_residual.jl index c9c8153..3e2f323 100644 --- a/test/linsolve_residual.jl +++ b/test/linsolve_residual.jl @@ -1,6 +1,7 @@ using TensorBinding, ITensors, ITensorMPS, LinearAlgebra, Logging, Random, Test using TensorBinding: get_Hamiltonian, add_onsite!, probe_state, get_correction_vector, - get_green_krylov, _linsolve_residual, _norm2_Ax, _RESIDUAL_MAX_ELEMENTS + get_green_krylov, interleave_mpo, _vec_mps_from_mpo, _linsolve_residual, + _norm2_Ax, _opnorm_estimate, _RESIDUAL_MAX_ELEMENTS # The residual ‖(a0 + a1·A)x − b‖/‖b‖ behind the checks of get_green_krylov, the # correction vectors and the RPA Dyson solves. From inner products its terms cancel to @@ -56,11 +57,33 @@ using TensorBinding: get_Hamiltonian, add_onsite!, probe_state, get_correction_v end # The vectorized system of get_green_krylov (a0 = 0), whose operator has a bond - # without a link index: the exact path still runs. + # without a link index: the exact path runs on it (the exact solution, whose + # residual the inner products cannot tell from their floor). Hg = get_Hamiltonian("chain_1d", 1.0; L=4) + sg = Hg.sites + s2 = siteinds("Qubit", 8) + Ag = interleave_mpo(swapprime(complex(2.3, 0.1) * MPO(sg, "Id") - Hg.mpo, 0 => 1), s2, 0) + bg = _vec_mps_from_mpo(MPO(sg, "Id"), s2) + @test any(j -> linkind(Ag, j) === nothing, 1:7) + Agd = ITensors.@disable_warn_order dmat(Ag, s2) + xg = tomps(Agd \ (ITensors.@disable_warn_order dvec(bg, s2)), s2) + rg = _linsolve_residual(Ag, bg, xg, 0.0, 1.0; threshold=1e-9) + @test rg.method === :exact && rg.residual < 1e-10 res = get_green_krylov(Hg, 2.3; η=0.1, return_info=true) @test res.residual >= res.residual_floor > 0 + # The scale of the floor is the norm of A, not its rms singular value: one strong + # bond on a long chain is 500 times the rms value, and the rounding of ⟨Ax|Ax⟩ on a + # state that sits on it follows the bond. The power-iteration estimate finds it. + L20 = 20 + H20 = get_Hamiltonian("chain_1d", 1.0; L=L20) + P20 = MPO(H20.sites, [d == 0 ? "Proj0" : "Proj1" + for d in reverse(digits(2^(L20 - 1) + 5; base=2, pad=L20))]) + A20 = +(H20.mpo, 1e4 * P20; alg="directsum") + @test sqrt(real(inner(A20, A20)) / 2.0^L20) < 20 + @test _opnorm_estimate(A20, H20.sites) ≈ 1e4 rtol=1e-3 + @test 1.9 < _opnorm_estimate(H20.mpo, H20.sites) <= 2.0 + 1e-8 + # Above the size bound the check is skipped with one message, never a warning: the # correction vector reports residual NaN and converged = false. old = _RESIDUAL_MAX_ELEMENTS[] diff --git a/test/rpa_column.jl b/test/rpa_column.jl index 9e8ff11..7710288 100644 --- a/test/rpa_column.jl +++ b/test/rpa_column.jl @@ -38,11 +38,36 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, @test r.residual < 1e-3 end - # Π₀|j⟩ = 0: x = 0 with residual 0, and no warning (it was 0.89, "not converged") - r0 = @test_logs _rpa_column_solve(0.0 * Π, 0.3Uc * MPO(s, "Id"), s, bj; nsweeps=40, - maxdim=400, cutoff=1e-12, residual_warn=1e-2, - return_info=true) - @test r0.residual == 0 && norm(r0.chi) == 0 + # Π₀|j⟩ = 0: x = 0 with residual 0, and no warning (it was 0.2–0.9, "not converged"), + # for Π₀ = 0 and for a Π₀ that annihilates |j⟩, where Π₀|j⟩ comes out at ~1e-15 + ej = dvec(bj, s) + for Πz in (0.0 * Π, mpo_of(Πd * (I - ej * ej'), s)) + r0 = @test_logs _rpa_column_solve(Πz, 0.3Uc * MPO(s, "Id"), s, bj; nsweeps=40, + maxdim=400, cutoff=1e-12, residual_warn=1e-2, + return_info=true) + @test r0.residual == 0 && norm(r0.chi) == 0 + end + + # The operator Π₀V is built at min(cutoff, 1e-14): at the solve cutoff 1e-12 it was + # off by √cutoff, which limited the column to 4e-7 here (128 sites, U = 0.999 U_c; + # the 32-site case above does not see it). + let H7 = get_Hamiltonian("chain_1d", 1.0; L=7) + s7 = H7.sites + E7, ψ7 = eigen(Hermitian(ITensors.@disable_warn_order dmat(H7.mpo, s7))) + D7 = length(E7) + f7 = Float64.(E7 .< 0) + W7 = reshape([ψ7[i, a] * conj(ψ7[i, b]) for i in 1:D7, a in 1:D7, b in 1:D7], D7, D7^2) + Π7d = W7 * Diagonal(vec([(f7[b] - f7[a]) / (0.03im - E7[b] + E7[a]) + for a in 1:D7, b in 1:D7])) * W7' + μ7 = eigvals(Π7d) + U7 = 0.999 / real(μ7[argmax(abs.(real.(μ7)))]) + b7 = _basis_state_mps(16, s7) + col = (I - U7 * Π7d) \ (Π7d * (ITensors.@disable_warn_order dvec(b7, s7))) + r7 = _rpa_column_solve((ITensors.@disable_warn_order mpo_of(Π7d, s7)), + U7 * MPO(s7, "Id"), s7, b7; nsweeps=40, maxdim=400, + cutoff=1e-12, residual_warn=1e-2, return_info=true) + @test norm((ITensors.@disable_warn_order dvec(r7.chi, s7)) - col) / norm(col) < 1e-7 + end # The Wynn series warns where it converges slowly: (‖T_K‖/‖T_{K−2}‖)^½ estimates # ρ(VΠ₀) from below, 1–6 % low at K = 6 and 25–40 % low at K = 2, with a threshold for @@ -50,9 +75,11 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=6) @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( [Π], 0.999Uc * MPO(s, "Id"); K_max=6) - @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=2) - @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( - [Π], 0.999Uc * MPO(s, "Id"); K_max=2) + for K in (2, 3, 4, 5) + @test_logs rpa_wynn_from_bubbles([Π], 0.3Uc * MPO(s, "Id"); K_max=K) + @test_logs (:warn, r"Wynn series") match_mode=:any rpa_wynn_from_bubbles( + [Π], 0.999Uc * MPO(s, "Id"); K_max=K) + end @test_logs rpa_wynn_from_bubbles([Π], 0.999Uc * MPO(s, "Id"); K_max=1) # Through the bubble: column j of get_rpa_susceptibility's χ, on a 4-site chain with @@ -89,7 +116,10 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, ϵF=0.0) @test_throws BoundsError get_rpa_susceptibility_column(Hc, V, 0.3, 5; kw...) @test_throws BoundsError get_rpa_susceptibility_column(Hc, V, 0.3, 0; kw...) + @test_throws ArgumentError get_rpa_susceptibility_column(Hc, V, 0.3, true; kw...) @test_throws ErrorException get_rpa_susceptibility_column(Hc, V, 0.3, 1; mode=:spin) + # a spinless H in the magnetic channel is refused as such, whatever j + @test_throws ErrorException get_rpa_susceptibility_column(Hc, V, 0.3, 99; mode=:magnetic) # The Krylov bubble takes the start and the residual threshold of get_green_krylov: # the start of v0.2.1 is reachable, and the check can be silenced. @@ -102,4 +132,8 @@ using TensorBinding: get_Hamiltonian, get_bubble_mpo, get_rpa_susceptibility, krylov_start=:identity) @test Π1 isa MPO && Π2 isa MPO @test dmat(Π1, sc) != dmat(Π2, sc) + # krylov_cutoff = nothing is the size-aware default of get_green_krylov, 1e-8 at this + # size (the doubled register has 2^4 states) + Π3 = get_bubble_mpo(Hc, Hc, 0.3; kwk..., krylov_residual_warn=Inf, krylov_cutoff=nothing) + @test dmat(Π3, sc) == dmat(Π1, sc) end From 16a9adee2b3068a4b149f34beaa58b5d5e3515bf Mon Sep 17 00:00:00 2001 From: Anouar Moustaj Date: Wed, 18 Nov 2026 02:16:45 +0200 Subject: [PATCH 13/13] Release 0.2.2 Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 13 ++++++++++++- Project.toml | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a5e7b..0495c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,18 @@ reading of semantic versioning: before 1.0, a change in the minor number (0.1 is breaking, and a change in the last number (0.1.0 → 0.1.1) adds features or fixes bugs without breaking the API. -## [Unreleased] +## [0.2.2] — 2026-11-18 + +This release keeps the v0.2 API. It adds solvers for one probe or one column at a time +(correction vectors, a column of the RPA susceptibility) and a MINRES local solver, and +it makes the linear solves report on themselves. Two things change for existing calls: + +- The entries under **Changed results** move the output of `get_green_krylov`, and of + the polarization bubble with `GF_method = :krylov` that is built on it. Each names + the keyword that restores the old behaviour. +- The residual checks under **Changed** are on by default: they add a warning, and + some time, to solves that ran silently before. `residual_warn = Inf` (`rpa_residual_warn`, + `krylov_residual_warn`) removes both. ### Added diff --git a/Project.toml b/Project.toml index a501d05..ec4f3cf 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "TensorBinding" uuid = "3b6805fc-d418-45a2-b1ea-024139a4a543" -version = "0.2.1" +version = "0.2.2" authors = ["Tiago Antao, Anouar Moustaj, Yitao Sun"] [deps]