tensorcircuit-nightly 1.3.0.dev20250731__py3-none-any.whl → 1.3.0.dev20250801__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tensorcircuit-nightly might be problematic. Click here for more details.

tensorcircuit/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "1.3.0.dev20250731"
1
+ __version__ = "1.3.0.dev20250801"
2
2
  __author__ = "TensorCircuit Authors"
3
3
  __creator__ = "refraction-ray"
4
4
 
@@ -15,6 +15,7 @@ from typing import (
15
15
  Union,
16
16
  TYPE_CHECKING,
17
17
  cast,
18
+ Set,
18
19
  )
19
20
 
20
21
  logger = logging.getLogger(__name__)
@@ -1446,3 +1447,54 @@ class CustomizeLattice(AbstractLattice):
1446
1447
  logger.info(
1447
1448
  f"{len(ids_to_remove)} sites removed. Lattice now has {self.num_sites} sites."
1448
1449
  )
1450
+
1451
+
1452
+ def get_compatible_layers(bonds: List[Tuple[int, int]]) -> List[List[Tuple[int, int]]]:
1453
+ """
1454
+ Partitions a list of pairs (bonds) into compatible layers for parallel
1455
+ gate application using a greedy edge-coloring algorithm.
1456
+
1457
+ This function takes a list of pairs, representing connections like
1458
+ nearest-neighbor (NN) or next-nearest-neighbor (NNN) bonds, and
1459
+ partitions them into the minimum number of sets ("layers") where no two
1460
+ pairs in a set share an index. This is a general utility for scheduling
1461
+ non-overlapping operations.
1462
+
1463
+ :Example:
1464
+
1465
+ >>> from tensorcircuit.templates.lattice import SquareLattice
1466
+ >>> sq_lattice = SquareLattice(size=(2, 2), pbc=False)
1467
+ >>> nn_bonds = sq_lattice.get_neighbor_pairs(k=1, unique=True)
1468
+
1469
+ >>> gate_layers = get_compatible_layers(nn_bonds)
1470
+ >>> print(gate_layers)
1471
+ [[[0, 1], [2, 3]], [[0, 2], [1, 3]]]
1472
+
1473
+ :param bonds: A list of tuples, where each tuple represents a bond (i, j)
1474
+ of site indices to be scheduled.
1475
+ :type bonds: List[Tuple[int, int]]
1476
+ :return: A list of layers. Each layer is a list of tuples, where each
1477
+ tuple represents a bond. All bonds within a layer are non-overlapping.
1478
+ :rtype: List[List[Tuple[int, int]]]
1479
+ """
1480
+ uncolored_edges: Set[Tuple[int, int]] = {(min(bond), max(bond)) for bond in bonds}
1481
+
1482
+ layers: List[List[Tuple[int, int]]] = []
1483
+
1484
+ while uncolored_edges:
1485
+ current_layer: List[Tuple[int, int]] = []
1486
+ qubits_in_this_layer: Set[int] = set()
1487
+
1488
+ edges_to_process = sorted(list(uncolored_edges))
1489
+
1490
+ for edge in edges_to_process:
1491
+ i, j = edge
1492
+ if i not in qubits_in_this_layer and j not in qubits_in_this_layer:
1493
+ current_layer.append(edge)
1494
+ qubits_in_this_layer.add(i)
1495
+ qubits_in_this_layer.add(j)
1496
+
1497
+ uncolored_edges -= set(current_layer)
1498
+ layers.append(sorted(current_layer))
1499
+
1500
+ return layers
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tensorcircuit-nightly
3
- Version: 1.3.0.dev20250731
3
+ Version: 1.3.0.dev20250801
4
4
  Summary: nightly release for tensorcircuit
5
5
  Home-page: https://github.com/refraction-ray/tensorcircuit-dev
6
6
  Author: TensorCircuit Authors
@@ -1,4 +1,4 @@
1
- tensorcircuit/__init__.py,sha256=3yUehn2Xo7YNGDZZMC3I-Pz8sbvVzWsuXV0XDFWkGoQ,2055
1
+ tensorcircuit/__init__.py,sha256=IL3iNxbN-D630LW977RNmxFHWyPUPXy8CxLIq0cZ8f0,2055
2
2
  tensorcircuit/about.py,sha256=DazTswU2nAwOmASTaDII3L04PVtaQ7oiWPty5YMI3Wk,5267
3
3
  tensorcircuit/abstractcircuit.py,sha256=0osacPqq7B1EJki-cI1aLYoVRmjFaG9q3XevWMs7SsA,44125
4
4
  tensorcircuit/asciiart.py,sha256=neY1OWFwtoW5cHPNwkQHgRPktDniQvdlP9QKHkk52fM,8236
