Skip to content

gilbert

gilbert

Module contains public interface of Gilbert algorithm.

Gilbert

Class interface of gilbert algorithm.

Source code in cssfinder/algorithm/gilbert.py
class Gilbert:
    """Class interface of gilbert algorithm."""

    def __init__(  # noqa: PLR0913
        self,
        *,
        initial: npt.NDArray[np.complex128],
        depth: int,
        quantity: int,
        mode: AlgoMode,
        backend: str,
        precision: Precision,
        visibility: float,
        is_debug: bool = False,
    ) -> None:
        self.initial = initial
        self.depth = int(depth)
        self.quantity = int(quantity)

        self.mode = mode
        self.precision = precision
        self.visibility = visibility

        self.is_debug = is_debug

        backend_type = Loader().new().get_backend(backend, self.precision)
        self.backend = backend_type(
            self.initial,
            self.depth,
            self.quantity,
            self.mode,
            self.visibility,
            is_debug=self.is_debug,
        )

    def set_symmetries(
        self,
        symmetries: list[list[npt.NDArray[np.complex128]]],
    ) -> None:
        """Set symmetries to use during calculations.

        This operation may involve type conversion and copying of symmetries, therefore
        if may be slow and should should be done only once.

        Parameters
        ----------
        symmetries : list[list[npt.NDArray[np.complex128]]]
            Array of symmetries.

        """
        self.backend.set_symmetries(symmetries)

    def set_projection(self, projection: npt.NDArray[np.complex128]) -> None:
        """Set projection to use during calculations.

        This operation may involve type conversion and copying of symmetries, therefore
        if may be slow and should should be done only once.

        Parameters
        ----------
        projection : npt.NDArray[np.complex128]
            Projection matrix.

        """
        self.backend.set_projection(projection)

    def run(
        self,
        max_epochs: int,
        iterations_per_epoch: int,
        max_corrections: int,
    ) -> Iterable[int]:
        """Run a specified number of epochs with a given number of iterations per epoch
        or until a certain amount of corrections is found.

        Parameters
        ----------
        max_epochs : int
            The number of epochs to run.
        iterations_per_epoch : int
            The number of iterations to run per epoch. Within epoch, no stop conditions
            are checked, therefore number of corrections can exceed expected threshold.
        max_corrections : int
            The maximum number of corrections allowed.

        Yields
        ------
        int
            An iterator over the epoch indices, up to the point where the maximum
            number of expected corrections is reached.

        Notes
        -----
        This method runs a certain number of epochs with a given number of iterations
        per epoch on a backend. It will yield the epoch index at each epoch iteration
        while running, and will stop if and when the number of corrections found by the
        backend exceeds the specified `max_corrections` or after performing number of
        epochs specified with `max_epochs`.

        """
        start = perf_counter()
        total_iterations = max_epochs * iterations_per_epoch
        epoch_index = 0

        for epoch_index in range(max_epochs):
            # Run N iterations of algorithm without checking stop conditions.
            self.backend.run_epoch(iterations_per_epoch, epoch_index)

            iterations_executed = (epoch_index + 1) * iterations_per_epoch
            logging.debug(
                "Executed %r iterations, total %r / %r (%.2f%%)",
                iterations_per_epoch,
                iterations_executed,
                total_iterations,
                (iterations_executed / total_iterations) * 100,
            )
            # Check if we already reached expected number of corrections
            if self.backend.get_corrections_count() >= max_corrections:
                logging.info(
                    "Reached expected maximal number of corrections %r",
                    max_corrections,
                )
                break

            yield epoch_index

        end = perf_counter()
        logging.info("Elapsed time: %r.", end - start)

        # Possibly trigger user defined code before ending execution.
        yield epoch_index
        return

    def get_state(self) -> npt.NDArray[np.complex128]:
        """Return current system state with all optimizations applied."""
        return self.backend.get_state()

    def get_corrections(self) -> list[tuple[int, int, float]]:
        """Return list of all corrections found during optimization."""
        return self.backend.get_corrections()

    def get_corrections_count(self) -> int:
        """Return number of all corrections found during optimization."""
        return self.backend.get_corrections_count()

set_symmetries

set_symmetries(
    symmetries: list[list[npt.NDArray[np.complex128]]],
) -> None

Set symmetries to use during calculations.

This operation may involve type conversion and copying of symmetries, therefore if may be slow and should should be done only once.

Parameters:

Name Type Description Default
symmetries list[list[NDArray[complex128]]]

Array of symmetries.

required
Source code in cssfinder/algorithm/gilbert.py
def set_symmetries(
    self,
    symmetries: list[list[npt.NDArray[np.complex128]]],
) -> None:
    """Set symmetries to use during calculations.

    This operation may involve type conversion and copying of symmetries, therefore
    if may be slow and should should be done only once.

    Parameters
    ----------
    symmetries : list[list[npt.NDArray[np.complex128]]]
        Array of symmetries.

    """
    self.backend.set_symmetries(symmetries)

set_projection

set_projection(
    projection: npt.NDArray[np.complex128],
) -> None

Set projection to use during calculations.

This operation may involve type conversion and copying of symmetries, therefore if may be slow and should should be done only once.

Parameters:

Name Type Description Default
projection NDArray[complex128]

Projection matrix.

