NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Optimizing Tensor Contraction Paths: A Greedy Algorithm Approach With Improved Cost Functions

Sheela Orgler, Mark BlacherarXiv 2024 · DOI

Abstract

Tensor contraction is a fundamental operation in quantum computing, machine learning, and model counting. Finding the optimal contraction order, or "path," is an NP-hard problem that significantly impacts computational efficiency. This paper introduces a modified greedy algorithm that utilizes multiple specialized cost functions to identify high-quality contraction paths. By evaluating various heuristics at runtime, the proposed approach achieves significant speedups and successfully processes massive tensor networks where existing state-of-the-art tools fail.

Introduction

Tensor contraction serves as the multidimensional extension of matrix multiplication, forming the computational core of diverse fields such as quantum circuit simulation and large-scale machine learning. However, the efficiency of these operations is highly sensitive to the contraction path—the specific sequence in which tensors are combined. For instance, changing the grouping of a three-matrix product can result in orders of magnitude difference in operation count.

Visual representation of tensor indices and the contraction of two tensors A and B over a shared index k.

In networks involving hundreds or thousands of tensors, determining the optimal path is computationally prohibitive. While libraries like opt_einsum and cotengra offer greedy and hypergraph-partitioning solutions, they often encounter scalability issues or excessive search times when faced with very large problem instances.

Methods: A Multi-Heuristic Greedy Approach

The researchers developed a modified greedy algorithm based on the framework of the opt_einsum library. Unlike standard greedy implementations that rely on a single heuristic (typically minimizing the size of the resulting intermediate tensor), this new approach incorporates:

  1. Custom Heuristics: Support for diverse cost functions tailored to different network topologies.
  2. Runtime Selection: The algorithm evaluates multiple cost functions against the specific problem instance to select the most effective one.
  3. Structural Optimization: Different cost functions are shown to influence the "shape" of the resulting contraction tree, allowing the algorithm to adapt to the natural structure of the data.

Key Insights: The Importance of Tree Shape

A critical finding of this work is that the "best" path is often determined by the overall architecture of the contraction tree rather than just local step-by-step minimization.

A contraction tree illustrating the hierarchical sequence of operations for tensors A, B, C, and D.

The choice of cost function directly dictates whether the resulting tree is "balanced" or "skewed." For example, a cost function defined as size12 - min(size1, size2) tends to produce balanced trees, which are advantageous for certain problem domains, while size12 - max(size1, size2) favors skewed structures. Different scientific domains naturally favor one over the other.

Experimental Results

The algorithm was evaluated across 40 problem instances, including model counting (up to 10,699 tensors), quantum circuits (up to 3,206 tensors), and various graph-based problems.

  • Efficiency: In time-constrained scenarios (e.g., a one-second timeout), the multi-heuristic approach consistently outperformed standard greedy and random-greedy variants across all categories.
  • Scalability: The method successfully generated paths for massive model counting instances that caused existing tools like opt_einsum and cotengra's random greedy variants to crash or fail.
  • Quantum Performance: For quantum circuit simulations, the new approach yielded lower total floating-point operations (FLOPs) compared to standard greedy implementations.
  • Resource Management: The algorithm is notably lightweight, performing effectively on standard consumer hardware like a MacBook Pro, making it accessible for researchers with limited computational resources.

Conclusion

By moving beyond a one-size-fits-all heuristic, the modified greedy algorithm provides a robust and efficient solution for tensor contraction path optimization. While sophisticated hypergraph partitioners may still find marginally better paths given unlimited time, the proposed method offers a superior balance of search speed and path quality, particularly for large-scale and resource-constrained applications. Future work will focus on refining automatic heuristic selection and extending the approach to exascale tensor networks.

Summary updated

BibTeX
@misc{orgler2024optimizing,
  author = {Sheela Orgler and Mark Blacher},
  title = {{Optimizing Tensor Contraction Paths: A Greedy Algorithm Approach With Improved Cost Functions}},
  year = {2024},
  eprint = {2405.09644},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.09644},
}
Figure