qiskit_addon_opt_mapper.problems.HigherOrderExpression

class HigherOrderExpression(optimization_problem, coefficients)[source]

Bases: OptimizationProblemElement

Representation of a symmetric k-th order expression by its coefficients.

We represent a symmetric polynomial term of order k as f(x) = sum_{t} C[t] * prod_{i in t} x[i], where t is a multiset of variable indices of length k.

When dealing with multidimensional array indices, the value is stored only at the lexicographically smallest permutation of the indices (obtained by sorting them in ascending order).

For example, for a 4th-order term 2⋅x1⋅x2⋅x3⋅x4, the coefficient “2” is stored at dict((1, 2, 3, 4)). Other permutations like dict((2, 1, 4, 3)) or dict((4, 3, 2, 1)) are left empty.

Creates a new higher-order expression.

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

  • coefficients (ndarray | dict[Key, float] | list) – Coefficients as either: - A dense ndarray or list with shape (n,)*k, or - A dict mapping a tuple of variable indices/names (length k) to float. Keys are canonicalized to ascending order and summed.

__init__(optimization_problem, coefficients)[source]

Creates a new higher-order expression.

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

  • coefficients (ndarray | dict[Key, float] | list) – Coefficients as either: - A dense ndarray or list with shape (n,)*k, or - A dict mapping a tuple of variable indices/names (length k) to float. Keys are canonicalized to ascending order and summed.

Return type:

None

Methods

__init__(optimization_problem, coefficients)

Creates a new higher-order expression.

evaluate(x)

Evaluate the expression: sum_{t} C[t] * prod_{i in t} x[i].

evaluate_gradient(x)

Evaluate gradient wrt x.

to_array([symmetric])

Returns a dense tensor.

to_dict([use_name])

Returns the internal coefficients as a dictionary.

Attributes

bounds

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

coefficients

Returns a copy of internal (canonical) coefficient dictionary.

num_variables

Returns the number of variables in this expression.

optimization_problem

Returns the parent OptimizationProblem.

order

Returns the order of the polynomial (k >= 3).

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

property coefficients: dict[tuple[int, ...], float]

Returns a copy of internal (canonical) coefficient dictionary.

evaluate(x)[source]

Evaluate the expression: sum_{t} C[t] * prod_{i in t} x[i].

Parameters:

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

Returns:

The value of the higher order expression given the variable values.

Return type:

float

evaluate_gradient(x)[source]

Evaluate gradient wrt x.

For each m, g[m] = sum_{t} C[t] * (count_m_in_t) * prod_{i in t / {one m}}.

Parameters:

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

Returns:

The value of the gradient of the higher order expression given the variable values.

Return type:

ndarray

property num_variables: int

Returns the number of variables in this expression.

property optimization_problem: OptimizationProblem

Returns the parent OptimizationProblem.

Returns:

The parent OptimizationProblem.

property order: int

Returns the order of the polynomial (k >= 3).

to_array(symmetric=False)[source]

Returns a dense tensor.

Parameters:

symmetric (bool) – If False, returns a tensor with coefficients at the lexicographically smallest index only (others zero). If True, distributes coefficients equally over all permutations of the multiset key.

Return type:

ndarray

to_dict(use_name=False)[source]

Returns the internal coefficients as a dictionary.

Parameters:

use_name (bool)

Return type:

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