required
Source code in cssfinder/algorithm/gilbert.py
def set_projection(self, projection: npt.NDArray[np.complex128]) -> None:
    """Set projection to use during calculations.

    This operation may involve type conversion and copying of symmetries, therefore
    if may be slow and should should be done only once.

    Parameters
    ----------
    projection : npt.NDArray[np.complex128]
        Projection matrix.

    """
    self.backend.set_projection(projection)

run

run(
    max_epochs: int,
    iterations_per_epoch: int,
    max_corrections: int,
) -> Iterable[int]

Run a specified number of epochs with a given number of iterations per epoch or until a certain amount of corrections is found.

Parameters:

Name Type Description Default
max_epochs int

The number of epochs to run.

required
iterations_per_epoch int

The number of iterations to run per epoch. Within epoch, no stop conditions are checked, therefore number of corrections can exceed expected threshold.

required
max_corrections int

The maximum number of corrections allowed.

required

Yields:

Type Description
int

An iterator over the epoch indices, up to the point where the maximum number of expected corrections is reached.

Notes

This method runs a certain number of epochs with a given number of iterations per epoch on a backend. It will yield the epoch index at each epoch iteration while running, and will stop if and when the number of corrections found by the backend exceeds the specified max_corrections or after performing number of epochs specified with max_epochs.

Source code in cssfinder/algorithm/gilbert.py
def run(
    self,
    max_epochs: int,
    iterations_per_epoch: int,
    max_corrections: int,
) -> Iterable[int]:
    """Run a specified number of epochs with a given number of iterations per epoch
    or until a certain amount of corrections is found.

    Parameters
    ----------
    max_epochs : int
        The number of epochs to run.
    iterations_per_epoch : int
        The number of iterations to run per epoch. Within epoch, no stop conditions
        are checked, therefore number of corrections can exceed expected threshold.
    max_corrections : int
        The maximum number of corrections allowed.

    Yields
    ------
    int
        An iterator over the epoch indices, up to the point where the maximum
        number of expected corrections is reached.

    Notes
    -----
    This method runs a certain number of epochs with a given number of iterations
    per epoch on a backend. It will yield the epoch index at each epoch iteration
    while running, and will stop if and when the number of corrections found by the
    backend exceeds the specified `max_corrections` or after performing number of
    epochs specified with `max_epochs`.

    """
    start = perf_counter()
    total_iterations = max_epochs * iterations_per_epoch
    epoch_index = 0

    for epoch_index in range(max_epochs):
        # Run N iterations of algorithm without checking stop conditions.
        self.backend.run_epoch(iterations_per_epoch, epoch_index)

        iterations_executed = (epoch_index + 1) * iterations_per_epoch
        logging.debug(
            "Executed %r iterations, total %r / %r (%.2f%%)",
            iterations_per_epoch,
            iterations_executed,
            total_iterations,
            (iterations_executed / total_iterations) * 100,
        )
        # Check if we already reached expected number of corrections
        if self.backend.get_corrections_count() >= max_corrections:
            logging.info(
                "Reached expected maximal number of corrections %r",
                max_corrections,
            )
            break

        yield epoch_index

    end = perf_counter()
    logging.info("Elapsed time: %r.", end - start)

    # Possibly trigger user defined code before ending execution.
    yield epoch_index
    return

get_state

get_state() -> npt.NDArray[np.complex128]

Return current system state with all optimizations applied.

Source code in cssfinder/algorithm/gilbert.py
def get_state(self) -> npt.NDArray[np.complex128]:
    """Return current system state with all optimizations applied."""
    return self.backend.get_state()

get_corrections

get_corrections() -> list[tuple[int, int, float]]

Return list of all corrections found during optimization.

Source code in cssfinder/algorithm/gilbert.py
def get_corrections(self) -> list[tuple[int, int, float]]:
    """Return list of all corrections found during optimization."""
    return self.backend.get_corrections()

get_corrections_count

get_corrections_count() -> int

Return number of all corrections found during optimization.

Source code in cssfinder/algorithm/gilbert.py
def get_corrections_count(self) -> int:
    """Return number of all corrections found during optimization."""
    return self.backend.get_corrections_count()

AlgorithmError

Bases: Exception

Base for exceptions raised by gilbert algorithm.

Source code in cssfinder/algorithm/gilbert.py
class AlgorithmError(Exception):
    """Base for exceptions raised by gilbert algorithm."""

AlgorithmNotSaturatedError

Bases: Exception

Raised when action was performed on which required algorithm to finish execution on instance which was not run.

Source code in cssfinder/algorithm/gilbert.py
class AlgorithmNotSaturatedError(Exception):
    """Raised when action was performed on which required algorithm to finish execution
    on instance which was not run.
    """

HookError

Bases: Exception

Base class for hook error wrappers.

Source code in cssfinder/algorithm/gilbert.py
class HookError(Exception):
    """Base class for hook error wrappers."""

SaveStateHookError

Bases: HookError

Wrapper for exceptions raised by save_state_hook.

Source code in cssfinder/algorithm/gilbert.py
class SaveStateHookError(HookError):
    """Wrapper for exceptions raised by save_state_hook."""

SaveCorrectionsHookError

Bases: HookError

Wrapper for exceptions raised by save_state_hook.

Source code in cssfinder/algorithm/gilbert.py
class SaveCorrectionsHookError(HookError):
    """Wrapper for exceptions raised by save_state_hook."""