Skip to content
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

Add nodes_to_linequbits method to LineTopology #5821

Merged
merged 5 commits into from
Aug 11, 2022

Conversation

Caffetaria
Copy link
Contributor

@Caffetaria Caffetaria commented Aug 9, 2022

Resolves #4710

Co-authored-by: Pavol Juhas juhas@google.com

@Caffetaria Caffetaria requested review from a team, vtomole and cduck as code owners August 9, 2022 10:53
@Caffetaria Caffetaria requested a review from pavoljuhas August 9, 2022 10:54
@CirqBot CirqBot added the size: S 10< lines changed <50 label Aug 9, 2022
@@ -142,6 +142,13 @@ def draw(self, ax=None, tilted: bool = True, **kwargs) -> Dict[Any, Tuple[int, i
g2 = nx.relabel_nodes(self.graph, {n: (n, 1) for n in self.graph.nodes})
return draw_gridlike(g2, ax=ax, tilted=tilted, **kwargs)

def nodes_to_linequbits(self, offset=0) -> Dict[Tuple[int, int], 'cirq.LineQubit']:
Copy link
Collaborator

Choose a reason for hiding this comment

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

LineQubit-s have only one index for their position so the return type should be Dict[int, 'cirq.LineQubit'].

cirq-core/cirq/devices/named_topologies.py Show resolved Hide resolved
def nodes_to_linequbits(self, offset=0) -> Dict[Tuple[int, int], 'cirq.LineQubit']:
"""Return a mapping from graph nodes to `cirq.LineQubit`
Args:
offset: Offset LineQubits by this amount.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's clarify a bit - how about Offset integer position of the resultant LineQubits by this amount.

@tanujkhattar - can you confirm we should have the offset argument for the 1D case here as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I think it makes sense to have the offset argument for 1-D case. Also, @Caffetaria please add type annotations to offset

Args:
offset: Offset LineQubits by this amount.
"""
return {(r, 1): LineQubit(r) + offset for r in self.graph.nodes}
Copy link
Collaborator

Choose a reason for hiding this comment

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

dictionary keys should be integers from 0 to n_nodes - 1.
We can also reduce the number of calls to the LineQubit constructor -
please consider (following #4710 (comment))

return dict(enumerate(LineQubit.range(offset, offset + self.n_nodes)))

Copy link
Contributor Author

@Caffetaria Caffetaria Aug 10, 2022

Choose a reason for hiding this comment

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

@pavoljuhas The draw method outputs tuples even for the LineTopology.

topology = LineTopology(5)
print(topology.draw(tilted = False))

Outputs:

{(0, 1): (1, 1), (1, 1): (2, 0), (2, 1): (3, -1), (3, 1): (4, -2), (4, 1): (5, -3)}
Screenshot 2022-08-10 at 6 51 36 AM

Given this, should the nodes for LineTopology be represented as integers or tuples?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The issue #4710 (comment) that this PR is for states that nodes_to_linequbits method is needed for relabeling nodes with

routing_graph = nx.relabel_nodes(topology.graph, topology.nodes_to_linequbits())

That does not work if it returns a dictionary with a pair-tuple keys instead of integers that are in the graph.

The issue does not say anything about drawing the graph or using nodes_to_linequbits for such.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, got it, thanks! Made the fixes.

@@ -85,6 +85,9 @@ def test_line_topology():
assert LineTopology(2).n_nodes == 2
assert LineTopology(2).graph.number_of_nodes() == 2

mapping = topo.nodes_to_linequbits(offset=3)
assert all(isinstance(q, cirq.LineQubit) and q >= cirq.LineQubit(0) for q in mapping.values())
Copy link
Collaborator

Choose a reason for hiding this comment

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

The test does not check keys in the mappings dictionary. It will also pass if values were generated with offset=2 instead of 3; please adjust so it only passes when the offset is exactly 3.

Also it is better to check only one condition per assert statement so this should rather be split to two assertions for (i) the type and (ii) specific values of the LineQubits in the mapping dictionary.

@pavoljuhas pavoljuhas requested a review from tanujkhattar August 9, 2022 18:12
@Caffetaria Caffetaria requested a review from pavoljuhas August 11, 2022 02:47
Copy link
Collaborator

@pavoljuhas pavoljuhas left a comment

Choose a reason for hiding this comment

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

Two final small suggestions - please add them in.

Otherwise it looks good to me. Thank you!

Caffetaria and others added 2 commits August 12, 2022 02:17
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
Co-authored-by: Pavol Juhas <pavol.juhas@gmail.com>
@Caffetaria
Copy link
Contributor Author

Caffetaria commented Aug 11, 2022

Two final small suggestions - please add them in.

Otherwise it looks good to me. Thank you!

Thanks! I've added the changes.

@pavoljuhas pavoljuhas added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Aug 11, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Aug 11, 2022
@CirqBot CirqBot merged commit f39bb8a into quantumlib:master Aug 11, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Aug 11, 2022
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Resolves quantumlib#4710

Co-authored-by: Pavol Juhas <juhas@google.com>
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Resolves quantumlib#4710

Co-authored-by: Pavol Juhas <juhas@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Named Topologies] Add nodes_to_linequbits method to LineTopology
4 participants