Skip to content

Fix cotengra bug for empty contraction paths with tests#114

Open
refraction-ray wants to merge 1 commit into
masterfrom
fix-cotengra-empty-path-3005992988580253242
Open

Fix cotengra bug for empty contraction paths with tests#114
refraction-ray wants to merge 1 commit into
masterfrom
fix-cotengra-empty-path-3005992988580253242

Conversation

@refraction-ray

Copy link
Copy Markdown
Member

Fixed a bug in _algebraic_base_contraction where cotengra would fail when given an empty contraction path (0 or 1 tensors). Added explicit handling for 0 and 1 tensor cases to bypass cotengra and return the correct result. Also added a guard for nodes[0] access to prevent IndexError when no nodes are provided. Added comprehensive test cases in tests/test_hyperedge.py covering 0 nodes, 1 node, and 1 node with a self-loop (trace).


PR created automatically by Jules for task 3005992988580253242 started by @refraction-ray

- 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>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Comment thread tests/test_hyperedge.py
# 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]

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tensorcircuit/cons.py
Comment on lines +595 to +598
if len(raw_tensors) == 0:
# Avoid cotengra bug for empty contraction paths
final_raw_tensor = be.ones([])
exponent = 0.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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.")

Comment thread tests/test_hyperedge.py
Comment on lines +551 to +552
from tensorcircuit.cons import _algebraic_base_contraction
import opt_einsum

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. Imports should be at the top of the file. (link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant