Skip to content

qten.precision

Module reference for qten.precision.

precision

Global numeric precision configuration for QTen.

This module stores the default floating and complex dtypes used by QTen helper functions when constructing NumPy arrays and PyTorch tensors. The configuration keeps NumPy and PyTorch precision choices paired so real and complex values use consistent 32-bit or 64-bit families throughout geometry, tensor, and physics workflows.

The default precision is 64-bit: torch.float64, torch.complex128, np.float64, and np.complex128.

PrecisionInput module-attribute

PrecisionInput = Union[
    Literal["32", "64"], int, dtype, dtype
]

PrecisionInfo module-attribute

PrecisionInfo = namedtuple(
    "PrecisionInfo",
    [
        "torch_float",
        "torch_complex",
        "np_float",
        "np_complex",
    ],
)

Container describing the active real and complex dtype family.

Attributes:

Name Type Description
torch_float dtype

PyTorch real floating dtype.

torch_complex dtype

PyTorch complex dtype paired with torch_float.

np_float dtype

NumPy real floating dtype.

np_complex dtype

NumPy complex dtype paired with np_float.

set_precision

set_precision(
    precision: PrecisionInput,
    set_torch_default: bool = True,
) -> None

Set QTen's global real and complex dtype family.

The selected precision updates the dtype values returned by get_precision_config(). When set_torch_default=True, it also calls torch.set_default_dtype() with the selected real PyTorch dtype.

Supported values
  • "32" or 32: use torch.float32, torch.complex64, np.float32, and np.complex64.
  • "64" or 64: use torch.float64, torch.complex128, np.float64, and np.complex128.
  • A supported real or complex torch.dtype selects its matching dtype family.
  • A supported real or complex np.dtype or NumPy scalar dtype selects its matching dtype family.

Parameters:

Name Type Description Default
precision PrecisionInput

Precision selector describing the desired 32-bit or 64-bit dtype family.

required
set_torch_default bool

If True, update PyTorch's process-wide default floating dtype to the selected real dtype. If False, only QTen's stored precision config is updated.

True

Returns:

Type Description
None

Raises:

Type Description
ValueError

If precision does not describe a supported 32-bit or 64-bit dtype family.

Source code in src/qten/precision.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def set_precision(
    precision: PrecisionInput,
    set_torch_default: bool = True,
) -> None:
    """
    Set QTen's global real and complex dtype family.

    The selected precision updates the dtype values returned by
    [`get_precision_config()`][qten.precision.get_precision_config]. When
    `set_torch_default=True`, it also calls `torch.set_default_dtype()` with the
    selected real PyTorch dtype.

    Supported values
    ----------------
    - `"32"` or `32`: use `torch.float32`, `torch.complex64`, `np.float32`, and
      `np.complex64`.
    - `"64"` or `64`: use `torch.float64`, `torch.complex128`, `np.float64`,
      and `np.complex128`.
    - A supported real or complex `torch.dtype` selects its matching dtype
      family.
    - A supported real or complex `np.dtype` or NumPy scalar dtype selects its
      matching dtype family.

    Parameters
    ----------
    precision : PrecisionInput
        Precision selector describing the desired 32-bit or 64-bit dtype family.
    set_torch_default : bool
        If `True`, update PyTorch's process-wide default floating dtype to the
        selected real dtype. If `False`, only QTen's stored precision config is
        updated.

    Returns
    -------
    None

    Raises
    ------
    ValueError
        If `precision` does not describe a supported 32-bit or 64-bit dtype
        family.
    """
    global _torch_float_dtype, _torch_complex_dtype
    global _np_float_dtype, _np_complex_dtype

    precision_info = _normalize_precision(precision)

    if set_torch_default:
        torch.set_default_dtype(precision_info.torch_float)

    _torch_float_dtype = precision_info.torch_float
    _torch_complex_dtype = precision_info.torch_complex
    _np_float_dtype = precision_info.np_float
    _np_complex_dtype = precision_info.np_complex

get_precision_config

get_precision_config() -> PrecisionInfo

Return QTen's current global precision configuration.

Returns:

Type Description
PrecisionInfo

Named tuple with fields torch_float, torch_complex, np_float, and np_complex.

See Also

set_precision(precision, set_torch_default) Update the stored precision configuration.

Source code in src/qten/precision.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def get_precision_config() -> PrecisionInfo:
    """
    Return QTen's current global precision configuration.

    Returns
    -------
    PrecisionInfo
        Named tuple with fields `torch_float`, `torch_complex`, `np_float`, and
        `np_complex`.

    See Also
    --------
    [`set_precision(precision, set_torch_default)`][qten.precision.set_precision]
        Update the stored precision configuration.
    """
    return PrecisionInfo(
        _torch_float_dtype,
        _torch_complex_dtype,
        _np_float_dtype,
        _np_complex_dtype,
    )