-
Notifications
You must be signed in to change notification settings - Fork 14
Support hyperedges in TensorCircuit with cotengra #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
refraction-ray
merged 9 commits into
master
from
hyperedge-support-16871959254681859317
Feb 26, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
126f2cc
Support hyperedges in TensorCircuit using cotengra.
google-labs-jules[bot] 852b902
Support hyperedges in TensorCircuit using cotengra.
google-labs-jules[bot] ae02ae0
Support hyperedges in TensorCircuit using cotengra.
google-labs-jules[bot] c497e26
Support hyperedges in TensorCircuit using cotengra.
google-labs-jules[bot] d41341a
Support hyperedges in TensorCircuit using cotengra with optimized exe…
google-labs-jules[bot] b1ffff1
Support hyperedges in TensorCircuit using cotengra with optimized exe…
google-labs-jules[bot] fa7f09e
Support hyperedges in TensorCircuit using cotengra with optimized exe…
google-labs-jules[bot] e712310
Support hyperedges in TensorCircuit using cotengra with optimized exe…
google-labs-jules[bot] 836cadd
Support hyperedges in TensorCircuit using cotengra with optimized exe…
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """ | ||
| Demonstration of hyperedge support using cotengra in TensorCircuit. | ||
| """ | ||
|
|
||
| import time | ||
| import numpy as np | ||
| import tensornetwork as tn | ||
| import tensorcircuit as tc | ||
|
|
||
|
|
||
| def hyperedge_demo(): | ||
| print("Demonstrating hyperedge contraction with cotengra...") | ||
|
|
||
| # 1. Single Hyperedge Example | ||
| # Three tensors A, B, C connected by a single hyperedge (CopyNode) | ||
| # Result should be sum_i A_i * B_i * C_i | ||
|
|
||
| dim = 2 | ||
| a = tn.Node(np.array([1.0, 2.0]), name="A") | ||
| b = tn.Node(np.array([1.0, 2.0]), name="B") | ||
| c = tn.Node(np.array([1.0, 2.0]), name="C") | ||
| cn = tn.CopyNode(3, dim, name="CN") | ||
|
|
||
| a[0] ^ cn[0] | ||
| b[0] ^ cn[1] | ||
| c[0] ^ cn[2] | ||
|
|
||
| nodes = [a, b, c, cn] | ||
|
|
||
| # Set contractor to cotengra | ||
| tc.set_contractor("cotengra") | ||
|
|
||
| res = tc.contractor(nodes) | ||
| print("Single Hyperedge Result:", res.tensor) | ||
| expected = 1 * 1 * 1 + 2 * 2 * 2 | ||
| print(f"Expected: {expected}") | ||
| assert np.allclose(res.tensor, expected) | ||
|
|
||
| # 2. Large Scale Hyperedge Example | ||
| # Demonstrate memory and time efficiency with a large number of legs | ||
| print("\nDemonstrating large scale hyperedge (20 legs)...") | ||
| num_legs = 20 | ||
| dim = 2 | ||
|
|
||
| # Create 20 random tensors connected to a single CopyNode | ||
| input_tensors = [ | ||
| tn.Node(np.random.rand(dim), name=f"T{i}") for i in range(num_legs) | ||
| ] | ||
| cn_large = tn.CopyNode(num_legs, dim, name="CN_Large") | ||
|
|
||
| for i, t in enumerate(input_tensors): | ||
| t[0] ^ cn_large[i] | ||
|
|
||
| large_nodes = input_tensors + [cn_large] | ||
|
|
||
| start_time = time.time() | ||
| res_large = tc.contractor(large_nodes) | ||
| end_time = time.time() | ||
|
|
||
| print(f"Contracted {num_legs} legs in {end_time - start_time:.4f} seconds.") | ||
| print("Large Hyperedge Result shape:", res_large.tensor.shape) | ||
|
|
||
| # Verification: Explicitly calculate the sum | ||
| # result = sum_k (prod_i T_i[k]) | ||
|
|
||
| # Transpose input tensors to shape (num_legs, dim) | ||
| tensor_matrix = np.stack([t.tensor for t in input_tensors]) | ||
| # Product along the tensor axis (0) for each dimension index | ||
| prod_along_legs = np.prod(tensor_matrix, axis=0) | ||
| expected_sum = np.sum(prod_along_legs) | ||
|
|
||
| print(f"Computed: {res_large.tensor}") | ||
| print(f"Expected: {expected_sum}") | ||
| assert np.allclose(res_large.tensor, expected_sum) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| hyperedge_demo() | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should demonstrate something large, say a copynode with 20 legs, to test the memory and time efficiency of the hyperedge solution end to end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a large-scale example with a 20-leg CopyNode to
examples/hyperedge_demo.pyto demonstrate efficiency.