Map IndexName to Int for contraction#197
Merged
Merged
Conversation
mtfishman
added a commit
to ITensor/TensorAlgebra.jl
that referenced
this pull request
Jun 30, 2026
## Summary Adds `label_type`, an opt-in that lets `contract` derive a contraction using a cheaper label type. Deriving a contraction makes several passes comparing labels (`setdiff` to find the destination labels, `findfirst` to 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 `IndexName` to `Int`, 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 #197 +/- ##
==========================================
+ Coverage 73.77% 73.78% +0.01%
==========================================
Files 29 29
Lines 1548 1549 +1
==========================================
+ Hits 1142 1143 +1
Misses 406 406
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:
|
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
Maps
IndexNametoIntfor contraction by definingTensorAlgebra.label_type(::Type{<:IndexName}) = Int, so contractingITensors runs the contraction-label bookkeeping on integers rather than onIndexName. AnIndexNamecarries 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_typeopt-in.