Skip to content

mode_util

mode_util

Gilbert modes related tools.

Dimensions dataclass

Container for system dimensions.

Source code in cssfinder/algorithm/mode_util.py
@dataclass
class Dimensions:
    """Container for system dimensions."""

    depth: int
    """Depth of system, ie.

    number of dimensions in qu(D)it. (d)

    """

    quantity: int
    """Quantity of systems.

    ie. number of qu(D)its in state. (n)

    """

depth instance-attribute

depth: int

Depth of system, ie.

number of dimensions in qu(D)it. (d)

quantity instance-attribute

quantity: int

Quantity of systems.

ie. number of qu(D)its in state. (n)

ModeUtil

Bases: ABC

Base class for implementing mode specific utilities eg.

shape deduction.

Source code in cssfinder/algorithm/mode_util.py
class ModeUtil(ABC):
    """Base class for implementing mode specific utilities eg.

    shape deduction.

    """

    @staticmethod
    def new(mode: AlgoMode) -> ModeUtil:
        """Create new ModeUtil subclass instance.

        Parameters
        ----------
        mode : AlgoMode
            Util set selector.

        Returns
        -------
        ModeUtil
            Instance of subclass of ModeUtil.

        Raises
        ------
        NotImplementedError
            For unsupported AlgoModes and incorrect input.

        """
        if mode == AlgoMode.FSnQd:
            return FSnQdUtil()

        if mode == AlgoMode.SBiPa:
            return SBiPaUtil()

        if mode == AlgoMode.G3PaE3qD:
            return G3PaE3qDUtil()

        msg = f"Unsupported mode {mode.name}"
        raise NotImplementedError(msg)

    def get_dimensions(self, state: npt.NDArray[np.complex128]) -> Dimensions:
        """Detect both system depth and system quantity.

        Parameters
        ----------
        state : int
            State matrix to detect dimensions for.

        Returns
        -------
        Dimensions
            System dimensions within dedicated container.

        Raises
        ------
        ValueError
            When depth and quantity can't be determined.

        """
        dim = self.detect_depth_and_quantity(len(state))

        logging.debug(
            "Deduced quantity %r and depth %r when given total size %r",
            dim.depth,
            dim.quantity,
            len(state),
        )
        return dim

    @abstractmethod
    def detect_depth_and_quantity(self, total: int) -> Dimensions:
        """Detect both system depth and system quantity.

        Parameters
        ----------
        total : int
            Dimension along one of axes. Matrix is expected to be square.

        Returns
        -------
        Dimensions
            System dimensions within dedicated container.

        Raises
        ------
        ValueError
            When depth and quantity can't be determined.

        """

new staticmethod

new(mode: AlgoMode) -> ModeUtil

Create new ModeUtil subclass instance.

Parameters:

Name Type Description Default
mode AlgoMode

Util set selector.

required

Returns:

Type Description
ModeUtil

Instance of subclass of ModeUtil.

Raises:

Type Description
NotImplementedError

For unsupported AlgoModes and incorrect input.

Source code in cssfinder/algorithm/mode_util.py
@staticmethod
def new(mode: AlgoMode) -> ModeUtil:
    """Create new ModeUtil subclass instance.

    Parameters
    ----------
    mode : AlgoMode
        Util set selector.

    Returns
    -------
    ModeUtil
        Instance of subclass of ModeUtil.

    Raises
    ------
    NotImplementedError
        For unsupported AlgoModes and incorrect input.

    """
    if mode == AlgoMode.FSnQd:
        return FSnQdUtil()

    if mode == AlgoMode.SBiPa:
        return SBiPaUtil()

    if mode == AlgoMode.G3PaE3qD:
        return G3PaE3qDUtil()

    msg = f"Unsupported mode {mode.name}"
    raise NotImplementedError(msg)

get_dimensions

get_dimensions(
    state: npt.NDArray[np.complex128],
) -> Dimensions

Detect both system depth and system quantity.

Parameters:

Name Type Description Default
state int

State matrix to detect dimensions for.

required

Returns:

Type Description
Dimensions

System dimensions within dedicated container.

Raises:

