|
| 1 | +# Copyright 2022 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import TYPE_CHECKING, Dict |
| 16 | +import abc |
| 17 | + |
| 18 | +from cirq import value |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + import cirq |
| 22 | + |
| 23 | + |
| 24 | +class AbstractInitialMapper(metaclass=abc.ABCMeta): |
| 25 | + """Base class for creating custom initial mapping strategies. |
| 26 | +
|
| 27 | + An initial mapping strategy is a placement strategy that places logical qubit variables in an |
| 28 | + input circuit onto physical qubits that correspond to a specified device. This placment can be |
| 29 | + thought of as a mapping k -> m[k] where k is a logical qubit and m[k] is the physical qubit it |
| 30 | + is mapped to. Any initial mapping strategy must satisfy two constraints: |
| 31 | + 1. all logical qubits must be placed on the device if the number of logical qubits is <= |
| 32 | + than the number of physical qubits. |
| 33 | + 2. if two logical qubits interact (i.e. there exists a 2-qubit operation on them) at any |
| 34 | + point in the input circuit, then they must lie in the same connected components of the |
| 35 | + device graph induced on the physical qubits in the initial mapping. |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + @abc.abstractmethod |
| 40 | + def initial_mapping(self, circuit: 'cirq.AbstractCircuit') -> Dict['cirq.Qid', 'cirq.Qid']: |
| 41 | + """Maps the logical qubits of a circuit onto physical qubits on a device. |
| 42 | +
|
| 43 | + Args: |
| 44 | + circuit: the input circuit with logical qubits. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + qubit_map: the initial mapping of logical qubits to physical qubits. |
| 48 | + """ |
| 49 | + |
| 50 | + |
| 51 | +@value.value_equality |
| 52 | +class HardCodedInitialMapper(AbstractInitialMapper): |
| 53 | + """Initial Mapper class takes a hard-coded mapping and returns it.""" |
| 54 | + |
| 55 | + def __init__(self, _map: Dict['cirq.Qid', 'cirq.Qid']) -> None: |
| 56 | + self._map = _map |
| 57 | + |
| 58 | + def initial_mapping(self, circuit: 'cirq.AbstractCircuit') -> Dict['cirq.Qid', 'cirq.Qid']: |
| 59 | + """Returns the hard-coded initial mapping. |
| 60 | +
|
| 61 | + Args: |
| 62 | + circuit: the input circuit with logical qubits. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + the hard-codded initial mapping. |
| 66 | +
|
| 67 | + Raises: |
| 68 | + ValueError: if the qubits in circuit are not a subset of the qubit keys in the mapping. |
| 69 | + """ |
| 70 | + if not circuit.all_qubits().issubset(set(self._map.keys())): |
| 71 | + raise ValueError("The qubits in circuit must be a subset of the keys in the mapping") |
| 72 | + return self._map |
| 73 | + |
| 74 | + def _value_equality_values_(self): |
| 75 | + return tuple(sorted(self._map.items())) |
| 76 | + |
| 77 | + def __repr__(self) -> str: |
| 78 | + return f'cirq.HardCodedInitialMapper({self._map})' |
0 commit comments