Template Function sequant::opt::detail::single_term_opt_impl

Function Documentation

template<typename CostFn>
EvalSequence sequant::opt::detail::single_term_opt_impl(TensorNetwork const &network, meta::range_of<Index> auto const &tidxs, CostFn &&cost_fn, bool subnet_cse)

Finds the optimal evaluation sequence for a single-term tensor contraction.

This function employs an exhaustive search using dynamic programming to determine the contraction order that minimizes the total cost, as defined by the provided cost function.

The optimization uses a bitmask-based dynamic programming approach where each state represents a subnetwork (subset of tensors). If subnet_cse is enabled, the algorithm precomputes canonical metadata for every possible subnetwork to identify common structures. This allows it to find trees that benefit from reusing intermediate results, which is particularly effective for expressions with repeating tensor patterns.

Template Parameters:

CostFn – A function object type that computes the cost of a single binary contraction. Expected signature:

double(meta::range_of<Index> auto const& lhs,
            meta::range_of<Index> auto const& rhs,
           meta::range_of<Index> auto const& res)

Parameters:
  • network – The TensorNetwork containing the tensors to be contracted.

  • tidxs – The set of indices that should remain open in the final result.

  • cost_fn – The cost model used to evaluate contractions (e.g., flop count).

  • subnet_cse – If true, enables Common Subexpression Elimination (CSE) for equivalent subnetworks. When enabled, the cost of evaluating structurally identical subnetworks is counted only once in the total cost of a contraction tree. Equivalence is determined by canonicalizing the subnetwork graph.

Returns:

An EvalSequence representing the optimal contraction order.