@@ -84,9 +84,9 @@ tensorcircuit/templates/conversions.py,sha256=D3chiKDr7G1ekCJngiol91k9iqrMag1DZQ
84
84
  tensorcircuit/templates/dataset.py,sha256=ldPvCUlwjHU_S98E2ISQp34KqJzJPpPHmDIKJ4K-qYo,1933
85
85
  tensorcircuit/templates/graphs.py,sha256=cPYrxjoem0xZ-Is9dZKAvEzWZL_FejfIRiCEOTA4qd4,3935
86
86
  tensorcircuit/templates/hamiltonians.py,sha256=Ag8djD6lckTeU7I99gCbXiQAb2VYqzm_p7-hpXo-5u4,5554
87
- tensorcircuit/templates/lattice.py,sha256=F35ebANk0DSmSHLR0-Q_hUbcznyCmZjb4fKmvCMywmA,58575
87
+ tensorcircuit/templates/lattice.py,sha256=P64OGUedE3o8vWekhM8XAs5nUe5CdG-gojLlTGA20TI,60534
88
88
  tensorcircuit/templates/measurements.py,sha256=pzc5Aa9S416Ilg4aOY77Z6ZhUlYcXnAkQNQFTuHjFFs,10943
89
- tensorcircuit_nightly-1.3.0.dev20250731.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
89
+ tensorcircuit_nightly-1.3.0.dev20250801.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
90
90
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
91
91
  tests/conftest.py,sha256=J9nHlLE3Zspz1rMyzadEuBWhaS5I4Q9sq0lnWybcdIA,1457
92
92
  tests/test_backends.py,sha256=MLxpRWavWF0qWcjVI61lIa3CYGbztvTO8ITFOYX47ao,38312
@@ -102,7 +102,7 @@ tests/test_gates.py,sha256=rAIV2QFpFsA5bT1QivTSkhdarvwu5t0N3IOz4SEDrzg,4593
102
102
  tests/test_hamiltonians.py,sha256=E0E5ABhUeG7XLMLRkb3AIAPi7aJgnIeMWTgqzF1Q6yc,5724
103
103
  tests/test_interfaces.py,sha256=iJPmes8S8HkA9_PGjsu4Ike-vCXYyS1EMgnNKKXDNaU,16938
104
104
  tests/test_keras.py,sha256=U453jukavmx0RMeTSDEgPzrNdHNEfK1CW0CqO3XCNKo,4841
105
- tests/test_lattice.py,sha256=_ptDVK3EhS-X5fCQWiP8sHk3azdyGFuwqg6KMkBTkDE,65789
105
+ tests/test_lattice.py,sha256=DJoQ3Dr6uAHrdaKofEApc2LD8FgjYAH_a3Ux0cIkgO8,68917
106
106
  tests/test_miscs.py,sha256=4fXKsW0kYu2JYO0iGlwWLAYlkFD1rfeVc4xG4Zjn5FQ,8935
107
107
  tests/test_mpscircuit.py,sha256=mDXX8oQeFeHr_PdZvwqyDs_tVcVAqLmCERqlTAU7590,10552
108
108
  tests/test_noisemodel.py,sha256=UYoMtCjwDaB-CCn5kLosofz-qTMiY4KGAFBjVtqqLPE,5637
@@ -118,7 +118,7 @@ tests/test_templates.py,sha256=Xm9otFFaaBWG9TZpgJ-nNh9MBfRipTzFWL8fBOnie2k,7192
118
118
  tests/test_timeevol.py,sha256=zz17x21C-5f8ZvcgkXm30JzLgZMhsKaOCzyHCyS43h0,20333
119
119
  tests/test_torchnn.py,sha256=CHLTfWkF7Ses5_XnGFN_uv_JddfgenFEFzaDtSH8XYU,2848
120
120
  tests/test_van.py,sha256=kAWz860ivlb5zAJuYpzuBe27qccT-Yf0jatf5uXtTo4,3163
121
- tensorcircuit_nightly-1.3.0.dev20250731.dist-info/METADATA,sha256=QqZGkP34B7fJ3L38AiVOyyAgoYx_pZlrkWyWx8DcWEs,34922
122
- tensorcircuit_nightly-1.3.0.dev20250731.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
123
- tensorcircuit_nightly-1.3.0.dev20250731.dist-info/top_level.txt,sha256=O_Iqeh2x02lasEYMI9iyPNNNtMzcpg5qvwMOkZQ7n4A,20
124
- tensorcircuit_nightly-1.3.0.dev20250731.dist-info/RECORD,,
121
+ tensorcircuit_nightly-1.3.0.dev20250801.dist-info/METADATA,sha256=nHOy6uHlzXTlcH8Iq1t8lJKer9bJ0bTrkamRBgO2l1Q,34922
122
+ tensorcircuit_nightly-1.3.0.dev20250801.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
123
+ tensorcircuit_nightly-1.3.0.dev20250801.dist-info/top_level.txt,sha256=O_Iqeh2x02lasEYMI9iyPNNNtMzcpg5qvwMOkZQ7n4A,20
124
+ tensorcircuit_nightly-1.3.0.dev20250801.dist-info/RECORD,,
tests/test_lattice.py CHANGED
@@ -23,6 +23,8 @@ from tensorcircuit.templates.lattice import (
23
23
  RectangularLattice,
24
24
  SquareLattice,
25
25
  TriangularLattice,
26
+ AbstractLattice,
27
+ get_compatible_layers,
26
28
  )
27
29
 
28
30
 
@@ -1664,3 +1666,85 @@ class TestDistanceMatrix:
1664
1666
  # "The specialized PBC implementation is significantly slower "
1665
1667
  # "than the general-purpose implementation."
1666
1668
  # )
1669
+
1670
+
1671
+ def _validate_layers(bonds, layers) -> None:
1672
+ """
1673
+ A helper function to scientifically validate the output of get_compatible_layers.
1674
+ """
1675
+ # MODIFICATION: This function now takes the original bonds list for comparison.
1676
+ expected_edges = set(tuple(sorted(b)) for b in bonds)
1677
+ actual_edges = set(tuple(sorted(edge)) for layer in layers for edge in layer)
1678
+
1679
+ assert (
1680
+ expected_edges == actual_edges
1681
+ ), "Completeness check failed: The set of all edges in the layers must "
1682
+ "exactly match the input bonds."
1683
+
1684
+ for i, layer in enumerate(layers):
1685
+ qubits_in_layer: set[int] = set()
1686
+ for edge in layer:
1687
+ q1, q2 = edge
1688
+ assert (
1689
+ q1 not in qubits_in_layer
1690
+ ), f"Compatibility check failed: Qubit {q1} is reused in layer {i}."
1691
+ qubits_in_layer.add(q1)
1692
+ assert (
1693
+ q2 not in qubits_in_layer
1694
+ ), f"Compatibility check failed: Qubit {q2} is reused in layer {i}."
1695
+ qubits_in_layer.add(q2)
1696
+
1697
+
1698
+ @pytest.mark.parametrize(
1699
+ "lattice_instance",
1700
+ [
1701
+ SquareLattice(size=(3, 2), pbc=False),
1702
+ SquareLattice(size=(3, 3), pbc=True),
1703
+ HoneycombLattice(size=(2, 2), pbc=False),
1704
+ ],
1705
+ ids=[
1706
+ "SquareLattice_3x2_OBC",
1707
+ "SquareLattice_3x3_PBC",
1708
+ "HoneycombLattice_2x2_OBC",
1709
+ ],
1710
+ )
1711
+ def test_layering_on_various_lattices(lattice_instance: AbstractLattice):
1712
+ """Tests gate layering for various standard lattice types."""
1713
+ bonds = lattice_instance.get_neighbor_pairs(k=1, unique=True)
1714
+ layers = get_compatible_layers(bonds)
1715
+
1716
+ assert len(layers) > 0, "Layers should not be empty for non-trivial lattices."
1717
+ _validate_layers(bonds, layers)
1718
+
1719
+
1720
+ def test_layering_on_1d_chain_pbc():
1721
+ """Test layering on a 1D chain with periodic boundaries (a cycle graph)."""
1722
+ lattice_even = ChainLattice(size=(6,), pbc=True)
1723
+ bonds_even = lattice_even.get_neighbor_pairs(k=1, unique=True)
1724
+ layers_even = get_compatible_layers(bonds_even)
1725
+ _validate_layers(bonds_even, layers_even)
1726
+
1727
+ lattice_odd = ChainLattice(size=(5,), pbc=True)
1728
+ bonds_odd = lattice_odd.get_neighbor_pairs(k=1, unique=True)
1729
+ layers_odd = get_compatible_layers(bonds_odd)
1730
+ assert len(layers_odd) == 3, "A 5-site cycle graph should be 3-colorable."
1731
+ _validate_layers(bonds_odd, layers_odd)
1732
+
1733
+
1734
+ def test_layering_on_custom_star_graph():
1735
+ """Test layering on a custom lattice forming a star graph."""
1736
+ star_edges = [(0, 1), (0, 2), (0, 3)]
1737
+ layers = get_compatible_layers(star_edges)
1738
+ assert len(layers) == 3, "A star graph S_4 requires 3 layers."
1739
+ _validate_layers(star_edges, layers)
1740
+
1741
+
1742
+ def test_layering_on_edge_cases():
1743
+ """Test various edge cases: empty, single-site, and no-edge lattices."""
1744
+ layers_empty = get_compatible_layers([])
1745
+ assert layers_empty == [], "Layers should be empty for an empty set of bonds."
1746
+
1747
+ single_edge = [(0, 1)]
1748
+ layers_single = get_compatible_layers(single_edge)
1749
+ assert layers_single == [[(0, 1)]]
1750
+ _validate_layers(single_edge, layers_single)