-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Adds AbstractInitialMapper base class and IdentityInitialMapper #5829
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
tanujkhattar
merged 29 commits into
quantumlib:master
from
ammareltigani:routing-initial_mapping_setup
Aug 19, 2022
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
62100c1
added abstract initial mapper and identity initial mapper
ammareltigani 9df051b
added __str__ and __repr__ for MappingManager
ammareltigani 8610ae2
minor bug
ammareltigani d9a7a3c
made MappingManager not serializable
ammareltigani c774674
removed unused import
ammareltigani 24acc3d
addressed comments
ammareltigani dfe80a9
fixed bug with edges not being sorted for graph equality testing
ammareltigani 6ee60a9
fixed bug with digraphs repr method in MappingManager and added test …
ammareltigani 9eb1a82
rebase
ammareltigani 5d6ab84
minor lint fix
ammareltigani 388fff4
addressed comments
ammareltigani cdae41b
addressed some comments
ammareltigani c899e16
changed interface for AbstractInitialMapper
ammareltigani 87bcb0c
made MappingManager serializable
ammareltigani ee6474b
removed print statements
ammareltigani 7491ab7
ready for merging
ammareltigani bce5384
nit
ammareltigani 6155032
temp
ammareltigani f39646a
fix lint
ammareltigani 7e54d20
removed serialization
ammareltigani 9306fb9
removed unused imports
ammareltigani d54b510
addressed comments and made HardCodedInitialMapper not serializable
ammareltigani bab9639
fixed nit
ammareltigani d261a19
fixed raises docstring
ammareltigani e4788aa
merging with #5828
ammareltigani 0c28588
forgot to add import statement
ammareltigani 63b7c31
import bug
ammareltigani 6e502fa
removed debug print
ammareltigani 3dab6f4
Merge branch 'master' into routing-initial_mapping_setup
ammareltigani 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
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
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 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import TYPE_CHECKING, Dict | ||
import abc | ||
|
||
from cirq import value | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
|
||
class AbstractInitialMapper(metaclass=abc.ABCMeta): | ||
"""Base class for creating custom initial mapping strategies. | ||
|
||
An initial mapping strategy is a placement strategy that places logical qubit variables in an | ||
input circuit onto physical qubits that correspond to a specified device. This placment can be | ||
thought of as a mapping k -> m[k] where k is a logical qubit and m[k] is the physical qubit it | ||
is mapped to. Any initial mapping strategy must satisfy two constraints: | ||
1. all logical qubits must be placed on the device if the number of logical qubits is <= | ||
than the number of physical qubits. | ||
2. if two logical qubits interact (i.e. there exists a 2-qubit operation on them) at any | ||
point in the input circuit, then they must lie in the same connected components of the | ||
device graph induced on the physical qubits in the initial mapping. | ||
|
||
""" | ||
|
||
@abc.abstractmethod | ||
def initial_mapping(self, circuit: 'cirq.AbstractCircuit') -> Dict['cirq.Qid', 'cirq.Qid']: | ||
"""Maps the logical qubits of a circuit onto physical qubits on a device. | ||
|
||
Args: | ||
circuit: the input circuit with logical qubits. | ||
|
||
Returns: | ||
qubit_map: the initial mapping of logical qubits to physical qubits. | ||
""" | ||
|
||
|
||
@value.value_equality | ||
class HardCodedInitialMapper(AbstractInitialMapper): | ||
"""Initial Mapper class takes a hard-coded mapping and returns it.""" | ||
|
||
def __init__(self, _map: Dict['cirq.Qid', 'cirq.Qid']) -> None: | ||
self._map = _map | ||
|
||
def initial_mapping(self, circuit: 'cirq.AbstractCircuit') -> Dict['cirq.Qid', 'cirq.Qid']: | ||
"""Returns the hard-coded initial mapping. | ||
|
||
Args: | ||
circuit: the input circuit with logical qubits. | ||
|
||
Returns: | ||
the hard-codded initial mapping. | ||
|
||
Raises: | ||
ValueError: if the qubits in circuit are not a subset of the qubit keys in the mapping. | ||
""" | ||
if not circuit.all_qubits().issubset(set(self._map.keys())): | ||
raise ValueError("The qubits in circuit must be a subset of the keys in the mapping") | ||
return self._map | ||
ammareltigani marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _value_equality_values_(self): | ||
return tuple(sorted(self._map.items())) | ||
|
||
def __repr__(self) -> str: | ||
return f'cirq.HardCodedInitialMapper({self._map})' |
32 changes: 32 additions & 0 deletions
32
cirq-core/cirq/transformers/routing/initial_mapper_test.py
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,32 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
import cirq | ||
|
||
|
||
def test_hardcoded_initial_mapper(): | ||
input_map = {cirq.NamedQubit(str(i)): cirq.NamedQubit(str(-i)) for i in range(1, 6)} | ||
circuit = cirq.Circuit([cirq.H(cirq.NamedQubit(str(i))) for i in range(1, 6)]) | ||
initial_mapper = cirq.HardCodedInitialMapper(input_map) | ||
|
||
assert input_map == initial_mapper.initial_mapping(circuit) | ||
assert str(initial_mapper) == f'cirq.HardCodedInitialMapper({input_map})' | ||
cirq.testing.assert_equivalent_repr(initial_mapper) | ||
|
||
circuit.append(cirq.H(cirq.NamedQubit(str(6)))) | ||
with pytest.raises( | ||
ValueError, match="The qubits in circuit must be a subset of the keys in the mapping" | ||
): | ||
initial_mapper.initial_mapping(circuit) |
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.