|
| 1 | +# Copyright 2024-2025 Open Quantum Design |
| 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 | +# http://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 | +"""Diffrax / Dynamiqs integration settings for the TrICal Dynamiqs backend. |
| 16 | +
|
| 17 | +Unit convention (matches QuTiP and the atomic-interface lowering): |
| 18 | + - Level energies, phonon energies, Rabi frequencies, and detunings are angular |
| 19 | + frequencies in rad/s (inputs often carry explicit factors of 2*pi). |
| 20 | + - Pulse durations are in seconds. |
| 21 | + - The lowered Hamiltonian H and ``tsave`` passed to ``dq.sesolve`` use the same |
| 22 | + rad/s and second units; Dynamiqs integrates d|psi>/dt = -i H |psi> with hbar=1. |
| 23 | +
|
| 24 | +Adaptive step-size controllers are sensitive to |H| and the integration interval. |
| 25 | +Trapped-ion circuits after rotating-frame and RWA passes typically have |H| ~ 2*pi |
| 26 | +* 1e6 rad/s or smaller; default tolerances are relaxed slightly versus Dynamiqs' |
| 27 | +library defaults so stiff sideband dynamics do not exhaust ``max_steps``. |
| 28 | +""" |
| 29 | + |
| 30 | +from __future__ import annotations |
| 31 | + |
| 32 | +from dataclasses import dataclass |
| 33 | +from typing import Any, Dict, Optional, Union |
| 34 | + |
| 35 | +import dynamiqs as dq |
| 36 | + |
| 37 | +######################################################################################## |
| 38 | + |
| 39 | +# Tsit5 uses a Diffrax PID step-size controller (see dynamiqs.method.Tsit5). |
| 40 | +# 1e-6 (library default) can exhaust max_steps on ~MHz-scale trapped-ion dynamics; |
| 41 | +# 1e-3 matches QuTiP SESolver on reference circuits to within ~5% on populations. |
| 42 | +DEFAULT_RTOL = 1e-3 |
| 43 | +DEFAULT_ATOL = 1e-3 |
| 44 | +DEFAULT_MAX_STEPS = 2_000_000 |
| 45 | + |
| 46 | + |
| 47 | +@dataclass |
| 48 | +class DynamiqsSolverOptions: |
| 49 | + """Options forwarded to ``dq.sesolve`` / ``dq.mesolve`` (Diffrax under the hood). |
| 50 | +
|
| 51 | + Pass an instance via ``DynamiqsBackend(solver_options=...)`` or a plain dict with |
| 52 | + the same keys. Mirrors the role of QuTiP's ``solver_options`` on |
| 53 | + ``QutipBackend``. |
| 54 | + """ |
| 55 | + |
| 56 | + rtol: float = DEFAULT_RTOL |
| 57 | + atol: float = DEFAULT_ATOL |
| 58 | + max_steps: int = DEFAULT_MAX_STEPS |
| 59 | + method: Optional[Any] = None |
| 60 | + options: Optional[Any] = None |
| 61 | + # Rescale t -> omega*t and H -> H/omega so Diffrax sees O(1) frequencies (rad/s in, seconds in). |
| 62 | + rescale_time: bool = True |
| 63 | + time_scale: Optional[float] = None |
| 64 | + |
| 65 | + def to_solver_dict(self) -> Dict[str, Any]: |
| 66 | + method = self.method |
| 67 | + if method is None: |
| 68 | + method = dq.method.Tsit5( |
| 69 | + rtol=self.rtol, atol=self.atol, max_steps=self.max_steps |
| 70 | + ) |
| 71 | + options = self.options |
| 72 | + if options is None: |
| 73 | + options = dq.Options(progress_meter=False) |
| 74 | + return { |
| 75 | + "method": method, |
| 76 | + "options": options, |
| 77 | + "rescale_time": self.rescale_time, |
| 78 | + "time_scale": self.time_scale, |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +def normalize_solver_options( |
| 83 | + solver_options: Optional[Union[DynamiqsSolverOptions, Dict[str, Any]]] = None, |
| 84 | +) -> Dict[str, Any]: |
| 85 | + if solver_options is None: |
| 86 | + return DynamiqsSolverOptions().to_solver_dict() |
| 87 | + if isinstance(solver_options, DynamiqsSolverOptions): |
| 88 | + return solver_options.to_solver_dict() |
| 89 | + if "method" not in solver_options: |
| 90 | + opts = DynamiqsSolverOptions( |
| 91 | + rtol=solver_options.get("rtol", DEFAULT_RTOL), |
| 92 | + atol=solver_options.get("atol", DEFAULT_ATOL), |
| 93 | + max_steps=solver_options.get("max_steps", DEFAULT_MAX_STEPS), |
| 94 | + method=solver_options.get("method"), |
| 95 | + options=solver_options.get("options"), |
| 96 | + rescale_time=solver_options.get("rescale_time", True), |
| 97 | + time_scale=solver_options.get("time_scale"), |
| 98 | + ) |
| 99 | + merged = opts.to_solver_dict() |
| 100 | + merged.update({k: v for k, v in solver_options.items() if k not in merged}) |
| 101 | + return merged |
| 102 | + if "options" not in solver_options: |
| 103 | + solver_options = { |
| 104 | + **solver_options, |
| 105 | + "options": dq.Options(progress_meter=False), |
| 106 | + } |
| 107 | + return solver_options |
0 commit comments