Skip to content

Source default tensor symmetries from the Context#557

Open
evaleev wants to merge 2 commits into
default-tensor-symmetryfrom
default-tensor-symmetry-context
Open

Source default tensor symmetries from the Context#557
evaleev wants to merge 2 commits into
default-tensor-symmetryfrom
default-tensor-symmetry-context

Conversation

@evaleev

@evaleev evaleev commented Jun 16, 2026

Copy link
Copy Markdown
Member

Stacked on top of #553 (base branch default-tensor-symmetry); this PR shows only the incremental changes on top of it.

Motivation

#553 makes tensors non-symmetric by default, but hardcodes the defaults and leaves the deserializer's column default (Nonsymm) inconsistent with the Tensor ctor's (Symm). That inconsistency made particle-symmetric amplitudes parse as column-Nonsymm (so they canonicalized differently and no longer merged), and broke ~18 unit + 5 integration tests.

Approach

Make the default tensor symmetries a property of the active sequant::Context, so the library default is the safest (fully non-symmetric) while applications fine-tune for ergonomics.

  • Core Context gains symmetry, hermiticity, and column_symmetry defaults (Defaults + Options + accessors + set()). Library defaults: Nonsymm / NonHermitian / Nonsymm.
  • BraKet symmetry is never a Context knob — it is a derived property, computed per-tensor from the default Hermiticity and base_field, consistent with the earlier removal of Context::braket_symmetry (8c4669d).
  • Tensor ctors resolve unspecified perm/column/hermiticity against the active Context, reading it only when something is unspecified, so explicit construction (the hot path) stays lock-free.
  • Deserializer default symmetries are sourced from the active Context.
  • Context::operator== now compares the new symmetry defaults (otherwise set_scoped_default_context would silently refuse to switch a context that differs only in these — a latent bug surfaced by the new tests).
  • mbpt applications (unit-test harness + srcc/stcc integration) opt into the particle-symmetric convention via column = Symm.
  • Test expectations updated to the resulting (physically correct) symmetries; new tests cover the Context-driven defaults and the rejection of braket symmetry on (anti)symmetrization operators.

Testing

Built with the full debug config (clang-17, AddressSanitizer, Unity, ccache, all eval backends: TiledArray/BTAS/TAPP/Torch).

  • unit_tests-sequant: 7069 assertions / 70 cases pass.
  • check-sequant: all tests pass except sequant/unit/python/basic/run, which aborts at AddressSanitizer init (ASan-instrumented module dlopened into a non-ASan Python on macOS) — an environment limitation, unrelated to these changes.
  • All changed files pass clang-format-17 --Werror.

Builds on #553 ("non-symmetric by default") by making the default tensor
symmetries configurable through sequant::Context rather than hardcoding them.

- Context gains default `symmetry`, `hermiticity`, and `column_symmetry`
  (Defaults + Options + accessors + set()). Library defaults are the safest
  (Nonsymm / NonHermitian / Nonsymm). BraKet symmetry is never a Context knob:
  it is a derived property, computed per-tensor from the default Hermiticity
  and base_field, consistent with the removal of Context::braket_symmetry.
- Tensor ctors resolve unspecified perm/column/hermiticity against the active
  Context; the Context is consulted only when something is unspecified, so
  fully-explicit construction (the hot path) stays lock-free.
- The deserializer's default symmetries are sourced from the active Context.
- Context::operator== now also compares the symmetry defaults, so scoped/default
  context switches that differ only in these actually take effect.
- mbpt applications (unit-test harness + srcc/stcc integration) opt into the
  particle-symmetric convention via column = Symm.
- Update unit/integration expectations to the resulting (physically correct)
  symmetries; add tests for the Context-driven defaults and for rejecting
  braket symmetry on (anti)symmetrization operators.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR moves the “default tensor symmetry” behavior out of hardcoded defaults and into the active sequant::Context, so that library defaults are conservative (fully non-symmetric / non-Hermitian / non-column-symmetric) while applications can opt into more ergonomic conventions (e.g., particle/column symmetry for MBPT). It also updates deserialization and tests to reflect Context-sourced defaults and the updated canonicalization/ordering behavior.

Changes:

  • Add Context-level defaults for Symmetry, Hermiticity, and ColumnSymmetry, and include them in Context::operator==.
  • Update Tensor construction to resolve unspecified symmetry attributes against the active Context (deriving BraKetSymmetry from Hermiticity + base field).
  • Update v1 deserialization to source default symmetries from the active Context, and adjust unit/integration tests to match the new defaults and ordering.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
SeQuant/core/context.hpp Adds Context defaults + Options fields + accessors/setters for symmetry/hermiticity/column symmetry.
SeQuant/core/context.cpp Wires new Options fields into construction, adds accessors/setters, and updates operator== to compare new defaults.
SeQuant/core/expressions/tensor.hpp Introduces Context-driven defaulting via resolve_symmetries() and updates constructors accordingly.
SeQuant/core/io/serialization/v1/deserialize.cpp Sources deserializer default symmetries from get_default_context().
SeQuant/core/io/serialization/v1/ast_conversions.hpp Clarifies reserved (anti)symmetrizer symmetry handling; converts AST symmetries into Tensor construction.
tests/unit/test_main.cpp Sets test default Context to column_symmetry = Symm for MBPT-oriented expectations.
tests/unit/test_tensor.cpp Updates braket/adjoint expectations and adds coverage for Context-driven defaults.
tests/unit/test_parse.cpp Adds tests for rejecting braket symmetry on reserved (anti)symmetrizers.
tests/unit/test_wick.cpp Updates latex expectation due to changed canonical ordering under new defaults.
tests/unit/test_tensor_network.cpp Updates expected index ordering and adjusts a deserialize string to rely on Context defaults.
tests/unit/test_spin.cpp Updates expected serialized symmetry specifiers to match new defaulting/derivation behavior.
tests/unit/test_mbpt.cpp Updates expected strings to reflect Context-driven defaults and explicit symmetry tags.
tests/unit/test_canonicalize.cpp Updates deserialize fixtures to omit now-defaulted braket specifiers for Ŝ where appropriate.
tests/unit/test_biorthogonalization.cpp Removes per-call def_col_symm overrides, relying on Context defaults instead.
tests/unit/data/sf_r2_direct_real_inc.hpp Updates a large expected fixture string to match updated defaulting/serialization behavior.
tests/integration/srcc.cpp Sets MBPT integration Context default column_symmetry = Symm.
tests/integration/stcc.cpp Sets MBPT integration Context default column_symmetry = Symm.
Comments suppressed due to low confidence (1)

SeQuant/core/io/serialization/v1/ast_conversions.hpp:337

  • Deserializing reserved (anti)symmetrization operators (Ŝ/Â) currently inherits the Context-driven default Hermiticity when no explicit braket specifier is provided. If the active Context defaults to Hermitian, this will derive a non-Nonsymm braket symmetry and make plain "Ŝ{...}" / "Â{...}" fail to parse even though the default form should be accepted. These operators should ignore Context braket defaults and remain braket-Nonsymm unless the user explicitly requests otherwise (in which case the ctor should reject it).
    if (tensor.name == reserved::antisymm_label()) {
      perm_symm = Symmetry::Antisymm;
    } else if (tensor.name == reserved::symm_label()) {
      column_symm = ColumnSymmetry::Symm;
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread SeQuant/core/expressions/tensor.hpp
…ss of Context

Addresses Copilot review on #557:
- A bare "Ŝ{...}"/"Â{...}" previously inherited the Context's default
  Hermiticity, so under a Hermitian-default Context it would derive a
  non-Nonsymm braket and fail to construct. Force braket = Nonsymm for these
  reserved operators when no braket spec is given (an explicit spec is left for
  the Tensor ctor to reject). Covered by a new test under a Hermitian Context.
- Fix a misleading comment in Tensor::resolve_symmetries() about when the
  resolved Hermiticity is used.
@evaleev

evaleev commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

Also addressed the low-confidence note on ast_conversions.hpp (reserved (anti)symmetrizers inheriting the Context braket default): that was a real latent bug — under a Hermitian-default Context a plain Ŝ{...}/Â{...} would derive a non-Nonsymm braket and fail to construct. Fixed in 7fdb794 by forcing braket = Nonsymm for these operators when no braket spec is given (an explicit spec is still left for the Tensor ctor to reject), with a new regression test parsing them under a Hermitian Context.

@evaleev evaleev requested a review from Krzmbrzl June 16, 2026 04:50
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.

2 participants