-
Notifications
You must be signed in to change notification settings - Fork 1
Add basis #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
johannes-moegerle
wants to merge
11
commits into
pairinteraction:restructure-rydberg-state
Choose a base branch
from
johannes-moegerle:add-basis
base: restructure-rydberg-state
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add basis #17
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
723a1ba
AngularKet add calc_exp_qn
johannes-moegerle 111075f
add is_angular_momentum_quantum_number and is_angular_operator_type
johannes-moegerle df9aea6
small fix angular state
johannes-moegerle cf19032
start adding sqdt basis
johannes-moegerle c24a923
split up basis base
johannes-moegerle 434c4a4
BasisBase start adding calc_reduced_...
johannes-moegerle 2994ac0
Basis update filter_states and add copy
johannes-moegerle da4923a
basis add sort_states
johannes-moegerle 0820463
fixup basis sqdt
johannes-moegerle 8cdcebf
improve rydberg sqdt alkali repr
johannes-moegerle 7753ed7
[tests] start adding tests for basis
johannes-moegerle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from rydstate.basis.basis_sqdt import BasisSQDTAlkali, BasisSQDTAlkalineLS | ||
|
|
||
| __all__ = ["BasisSQDTAlkali", "BasisSQDTAlkalineLS"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC | ||
| from typing import TYPE_CHECKING, Any, Generic, TypeVar, overload | ||
|
|
||
| import numpy as np | ||
| from typing_extensions import Self | ||
|
|
||
| from rydstate.angular.angular_matrix_element import is_angular_momentum_quantum_number | ||
| from rydstate.rydberg.rydberg_base import RydbergStateBase | ||
| from rydstate.species.species_object import SpeciesObject | ||
| from rydstate.units import ureg | ||
|
|
||
| if TYPE_CHECKING: | ||
| from rydstate.units import MatrixElementOperator, NDArray, PintArray, PintFloat | ||
|
|
||
| _RydbergState = TypeVar("_RydbergState", bound=RydbergStateBase) | ||
|
|
||
|
|
||
| class BasisBase(ABC, Generic[_RydbergState]): | ||
| states: list[_RydbergState] | ||
|
|
||
| def __init__(self, species: str | SpeciesObject) -> None: | ||
| if isinstance(species, str): | ||
| species = SpeciesObject.from_name(species) | ||
| self.species = species | ||
|
|
||
| def __len__(self) -> int: | ||
| return len(self.states) | ||
|
|
||
| def copy(self) -> Self: | ||
| new_basis = self.__class__.__new__(self.__class__) | ||
| new_basis.species = self.species | ||
| new_basis.states = list(self.states) | ||
| return new_basis | ||
|
|
||
| @overload | ||
| def filter_states(self, qn: str, value: tuple[float, float], *, delta: float = 1e-10) -> Self: ... | ||
|
|
||
| @overload | ||
| def filter_states(self, qn: str, value: float, *, delta: float = 1e-10) -> Self: ... | ||
|
|
||
| def filter_states(self, qn: str, value: float | tuple[float, float], *, delta: float = 1e-10) -> Self: | ||
| if isinstance(value, tuple): | ||
| qn_min = value[0] - delta | ||
| qn_max = value[1] + delta | ||
| else: | ||
| qn_min = value - delta | ||
| qn_max = value + delta | ||
|
|
||
| if is_angular_momentum_quantum_number(qn): | ||
| self.states = [state for state in self.states if qn_min <= state.angular.calc_exp_qn(qn) <= qn_max] | ||
| elif qn in ["n", "nu", "nu_energy"]: | ||
| self.states = [state for state in self.states if qn_min <= getattr(state, qn) <= qn_max] | ||
| else: | ||
| raise ValueError(f"Unknown quantum number {qn}") | ||
|
|
||
| return self | ||
|
|
||
| def sort_states(self, *qns: str) -> Self: | ||
| """Sort the basis states according to the given quantum numbers. | ||
|
|
||
| The first quantum number given is the primary sorting key, the second quantum number | ||
| is the secondary sorting key, and so on. | ||
| """ | ||
| values = np.array([self.calc_exp_qn(qn) for qn in qns]) | ||
| sorted_indices = np.lexsort(values[::-1]) | ||
| self.states = [self.states[i] for i in sorted_indices] | ||
| return self | ||
|
|
||
| def calc_exp_qn(self, qn: str) -> list[float]: | ||
| if is_angular_momentum_quantum_number(qn): | ||
| return [state.angular.calc_exp_qn(qn) for state in self.states] | ||
| if qn in ["n", "nu", "nu_energy"]: | ||
| return [getattr(state, qn) for state in self.states] | ||
| raise ValueError(f"Unknown quantum number {qn}") | ||
|
|
||
| def calc_std_qn(self, qn: str) -> list[float]: | ||
| if is_angular_momentum_quantum_number(qn): | ||
| return [state.angular.calc_std_qn(qn) for state in self.states] | ||
| if qn in ["n", "nu", "nu_energy"]: | ||
| return [0 for state in self.states] | ||
| raise ValueError(f"Unknown quantum number {qn}") | ||
|
|
||
| def calc_reduced_overlap(self, other: RydbergStateBase) -> NDArray: | ||
| """Calculate the reduced overlap <self|other> (ignoring the magnetic quantum number m).""" | ||
| return np.array([bra.calc_reduced_overlap(other) for bra in self.states]) | ||
|
|
||
| def calc_reduced_overlaps(self, other: BasisBase[Any]) -> NDArray: | ||
| """Calculate the reduced overlap <bra|ket> for all states in the bases self and other. | ||
|
|
||
| Returns a numpy array overlaps, where overlaps[i,j] corresponds to the overlap of the | ||
| i-th state of self and the j-th state of other. | ||
| """ | ||
| return np.array([[bra.calc_reduced_overlap(ket) for ket in other.states] for bra in self.states]) | ||
|
|
||
| @overload | ||
| def calc_reduced_matrix_element( | ||
| self, other: RydbergStateBase, operator: MatrixElementOperator, unit: None = None | ||
| ) -> PintArray: ... | ||
|
|
||
| @overload | ||
| def calc_reduced_matrix_element( | ||
| self, other: RydbergStateBase, operator: MatrixElementOperator, unit: str | ||
| ) -> NDArray: ... | ||
|
|
||
| def calc_reduced_matrix_element( | ||
| self, other: RydbergStateBase, operator: MatrixElementOperator, unit: str | None = None | ||
| ) -> PintArray | NDArray: | ||
| r"""Calculate the reduced matrix element.""" | ||
| values_list = [bra.calc_reduced_matrix_element(other, operator, unit=unit) for bra in self.states] | ||
| if unit is not None: | ||
| return np.array(values_list) | ||
|
|
||
| values: list[PintFloat] = values_list # type: ignore[assignment] | ||
| _unit = values[0].units | ||
| _values = np.array([v.magnitude for v in values]) | ||
| return ureg.Quantity(_values, _unit) | ||
johannes-moegerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @overload | ||
| def calc_reduced_matrix_elements( | ||
| self, other: BasisBase[Any], operator: MatrixElementOperator, unit: None = None | ||
| ) -> PintArray: ... | ||
|
|
||
| @overload | ||
| def calc_reduced_matrix_elements( | ||
| self, other: BasisBase[Any], operator: MatrixElementOperator, unit: str | ||
| ) -> NDArray: ... | ||
|
|
||
| def calc_reduced_matrix_elements( | ||
| self, other: BasisBase[Any], operator: MatrixElementOperator, unit: str | None = None | ||
| ) -> PintArray | NDArray: | ||
| r"""Calculate the reduced matrix element.""" | ||
| values_list = [ | ||
| [bra.calc_reduced_matrix_element(ket, operator, unit=unit) for ket in other.states] for bra in self.states | ||
| ] | ||
| if unit is not None: | ||
| return np.array(values_list) | ||
|
|
||
| values: list[list[PintFloat]] = values_list # type: ignore[assignment] | ||
| _unit = values[0][0].units | ||
| _values = np.array([[v.magnitude for v in vs] for vs in values]) | ||
| return ureg.Quantity(_values, _unit) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
|
|
||
| from rydstate.basis.basis_base import BasisBase | ||
| from rydstate.rydberg import RydbergStateSQDTAlkali, RydbergStateSQDTAlkalineLS | ||
|
|
||
|
|
||
| class BasisSQDTAlkali(BasisBase[RydbergStateSQDTAlkali]): | ||
| def __init__(self, species: str, n_min: int = 1, n_max: int | None = None) -> None: | ||
| super().__init__(species) | ||
|
|
||
| if n_max is None: | ||
| raise ValueError("n_max must be given") | ||
|
|
||
| s = 1 / 2 | ||
| i_c = self.species.i_c if self.species.i_c is not None else 0 | ||
|
|
||
| self.states = [] | ||
| for n in range(n_min, n_max + 1): | ||
| for l in range(n): | ||
| if not self.species.is_allowed_shell(n, l, s): | ||
| continue | ||
| for j in np.arange(abs(l - s), l + s + 1): | ||
| for f in np.arange(abs(j - i_c), j + i_c + 1): | ||
| state = RydbergStateSQDTAlkali(species, n=n, l=l, j=float(j), f=float(f)) | ||
| self.states.append(state) | ||
|
|
||
|
|
||
| class BasisSQDTAlkalineLS(BasisBase[RydbergStateSQDTAlkalineLS]): | ||
| def __init__(self, species: str, n_min: int = 1, n_max: int | None = None) -> None: | ||
| super().__init__(species) | ||
|
|
||
| if n_max is None: | ||
| raise ValueError("n_max must be given") | ||
|
|
||
| i_c = self.species.i_c if self.species.i_c is not None else 0 | ||
|
|
||
| self.states = [] | ||
| for n in range(n_min, n_max + 1): | ||
| for l in range(n): | ||
| for s_tot in [0, 1]: | ||
| if not self.species.is_allowed_shell(n, l, s_tot): | ||
| continue | ||
| for j_tot in range(abs(l - s_tot), l + s_tot + 1): | ||
| for f_tot in np.arange(abs(j_tot - i_c), j_tot + i_c + 1): | ||
| state = RydbergStateSQDTAlkalineLS( | ||
| species, n=n, l=l, s_tot=s_tot, j_tot=j_tot, f_tot=float(f_tot) | ||
johannes-moegerle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| self.states.append(state) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.