Skip to content

refactor: replace RatComplexNum by Mathlib's GaussianInt - #1682

Merged
zhikaip merged 2 commits into
masterfrom
pauli_gaussian_int
Sep 24, 2026
Merged

zhikaip merged 2 commits into
masterfrom
pauli_gaussian_int

Conversation

@zhikaip

@zhikaip zhikaip commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

1. Switch to GaussianInt, rename ofRat → ofGaussianInt

Every coefficient that went through Physlib.RatComplexNum is 0, ±1, ±i or an integer
Levi-Civita sign, so Mathlib's GaussianInt (GaussianInt.toComplex,
GaussianInt.toComplex_injective) replaces it. No statement about Pauli matrices,
metrics, units or the Levi-Civita tensor changes.

  • Removed Physlib/Mathematics/RatComplexNum.lean (the type, its ring structure,
    toComplexNum and its helper lemmas).
  • Renamed ComplexTensor/OfRat.lean → OfGaussianInt.lean, and every …ofRat…
    declaration → …ofGaussianInt… (33 names); contr_basis_ratComplexNum →
    contr_basis_gaussianInt.
  • pauliContrComponent, pauliContrDownComponent and the Levi-Civita components are
    now GaussianInt-valued.

2. Mini-refactor: define ofGaussianInt via Tensor.ofComponents

ofGaussianInt f is now ofComponents c (fun b => GaussianInt.toComplex (f b)), reusing
the existing tensor API instead of going through (Tensor.basis c).repr.symm and
Finsupp.linearEquivFunOnFinite. ofGaussianInt_basis_repr_apply now follows from
componentMap_ofComponents. Performance-neutral.

Performance (heartbeats, same base)

File master this PR
PauliMatrices/Relations.lean 770,444k 707,080k (−8.2%)
PauliMatrices/ToTensor.lean (declarations > 20M) 538,614k 521,041k (−3.3%)

The gain comes from commit 1: kernel decide is cheaper on integer than on rational
arithmetic. No declaration got slower.

Reviewer map

Review commit by commit. In commit 1, start with ComplexTensor/OfGaussianInt.lean,
then PauliMatrices/ToTensor.lean, LeviCivita/Complex.lean and
PauliMatrices/Relations.lean; the rest is the mechanical rename. Commit 2 touches only
OfGaussianInt.lean.

Disclaimer: I instructed claude to make these changes and everything looks reasonable to me

🤖 Generated with Claude Code

zhikaip and others added 2 commits September 23, 2026 12:57
…ssianInt

All components that went through `Physlib.RatComplexNum` are 0, ±1, ±i or integer
Levi-Civita signs, so Mathlib's `GaussianInt` with `GaussianInt.toComplex` and
`GaussianInt.toComplex_injective` replaces it.

- Delete `Physlib/Mathematics/RatComplexNum.lean`.
- Rename `ComplexTensor/OfRat.lean` to `ComplexTensor/OfGaussianInt.lean`; `ofRat` becomes
  `ofGaussianInt : (ComponentIdx c → GaussianInt) →ₛₗ[GaussianInt.toComplex] ℂT(c)`, and all
  `*_ofRat*` lemmas become `*_ofGaussianInt*`; `contr_basis_ratComplexNum` becomes
  `contr_basis_gaussianInt`.
- `pauliContrComponent`, `pauliContrDownComponent` and the Levi-Civita components are now
  `GaussianInt`-valued.
- Replace the `RatComplexNum` helper lemmas in `PauliMatrices/Relations.lean` by inline
  `show … from` rewrites, and close `toTensor_dualLeft_eq_ofGaussianInt` and
  `toTensor_dualLorentzLeft_eq_ofGaussianInt` with `congrArg` instead of `congr`.

Heartbeats (v4.34): Relations.lean 770,274k -> 707,080k; ToTensor.lean (declarations above
20M) 538.6M -> 521.0M. Kernel `decide` is cheaper on integers than on rationals.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
`ofGaussianInt f` is now `ofComponents c (fun b => GaussianInt.toComplex (f b))` instead of
going through `(Tensor.basis c).repr.symm` and `Finsupp.linearEquivFunOnFinite`;
`ofGaussianInt_basis_repr_apply` follows from `componentMap_ofComponents`. Performance
neutral (Relations.lean and ToTensor.lean within 0.2%).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the large label Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for this pull-request (PR). If this is your first PR, welcome to the community!

Below is what will happen next. Please read carefully if you are not familiar with the process. You may open other PRs while this one is being reviewed, and can stack PRs on top of each other, so don't let these steps slow you down.

  1. Some automated checks will be run on your PR. You can see the results of these checks at the buttom of your PR page. If any of these checks fail, you will need to fix the issues before your PR can be merged. You can learn more about these here, including how to run them locally, which is sometimes quicker than relying on the GitHub Actions. If you have never had a PR merged before, you may have to wait for a reviewer to manually start these checks (this is for security).

  2. A reviewer will look at your PR and may ask you to make changes. This may happen a couple of days after you submit your PR, so you may need to be patient. But it should not be longer than that - if it is please bring it to the attention of the community on the Zulip. The level of review will depend on where your PR is submitted. If it is submitted to ./Physlib or ./QuantumInfo, the review will be more thorough than if it is submitted to ./PhyslibAlpha. You can find out more about what the review process is looking for in our review guidelines. If a reviewer adds an awaiting-author label to your PR, address the review comments, then please remove that label by adding a comment with -awaiting-author. This helps us keep track of reviews.

  3. The reviewer will either approve your PR, or request more changes (in which case we return to step 2). Once your PR is approved, it will be merged by a maintainer, this should happen shortly after approval, though you may get more comments at this stage.

Tip: The easiest way to get have a fast review is to submit a PR that is small and self-contained, and has clear documentation explaining why things are the way they are in your chages.

If you have any problems or questions, please reach out to the community on the Zulip.

@jstoobysmith jstoobysmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved - many thanks. Looks good to me.

@jstoobysmith jstoobysmith added the ready-to-merge This PR is approved and will be merged shortly label Sep 24, 2026
@zhikaip
zhikaip merged commit 3b203ef into master Sep 24, 2026
11 checks passed
@zhikaip
zhikaip deleted the pauli_gaussian_int branch September 24, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

large ready-to-merge This PR is approved and will be merged shortly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants