Skip to content

Add an opt-in to derive contractions using integer labels#193

Merged
mtfishman merged 3 commits into
mainfrom
mf/contract-int-labels
Jun 30, 2026
Merged

Add an opt-in to derive contractions using integer labels#193
mtfishman merged 3 commits into
mainfrom
mf/contract-int-labels

Conversation

@mtfishman

@mtfishman mtfishman commented Jun 30, 2026

Copy link
Copy Markdown
Member

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.

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

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.10%. Comparing base (8c48b01) to head (9980526).

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              
Flag Coverage Δ
docs 28.06% <0.00%> (-0.70%) ⬇️

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 merged commit 679f479 into main Jun 30, 2026
21 checks passed
@mtfishman mtfishman deleted the mf/contract-int-labels branch June 30, 2026 13:58
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.
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