Add an opt-in to derive contractions using integer labels#193
Merged
Conversation
Adds `use_int_labels`, an opt-in that lets `contract` match its labels to integers before deriving the contraction. Deriving a contraction makes several passes comparing labels (`setdiff` to find the destination labels, `findfirst` to align the contracted groups), so for label types that are costly to compare it is faster to match the labels to integers once, run the bookkeeping on the integers, and map the derived labels back. The trait is `false` by default, so this is a no-op until a label type opts in. The relabel happens only in the label-deriving `contract(a1, labels1, a2, labels2)` entry, not the entries that are given the destination labels. The branch is gated on a compile-time-constant predicate, so callers that do not opt in keep the original path with no added overhead or inference change. A follow-up in ITensorBase opts `IndexName` in, where comparing labels is expensive enough for this to pay off.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #193 +/- ##
==========================================
+ Coverage 82.69% 83.10% +0.40%
==========================================
Files 22 22
Lines 630 645 +15
==========================================
+ Hits 521 536 +15
Misses 109 109
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mtfishman
added a commit
to ITensor/ITensorBase.jl
that referenced
this pull request
Jun 30, 2026
## Summary
Maps `IndexName` to `Int` for contraction by defining
`TensorAlgebra.label_type(::Type{<:IndexName}) = Int`, so contracting
`ITensor`s runs the contraction-label bookkeeping on integers rather
than on `IndexName`. An `IndexName` carries an id and a tag dictionary
and is far costlier to compare than an integer, and deriving a
contraction makes several comparison passes over the labels. For a
contraction of two rank-2 tensors this is about 12% faster, and the gain
grows with the number of indices.
Builds on ITensor/TensorAlgebra.jl#193, which
adds the `label_type` opt-in.
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
label_type, an opt-in that letscontractderive a contraction using a cheaper label type. Deriving a contraction makes several passes comparing labels (setdiffto find the destination labels,findfirstto align the contracted groups), so a label type that is costly to compare can map to an integer type: the labels are matched to integers by equality pattern, the bookkeeping runs on the integers, and the derived labels are mapped back. The default is the identity label type, so this is a no-op until a label type opts in by mapping itself to an integer.The relabel happens only in the label-deriving
contract(a1, labels1, a2, labels2)entry, not the entries that are given the destination labels. The encode and decode steps are no-ops for the default identity label type, so callers that do not opt in keep the original path with no added overhead or inference change.A follow-up in ITensorBase (ITensor/ITensorBase.jl#197) maps
IndexNametoInt, where comparing labels is expensive enough for this to pay off.