microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/_device/_device.py
144lines · 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 | Args: |
| 62 | qubit_id (int): The id of the qubit. |
| 63 | |
| 64 | Returns: |
| 65 | tuple[int, int]: The (row, column) location of the qubit. |
| 66 | """ |
| 67 | if qubit_id < 0 or qubit_id >= len(self.home_locs): |
| 68 | raise ValueError(f"Qubit id {qubit_id} is out of range") |
| 69 | return self.home_locs[qubit_id] |
| 70 | |
| 71 | def get_ordering(self, qubit_id: int) -> int: |
| 72 | """ |
| 73 | Get the ordering index of the qubit with the given id. |
| 74 | |
| 75 | Args: |
| 76 | qubit_id (int): The id of the qubit. |
| 77 | |
| 78 | Returns: |
| 79 | int: The ordering index of the qubit. |
| 80 | """ |
| 81 | if qubit_id < 0 or qubit_id >= len(self.home_locs): |
| 82 | raise ValueError(f"Qubit id {qubit_id} is out of range") |
| 83 | row, col = self.home_locs[qubit_id] |
| 84 | return row * self.column_count + col |
| 85 | |
| 86 | def get_register_zones(self) -> list[Zone]: |
| 87 | """ |
| 88 | Get the register zones in the device. |
| 89 | |
| 90 | Returns: |
| 91 | list[Zone]: The register zones. |
| 92 | """ |
| 93 | return [zone for zone in self.zones if zone.type == ZoneType.REG] |
| 94 | |
| 95 | def get_interaction_zones(self) -> list[Zone]: |
| 96 | """ |
| 97 | Get the interaction zones in the device. |
| 98 | |
| 99 | Returns: |
| 100 | list[Zone]: The interaction zones. |
| 101 | """ |
| 102 | return [zone for zone in self.zones if zone.type == ZoneType.INTER] |
| 103 | |
| 104 | def get_measurement_zones(self) -> list[Zone]: |
| 105 | """ |
| 106 | Get the measurement zones in the device. |
| 107 | |
| 108 | Returns: |
| 109 | list[Zone]: The measurement zones. |
| 110 | """ |
| 111 | return [zone for zone in self.zones if zone.type == ZoneType.MEAS] |
| 112 | |
| 113 | def compile(self, program: str) -> QirInputData: |
| 114 | """ |
| 115 | Compile the given program for the device. |
| 116 | |
| 117 | Args: |
| 118 | program (str): The program to compile. |
| 119 | """ |
| 120 | raise NotImplementedError("Subclasses must implement compile") |
| 121 | |
| 122 | def as_dict(self) -> dict: |
| 123 | """ |
| 124 | Get the device layout as a dictionary. |
| 125 | |
| 126 | Returns: |
| 127 | dict: The device layout as a dictionary. |
| 128 | """ |
| 129 | return { |
| 130 | "cols": self.column_count, |
| 131 | "zones": [ |
| 132 | {"title": zone.name, "rows": zone.row_count, "kind": zone.type.value} |
| 133 | for zone in self.zones |
| 134 | ], |
| 135 | } |
| 136 | |
| 137 | def get_layout(self) -> dict: |
| 138 | """ |
| 139 | Get the device layout as a dictionary. |
| 140 | |
| 141 | Returns: |
| 142 | dict: The device layout as a dictionary. |
| 143 | """ |
| 144 | return self.as_dict() |
| 145 | |