Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error#205
Merged
Merged
Conversation
Small additive utilities on the tensor-algebra interface: a `datatype` storage-type accessor, public `scalar` and `similar_map`, and the in-place `one!` / `unmatricize!` primitives.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #205 +/- ##
==========================================
+ Coverage 80.32% 80.99% +0.67%
==========================================
Files 23 24 +1
Lines 869 905 +36
==========================================
+ Hits 698 733 +35
- Misses 171 172 +1
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:
|
`svd_trunc` now returns `(U, S, Vᴴ, ϵ)`, matching MatrixAlgebraKit's four-output `svd_trunc`, where `ϵ` is the 2-norm of the discarded singular values. This gives downstream callers the truncation error cheaply and without catastrophic cancellation, instead of recomputing it from norms.
ungrade returns the ungraded plain range underlying an axis, dropping block structure, sectors, charge labels, and arrow direction while keeping its extent. The generic fallback is the identity on a plain AbstractUnitRange, preserving any offset. Downstream packages extend it: a graded range maps to the OneTo of its total dimension, and a native TensorKit space to the OneTo of its dimension. It is the value that keys equality of named axes so an axis compares equal to its dual. TensorAlgebra.tr traces a generic array viewed as a linear map from its domain to its codomain by matricizing into the square matrix and taking the matrix trace, so the backend's own matrix tr (dense, graded, or TensorMap) does the work. The partition is given via labels, a bi-permutation, or the codomain rank, matching the factorization entry points. It is distinct from LinearAlgebra.tr, since the multi-argument forms take a codomain/domain partition rather than a bare matrix. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add `data(a)`, which follows `parent` to its fixed point and returns that leaf value, and define `datatype(a) = typeof(data(a))`. Both are untyped, so any type with a `parent` resolves through the generic with no bespoke method. Drop the type-form `datatype(::Type)` method. Mark `data` public, add doctests for both, and tighten the `ungrade` docstring to describe only what it does at this layer. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Give the other docstrings introduced or updated in this PR runnable examples, matching the reconstruction-invariant style already used by the factorization doctests. The svd_trunc example shows the new `ϵ` return and reconstructs the input from the factors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Doctests for the `public` (not exported) names `data`, `datatype`, `scalar`, `tr`, and `ungrade` now use `import TensorAlgebra` and call them as `TensorAlgebra.<name>`, since these are meant to be reached qualified rather than brought into the namespace (`tr` would also collide with `LinearAlgebra.tr`). The exported `svd_trunc`/`contract` example keeps `using`. Also make the `tr` example a rank-4 array traced over a non-trivial codomain/domain bipartition rather than a bare matrix, which shows what the partition arguments actually do. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`data` was added to the `public` list in this PR, so it appears in `names(TensorAlgebra)` on Julia 1.11+. Add it to the expected public-names list in the exports test so the set comparison passes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mtfishman
added a commit
to ITensor/GradedArrays.jl
that referenced
this pull request
Jul 9, 2026
## Summary Fill in the matrix-level linear algebra for a bare `AbelianGradedMatrix`, and add graded `ungrade` and `tr`. The `MatrixAlgebraKit` factorizations now accept a bare `AbelianGradedMatrix`: each matricizes to a `FusedGradedMatrix`, runs the block factorization, and unmatricizes the factors back. `Base.:*` on two graded matrices routes through `contract`, and `one!` fills the graded identity in place, delegating to `TensorAlgebra.one!` (added in ITensor/TensorAlgebra.jl#205). `ungrade` maps a graded range to its plain ungraded range, and `tr` traces graded and sector arrays, both consistent with the generic definitions in ITensor/TensorAlgebra.jl#205. --------- 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 10, 2026
… rename (#213) ## Summary A batch of next-generation index and identity utilities for `AbstractNamedTensor`, plus one breaking rename. Builds on the TensorAlgebra additions in ITensor/TensorAlgebra.jl#205 (TensorAlgebra 0.17.1). - **Name-based index-set algebra:** `commoninds`/`uniqueinds`/`unioninds`/`noncommoninds`/`hascommoninds`, the singular `commonind`/`uniqueind`, and the non-erroring `trycommonind`/`trynoncommonind`. Indices match by name, and `NamedUnitRange` equality is now dual-insensitive, so a shared bond matches even when one tensor carries its dual. - **Whole-tensor index ops:** `prime`, `noprime`, and `sim` relabel every index of a tensor. - **Identity and trace:** `id(elt, codomain, domain)` builds the identity operator from indices and an element type alone, and `tr` traces a named tensor viewed as a codomain/domain map. Both rest on a map-shaped `similar(a, codomain, domain)` that follows the tensor's backend and takes its axes as any iterable. - **Matricize:** the positional `matricize(a, codomain, domain)` fuses two dimension groups into a matrix and generates fresh names for the fused dimensions, next to the pair form that takes explicit names. Both forms accept any iterable of dimensions per group. - **`svd_trunc`** now returns the truncation error as a fourth output, matching the matrix-level primitive. - Smaller fixes: a clearer error when building a tensor from a single named range, index-keyed pairs in `replacedimnames`, and `trivialrange` for a `NamedUnitRange`. **Breaking:** the unique-identifier accessor on an index or index name is renamed from `id` to `uuid`, freeing `id` for the identity constructor. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
mtfishman
added a commit
to mtfishman/TensorNetworkQuantumSimulator.jl
that referenced
this pull request
Jul 10, 2026
…upstream API (#2) ## Summary Removes the `ITensorsITensorBaseCompat` submodule and shrinks the remaining legacy shim onto the upstream `ITensorBase` / `TensorAlgebra` / `MatrixAlgebraKit` API. The operator and named-state system, which has no upstream equivalent, becomes a TNQS-owned `Ops` submodule (custom gates extend `Ops.op`, and `state` is called qualified as `Ops.state` so it does not collide with `ITensorBase.state`). The remaining legacy tensor helpers move into an `itensors.jl` shim that this PR then trims: - **Index and tensor ops** move to the upstream versions: `prime`/`noprime` (including the whole-tensor forms), `id`, `sim`, `scalartype`, and the name-based index-set algebra (`commoninds`/`uniqueinds`/`trycommonind`/`trynoncommonind`). `Index` equality is name-based (dual-insensitive), so plain `Base` set-ops work directly on index collections and the local `small`/`name` set helpers and the `commonind`/`noncommonind`/`similar_map` wrappers are gone. - **More index and storage helpers move to the upstream API too:** index relabeling uses ITensorBase's `replaceinds` directly (dropping the vendored `replaceind`/`replaceinds`/`swapind` wrappers, with collection relabels spelled `(from .=> to)...`), storage access uses `unnamed`/`Array` and a `datatype` that delegates to `TensorAlgebra.datatype` (ITensor/ITensorBase.jl#213), and in-place scaling uses ITensorBase's `rmul!`. The `matricize(a::ITensor)` helper is dropped for the upstream `matricize(a, codomain, domain)`, which generates the fused output names itself, so no fresh-name minting remains at the TNQS call sites. The list-contraction verb `contract` is renamed to `contract_network`, and tags are set through `key => value` pairs now that the comma-string `settags` form is gone (`settags` accepts any iterable of pairs). - **Factorizations** at the call sites go straight through MatrixAlgebraKit (`qr_compact`/`svd_compact`/`svd_trunc`/`left_orth`/`right_orth`), and the operator trace is `tr(a, operator_inds(a)...)`. The compat `factorize` and `eigen` wrappers are removed, keeping `itensor_trunc` for the `cutoff`/`maxdim` to `trunc` translation. A call that names the new bond inlines the relabel until tag support lands in the upstream factorizations. The apply routines report their truncation error from `svd_trunc`'s returned `ϵ` instead of a cancellation-prone norm subtraction. - Because `Index` equality is name-based, the contraction-sequence optimizers, the graph index-set operations, and `directsum` take `Index` objects directly, dropping the `name.(...)` labels and comparisons that only worked around dual-mismatched indices. `directsum` now aligns each tensor with `unname`, and gains a first test through the `+`/`add` path. - Legacy no-ops (`disable_warn_order`, `denseblocks`, `dense`) and the first-shrink shims (`random_itensor`, `itensor`, `algorithm_name`, `dim`, `dag`, filtered-`inds`) are dropped for `randn`, `TensorAlgebra.scalar`, `length`, `conj`, and inline `filter` at the call sites. Builds on the ITensorBase and TensorAlgebra API additions in ITensor/ITensorBase.jl#213 and ITensor/TensorAlgebra.jl#205. The `[compat]` floors are ITensorBase 0.11.2 and TensorAlgebra 0.17.2, since those releases additionally carry the `Array(::ITensor)` densification, `namedunitrange(::ElementarySpace)`, and `data` / `datatype` over TensorMap storage that the TensorKit-backed path relies on. --------- 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
A batch of additive utilities on the tensor-algebra interface.
datatype(a)returns the underlying storage array type, the element type together with the container or device (e.g.Matrix{Float64}), complementingscalartype/eltype, which give the element type alone. It is defined astypeof(data(a)), where the newdata(a)followsparentto the underlying storage value.one!/unmatricize!fill and scatter a matricized identity in place across a codomain/domain split, andsvd_truncnow returns the truncation errorϵ(the 2-norm of the discarded singular values) as a fourth output.ungradestrips the grading from a unit range, returning the plain range and acting as the identity on an already-plain range.trtraces a tensor viewed as a matricized operator over a codomain/domain bipartition.data,datatype,scalar, andsimilar_mapare public.