Skip to content

Conversation

@ajay-mk
Copy link
Member

@ajay-mk ajay-mk commented Dec 24, 2025

Custom Operator Registry Support

This PR refactors Operator logic from depending on a predefined OpType enum to a runtime OpRegistry, enabling users to define custom operators beyond the predefined set.

Major Changes

  • OpRegistry: a registry for operator labels and their classifications
  • mbpt::Context now holds an OpRegistry along with the CSV setting.
  • There are two predefined registries available (minimal and legacy).
  • Some labels are reserved for internal use (antisymmetrizer, symmetrizer, kroneker, overlap), and cannot be registered.
  • OpType enum is removed, all related logic is gone:
    • OpMaker constructors now use strings.
    • OpConnections now uses strings to specify operator pairs. For example: {{L"f", L"A"}, {L"g", L"A"}, ...}
  • Built-in operators (both tensor and Operator level) check the registry if operators are registered.
  • Perturbation-related operators now support higher-order perturbations (up to 9).
  • mbpt::Context manipulations are thread-safe, and can be toggled by SEQUANT_CONTEXT_MANIPULATION_THREADSAFE option; follows the same pattern as core::Context.

Example Usage

  auto custom_registry = std::make_shared<OpRegistry>();

  custom_registry->add(L"f", OpClass::gen)
      .add(L"g", OpClass::gen)
      .add(L"t", OpClass::ex)
      .add(L"λ", OpClass::deex)
      .add(L"X", OpClass::ex);  // Custom excitation operator

  // set MBPT context
  set_default_mbpt_context({.op_registry_ptr = custom_registry});

  // Built-in operators work as before
  auto t2 = T_(2);  // Uses 't' from registry

  // new custom Operator using OpMaker
  auto X2_tensor = OpMaker<Statistics::FermiDirac>(L"X", 2)();

  // new custom Operator using mbpt::Operator
  auto X2_op = ex<op_t>(
      []() -> std::wstring_view { return L"X"; },
      []() -> ExprPtr { return OpMaker<Statistics::FermiDirac>(L"X", 2)(); },
      [](qns_t& qns) { qns += excitation_type_qns(2); });

  auto expr1 = H_(2) * X2_op * t2;

TODO

  • Remove all use cases of L"A" and L"S" and replace them with functions.
  • Add documentation
  • Cleanup

All connectivity info is now encoded using plain strings
All connectivity info is now encoded using plain strings
…ation order

Constructors now take strings as Operator identifiers
…f perturbation order.

Perturbation order does not make any difference on how the operator acts, so it is only for bookkeeping here.
All methods except A and S will check registry make sure the Operator label is registered.
@ajay-mk ajay-mk force-pushed the ajay/feature/runtime-op-support branch from fe8a7d9 to 1fe1c93 Compare December 26, 2025 19:23
@ajay-mk ajay-mk force-pushed the ajay/feature/runtime-op-support branch from 4040245 to 40e6366 Compare December 28, 2025 18:44
@ajay-mk ajay-mk marked this pull request as ready for review December 28, 2025 20:52
@ajay-mk ajay-mk requested a review from evaleev December 28, 2025 20:52
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