Skip to content

fix(rotator): make MatrixRotator Haar distributed - #258

Merged
gouyt13 merged 1 commit into
dev/0.9from
fix/matrix-rotator-haar
Nov 20, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
fix/matrix-rotator-haar

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 20, 2026

Copy link
Copy Markdown
Member

Summary

MatrixRotator used the Q factor of Eigen::HouseholderQR on a Gaussian matrix as returned. Eigen, like LAPACK, sets R(k, k) = -sign(pivot) * norm for k < n and makes the last reflector the identity, so Q = H_1 ... H_{n-1}: det Q = (-1)^(n-1) and, for dimension 2 and up, Q(0, 0) < 0 for every seed. Such a Q is not Haar distributed, neither on O(n) nor on its coset.

This PR gives column i of Q the sign of R(i, i) (Mezzadri, Notices of the AMS 54 (2007), Section 5), which makes Q Haar distributed. Only the first dim rows of Q^T are stored, so only those rows are flipped.

Effect

  • A given seed now produces a different matrix: C++ MatrixRotator (RotatorType::MatrixRotator, also when IVF or QuantizedGraph use it) and Python quant.Rotator(method="matrix"). Saved rotators and indexes store their matrix and load unchanged; the persistence fixtures pass unchanged.
  • Accuracy is not expected to change. Multiplying the old Q on the left by an independent uniformly random sign matrix gives a Haar-distributed matrix, and RaBitQ's codes and estimator do not change when the same rotated coordinate of every vector changes sign, so for fixed data and queries the old estimates already had the same distribution as under a Haar rotation, apart from query quantization, which depends on the range of the rotated query. No benchmark was run.
  • The extra work is one pass over dim * padded_dim entries after the QR factorization.

Tests

  • C++ MatrixRotatorFactoryTest.SeededMatricesTakeBothSignsAsHaarRequires: over 64 seeds at dimension 3, both signs of Q(0, 0) and of det Q must occur. The old code fails it (64/64 negative entries, 0/64 negative determinants); a Haar-distributed rotator fails it with probability about 2^-62.
  • Python test_matrix_rotation_takes_both_signs: the same check through quant.Rotator at dimension 32.
  • ctest: 615 tests, 1 skipped (ThreadCount.BoundParallelForUsesRuntimePlacementAndPropagatesErrors, as before). Python: 1208 passed, 2 skipped, with the package rebuilt (native optimization off). check-format.sh, check-python.sh, full check-tidy.sh and mkdocs build --strict pass.

The Householder Q factor that MatrixRotator stored has a fixed determinant
and a negative first entry, so it is not Haar distributed. Give column i of
Q the sign of R(i, i) (Mezzadri 2007, Section 5). A given seed now yields a
different matrix; saved rotators and indexes are unaffected.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 20, 2026
@gouyt13
gouyt13 merged commit 5f27939 into dev/0.9 Nov 20, 2026
35 checks passed
@gouyt13
gouyt13 deleted the fix/matrix-rotator-haar branch November 20, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fixed Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant