Skip to content

Add map-shaped array construction and TensorMap broadcasting#200

Merged
mtfishman merged 4 commits into
mainfrom
mf/construction-broadcast-hooks
Jul 2, 2026
Merged

Add map-shaped array construction and TensorMap broadcasting#200
mtfishman merged 4 commits into
mainfrom
mf/construction-broadcast-hooks

Conversation

@mtfishman

@mtfishman mtfishman commented Jul 2, 2026

Copy link
Copy Markdown
Member

Summary

Adds axis-friendly array constructors that mirror similar_map. zeros/randn/rand take the axes as a single tuple and fill the gap where Base.randn/Base.rand reject Base.OneTo ranges, forwarding to Base otherwise so a graded-axis backend that extends Base.randn/rand on its own axis type is used unchanged. Their map-shaped companions zeros_map/randn_map/rand_map construct an array shaped as a linear map from a codomain and a domain, with the domain given codomain-facing and stored dual as in similar_map. The TensorKit extension overloads the map constructors to build a TensorMap's codomain and domain directly rather than flattening.

Moves the TensorMap linear-combination broadcasting into the TensorKit extension. A TensorMap is not an AbstractArray, so it gets a BroadcastStyle and stays a broadcast leaf, a linear combination flattens to a LinearBroadcasted (owned by TensorAlgebra) and materializes into a TensorMap through copyto!, and a nonlinear element-wise broadcast errors rather than dense-converting.

These support wrapping a TensorMap directly as the array backing an ITensor.

Adds axis-friendly array constructors that mirror `similar_map`. `zeros`/`randn`/`rand` take the axes as a single tuple and fill the gap where `Base.randn`/`Base.rand` reject `Base.OneTo` ranges, forwarding to `Base` otherwise so a graded-axis backend that extends `Base.randn`/`rand` on its own axis type is used unchanged. Their map-shaped companions `zeros_map`/`randn_map`/`rand_map` construct an array shaped as a linear map from a codomain and a domain, with the domain given codomain-facing and stored dual as in `similar_map`. The TensorKit extension overloads the map constructors to build a `TensorMap`'s codomain and domain directly rather than flattening.

Moves the `TensorMap` linear-combination broadcasting into the TensorKit extension. A `TensorMap` is not an `AbstractArray`, so it gets a `BroadcastStyle` and stays a broadcast leaf, a linear combination flattens to a `LinearBroadcasted` (owned by TensorAlgebra) and materializes into a `TensorMap` through `copyto!`, and a nonlinear element-wise broadcast errors rather than dense-converting.

These support wrapping a `TensorMap` directly as the array backing an ITensor.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.84848% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.01%. Comparing base (46233e2) to head (19ca62b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/similar_map.jl 71.42% 4 Missing ⚠️
ext/TensorAlgebraTensorKitExt.jl 94.73% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #200      +/-   ##
==========================================
+ Coverage   82.83%   84.01%   +1.18%     
==========================================
  Files          24       24              
  Lines         699      732      +33     
==========================================
+ Hits          579      615      +36     
+ Misses        120      117       -3     
Flag Coverage Δ
docs 25.28% <0.00%> (-1.21%) ⬇️

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 added a commit to ITensor/ITensorBase.jl that referenced this pull request Jul 2, 2026
…adcast hooks

Routes cold-start `rand`/`randn`/`zeros` over index spaces through TensorAlgebra's map-shaped construction hooks (ITensor/TensorAlgebra.jl#200), which dispatch on the axis type to build a dense `Array`, a block-sparse array, or a `TensorMap`, so ITensorBase no longer carries backend-specific construction. The generic named-tensor machinery (`nameddims`, named `getindex`, and the `copy`/`zero` helper) is loosened to accept any parent a `NamedTensor` can wrap rather than only an `AbstractArray`, and the linear-combination broadcasting now lives in TensorAlgebra, so the TensorKit extension is left holding just the space-backed `Index` adapters.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mtfishman and others added 3 commits July 2, 2026 13:24
Fold the small `_codomain_domain` helper into `zeros_map`/`randn_map`/`rand_map` directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Dispatch the codomain axes on `Tuple{S, Vararg{S}}` so the elementary space type `S` comes from the method rather than from a `spacetype` call, and build both the codomain and domain `ProductSpace{S}` with it. The domain axes take `Tuple{Vararg{S}}`, which admits the empty domain of an all-codomain cold-start while tying the domain spaces to the same `S`.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Factor the codomain/domain `ProductSpace{S}` building into a `_map_homspace` helper that takes the elementary space type `S` explicitly, so the codomain-nonempty and codomain-empty dispatch entries of each map constructor forward to one body. This adds the all-domain case (empty codomain, space type read from the domain) alongside the all-codomain and general cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mtfishman mtfishman changed the title [WIP] Add map-shaped array construction and TensorMap broadcasting Add map-shaped array construction and TensorMap broadcasting Jul 2, 2026
@mtfishman mtfishman marked this pull request as ready for review July 2, 2026 17:52
@mtfishman mtfishman enabled auto-merge (squash) July 2, 2026 17:52
@mtfishman mtfishman merged commit 0890e18 into main Jul 2, 2026
29 of 31 checks passed
@mtfishman mtfishman deleted the mf/construction-broadcast-hooks branch July 2, 2026 18:17
mtfishman added a commit to ITensor/ITensorBase.jl that referenced this pull request Jul 2, 2026
## Summary

Lets a native TensorKit space be an `Index`, so an `ITensor` wraps a
TensorKit `TensorMap` directly as its parent with no `AbstractArray` in
between. `NamedTensor` and `NamedUnitRange`/`Index` are loosened to hold
a `TensorMap` and a native space, and the generic named-tensor machinery
(`nameddims`, named `getindex`, and the `copy`/`zero` helper) is
loosened to accept any parent a `NamedTensor` can wrap rather than only
an `AbstractArray`, so a `TensorMap` flows through with no
backend-specific path. Cold-start construction (`randn`/`rand`/`zeros`
over index spaces) and linear-combination broadcasting (`a .+ b`, `2 .*
a`) build on the map-shaped construction and broadcast hooks in
ITensor/TensorAlgebra.jl#200, which dispatch on
the axis type, so the TensorKit extension is left holding just the
space-backed `Index` adapters. Element-wise broadcasting on a symmetric
tensor has no meaning and errors cleanly. Contraction and factorization
work through the TensorAlgebra TensorKit backend
(ITensor/TensorAlgebra.jl#197) with no
additional code.

This is the direct route to non-abelian symmetries such as SU(2) through
the `ITensorBase` to `TensorAlgebra` to `TensorKit` path.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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