skillfed

cotengra

Hyper optimized contraction trees for large tensor networks and einsums.

cotengra v0.8.2 177.8K downloads/30d#10,206 on PyPI247
Permissive license Apache-2.0 Active released

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 cotengra

uv

uv add cotengra

poetry

poetry add cotengra

Installing 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

Development Status :: 4 - BetaIntended Audience :: Science/ResearchLicense :: OSI Approved :: Apache Software LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: Scientific/EngineeringTopic :: Scientific/Engineering :: Physics

Tags

tensor network contraction optimizationeinsum path optimizationtensor contraction orderinglarge tensor network solvereinsum replacement librarycontraction tree buildertensor slicing and parallelism
tensor-networksscientific-computingoptimization

More Scientific/Engineering packages