Type Description
ValueError

When depth and quantity can't be determined.

Source code in cssfinder/algorithm/mode_util.py
def get_dimensions(self, state: npt.NDArray[np.complex128]) -> Dimensions:
    """Detect both system depth and system quantity.

    Parameters
    ----------
    state : int
        State matrix to detect dimensions for.

    Returns
    -------
    Dimensions
        System dimensions within dedicated container.

    Raises
    ------
    ValueError
        When depth and quantity can't be determined.

    """
    dim = self.detect_depth_and_quantity(len(state))

    logging.debug(
        "Deduced quantity %r and depth %r when given total size %r",
        dim.depth,
        dim.quantity,
        len(state),
    )
    return dim

detect_depth_and_quantity abstractmethod

detect_depth_and_quantity(total: int) -> Dimensions

Detect both system depth and system quantity.

Parameters:

Name Type Description Default
total int

Dimension along one of axes. Matrix is expected to be square.

required

Returns:

Type Description
Dimensions

System dimensions within dedicated container.

Raises:

Type Description
ValueError

When depth and quantity can't be determined.

Source code in cssfinder/algorithm/mode_util.py
@abstractmethod
def detect_depth_and_quantity(self, total: int) -> Dimensions:
    """Detect both system depth and system quantity.

    Parameters
    ----------
    total : int
        Dimension along one of axes. Matrix is expected to be square.

    Returns
    -------
    Dimensions
        System dimensions within dedicated container.

    Raises
    ------
    ValueError
        When depth and quantity can't be determined.

    """

FSnQdUtil

Bases: ModeUtil

FSnQd specific implementation of utilities eg.

shape deduction.

Source code in cssfinder/algorithm/mode_util.py
class FSnQdUtil(ModeUtil):
    """FSnQd specific implementation of utilities eg.

    shape deduction.

    """

    def detect_depth_and_quantity(self, total: int) -> Dimensions:
        """Detect both system depth and system quantity.

        Parameters
        ----------
        total : int
            Dimension along one of axes. Matrix is expected to be square.

        Returns
        -------
        Dimensions
            System dimensions within dedicated container.

        Raises
        ------
        ValueError
            When depth and quantity can't be determined.

        """
        for depth in PRIMES:
            quantity = int(math.log(total, depth))

            if quantity == int(quantity):
                return Dimensions(depth, quantity)

        reason = "prime number range exceeded"
        raise UndefinedSystemSizeError(reason)

detect_depth_and_quantity

detect_depth_and_quantity(total: int) -> Dimensions

Detect both system depth and system quantity.

Parameters:

Name Type Description Default
total int

Dimension along one of axes. Matrix is expected to be square.

required

Returns:

Type Description
Dimensions

System dimensions within dedicated container.

Raises:

Type Description
ValueError

When depth and quantity can't be determined.

Source code in cssfinder/algorithm/mode_util.py
def detect_depth_and_quantity(self, total: int) -> Dimensions:
    """Detect both system depth and system quantity.

    Parameters
    ----------
    total : int
        Dimension along one of axes. Matrix is expected to be square.

    Returns
    -------
    Dimensions
        System dimensions within dedicated container.

    Raises
    ------
    ValueError
        When depth and quantity can't be determined.

    """
    for depth in PRIMES:
        quantity = int(math.log(total, depth))

        if quantity == int(quantity):
            return Dimensions(depth, quantity)

    reason = "prime number range exceeded"
    raise UndefinedSystemSizeError(reason)

SBiPaUtil

Bases: ModeUtil

SBiPa specific implementation of utilities eg.

shape deduction.

Source code in cssfinder/algorithm/mode_util.py
class SBiPaUtil(ModeUtil):
    """SBiPa specific implementation of utilities eg.

    shape deduction.

    """

    def detect_depth_and_quantity(self, total: int) -> Dimensions:
        """Detect both system depth and system quantity.

        Parameters
        ----------
        total : int
            Dimension along one of axes. Matrix is expected to be square.

        Returns
        -------
        Dimensions
            System dimensions within dedicated container.

        Raises
        ------
        ValueError
            When depth and quantity can't be determined.

        """
        total_sqrt = math.sqrt(total)
        floored_total_sqrt = int(total_sqrt)

        if total_sqrt == floored_total_sqrt:
            return Dimensions(floored_total_sqrt, floored_total_sqrt)

        for depth in PRIMES:
            if total % depth == 0:
                quantity = int(total / depth)
                return Dimensions(depth, quantity)

        reason = "prime number range exceeded"
        raise UndefinedSystemSizeError(reason)

