Refactor get_random_contraction_path to use opt_einsum for better initial paths#47
Draft
Refactor get_random_contraction_path to use opt_einsum for better initial paths#47
get_random_contraction_path to use opt_einsum for better initial paths#47Conversation
… 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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The original
get_random_contraction_pathimplementation generated paths with extremely high contraction costs. This frequently resulted inOverflowError(integers too large to convert to floats) when these paths were processed by the C++ API.To mitigate this,
get_random_contraction_pathnow leveragesopt_einsum.contract_pathwith aRandomGreedyoptimizer. This approach produces more efficient initial contraction paths within a tunable time limit (max_time), effectively preventing overflows.Key changes:
get_random_contraction_pathintnco/utils/tn.pyusingopt_einsum.get_random_contraction_pathAPI to requireoutput_indsanddims, and added amax_timeparameter for the greedy search.opt_einsumfrom a testing dependency to a core dependency inpyproject.toml.tnco/appand the test suite to support the new API and configurablerandom_greedy_max_time.include/tnco/ctree.hppandtnco/ctree.py.OverflowErrorhandling intests/test_core.pyto gracefully skip tests if costs remain excessively large.