Skip to content

Relabel dimension names to integers when contracting#196

Closed
mtfishman wants to merge 3 commits into
mainfrom
mf/integer-contraction-labels
Closed

Relabel dimension names to integers when contracting#196
mtfishman wants to merge 3 commits into
mainfrom
mf/integer-contraction-labels

Conversation

@mtfishman

@mtfishman mtfishman commented Jun 30, 2026

Copy link
Copy Markdown
Member

Summary

Relabels the dimension names to integers before contracting in mul_nameddims, so the contraction-label bookkeeping in TensorAlgebra.contract runs on Ints rather than on the dimension-name type. A contraction's structure depends only on the equality pattern of the names, so the names are matched to integers once (shared names get the same integer), the bookkeeping runs on the integers, and the result names are recovered by position.

This is aimed at IndexName, which carries a UUID and a tag dictionary and is comparatively expensive to compare and store. The previous path handed the IndexNames straight to TensorAlgebra.contract, which then re-compared them across every setdiff and findfirst pass. Matching once up front collapses that to a single name-matching pass. The speedup grows with the number of indices, from roughly 7% for two rank-2 tensors to roughly 34% for two rank-5 tensors, in the small-dimension regime where this bookkeeping is the dominant cost. The saving is a fixed per-call amount, so it fades to nothing at larger dimensions once the matrix multiply dominates.

Builds directly on the type-stable contraction-label derivation in ITensor/TensorAlgebra.jl#192, which is what makes Int labels cheap to run through contract. Confined to mul_nameddims, with no change to IndexName. The in-place mul! paths are left as-is.

Relabels the dimension names to integers before contracting in `mul_nameddims`, so the contraction-label bookkeeping in `TensorAlgebra.contract` runs on `Int`s rather than on the dimension-name type. A contraction's structure depends only on the equality pattern of the names, so the names are matched to integers once (shared names get the same integer), the bookkeeping runs on the integers, and the result names are recovered by position.

This is aimed at `IndexName`, which carries a `UUID` and a tag dictionary and is comparatively expensive to compare and store. The previous path handed the `IndexName`s straight to `TensorAlgebra.contract`, which then re-compared them across every `setdiff` and `findfirst` pass. Matching once up front collapses that to a single name-matching pass. The speedup grows with the number of indices, from roughly 7% for two rank-2 tensors to roughly 34% for two rank-5 tensors, in the small-dimension regime where this bookkeeping is the dominant cost. The gain is dimension-independent in absolute terms, so it fades to nothing once the matrix multiply dominates.

Builds directly on the type-stable contraction-label derivation in ITensor/TensorAlgebra.jl#192, which is what makes `Int` labels cheap to run through `contract`. Confined to `mul_nameddims`, with no change to `IndexName`. The in-place `mul!` paths are left as-is.
@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.99%. Comparing base (bde999b) to head (704b10d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #196      +/-   ##
==========================================
+ Coverage   73.77%   73.99%   +0.21%     
==========================================
  Files          29       29              
  Lines        1548     1561      +13     
==========================================
+ Hits         1142     1155      +13     
  Misses        406      406              
Flag Coverage Δ
docs 24.90% <100.00%> (+0.54%) ⬆️

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 enabled auto-merge (squash) June 30, 2026 03:08
@mtfishman mtfishman disabled auto-merge June 30, 2026 03:20
@mtfishman mtfishman closed this Jun 30, 2026
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