cotengra
Hyper optimized contraction trees for large tensor networks and einsums.
What it is and what it does
Cotengra is a Python library for optimizing the contraction of large tensor networks and einsum expressions. It addresses a core computational challenge: when contracting many tensors together, the order in which you combine them dramatically affects memory use and runtime. Cotengra provides multiple strategies—including a hyper optimizer that samples and tunes contraction trees, simulated annealing, and dynamic slicing—to find efficient contraction orders. It works as a drop-in replacement for numpy.einsum and ncon, and can generate paths compatible with opt_einsum, quimb, and other libraries.
The package is built on autoray, which abstracts tensor operations across different backends, so you can optimize contractions for tensors from many libraries even if they don't natively support einsum. It's designed for scientific and engineering workflows involving large hypergraph tensor networks, particularly in physics and machine learning applications where contraction cost dominates.
Use it for:
- Optimize contraction order for quantum circuit simulations or tensor network states in physics.
- Find efficient einsum paths for large matrix chain multiplications in machine learning pipelines.
- Parallelize and reduce memory footprint of tensor contractions via dynamic slicing.
- Integrate optimized contraction strategies into existing code using einsum or ncon without rewriting.
- Benchmark and compare contraction performance across different tensor libraries via autoray.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Cotengra optimizes the contraction order of large tensor networks and einsum expressions, providing drop-in replacements for einsum and ncon with advanced slicing and path-finding strategies.
Yes, if you work with large tensor networks or complex einsum expressions where contraction order matters. The library is actively maintained, has no known vulnerabilities, installs with minimal friction, and is permissively licensed. It's most valuable in scientific computing and physics simulations; less critical for small-scale tensor operations where contraction cost is negligible.
Install
cotengra on PyPI
pip
pip install cotengrauv
uv add cotengrapoetry
poetry add cotengraInstalling cotengra
Before you install
Low friction—pure Python wheel with a single runtime dependency (autoray). Actively maintained with a recent release and no known vulnerabilities.
License in practice
Apache-2.0 permissive license allows commercial and private use with minimal restrictions; you may use, modify, and distribute the package freely provided you include the license notice.
Quickstart
pip install cotengra
import cotengra as ctg
import autoray as ar
# Find optimized contraction path for einsum expression
path_info = ctg.contract_path('ij,jk->ik', ar.ones((10, 10)), ar.ones((10, 10)))
Requires Python 3.10 or later.
Verify before relying
- Whether the hyper optimizer's performance gains justify overhead for small tensor networks.
- How dynamic slicing memory savings scale with network size and sparsity patterns.
- Compatibility and performance with tensor libraries beyond those explicitly mentioned.
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — autoray |
| Maintenance | actively maintained — 53 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 177,800/month — #10,206 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: cotengra-0.8.2-py3-none-any.whl
Keywords: contraction, einsum, graph, hypergraph, network, partition, tensor
Tags
More Scientific/Engineering packages
NumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
pandaspandas provides fast, flexible data structures…
permissive · top 100 on PyPI
scipyscipy provides numerical algorithms for…
permissive · top 100 on PyPI
scikit-learnscikit-learn provides a comprehensive Python…
permissive · top 1,000 on PyPI
dilldill extends Python's pickle module to…
permissive · top 1,000 on PyPI
multiprocessMultiprocess is an enhanced fork of Python's…
permissive · top 1,000 on PyPI
opt-einsumOptimizes the contraction order of einsum…
permissive · top 1,000 on PyPI
opt-einsum-fxOptimizes PyTorch einsum operations and…
permissive · top 15,000 on PyPI
einopsEinops provides readable tensor operations…
permissive · top 1,000 on PyPI
spo4onnxApplies specialized optimization techniques to…
permissive · top 15,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI
onnx-graphsurgeonONNX GraphSurgeon lets you programmatically…
permissive · top 15,000 on PyPI
e3nne3nn provides PyTorch-based operations for…
permissive · top 15,000 on PyPI
instanttensorInstantTensor provides a high-throughput…
permissive · top 15,000 on PyPI
onnxoptimizerApplies graph-level optimizations to ONNX…
permissive · top 15,000 on PyPI
einops-extsProvides helper functions that extend Einops, a…
permissive · top 15,000 on PyPI