Fix cotengra bug for empty contraction paths with tests#114
Fix cotengra bug for empty contraction paths with tests#114refraction-ray wants to merge 1 commit into
Conversation
- Handle len(raw_tensors) == 0 by returning a scalar identity tensor. - Handle len(raw_tensors) == 1 by using be.einsum directly. - Guard against IndexError when accessing nodes[0].backend. - Ensure exponent is initialized for these edge cases. - Add edge case tests in tests/test_hyperedge.py for 0 and 1 tensor cases. Co-authored-by: refraction-ray <35157286+refraction-ray@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
| # 1 node with self-loop (trace) | ||
| # _extract_topology handles traces by mapping them to symbols | ||
| b = tn.Node(tc.backend.convert_to_tensor(np.eye(2))) | ||
| b[0] ^ b[1] |
There was a problem hiding this comment.
Code Review
This pull request updates _algebraic_base_contraction in tensorcircuit/cons.py to handle empty node lists and contraction paths by providing a default backend and returning a scalar tensor. It also introduces unit tests for these edge cases in tests/test_hyperedge.py. Reviewers identified a potential crash when handling networks consisting solely of CopyNodes with dangling edges and recommended refining the logic to distinguish between empty node lists and empty tensor lists. Furthermore, moving local imports to the top of the test file was suggested to align with PEP 8 guidelines.
| if len(raw_tensors) == 0: | ||
| # Avoid cotengra bug for empty contraction paths | ||
| final_raw_tensor = be.ones([]) | ||
| exponent = 0.0 |
There was a problem hiding this comment.
The check len(raw_tensors) == 0 correctly handles the case where nodes is empty, but it also catches cases where nodes contains only CopyNodes (hyperedges). In the latter case, if there are dangling edges, returning a scalar ones([]) will cause a crash at line 632 because the tensor rank (0) will not match the number of dangling edges. It is safer to explicitly check if nodes is empty for the scalar 1.0 result, or handle the CopyNode-only case specifically.
| if len(raw_tensors) == 0: | |
| # Avoid cotengra bug for empty contraction paths | |
| final_raw_tensor = be.ones([]) | |
| exponent = 0.0 | |
| if len(nodes) == 0: | |
| # Avoid cotengra bug for empty contraction paths | |
| final_raw_tensor = be.ones([]) | |
| exponent = 0.0 | |
| elif len(raw_tensors) == 0: | |
| # Networks with only CopyNodes are not yet supported in the algebraic path | |
| # as they require constructing a dense delta tensor. | |
| raise ValueError("Algebraic contraction of networks with only CopyNodes is not supported.") |
| from tensorcircuit.cons import _algebraic_base_contraction | ||
| import opt_einsum |
There was a problem hiding this comment.
It is generally preferred to place imports at the top of the file to adhere to PEP 8 guidelines and improve readability, unless there is a specific reason for a local import (e.g., avoiding circular dependencies). Since tensorcircuit is already imported as tc, you can also access the function via tc.cons._algebraic_base_contraction.
References
- Imports should be at the top of the file. (link)




Fixed a bug in
_algebraic_base_contractionwherecotengrawould fail when given an empty contraction path (0 or 1 tensors). Added explicit handling for 0 and 1 tensor cases to bypasscotengraand return the correct result. Also added a guard fornodes[0]access to preventIndexErrorwhen no nodes are provided. Added comprehensive test cases intests/test_hyperedge.pycovering 0 nodes, 1 node, and 1 node with a self-loop (trace).PR created automatically by Jules for task 3005992988580253242 started by @refraction-ray