Skip to content

Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error#205

Merged
mtfishman merged 7 commits into
mainfrom
mf/nextgen-followups-round1
Jul 9, 2026
Merged

Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error#205
mtfishman merged 7 commits into
mainfrom
mf/nextgen-followups-round1

Conversation

@mtfishman

@mtfishman mtfishman commented Jul 8, 2026

Copy link
Copy Markdown
Member

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}), complementing scalartype/eltype, which give the element type alone. It is defined as typeof(data(a)), where the new data(a) follows parent to the underlying storage value.

one!/unmatricize! fill and scatter a matricized identity in place across a codomain/domain split, and svd_trunc now returns the truncation error ϵ (the 2-norm of the discarded singular values) as a fourth output.

ungrade strips the grading from a unit range, returning the plain range and acting as the identity on an already-plain range. tr traces a tensor viewed as a matricized operator over a codomain/domain bipartition.

data, datatype, scalar, and similar_map are public.

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

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.22222% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.99%. Comparing base (8567e35) to head (7f4e98d).

Files with missing lines Patch % Lines
src/factorizations.jl 95.83% 1 Missing ⚠️
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     
Flag Coverage Δ
docs 24.31% <61.76%> (+1.90%) ⬆️

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.

`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>
@mtfishman mtfishman changed the title [WIP] Add datatype, in-place one!/unmatricize!, make scalar/similar_map public [WIP] Add datatype, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error Jul 9, 2026
mtfishman and others added 2 commits July 9, 2026 15:56
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>
@mtfishman mtfishman changed the title [WIP] Add datatype, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error [WIP] Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error Jul 9, 2026
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>
@mtfishman mtfishman changed the title [WIP] Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error Add datatype/data, one!/unmatricize!, ungrade, matricize-based tr, and svd_trunc error Jul 9, 2026
@mtfishman mtfishman marked this pull request as ready for review July 9, 2026 20:13
`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 mtfishman enabled auto-merge (squash) July 9, 2026 22:02
@mtfishman mtfishman merged commit 727e6d3 into main Jul 9, 2026
21 checks passed
@mtfishman mtfishman deleted the mf/nextgen-followups-round1 branch July 9, 2026 22:28
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>
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