Add map-shaped array construction and TensorMap broadcasting#200
Merged
Conversation
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 Report❌ Patch coverage is
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
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:
|
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>
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
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>
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
Adds axis-friendly array constructors that mirror
similar_map.zeros/randn/randtake the axes as a single tuple and fill the gap whereBase.randn/Base.randrejectBase.OneToranges, forwarding toBaseotherwise so a graded-axis backend that extendsBase.randn/randon its own axis type is used unchanged. Their map-shaped companionszeros_map/randn_map/rand_mapconstruct an array shaped as a linear map from a codomain and a domain, with the domain given codomain-facing and stored dual as insimilar_map. The TensorKit extension overloads the map constructors to build aTensorMap's codomain and domain directly rather than flattening.Moves the
TensorMaplinear-combination broadcasting into the TensorKit extension. ATensorMapis not anAbstractArray, so it gets aBroadcastStyleand stays a broadcast leaf, a linear combination flattens to aLinearBroadcasted(owned by TensorAlgebra) and materializes into aTensorMapthroughcopyto!, and a nonlinear element-wise broadcast errors rather than dense-converting.These support wrapping a
TensorMapdirectly as the array backing an ITensor.