qiskit_addon_opt_mapper.problems.QuadraticExpression

class QuadraticExpression(optimization_problem, coefficients)[source]

Bases: OptimizationProblemElement

Representation of a quadratic expression by its coefficients.

Creates a new quadratic expression.

The quadratic 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. We stores values in a compressed way, i.e., values at symmetric positions are summed up in the upper triangle. For example, {(0, 1): 1, (1, 0): 2} -> {(0, 1): 3}.

Parameters:
__init__(optimization_problem, coefficients)[source]

Creates a new quadratic expression.

The quadratic 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. We stores values in a compressed way, i.e., values at symmetric positions are summed up in the upper triangle. For example, {(0, 1): 1, (1, 0): 2} -> {(0, 1): 3}.

Parameters:
Return type:

None

Methods

__init__(optimization_problem, coefficients)

Creates a new quadratic expression.

evaluate(x)

Evaluate the quadratic expression for given variables: x * Q * x.

evaluate_gradient(x)

Evaluate the gradient of the quadratic expression for given variables.

to_array([symmetric])

Returns the coefficients of the quadratic expression as array.

to_dict([symmetric, use_name])

Returns the coefficients of the quadratic expression as dictionary.

Attributes

bounds

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

coefficients

Returns the coefficients of the quadratic expression.

optimization_problem

Returns the parent OptimizationProblem.

property bounds: ExpressionBounds

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

Returns:

The lower bound and the upper bound of the quadratic expression

Raises:

OptimizationError – if the quadratic expression contains any unbounded variable

property coefficients: dok_matrix

Returns the coefficients of the quadratic expression.

Returns:

The coefficients of the quadratic expression.

evaluate(x)[source]

Evaluate the quadratic expression for given variables: x * Q * x.

Parameters:

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

Returns:

The value of the quadratic expression given the variable values.

Return type:

float

evaluate_gradient(x)[source]

Evaluate the gradient of the quadratic expression for given variables.

Parameters:

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

Returns:

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

Return type:

ndarray

property optimization_problem: OptimizationProblem

Returns the parent OptimizationProblem.

Returns:

The parent OptimizationProblem.

to_array(symmetric=False)[source]

Returns the coefficients of the quadratic expression as array.

Parameters:

symmetric (bool) – Determines whether the output is in a symmetric form or not.

Returns:

An array with the coefficients corresponding to the quadratic expression.

Return type:

ndarray

to_dict(symmetric=False, use_name=False)[source]

Returns the coefficients of the quadratic expression as dictionary.

Either using tuples of variable names or indices as keys.

Parameters:
  • symmetric (bool) – Determines whether the output is in a symmetric form or not.

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

Returns:

An dictionary with the coefficients corresponding to the quadratic expression.

Return type:

dict[tuple[int, int] | tuple[str, str], float]