-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[cirqflow] Hardcoded qubit placement #5194
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
CirqBot
merged 10 commits into
quantumlib:master
from
mpharrigan:2022-01-qubit-placement-c
Apr 22, 2022
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
284cbb7
[cirqflow] Hardcoded Qubit Placement
mpharrigan 4eadf50
coverage
mpharrigan ab34237
autosave strikes again
mpharrigan 19c7438
what is happening
mpharrigan 767cb06
Update cirq-core/cirq/devices/named_topologies.py
mpharrigan a25ef29
some of the review comments
mpharrigan 80c3627
Merge remote-tracking branch 'origin/master' into 2022-01-qubit-place…
mpharrigan 2511293
other review comments
mpharrigan 85872bd
document exception
mpharrigan dae0ed0
Merge branch 'master' into 2022-01-qubit-placement-c
CirqBot 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 |
---|---|---|
|
@@ -102,6 +102,7 @@ | |
LineTopology, | ||
TiltedSquareLattice, | ||
get_placements, | ||
is_valid_placement, | ||
draw_placements, | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
LineTopology, | ||
TiltedSquareLattice, | ||
get_placements, | ||
is_valid_placement, | ||
draw_placements, | ||
) | ||
|
||
|
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 |
---|---|---|
|
@@ -290,13 +290,45 @@ def get_placements( | |
return small_to_bigs | ||
|
||
|
||
def _is_valid_placement_helper( | ||
big_graph: nx.Graph, small_mapped: nx.Graph, small_to_big_mapping: Dict | ||
): | ||
"""Helper function for `is_valid_placement` that assumes the mapping of `small_graph` has | ||
already occurred. | ||
|
||
This is so we don't duplicate work when checking placements during `draw_placements`. | ||
""" | ||
subgraph = big_graph.subgraph(small_to_big_mapping.values()) | ||
return (subgraph.nodes == small_mapped.nodes) and (subgraph.edges == small_mapped.edges) | ||
|
||
|
||
def is_valid_placement(big_graph: nx.Graph, small_graph: nx.Graph, small_to_big_mapping: Dict): | ||
"""Return whether the given placement is a valid placement of small_graph onto big_graph. | ||
|
||
This is done by making sure all the nodes and edges on the mapped version of `small_graph` | ||
are present in `big_graph`. | ||
|
||
Args: | ||
big_graph: A larger graph we're placing `small_graph` onto. | ||
small_graph: A smaller, (potential) sub-graph to validate the given mapping. | ||
small_to_big_mapping: A mappings from `small_graph` nodes to `big_graph` | ||
nodes. After the mapping occurs, we check whether all of the mapped nodes and | ||
edges exist on `big_graph`. | ||
""" | ||
small_mapped = nx.relabel_nodes(small_graph, small_to_big_mapping) | ||
return _is_valid_placement_helper( | ||
big_graph=big_graph, small_mapped=small_mapped, small_to_big_mapping=small_to_big_mapping | ||
) | ||
|
||
|
||
def draw_placements( | ||
big_graph: nx.Graph, | ||
small_graph: nx.Graph, | ||
small_to_big_mappings: Sequence[Dict], | ||
max_plots: int = 20, | ||
axes: Sequence[plt.Axes] = None, | ||
tilted=True, | ||
bad_placement_callback=None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Add type annotation (preferably also to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done; thanks |
||
): | ||
"""Draw a visualization of placements from small_graph onto big_graph using Matplotlib. | ||
|
||
|
@@ -312,6 +344,9 @@ def draw_placements( | |
`max_plots` plots. | ||
axes: Optional list of matplotlib Axes to contain the drawings. | ||
tilted: Whether to draw gridlike graphs in the ordinary cartesian or tilted plane. | ||
bad_placement_callback: If provided, we check that the given mappings are valid. If not, | ||
this callback is called. The callback should accept `ax` and `i` keyword arguments | ||
for the current axis and mapping index, respectively. | ||
""" | ||
if len(small_to_big_mappings) > max_plots: | ||
# coverage: ignore | ||
|
@@ -331,6 +366,15 @@ def draw_placements( | |
ax = plt.gca() | ||
|
||
small_mapped = nx.relabel_nodes(small_graph, small_to_big_map) | ||
if bad_placement_callback is not None: | ||
# coverage: ignore | ||
if not _is_valid_placement_helper( | ||
big_graph=big_graph, | ||
small_mapped=small_mapped, | ||
small_to_big_mapping=small_to_big_map, | ||
): | ||
bad_placement_callback(ax=ax, i=i) | ||
|
||
draw_gridlike(big_graph, ax=ax, tilted=tilted) | ||
draw_gridlike( | ||
small_mapped, | ||
|
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.