Skip to content

Map IndexName to Int for contraction#197

Merged
mtfishman merged 2 commits into
mainfrom
mf/label-type-indexname
Jun 30, 2026
Merged

Map IndexName to Int for contraction#197
mtfishman merged 2 commits into
mainfrom
mf/label-type-indexname

Conversation

@mtfishman

@mtfishman mtfishman commented Jun 30, 2026

Copy link
Copy Markdown
Member

Summary

Maps IndexName to Int for contraction by defining TensorAlgebra.label_type(::Type{<:IndexName}) = Int, so contracting ITensors 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.

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

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.78%. Comparing base (bde999b) to head (6e5eb1a).
⚠️ Report is 1 commits behind head on main.

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              
Flag Coverage Δ
docs 24.40% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mtfishman mtfishman changed the title [WIP] Map IndexName to Int for contraction Map IndexName to Int for contraction Jun 30, 2026
@mtfishman mtfishman marked this pull request as ready for review June 30, 2026 14:41
@mtfishman mtfishman enabled auto-merge (squash) June 30, 2026 14:41
@mtfishman mtfishman merged commit 0b71f76 into main Jun 30, 2026
26 of 28 checks passed
@mtfishman mtfishman deleted the mf/label-type-indexname branch June 30, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant