Skip to content

Refactor get_random_contraction_path to use opt_einsum for better initial paths#47

Draft
s-mandra wants to merge 2 commits intomainfrom
improve-random-contraction-path
Draft

Refactor get_random_contraction_path to use opt_einsum for better initial paths#47
s-mandra wants to merge 2 commits intomainfrom
improve-random-contraction-path

Conversation

@s-mandra
Copy link
Copy Markdown
Collaborator

The original get_random_contraction_path implementation generated paths with extremely high contraction costs. This frequently resulted in OverflowError (integers too large to convert to floats) when these paths were processed by the C++ API.

To mitigate this, get_random_contraction_path now leverages opt_einsum.contract_path with a RandomGreedy optimizer. This approach produces more efficient initial contraction paths within a tunable time limit (max_time), effectively preventing overflows.

Key changes:

  • Re-implemented get_random_contraction_path in tnco/utils/tn.py using opt_einsum.
  • Updated the get_random_contraction_path API to require output_inds and dims, and added a max_time parameter for the greedy search.
  • Promoted opt_einsum from a testing dependency to a core dependency in pyproject.toml.
  • Updated all callers in tnco/app and the test suite to support the new API and configurable random_greedy_max_time.
  • Fixed a bug in scalar contraction validation (where shared indices are empty) in both include/tnco/ctree.hpp and tnco/ctree.py.
  • Added OverflowError handling in tests/test_core.py to gracefully skip tests if costs remain excessively large.

… initial paths

The original `get_random_contraction_path` implementation generated
paths with extremely high contraction costs due to its purely random
nature. This frequently resulted in `OverflowError` (integers too large
to convert to floats) when these paths were processed by the C++ API.

To mitigate this, `get_random_contraction_path` now leverages
`opt_einsum.contract_path` with a `RandomGreedy` optimizer. This
approach produces more efficient initial contraction paths within a
tunable time limit (`max_time`), effectively preventing overflows.

Key changes:
- Re-implemented `get_random_contraction_path` in `tnco/utils/tn.py`
  using `opt_einsum`.
- Updated the `get_random_contraction_path` API to require `output_inds`
  and `dims`, and added a `max_time` parameter for the greedy search.
- Promoted `opt_einsum` from a testing dependency to a core dependency
  in `pyproject.toml`.
- Updated all callers in `tnco/app` and the test suite to support the
  new API and configurable `random_greedy_max_time`.
- Fixed a bug in scalar contraction validation (where shared indices are
  empty) in both `include/tnco/ctree.hpp` and `tnco/ctree.py`.
- Added `OverflowError` handling in `tests/test_core.py` to gracefully
  skip tests if costs remain excessively large.
@s-mandra s-mandra self-assigned this Mar 10, 2026
@s-mandra s-mandra added the enhancement New feature or request label Mar 10, 2026
@s-mandra s-mandra marked this pull request as draft March 11, 2026 15:54
At the moment, `tnco` guarantees that multiple calls, even on different
python environments, will provide the same results. However,
`opt_einsum.contract_path` does not provide a way to fix the seed across
multiple Python environments.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant