Skip to content

Conversation

@Stellogic
Copy link
Contributor

Introduces the get_nn_gate_layers utility function. This function takes a lattice object and partitions its nearest-neighbor pairs into the minimum number of compatible layers for parallel two-qubit gate application.

This implementation uses a greedy edge-coloring algorithm to ensure that no two gates within the same layer act on the same qubit. The output is deterministic, with both layers and the edges within them being sorted.

This functionality is essential for efficiently scheduling gates in algorithms like Trotterized Hamiltonian evolution and directly addresses Task 2 of the lattice API follow-up plan.

Introduces the `get_nn_gate_layers` utility function. This function takes a lattice object and partitions its nearest-neighbor pairs into the minimum number of compatible layers for parallel two-qubit gate application.

This implementation uses a greedy edge-coloring algorithm to ensure that no two gates within the same layer act on the same qubit. The output is deterministic, with both layers and the edges within them being sorted.

This functionality is essential for efficiently scheduling gates in algorithms like Trotterized Hamiltonian evolution and directly addresses Task 2 of the lattice API follow-up plan.
@Stellogic
Copy link
Contributor Author

Hi @refraction-ray,

I've just opened this Pull Request for Task 2, which implements the get_nn_gate_layers function as we discussed.

This should be ready for your review when you have a moment. Please let me know if you have any suggestions or if any changes are needed.

Thanks again for your guidance!

@refraction-ray
Copy link
Member

some general suggestions:

  1. just add the helper function in tc.tempaltes.lattice, no need for new independent test and moduel files
  2. the function signature better be get_compatible_layers(List[Tuple[int, int]]). By this function, we can not only get compatible layers of NN bonds, we can also similarly get NNN bonds arangement etc.
  3. with the lattice class and this helper function, you can implement a 2d circuit construction and vqe example in /examples similar to https://github.com/tensorcircuit/tensorcircuit-ng/blob/master/examples/vqe2d_gpu.py, where the Grid2D_entangling blocks are replaced by individual two qubit gates, say c.rzz(i, j, theta=) where i, j are neighbor pairs of given lattice arranged in the order compatible layers by get_compatible_layers helper function

@Stellogic
Copy link
Contributor Author

Sorry, I pushed the wrong version

@Stellogic Stellogic force-pushed the feature/gate-layering branch from d7578f1 to ebdebc9 Compare July 30, 2025 07:40
@Stellogic
Copy link
Contributor Author

@refraction-ray
Thank you for the guidance.
I have modified the file according to the guidance, moving the functions to lattice.py.
I also wrote the vqe2d_lattice.py script, but this file takes a very long time to run on my computer. I'm not sure if it's a performance issue with my computer or a problem with the file itself.

K = tc.set_backend("jax")
tc.set_dtype("complex64")
# On Windows, cotengra's multiprocessing can cause issues.
tc.set_contractor("cotengra-8192-8192", parallel=False)
Copy link
Member

Choose a reason for hiding this comment

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

parallel=False is subtle (very slow), maybe you can try other parallel options to see whether they work in windows? https://cotengra.readthedocs.io/en/latest/advanced.html#parallelization

@@ -1,5 +1,6 @@
from unittest.mock import patch
import logging
from typing import List, Set, Tuple
Copy link
Member

Choose a reason for hiding this comment

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

we dont need to add type annotation for test functions, though it is okay to have them

for i in range(nlayers):
for layer in gate_layers:
for j, k in layer:
c.rzz(int(j), int(k), theta=param[i, 0])
Copy link
Member

@refraction-ray refraction-ray Jul 30, 2025

Choose a reason for hiding this comment

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

we should make each parameter different for each gate, a naive and also bruteforce way is to use a one-dimensional tensor for param, and for each use of param, we call param[count] then count+=1

# )


class MockLattice(AbstractLattice):
Copy link
Member

Choose a reason for hiding this comment

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

what is this class for, I dont see it being used, maybe deleted?

@refraction-ray
Copy link
Member

overall, very nice. Leave some minor comments to be fixed.

@refraction-ray
Copy link
Member

refraction-ray commented Jul 30, 2025

for the slow speed a better way is to try the script with n=4, m=2, and no tc.set_conctractor, you can debug in this settings and commit with m=4 and set_contractor

@Stellogic
Copy link
Contributor Author

Stellogic commented Aug 1, 2025

@refraction-ray
I've updated the code according to the guidelines. I encountered some network issues yesterday that prevented me from pushing the changes. Everything is resolved now.

@refraction-ray refraction-ray merged commit 1e06171 into tensorcircuit:master Aug 1, 2025
1 check passed
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.

2 participants