|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import TYPE_CHECKING, Any, Generic, TypeVar, overload |
| 5 | + |
| 6 | +import numpy as np |
| 7 | + |
| 8 | +from rydstate.angular import AngularState |
| 9 | +from rydstate.rydberg.rydberg_base import RydbergStateBase |
| 10 | +from rydstate.rydberg.rydberg_sqdt import RydbergStateSQDT |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from collections.abc import Iterator, Sequence |
| 14 | + |
| 15 | + from rydstate.units import MatrixElementOperator, PintFloat |
| 16 | + |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +_RydbergState = TypeVar("_RydbergState", bound=RydbergStateSQDT) |
| 22 | + |
| 23 | + |
| 24 | +class RydbergStateMQDT(RydbergStateBase, Generic[_RydbergState]): |
| 25 | + angular: AngularState[Any] |
| 26 | + """Return the angular part of the MQDT state as an AngularState.""" |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + coefficients: Sequence[float], |
| 31 | + sqdt_states: Sequence[_RydbergState], |
| 32 | + *, |
| 33 | + nu_energy: float | None = None, |
| 34 | + warn_if_not_normalized: bool = True, |
| 35 | + ) -> None: |
| 36 | + self.coefficients = np.array(coefficients) |
| 37 | + self.sqdt_states = sqdt_states |
| 38 | + self.nu_energy = nu_energy |
| 39 | + self.angular = AngularState(self.coefficients.tolist(), [ket.angular for ket in sqdt_states]) |
| 40 | + |
| 41 | + if len(coefficients) != len(sqdt_states): |
| 42 | + raise ValueError("Length of coefficients and sqdt_states must be the same.") |
| 43 | + if not all(type(sqdt_state) is type(sqdt_states[0]) for sqdt_state in sqdt_states): |
| 44 | + raise ValueError("All sqdt_states must be of the same type.") |
| 45 | + if len(set(sqdt_states)) != len(sqdt_states): |
| 46 | + raise ValueError("RydbergStateMQDT initialized with duplicate sqdt_states.") |
| 47 | + if abs(self.norm - 1) > 1e-10 and warn_if_not_normalized: |
| 48 | + logger.warning( |
| 49 | + "RydbergStateMQDT initialized with non-normalized coefficients " |
| 50 | + "(norm=%s, coefficients=%s, sqdt_states=%s)", |
| 51 | + self.norm, |
| 52 | + coefficients, |
| 53 | + sqdt_states, |
| 54 | + ) |
| 55 | + if self.norm > 1: |
| 56 | + self.coefficients /= self.norm |
| 57 | + |
| 58 | + def __iter__(self) -> Iterator[tuple[float, _RydbergState]]: |
| 59 | + return zip(self.coefficients, self.sqdt_states).__iter__() |
| 60 | + |
| 61 | + def __repr__(self) -> str: |
| 62 | + terms = [f"{coeff}*{sqdt_state!r}" for coeff, sqdt_state in self] |
| 63 | + return f"{self.__class__.__name__}({', '.join(terms)})" |
| 64 | + |
| 65 | + def __str__(self) -> str: |
| 66 | + terms = [f"{coeff}*{sqdt_state!s}" for coeff, sqdt_state in self] |
| 67 | + return f"{', '.join(terms)}" |
| 68 | + |
| 69 | + @property |
| 70 | + def norm(self) -> float: |
| 71 | + """Return the norm of the state (should be 1).""" |
| 72 | + return np.linalg.norm(self.coefficients) # type: ignore [return-value] |
| 73 | + |
| 74 | + def calc_reduced_overlap(self, other: RydbergStateBase) -> float: |
| 75 | + """Calculate the reduced overlap <self|other> (ignoring the magnetic quantum number m).""" |
| 76 | + if isinstance(other, RydbergStateSQDT): |
| 77 | + other = other.to_mqdt() |
| 78 | + |
| 79 | + if isinstance(other, RydbergStateMQDT): |
| 80 | + ov = 0 |
| 81 | + for coeff1, sqdt1 in self: |
| 82 | + for coeff2, sqdt2 in other: |
| 83 | + ov += np.conjugate(coeff1) * coeff2 * sqdt1.calc_reduced_overlap(sqdt2) |
| 84 | + return ov |
| 85 | + |
| 86 | + raise NotImplementedError(f"calc_reduced_overlap not implemented for {type(self)=}, {type(other)=}") |
| 87 | + |
| 88 | + @overload # type: ignore [override] |
| 89 | + def calc_reduced_matrix_element( |
| 90 | + self, other: RydbergStateBase, operator: MatrixElementOperator, unit: None = None |
| 91 | + ) -> PintFloat: ... |
| 92 | + |
| 93 | + @overload |
| 94 | + def calc_reduced_matrix_element( |
| 95 | + self, other: RydbergStateBase, operator: MatrixElementOperator, unit: str |
| 96 | + ) -> float: ... |
| 97 | + |
| 98 | + def calc_reduced_matrix_element( |
| 99 | + self, other: RydbergStateBase, operator: MatrixElementOperator, unit: str | None = None |
| 100 | + ) -> PintFloat | float: |
| 101 | + r"""Calculate the reduced angular matrix element. |
| 102 | +
|
| 103 | + This means, calculate the following matrix element: |
| 104 | +
|
| 105 | + .. math:: |
| 106 | + \left\langle self || \hat{O}^{(\kappa)} || other \right\rangle |
| 107 | +
|
| 108 | + """ |
| 109 | + if isinstance(other, RydbergStateSQDT): |
| 110 | + other = other.to_mqdt() |
| 111 | + |
| 112 | + if isinstance(other, RydbergStateMQDT): |
| 113 | + value = 0 |
| 114 | + for coeff1, sqdt1 in self: |
| 115 | + for coeff2, sqdt2 in other: |
| 116 | + value += ( |
| 117 | + np.conjugate(coeff1) * coeff2 * sqdt1.calc_reduced_matrix_element(sqdt2, operator, unit=unit) |
| 118 | + ) |
| 119 | + return value |
| 120 | + |
| 121 | + raise NotImplementedError(f"calc_reduced_overlap not implemented for {type(self)=}, {type(other)=}") |
0 commit comments