qiskit_addon_opt_mapper.problems.LinearExpression

class LinearExpression(optimization_problem, coefficients)[source]

Bases: OptimizationProblemElement

Representation of a linear expression by its coefficients.

Creates a new linear expression.

The linear expression can be defined via an array, a list, a sparse matrix, or a dictionary that uses variable names or indices as keys and stores the values internally as a dok_matrix.

Parameters:
  • optimization_problem (Any) – The parent OptimizationProblem.

  • coefficients (ndarray | spmatrix | list[float] | dict[int | str, float]) – The (sparse) representation of the coefficients.

__init__(optimization_problem, coefficients)[source]

Creates a new linear expression.

The linear expression can be defined via an array, a list, a sparse matrix, or a dictionary that uses variable names or indices as keys and stores the values internally as a dok_matrix.

Parameters:
  • optimization_problem (Any) – The parent OptimizationProblem.

  • coefficients (ndarray | spmatrix | list[float] | dict[int | str, float]) – The (sparse) representation of the coefficients.

Return type:

None

Methods

__init__(optimization_problem, coefficients)

Creates a new linear expression.

evaluate(x)

Evaluate the linear expression for given variables.

evaluate_gradient(x)

Evaluate the gradient of the linear expression for given variables.

to_array()

Returns the coefficients of the linear expression as array.

to_dict([use_name])

Returns the coefficients of the linear expression as dictionary.

Attributes

bounds

Returns the lower bound and the upper bound of the linear expression.

coefficients

Returns the coefficients of the linear expression.

optimization_problem

Returns the parent OptimizationProblem.

property bounds: ExpressionBounds

Returns the lower bound and the upper bound of the linear expression.

Returns:

The lower bound and the upper bound of the linear expression

Raises:

OptimizationError – if the linear expression contains any unbounded variable

property coefficients: dok_matrix

Returns the coefficients of the linear expression.

Returns:

The coefficients of the linear expression.

evaluate(x)[source]

Evaluate the linear expression for given variables.

Parameters:

x (ndarray | list | dict[int | str, float]) – The values of the variables to be evaluated.

Returns:

The value of the linear expression given the variable values.

Return type:

float

evaluate_gradient(x)[source]

Evaluate the gradient of the linear expression for given variables.

Parameters:

x (ndarray | list | dict[int | str, float]) – The values of the variables to be evaluated.

Returns:

The value of the gradient of the linear expression given the variable values.

Return type:

ndarray

property optimization_problem: OptimizationProblem

Returns the parent OptimizationProblem.

Returns:

The parent OptimizationProblem.

to_array()[source]

Returns the coefficients of the linear expression as array.

Returns:

An array with the coefficients corresponding to the linear expression.

Return type:

ndarray

to_dict(use_name=False)[source]

Returns the coefficients of the linear expression as dictionary.

Either using variable names or indices as keys.

Parameters:

use_name (bool) – Determines whether to use index or names to refer to variables.

Returns:

An dictionary with the coefficients corresponding to the linear expression.

Return type:

dict[int | str, float]