microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/_device/_device.py
139lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from enum import Enum |
| 5 | from .._qsharp import QirInputData |
| 6 | |
| 7 | |
| 8 | class ZoneType(Enum): |
| 9 | """ |
| 10 | Enum representing different types of zones in the device layout. |
| 11 | """ |
| 12 | |
| 13 | REG = "register" |
| 14 | INTER = "interaction" |
| 15 | MEAS = "measurement" |
| 16 | |
| 17 | |
| 18 | class Zone: |
| 19 | """ |
| 20 | Represents a zone in the device layout. |
| 21 | """ |
| 22 | |
| 23 | offset: int = 0 |
| 24 | |
| 25 | def __init__(self, name: str, row_count: int, type: ZoneType): |
| 26 | self.name = name |
| 27 | self.row_count = row_count |
| 28 | self.type = type |
| 29 | |
| 30 | def set_offset(self, offset: int): |
| 31 | self.offset = offset |
| 32 | |
| 33 | |
| 34 | class Device: |
| 35 | """ |
| 36 | Represents a quantum device with specific layout expressed as zones. |
| 37 | """ |
| 38 | |
| 39 | def __init__(self, column_count: int, zones: list[Zone]): |
| 40 | self.column_count = column_count |
| 41 | self.zones = zones |
| 42 | offset = 0 |
| 43 | # Ensure the zones have correct offsets set based on their ordering when passed in. |
| 44 | for zone in self.zones: |
| 45 | zone.set_offset(offset) |
| 46 | offset += zone.row_count * self.column_count |
| 47 | |
| 48 | self.home_locs = [] |
| 49 | self._init_home_locs() |
| 50 | |
| 51 | def _init_home_locs(self): |
| 52 | """ |
| 53 | Initialize the home locations of qubits in the device layout. |
| 54 | """ |
| 55 | raise NotImplementedError("Subclasses must implement _init_home_locs") |
| 56 | |
| 57 | def get_home_loc(self, qubit_id: int) -> tuple[int, int]: |
| 58 | """ |
| 59 | Get the home location (row, column) of the qubit with the given id. |
| 60 | |
| 61 | :param qubit_id: The id of the qubit. |
| 62 | :return: The (row, column) location of the qubit. |
| 63 | :rtype: tuple[int, int] |
| 64 | """ |
| 65 | if qubit_id < 0 or qubit_id >= len(self.home_locs): |
| 66 | raise ValueError(f"Qubit id {qubit_id} is out of range") |
| 67 | return self.home_locs[qubit_id] |
| 68 | |
| 69 | def get_ordering(self, qubit_id: int) -> int: |
| 70 | """ |
| 71 | Get the ordering index of the qubit with the given id. |
| 72 | |
| 73 | :param qubit_id: The id of the qubit. |
| 74 | :return: The ordering index of the qubit. |
| 75 | :rtype: int |
| 76 | """ |
| 77 | if qubit_id < 0 or qubit_id >= len(self.home_locs): |
| 78 | raise ValueError(f"Qubit id {qubit_id} is out of range") |
| 79 | row, col = self.home_locs[qubit_id] |
| 80 | return row * self.column_count + col |
| 81 | |
| 82 | def get_register_zones(self) -> list[Zone]: |
| 83 | """ |
| 84 | Get the register zones in the device. |
| 85 | |
| 86 | :return: The register zones. |
| 87 | :rtype: list[Zone] |
| 88 | """ |
| 89 | return [zone for zone in self.zones if zone.type == ZoneType.REG] |
| 90 | |
| 91 | def get_interaction_zones(self) -> list[Zone]: |
| 92 | """ |
| 93 | Get the interaction zones in the device. |
| 94 | |
| 95 | :return: The interaction zones. |
| 96 | :rtype: list[Zone] |
| 97 | """ |
| 98 | return [zone for zone in self.zones if zone.type == ZoneType.INTER] |
| 99 | |
| 100 | def get_measurement_zones(self) -> list[Zone]: |
| 101 | """ |
| 102 | Get the measurement zones in the device. |
| 103 | |
| 104 | :return: The measurement zones. |
| 105 | :rtype: list[Zone] |
| 106 | """ |
| 107 | return [zone for zone in self.zones if zone.type == ZoneType.MEAS] |
| 108 | |
| 109 | def compile(self, program: str) -> QirInputData: |
| 110 | """ |
| 111 | Compile the given program for the device. |
| 112 | |
| 113 | :param program: The program to compile. |
| 114 | """ |
| 115 | raise NotImplementedError("Subclasses must implement compile") |
| 116 | |
| 117 | def as_dict(self) -> dict: |
| 118 | """ |
| 119 | Get the device layout as a dictionary. |
| 120 | |
| 121 | :return: The device layout as a dictionary. |
| 122 | :rtype: dict |
| 123 | """ |
| 124 | return { |
| 125 | "cols": self.column_count, |
| 126 | "zones": [ |
| 127 | {"title": zone.name, "rows": zone.row_count, "kind": zone.type.value} |
| 128 | for zone in self.zones |
| 129 | ], |
| 130 | } |
| 131 | |
| 132 | def get_layout(self) -> dict: |
| 133 | """ |
| 134 | Get the device layout as a dictionary. |
| 135 | |
| 136 | :return: The device layout as a dictionary. |
| 137 | :rtype: dict |
| 138 | """ |
| 139 | return self.as_dict() |
| 140 | |