Skip to content

Death to BraidingTensor#444

Draft
lkdvos wants to merge 9 commits into
mainfrom
ld-fubraiding
Draft

Death to BraidingTensor#444
lkdvos wants to merge 9 commits into
mainfrom
ld-fubraiding

Conversation

@lkdvos

@lkdvos lkdvos commented Jun 25, 2026

Copy link
Copy Markdown
Member

This is a (WIP) implementation that redesigns the JordanMPOTensor interface, instead of keeping the BraidingTensors explicitly in the block tensor, which causes a lot of promotion issues, to keep them separate in a "scalars" field.

Right now, we aren't really using the scalars for anything other than detecting where the identity tensors are on the diagonal, so we can properly renormalize variances etc. I'm not sure if this ever really factors into the computations, and in any case with the altered contraction orders for the derivatives this never matters anymore, since we always contract the braiding tensors on one leg only (with the environments instead of with the environment + mps).

@kshyatt I'm not sure if there is a way to check if/how many issues this solves?

@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.20312% with 43 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/operators/jordanmpotensor.jl 78.26% 35 Missing ⚠️
src/operators/mpohamiltonian.jl 60.00% 8 Missing ⚠️
Files with missing lines Coverage Δ
ext/MPSKitAdaptExt.jl 92.59% <100.00%> (+1.28%) ⬆️
src/MPSKit.jl 100.00% <ø> (ø)
.../algorithms/derivatives/hamiltonian_derivatives.jl 92.27% <100.00%> (-0.87%) ⬇️
src/operators/ortho.jl 100.00% <100.00%> (ø)
src/operators/mpohamiltonian.jl 88.66% <60.00%> (-2.25%) ⬇️
src/operators/jordanmpotensor.jl 73.10% <78.26%> (-10.31%) ⬇️

... and 60 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

lkdvos and others added 9 commits June 26, 2026 09:35
Drive JordanMPOTensor off a single full-space `tensors` SparseBlockTensorMap
plus a `scalars` Dict of identity coefficients, instead of the four reduced-leg
A/B/C/D fields. The four-block decomposition is preserved as derived
`getproperty` accessors (with a 5-arg constructor that writes back), so existing
consumers keep working. Generic `getindex` materializes scalar entries as
`TensorMap`s; the `.A` accessor preserves `BraidingTensor`s for hot kernels.

Drop the now-redundant `JordanMPOTensorMap` alias and its export.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- MPOHamiltonian getproperty `:A` delegates to the tensor-level accessor so
  identity levels stay `BraidingTensor`s (keeps `isidentitylevel` correct).
- Rewrite `scale!` to act directly on `tensors`/`scalars` (the `.A/.B/.C/.D`
  accessors share data via `removeunit`, so in-place scaling of a copy leaked
  into the source).
- Add a 2-field `JordanMPOTensor(tensors, scalars)` constructor and use it in
  the Adapt extension and the `force_planar` test helper.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- 5-arg constructor: insert the trivial boundary legs with the duality dictated
  by V (via isdual of the corner space), so conjugated MPOs (_conj_mpo) embed
  correctly instead of hitting a SpaceMismatch.
- Adapt extension: convert the `scalars` coefficients to the adapted scalartype
  so precision changes (e.g. ComplexF64 -> ComplexF32) construct cleanly.
- Replace internal `getfield(W, :tensors/:scalars)` with `W.tensors`/`W.scalars`
  (the getproperty passthrough), keeping only the one passthrough getfield.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The A/B/C/D blocks are computed properties that rebuild a fresh
SparseBlockTensorMap on every access. Avoid redundant reconstruction:

- jordanmpotensor: rewrite `_jordan_B`/`_jordan_C` and `nonzero_values`/
  `nonzero_pairs` to iterate `tensors`/`scalars` directly instead of
  `nonzero_keys(W)` + `W[I]`; add `_identity_tensor` helper.
- hamiltonian_derivatives: bind `W.A/.B/.C/.D` once in the AC and AC2
  derivative builders (W*.A was rebuilt ~8x per AC2 build).
- ortho: cache blocks in left/right_canonicalize! and drop
  `typeof(W.A)`-style reconstructions.
- mpohamiltonian: answer `isidentitylevel` from `scalars` instead of
  reconstructing every site's A block.

Also expand the JordanMPOTensor docstring.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…x{4}

- SparseBlockTensorMap(::JordanMPOTensor) now produces a uniform concrete
  `TensorMap` block type with the identity scalars folded in, instead of an
  abstract eltype carrying BraidingTensors.
- Store `scalars` as `Dict{CartesianIndex{4},T}` keyed by the full
  `(row, 1, 1, col)` virtual position, matching `tensors`; update getindex,
  setindex!, haskey, the nonzero_* iterators, the block accessors, scale!,
  isidentitylevel and the Adapt extension accordingly.
- `_identity_tensor` now takes the CartesianIndex{4} position directly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The corner assertion now accepts entries that are approximately identity
braidings (not only literal `BraidingTensor`s), via the `_isbraiding` helper.
The corners are then folded back into scalar identities by the constructor, so
`JordanMPOTensor(SparseBlockTensorMap(W))` round-trips even after the converter
densifies identities into `TensorMap`s.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- `_jordan_A` now returns a concrete `SparseBlockTensorMap{TensorMap}` with the
  identity scalars folded in, instead of `Union{TA,BraidingTensor}` carrying
  BraidingTensors. All four block accessors now share the same TensorMap eltype.
- getindex/setindex! are now written on `CartesianIndex{4}` as the primary form;
  the `Vararg{Int,4}` and linear-index methods delegate to it.
- setindex! accepts a `Number` or a `BraidingTensor`, both of which are stored as
  a scalar identity entry (the number as its coefficient, the braiding as one);
  any other tensor is stored in `tensors`.

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