Add Stochastic Lanczos Quadrature example for von Neumann entropy#1422
Open
akaiHuang wants to merge 1 commit into
Open
Add Stochastic Lanczos Quadrature example for von Neumann entropy#1422akaiHuang wants to merge 1 commit into
akaiHuang wants to merge 1 commit into
Conversation
This example demonstrates SLQ (Ubaru, Chen & Saad 2017) for estimating Tr[f(A)] with f(x) = x ln x on the Metal GPU. The application is the von Neumann entropy S(rho) = -Tr[rho ln rho] of an N x N density matrix. The standard eigh-based path costs O(N^3); SLQ replaces it with m independent k-step Lanczos recurrences for O(k * m * N^2) matvecs. At N = 4000 the example achieves ~22x speedup over NumPy eigh with 0.4% relative error vs the float64 reference. Files: - slq.py pedagogical Lanczos + Gaussian-quadrature core - main.py benchmark harness (eigh vs SLQ across N) - README.md math, expected output, when to reach for SLQ - requirements.txt mlx + numpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a self-contained example demonstrating Stochastic Lanczos Quadrature (Ubaru, Chen & Saad 2017) for estimating
Tr[f(A)]on the Metal GPU. The application is the von Neumann entropyS(rho) = -Tr[rho ln rho]of anN x Ndensity matrix — a workhorse quantity in quantum information, lattice field theory, and quantum machine-learning regularisers.The standard
eigh-based path costsO(N^3); SLQ replaces it withmindependentk-step Lanczos recurrences, costingO(k * m * N^2)matvecs. AllO(N^2)work runs on the Metal GPU; the innerk x ktridiagonal eigh runs on NumPy (k <= 30, dispatch overhead beats GPU at that size).Why this fits mlx-examples
log det A,Tr[exp(A)], etc.mlxandnumpy(no extra ML framework, no pretrained weights).slq.py;main.pyadds aneighreference for accuracy comparison and a small CLI.Numbers (M1 Max, k=25, m=20)
t_exactisnumpy.linalg.eigvalshon float64;t_slqis the MLX path on the Metal GPU. AtN = 4000the example is ~22x faster than the CPUeighreference. This is the pedagogical version — sequential over probes, nomx.compilefusion; a production version with batched probes lives in mlx-qre on PyPI and pushes the speedup another order of magnitude past the SLQ shown here.Test plan
References
tr(f(A))via stochastic Lanczos quadrature, SIAM J. Matrix Anal. Appl. 38(4), 1075-1099 (2017).