detect_depth_and_quantity

detect_depth_and_quantity(total: int) -> Dimensions

Detect both system depth and system quantity.

Parameters:

Name Type Description Default
total int

Dimension along one of axes. Matrix is expected to be square.

required

Returns:

Type Description
Dimensions

System dimensions within dedicated container.

Raises:

Type Description
ValueError

When depth and quantity can't be determined.

Source code in cssfinder/algorithm/mode_util.py
def detect_depth_and_quantity(self, total: int) -> Dimensions:
    """Detect both system depth and system quantity.

    Parameters
    ----------
    total : int
        Dimension along one of axes. Matrix is expected to be square.

    Returns
    -------
    Dimensions
        System dimensions within dedicated container.

    Raises
    ------
    ValueError
        When depth and quantity can't be determined.

    """
    total_sqrt = math.sqrt(total)
    floored_total_sqrt = int(total_sqrt)

    if total_sqrt == floored_total_sqrt:
        return Dimensions(floored_total_sqrt, floored_total_sqrt)

    for depth in PRIMES:
        if total % depth == 0:
            quantity = int(total / depth)
            return Dimensions(depth, quantity)

    reason = "prime number range exceeded"
    raise UndefinedSystemSizeError(reason)

UndefinedSystemSizeError

Bases: ValueError

Raised when it is not possible to determine system dimensions.

Source code in cssfinder/algorithm/mode_util.py
class UndefinedSystemSizeError(ValueError):
    """Raised when it is not possible to determine system dimensions."""

    def __init__(self, reason: str) -> None:
        super().__init__(f"Couldn't determine size of system: {reason}.")

G3PaE3qDUtil

Bases: ModeUtil

G3PaE3q specific implementation of utilities eg.

shape deduction.

Source code in cssfinder/algorithm/mode_util.py
class G3PaE3qDUtil(ModeUtil):
    """G3PaE3q specific implementation of utilities eg.

    shape deduction.

    """

    def detect_depth_and_quantity(self, total: int) -> Dimensions:
        """Detect both system depth and system quantity.

        Parameters
        ----------
        total : int
            Dimension along one of axes. Matrix is expected to be square.

        Returns
        -------
        Dimensions
            System dimensions within dedicated container.

        Raises
        ------
        ValueError
            When depth and quantity can't be determined.

        """
        if round(total ** (1.0 / 3), 3) == round(total ** (1.0 / 3), 0):
            return Dimensions(depth=int(total ** (1.0 / 3)), quantity=3)

        reason = "prime number range exceeded"
        raise UndefinedSystemSizeError(reason)

detect_depth_and_quantity

detect_depth_and_quantity(total: int) -> Dimensions

Detect both system depth and system quantity.

Parameters:

Name Type Description Default
total int

Dimension along one of axes. Matrix is expected to be square.

required

Returns:

Type Description
Dimensions

System dimensions within dedicated container.

Raises:

Type Description
ValueError

When depth and quantity can't be determined.

Source code in cssfinder/algorithm/mode_util.py
def detect_depth_and_quantity(self, total: int) -> Dimensions:
    """Detect both system depth and system quantity.

    Parameters
    ----------
    total : int
        Dimension along one of axes. Matrix is expected to be square.

    Returns
    -------
    Dimensions
        System dimensions within dedicated container.

    Raises
    ------
    ValueError
        When depth and quantity can't be determined.

    """
    if round(total ** (1.0 / 3), 3) == round(total ** (1.0 / 3), 0):
        return Dimensions(depth=int(total ** (1.0 / 3)), quantity=3)

    reason = "prime number range exceeded"
    raise UndefinedSystemSizeError(reason)