core/tensor_network: braket-symmetric tensor shade invariant under bra↔ket exchange#542
Open
evaleev wants to merge 4 commits into
Open
core/tensor_network: braket-symmetric tensor shade invariant under bra↔ket exchange#542evaleev wants to merge 4 commits into
evaleev wants to merge 4 commits into
Conversation
…under bra<->ket
VertexPainterImpl::to_hash_value() seeds a tensor's "shade" (the color salting
all of its slot/index vertices in the canonicalization graph) from a hash that
included bra_rank and ket_rank in fixed order. For a braket-symmetric tensor,
bra<->ket exchange is a symmetry, so a half-tensor X{a;;x} and its swapped form
X{;a;x} got different shades — hence different canonical graphs/hashes — even
though they are equivalent. TNV3 already colors bra/ket slot and bundle vertices
symmetrically for such tensors; this was the last ordered-by-bra/ket asymmetry.
Hash the two ranks as an unordered pair when braket_symmetry == Symm, so the
shade is bra<->ket invariant. No effect on tensors with equal bra/ket ranks or
non-symmetric braket (the common cases), so the canonicalization snapshots are
unchanged; adds a regression test for half-tensor folding.
Krzmbrzl
approved these changes
Jun 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes tensor-network canonicalization bra↔ket-invariant for braket-symmetric tensors by ensuring the vertex painter’s per-tensor “shade” hash no longer depends on an ordered (bra_rank, ket_rank) pair. This aligns the core-vertex coloring with existing TNV3 symmetric coloring behavior so swapped half-tensor forms canonicalize identically when they should.
Changes:
- Update
VertexPainterImpl::to_hash_value()to hash bra/ket ranks as an unordered pair whenBraKetSymmetry::Symm. - Add a regression test verifying swapped half-tensor forms share a canonical hash under braket symmetry, while remaining distinct without it.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| SeQuant/core/tensor_network/vertex_painter.cpp | Makes tensor “shade” hashing bra↔ket-invariant for braket-symmetric tensors by sorting ranks before hashing. |
| tests/unit/test_canonicalize.cpp | Adds regression coverage ensuring braket-symmetric half-tensors canonicalize identically under bra↔ket exchange. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+29
to
+30
| if (tensor._braket_symmetry() == BraKetSymmetry::Symm && bra_rank > ket_rank) | ||
| std::swap(bra_rank, ket_rank); |
Member
Author
There was a problem hiding this comment.
Braced it to match the rest of the file (f-up commit).
The braket-symmetric half-tensor test only uses occupied/unoccupied indices (i, a), so the legacy spaces + PAO setup (copied from the existing canonicalization test) was unnecessary; make_sr_spaces suffices.
Every other if in this file uses braces; match that (Copilot review).
The global test harness (test_main) already sets the default context to make_sr_spaces() and the cardinal tensor labels, so the test needs no custom index-space registry or canonicalizer registration — just deserialize and canonicalize against the default context.
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
VertexPainterImpl::to_hash_value()computes a tensor's shade — the color that salts every one of its slot/index vertices in the canonicalization graph — from a hash that includedbra_rankandket_rankin fixed order, with no regard forbraket_symmetry().For a braket-symmetric tensor, bra↔ket exchange is a symmetry, so a half-tensor
X{a;;x}(bra-orbital) and its swapped formX{;a;x}(ket-orbital) should canonicalize identically. They didn't: the ordered(bra_rank, ket_rank)gave them different shades → different canonical graphs → different canonical hashes. TNV3 already colors bra/ket slot and bundle vertices symmetrically for such tensors (is_braket_symm); the core-vertex shade was the last ordered-by-bra/ket asymmetry.Change
vertex_painter.cpp: whenbraket_symmetry == Symm, hash the two ranks as an unordered pair (sort them) so the shade is bra↔ket-invariant.test_canonicalize.cpp): a braket-symmetric half-tensor's bra-orbital and ket-orbital forms now share a canonical hash; the non-symmetric forms stay distinct.Impact
No effect on tensors with equal bra/ket ranks or non-symmetric braket (the common cases) — the existing canonicalization snapshots are unchanged (full unit suite passes, no rebaselines).
Scope note
This makes the canonicalizer recognize braket-symmetric half-tensors as equivalent. It is a prerequisite for, but does not by itself deliver, auto-folding of such half-tensors in the eval/export path (e.g. noncovariant-THC export), which additionally requires routing regular (non-ToT) eval leaves through tensor-network canonicalization — a separate, broader follow-up.