cotengra is a Python library designed for the hyper-optimized contraction of large tensor networks and einsums. It provides advanced pathfinding algorithms, including those based on hyper-optimization, to minimize computational cost (FLOPs, memory). The current version is 0.7.5, and it is actively maintained with regular releases focusing on performance enhancements, new optimization strategies, and bug fixes.
pip install cotengraVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a tensor network using an einsum expression and array shapes, initialize a `ContractionTree`, tune its contraction path using cotengra's hyper-optimization, and then perform the contraction. It also shows how to get the resulting shape and the optimized path.
Explicitly set `n_workers` in `HyperOptimizer` or `ContractionTree` methods, or manage the `COTENGRA_NUM_WORKERS` environment variable for consistent parallel execution behavior.
To explicitly use `optuna` or `cmaes`, specify `optimizer_options={'optlib': 'optuna'}` or `{'optlib': 'cmaes'}` when initializing `HyperOptimizer` or calling `tree.tune()`.Review code that relies on the exact numerical representation of exponents when using `strip_exponent` with slicing, especially for zero-value slices. Adapt expectations for `float('-inf')` and combined exponents.Ensure `optuna` or `cmaes` are installed (e.g., `pip install cotengra[optuna,cmaes]`) if you intend to use advanced hyper-optimization features to avoid runtime warnings and ensure full functionality.
Install the required optional dependency: `pip install cotengra[optuna]` or `pip install cotengra[cmaes]` (or both with `pip install cotengra[optuna,cmaes]`).
Carefully check the `expr` string and ensure that the `shapes` provided in the list match the order and dimensions of the indices for each tensor.
When using `HyperOptimizer` or `ContractionTree.tune()` in an interactive environment, consider setting `n_workers=1` explicitly. If using a script with custom multiprocessing, ensure code that creates child processes is guarded by `if __name__ == '__main__':`.