Skip to content

API reference

bartiq

CompiledRoutine dataclass

Bases: Generic[T]

from_qref classmethod

from_qref(
    qref_obj: AnyQrefType, backend: SymbolicBackend[T]
) -> CompiledRoutine[T]

Load CompiledRoutine from a QREF definition, using specified backend for parsing expressions.

PortDirection

Bases: str, Enum

Class for representing port direction.

ResourceType

Bases: str, Enum

Class for representing types of resources.

Routine dataclass

Bases: Generic[T]

from_qref classmethod

from_qref(
    qref_obj: AnyQrefType, backend: SymbolicBackend[T]
) -> Routine[T]

Load Routine from a QREF definition, using specified backend for parsing expressions.

compile_routine

compile_routine(
    routine: SchemaV1 | RoutineV1 | Routine[T],
    *,
    backend: SymbolicBackend[T] = sympy_backend,
    preprocessing_stages: Iterable[
        PreprocessingStage[T]
    ] = DEFAULT_PREPROCESSING_STAGES,
    skip_verification: bool = False
) -> CompilationResult[T]

Performs symbolic compilation of a given routine.

In this context, compilation means transforming a routine defined in terms of routine-local variables into one defined in terms of global input parameters.

Parameters:

Name Type Description Default
routine SchemaV1 | RoutineV1 | Routine[T]

routine to be compiled.

required
backend SymbolicBackend[T]

a backend used for manipulating symbolic expressions.

sympy_backend
preprocessing_stages Iterable[PreprocessingStage[T]]

functions used for preprocessing of a given routine to make sure it can be correctly compiled by Bartiq.

DEFAULT_PREPROCESSING_STAGES
skip_verification bool

a flag indicating whether verification of the routine should be skipped.

False

evaluate

evaluate(
    compiled_routine: CompiledRoutine[T],
    assignments: Assignments[T],
    *,
    backend: SymbolicBackend[T] = sympy_backend,
    functions_map: FunctionsMap[T] | None = None
) -> EvaluationResult[T]

Substitutes variables into compiled routine.

Parameters:

Name Type Description Default
compiled_routine CompiledRoutine[T]

a compiled routine to be evaluated.

required
assignments Assignments[T]

a dictionary mapping a subset of input params of compiled_routine either into concrete values, or other expressions. Expressions can be provided either as concrete instances of symbolic expressions understood by backend, or via strings, e.g. `{"N": 2, "M": "k+3"}.

required
backend SymbolicBackend[T]

a backend used for manipulating symbolic expressions.

sympy_backend
functions_map FunctionsMap[T] | None

a dictionary mapping function names to their concrete implementations.

None

Returns:

Type Description
EvaluationResult[T]

A new instance of CompiledRoutine with appropriate substitutions made.

bartiq.analysis

BigO

__init__

__init__(expr: Expr, variable: Symbol | None = None)

Class for representing expressions in Big O notation.

It analyzes given expression and returns all the Big O terms in it. If variable is provided, it analyses scaling in this particular variable, otherwise it assumes all the symbols are variables.

Note

It's an experimental tool and is meant to facilitate the analysis, but it might not produce correct results, especially for more complicated expressions. In case of any problems please create an issue on project's GitHub, we'd love to hear your feedback on this!

Parameters:

Name Type Description Default
expr Expr

sympy expression we want to analyze

required
variable Symbol | None

variable for which we want to performa analysis.

None

Optimizer

gradient_descent staticmethod

gradient_descent(
    cost_func: Callable[[float], float],
    x0: Optional[float] = None,
    bounds: Optional[Tuple[float, float]] = None,
    learning_rate: float = 1e-06,
    max_iter: int = 1000,
    tolerance: float = 1e-08,
    momentum: float = 0.9,
) -> Dict[str, Any]

Perform gradient descent optimization to find the minimum of the expression with respect to the specified parameter.

Parameters:

Name Type Description Default
cost_func Callable[[float], float]

The objective cost function to be minimized.

required
x0 Optional[float]

The starting point for the optimization. If None, a random value is

None
bounds Optional[Tuple[float, float]]

A tuple specifying the (min, max) range for the parameter value.

None
learning_rate float

The step size for each iteration. Default is 1e-6.

1e-06
max_iter int

The maximum number of iterations to perform. Default is 1000.

1000
tolerance float

The tolerance level for stopping criteria. Default is 1e-8.

1e-08
momentum float

The momentum factor to control the influence of previous updates.

0.9

Returns:

Name Type Description
Dict Dict[str, Any]

A dictionary containing the final value of the parameter and the history of values

Dict[str, Any]

during optimization.

minimize

minimize(
    expression: str,
    param: str,
    optimizer: str = "gradient_descent",
    optimizer_kwargs: Optional[OptimizerKwargs] = None,
    scipy_kwargs: Optional[ScipyOptimizerKwargs] = None,
    backend=Backend,
) -> Dict[str, Any]

Find the optimal parameter value that minimizes a given expression.

To visualize minimize results using a plotting library like matplotlib:

  1. Plot x_history (parameter values) on the x-axis.
  2. Plot minimum_cost on the y-axis.

bartiq.symbolics

bartiq.errors

BartiqCompilationError

Bases: Exception

Raised for errors during Bartiq function compilation.

BartiqPrecompilationError

Bases: Exception

Raised for errors during Bartiq function pre-compilation.

bartiq.integrations

routine_to_latex

routine_to_latex(
    routine: SchemaV1 | RoutineV1,
    show_non_root_resources: bool = True,
) -> str

Returns a snippet of LaTeX used to render the routine using clear LaTeX.

Parameters:

Name Type Description Default
routine SchemaV1 | RoutineV1

The routine to render.

required
show_non_root_costs

If True (default), displays all costs, otherwise only includes costs from the root node.

required

Returns:

Type Description
str

A LaTeX snippet of the routine.

bartiq.transform

add_aggregated_resources

add_aggregated_resources(
    routine: Routine[T],
    aggregation_dict: dict[str, dict[str, Any]],
    remove_decomposed: bool = True,
    backend: SymbolicBackend[T] = sympy_backend,
) -> Routine[T]

Add aggregated resources to bartiq routine based on the aggregation dictionary.

Parameters:

Name Type Description Default
routine Routine[T]

The program to which the resources will be added.

required
aggregation_dict dict[str, dict[str, Any]]

A dictionary that decomposes resources into more fundamental components along with their

required
remove_decomposed

Whether to remove the decomposed resources from the routine. Defaults to True.

True
backend

Backend instance to use for handling expressions. Defaults to sympy_backend.

sympy_backend

Returns:

Name Type Description
Routine Routine[T]

The program with aggregated resources.