|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from oqd_compiler_infrastructure import Chain, Pre, RewriteRule |
| 15 | +from functools import partial, reduce |
16 | 16 |
|
17 | | -######################################################################################## |
| 17 | +from oqd_compiler_infrastructure import Chain, Post, Pre, RewriteRule |
| 18 | + |
| 19 | +from oqd_core.compiler.math.rules import SubstituteMathVar |
18 | 20 | from oqd_core.interface.atomic import Level, Transition |
| 21 | +from oqd_core.interface.atomic.protocol import ParallelProtocol, SequentialProtocol |
| 22 | +from oqd_core.interface.math import MathVar |
19 | 23 |
|
20 | 24 | ######################################################################################## |
21 | 25 |
|
22 | 26 |
|
23 | 27 | class UnrollLevelLabel(RewriteRule): |
24 | 28 | """ |
25 | 29 | Unrolls the [`Level`][oqd_core.interface.atomic.system.Level] labels present in [`Transitions`][oqd_core.interface.atomic.system.Transition]. |
| 30 | +
|
| 31 | + Args: |
| 32 | + model (AtomicCircuit): The rule only acts on [`AtomicCircuit`][oqd_core.interface.atomic.AtomicCircuit] objects. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + model (AtomicCircuit): |
| 36 | +
|
| 37 | + Assumptions: |
| 38 | + None |
| 39 | +
|
26 | 40 | """ |
27 | 41 |
|
28 | 42 | def map_Ion(self, model): |
@@ -54,6 +68,15 @@ def map_Transition(self, model): |
54 | 68 | class UnrollTransitionLabel(RewriteRule): |
55 | 69 | """ |
56 | 70 | Unrolls the [`Transition`][oqd_core.interface.atomic.system.Transition] labels present in [`Beams`][oqd_core.interface.atomic.protocol.Beam]. |
| 71 | +
|
| 72 | + Args: |
| 73 | + model (AtomicCircuit): The rule only acts on [`AtomicCircuit`][oqd_core.interface.atomic.AtomicCircuit] objects. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + model (AtomicCircuit): |
| 77 | +
|
| 78 | + Assumptions: |
| 79 | + None |
57 | 80 | """ |
58 | 81 |
|
59 | 82 | def map_System(self, model): |
@@ -85,7 +108,210 @@ def map_Beam(self, model): |
85 | 108 | ) |
86 | 109 |
|
87 | 110 |
|
| 111 | +class ResolveNestedProtocol(RewriteRule): |
| 112 | + """ |
| 113 | + Unfolds nested protocols into a standard form with only 2 hierarchy levels, a sequential protocol of parallel protocols. |
| 114 | +
|
| 115 | + Args: |
| 116 | + model (AtomicCircuit): The rule only acts on [`AtomicCircuit`][oqd_core.interface.atomic.AtomicCircuit] objects. |
| 117 | +
|
| 118 | + Returns: |
| 119 | + model (AtomicCircuit): |
| 120 | +
|
| 121 | + Assumptions: |
| 122 | + None |
| 123 | + """ |
| 124 | + |
| 125 | + def __init__(self): |
| 126 | + super().__init__() |
| 127 | + |
| 128 | + self.durations = [] |
| 129 | + |
| 130 | + @classmethod |
| 131 | + def _get_continuous_duration(self, model): |
| 132 | + if isinstance(model, ParallelProtocol): |
| 133 | + if len(model.sequence) == 1: |
| 134 | + return model.sequence[0].duration |
| 135 | + |
| 136 | + return min(map(lambda x: x.duration, model.sequence)) |
| 137 | + |
| 138 | + if isinstance(model, SequentialProtocol): |
| 139 | + return self._get_continuous_duration(model.sequence[0]) |
| 140 | + |
| 141 | + return model.duration |
| 142 | + |
| 143 | + @classmethod |
| 144 | + def _cut_protocol(cls, model, continuous_duration): |
| 145 | + if isinstance(model, ParallelProtocol): |
| 146 | + pairs = list( |
| 147 | + map( |
| 148 | + partial(cls._cut_protocol, continuous_duration=continuous_duration), |
| 149 | + model.sequence, |
| 150 | + ) |
| 151 | + ) |
| 152 | + |
| 153 | + cut = reduce(lambda x, y: x + y, map(lambda x: x[0], pairs)) |
| 154 | + |
| 155 | + remainder = [r for r in map(lambda x: x[1], pairs) if r is not None] |
| 156 | + |
| 157 | + if remainder: |
| 158 | + return cut, ParallelProtocol(sequence=remainder) |
| 159 | + |
| 160 | + return cut, None |
| 161 | + |
| 162 | + if isinstance(model, SequentialProtocol): |
| 163 | + cut, remainder = cls._cut_protocol( |
| 164 | + model.sequence[0], continuous_duration=continuous_duration |
| 165 | + ) |
| 166 | + |
| 167 | + if remainder: |
| 168 | + return cut, SequentialProtocol( |
| 169 | + sequence=[remainder, *model.sequence[1:]] |
| 170 | + ) |
| 171 | + if model.sequence[1:]: |
| 172 | + return cut, SequentialProtocol(sequence=model.sequence[1:]) |
| 173 | + |
| 174 | + return cut, None |
| 175 | + |
| 176 | + cut = model.model_copy(deep=True) |
| 177 | + if cut.duration == continuous_duration: |
| 178 | + return [cut], None |
| 179 | + cut.duration = continuous_duration |
| 180 | + |
| 181 | + remainder = model.model_copy(deep=True) |
| 182 | + remainder.duration = remainder.duration - continuous_duration |
| 183 | + |
| 184 | + return [cut], remainder |
| 185 | + |
| 186 | + def map_ParallelProtocol(self, model): |
| 187 | + sequence = model.sequence |
| 188 | + |
| 189 | + protocols = [] |
| 190 | + while sequence: |
| 191 | + continuous_duration = min(map(self._get_continuous_duration, sequence)) |
| 192 | + |
| 193 | + pairs = list( |
| 194 | + map( |
| 195 | + partial( |
| 196 | + self._cut_protocol, continuous_duration=continuous_duration |
| 197 | + ), |
| 198 | + sequence, |
| 199 | + ) |
| 200 | + ) |
| 201 | + |
| 202 | + protocols.append( |
| 203 | + ParallelProtocol( |
| 204 | + sequence=reduce(lambda x, y: x + y, map(lambda x: x[0], pairs)) |
| 205 | + ) |
| 206 | + ) |
| 207 | + |
| 208 | + sequence = [r for r in map(lambda x: x[1], pairs) if r is not None] |
| 209 | + |
| 210 | + return SequentialProtocol(sequence=protocols) |
| 211 | + |
| 212 | + def map_SequentialProtocol(self, model): |
| 213 | + if len(model.sequence) == 1: |
| 214 | + return model.sequence[0] |
| 215 | + |
| 216 | + new_sequence = [] |
| 217 | + for subprotocol in model.sequence: |
| 218 | + if isinstance(subprotocol, SequentialProtocol): |
| 219 | + new_sequence.extend( |
| 220 | + list( |
| 221 | + map( |
| 222 | + lambda x: x |
| 223 | + if isinstance(x, ParallelProtocol) |
| 224 | + else ParallelProtocol(sequence=[x]), |
| 225 | + subprotocol.sequence, |
| 226 | + ) |
| 227 | + ) |
| 228 | + ) |
| 229 | + elif isinstance(subprotocol, ParallelProtocol): |
| 230 | + new_sequence.append(subprotocol) |
| 231 | + else: |
| 232 | + new_sequence.append(ParallelProtocol(sequence=[subprotocol])) |
| 233 | + return model.__class__(sequence=new_sequence) |
| 234 | + |
| 235 | + def map_Pulse(self, model): |
| 236 | + return SequentialProtocol(sequence=[model]) |
| 237 | + |
| 238 | + |
| 239 | +class ResolveRelativeTime(RewriteRule): |
| 240 | + """ |
| 241 | + Handles conversion of relative time to absolute time. |
| 242 | +
|
| 243 | + Args: |
| 244 | + model (AtomicCircuit): The rule only acts on [`AtomicCircuit`][oqd_core.interface.atomic.AtomicCircuit] objects. |
| 245 | +
|
| 246 | + Returns: |
| 247 | + model (AtomicCircuit): |
| 248 | +
|
| 249 | + Assumptions: |
| 250 | + None |
| 251 | + """ |
| 252 | + |
| 253 | + def __init__(self): |
| 254 | + super().__init__() |
| 255 | + |
| 256 | + def map_AtomicCircuit(self, model): |
| 257 | + protocol = Post( |
| 258 | + SubstituteMathVar( |
| 259 | + variable=MathVar(name="s"), substitution=MathVar(name="t") |
| 260 | + ) |
| 261 | + )(model.protocol) |
| 262 | + |
| 263 | + return model.__class__(system=model.system, protocol=protocol) |
| 264 | + |
| 265 | + @classmethod |
| 266 | + def _get_duration(cls, model): |
| 267 | + if isinstance(model, SequentialProtocol): |
| 268 | + return reduce( |
| 269 | + lambda x, y: x + y, |
| 270 | + [cls._get_duration(p) for p in model.sequence], |
| 271 | + ) |
| 272 | + if isinstance(model, ParallelProtocol): |
| 273 | + return max( |
| 274 | + *[cls._get_duration(p) for p in model.sequence], |
| 275 | + ) |
| 276 | + return model.duration |
| 277 | + |
| 278 | + def map_SequentialProtocol(self, model): |
| 279 | + current_time = 0 |
| 280 | + |
| 281 | + new_sequence = [] |
| 282 | + for p in model.sequence: |
| 283 | + duration = self._get_duration(p) |
| 284 | + |
| 285 | + new_p = Post( |
| 286 | + SubstituteMathVar( |
| 287 | + variable=MathVar(name="s"), |
| 288 | + substitution=MathVar(name="s") - current_time, |
| 289 | + ) |
| 290 | + )(p) |
| 291 | + new_sequence.append(new_p) |
| 292 | + |
| 293 | + current_time += duration |
| 294 | + |
| 295 | + return model.__class__(sequence=new_sequence) |
| 296 | + |
| 297 | + |
| 298 | +######################################################################################## |
| 299 | + |
88 | 300 | unroll_label_pass = Chain( |
89 | 301 | Pre(UnrollLevelLabel()), |
90 | 302 | Pre(UnrollTransitionLabel()), |
91 | 303 | ) |
| 304 | +""" |
| 305 | +Pass that unrolls the references to levels and transitions |
| 306 | +""" |
| 307 | + |
| 308 | + |
| 309 | +def canonicalize_atomic_circuit_factory(): |
| 310 | + """ |
| 311 | + Factory for creating a pass for canonicalizing an atomic circuit. |
| 312 | + """ |
| 313 | + return Chain( |
| 314 | + unroll_label_pass, |
| 315 | + Post(ResolveRelativeTime()), |
| 316 | + Post(ResolveNestedProtocol()), |
| 317 | + ) |
0 commit comments