--- id: cotengra version: "0.8.2" license: Apache-2.0 license_treatment: permissive maintenance: active --- # cotengra — Hyper optimized contraction trees for large tensor networks and einsums. License: permissive · Maintenance: active · Downloads: 177.8K/mo ## 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 above — 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 pip install cotengra uv add cotengra 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_current - Install friction: low - Maintenance: active - Downloads: 177.8K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags tensor network contraction optimization, einsum path optimization, tensor contraction ordering, large tensor network solver, einsum replacement library, contraction tree builder, tensor slicing and parallelism, tensor-networks, scientific-computing, optimization [View on SkillFed](https://skillfed.io/packages/cotengra) · [View on PyPI](https://pypi.org/project/cotengra/)