Skip to content

dtypes

dtypes ¤

DataType ¤

Bases: IntEnum

The available symbolic data types. Note that these data types are precision-agnostic.

Source code in cirkit/symbolic/dtypes.py
 7
 8
 9
10
11
12
13
14
15
class DataType(IntEnum):
    """The available symbolic data types. Note that these data types are precision-agnostic."""

    INTEGER = auto()
    """The integer numbers data type."""
    REAL = auto()
    """The real numbers data type."""
    COMPLEX = auto()
    """The complex numbers data type."""

COMPLEX = auto() class-attribute instance-attribute ¤

The complex numbers data type.

INTEGER = auto() class-attribute instance-attribute ¤

The integer numbers data type.

REAL = auto() class-attribute instance-attribute ¤

The real numbers data type.

dtype_value(x) ¤

Given a number or Numpy array, return its symbolic data type.

Parameters:

Name Type Description Default
x Number | ndarray

A number, which can be a Python integer, float or complex number. Alternatively, it can be a Numpy array.

required

Returns:

Type Description
DataType

The symbolic data type associated to the given number or Numpy array.

Raises:

Type Description
ValueError

If the given number is neither an integer, nor a float, nor a complex number, nor a Numpy array.

Source code in cirkit/symbolic/dtypes.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def dtype_value(x: Number | np.ndarray) -> DataType:
    """Given a number or Numpy array, return its symbolic data type.

    Args:
        x: A number, which can be a Python integer, float or complex number.
            Alternatively, it can be a Numpy array.

    Returns:
        The symbolic data type associated to the given number or Numpy array.

    Raises:
        ValueError: If the given number is neither an integer, nor a float, nor a complex number,
            nor a Numpy array.
    """
    if isinstance(x, int):
        return DataType.INTEGER
    if isinstance(x, float):
        return DataType.REAL
    if isinstance(x, complex):
        return DataType.COMPLEX
    if isinstance(x, np.ndarray):
        if issubclass(x.dtype.type, np.integer):
            return DataType.INTEGER
        if issubclass(x.dtype.type, np.floating):
            return DataType.REAL
        if issubclass(x.dtype.type, np.complexfloating):
            return DataType.COMPLEX
    raise ValueError(f"Cannot retrieve data type of number of type {type(x)}")