|
| 1 | +import abc |
| 2 | +import typing |
| 3 | +from dataclasses import field, dataclass |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from kirin.interp import Interpreter |
| 8 | +from typing_extensions import Self |
| 9 | +from kirin.interp.exceptions import InterpreterError |
| 10 | + |
| 11 | +from bloqade.pyqrack.reg import Measurement |
| 12 | + |
| 13 | +if typing.TYPE_CHECKING: |
| 14 | + from pyqrack import QrackSimulator |
| 15 | + |
| 16 | + |
| 17 | +class PyQrackOptions(typing.TypedDict): |
| 18 | + qubitCount: int |
| 19 | + isTensorNetwork: bool |
| 20 | + isSchmidtDecomposeMulti: bool |
| 21 | + isSchmidtDecompose: bool |
| 22 | + isStabilizerHybrid: bool |
| 23 | + isBinaryDecisionTree: bool |
| 24 | + isPaged: bool |
| 25 | + isCpuGpuHybrid: bool |
| 26 | + isOpenCL: bool |
| 27 | + |
| 28 | + |
| 29 | +def _default_pyqrack_args() -> PyQrackOptions: |
| 30 | + return PyQrackOptions( |
| 31 | + qubitCount=-1, |
| 32 | + isTensorNetwork=False, |
| 33 | + isSchmidtDecomposeMulti=True, |
| 34 | + isSchmidtDecompose=True, |
| 35 | + isStabilizerHybrid=True, |
| 36 | + isBinaryDecisionTree=True, |
| 37 | + isPaged=True, |
| 38 | + isCpuGpuHybrid=True, |
| 39 | + isOpenCL=True, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +@dataclass |
| 44 | +class MemoryABC(abc.ABC): |
| 45 | + pyqrack_options: PyQrackOptions = field(default_factory=_default_pyqrack_args) |
| 46 | + sim_reg: "QrackSimulator" = field(init=False) |
| 47 | + |
| 48 | + @abc.abstractmethod |
| 49 | + def allocate(self, n_qubits: int) -> tuple[int, ...]: |
| 50 | + """Allocate `n_qubits` qubits and return their ids.""" |
| 51 | + ... |
| 52 | + |
| 53 | + def reset(self): |
| 54 | + """Reset the memory, releasing all qubits.""" |
| 55 | + from pyqrack import QrackSimulator |
| 56 | + |
| 57 | + # do not reset the simulator it might be used by |
| 58 | + # results of the simulation |
| 59 | + self.sim_reg = QrackSimulator(**self.pyqrack_options) |
| 60 | + |
| 61 | + |
| 62 | +@dataclass |
| 63 | +class MockMemory(MemoryABC): |
| 64 | + """Mock memory for testing purposes.""" |
| 65 | + |
| 66 | + allocated: int = field(init=False, default=0) |
| 67 | + |
| 68 | + def allocate(self, n_qubits: int): |
| 69 | + allocated = self.allocated + n_qubits |
| 70 | + result = tuple(range(self.allocated, allocated)) |
| 71 | + self.allocated = allocated |
| 72 | + return result |
| 73 | + |
| 74 | + def reset(self): |
| 75 | + self.allocated = 0 |
| 76 | + self.sim_reg = Mock() |
| 77 | + |
| 78 | + |
| 79 | +@dataclass |
| 80 | +class StackMemory(MemoryABC): |
| 81 | + total: int = field(kw_only=True) |
| 82 | + allocated: int = field(init=False, default=0) |
| 83 | + |
| 84 | + def allocate(self, n_qubits: int): |
| 85 | + curr_allocated = self.allocated |
| 86 | + self.allocated += n_qubits |
| 87 | + |
| 88 | + if self.allocated > self.total: |
| 89 | + raise InterpreterError( |
| 90 | + f"qubit allocation exceeds memory, " |
| 91 | + f"{self.total} qubits, " |
| 92 | + f"{self.allocated} allocated" |
| 93 | + ) |
| 94 | + |
| 95 | + return tuple(range(curr_allocated, self.allocated)) |
| 96 | + |
| 97 | + def reset(self): |
| 98 | + super().reset() |
| 99 | + self.allocated = 0 |
| 100 | + |
| 101 | + |
| 102 | +@dataclass |
| 103 | +class DynamicMemory(MemoryABC): |
| 104 | + def __post_init__(self): |
| 105 | + self.reset() |
| 106 | + |
| 107 | + if self.sim_reg.is_tensor_network: |
| 108 | + raise ValueError("DynamicMemory does not support tensor networks") |
| 109 | + |
| 110 | + def allocate(self, n_qubits: int): |
| 111 | + start = self.sim_reg.num_qubits() |
| 112 | + for i in range(start, start + n_qubits): |
| 113 | + self.sim_reg.allocate_qubit(i) |
| 114 | + |
| 115 | + return tuple(range(start, start + n_qubits)) |
| 116 | + |
| 117 | + |
| 118 | +@dataclass |
| 119 | +class PyQrackInterpreter(Interpreter): |
| 120 | + keys = ["pyqrack", "main"] |
| 121 | + memory: MemoryABC = field(kw_only=True) |
| 122 | + rng_state: np.random.Generator = field( |
| 123 | + default_factory=np.random.default_rng, kw_only=True |
| 124 | + ) |
| 125 | + loss_m_result: Measurement = field(default=Measurement.One, kw_only=True) |
| 126 | + """The value of a measurement result when a qubit is lost.""" |
| 127 | + |
| 128 | + def initialize(self) -> Self: |
| 129 | + super().initialize() |
| 130 | + self.memory.reset() # reset allocated qubits |
| 131 | + return self |
0 commit comments