qiskit.optimization.problems.LinearExpression
class LinearExpression(quadratic_program, coefficients)
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
- quadratic_program (
Any
) – The parent QuadraticProgram. - coefficients (
Union
[ndarray
,spmatrix
,List
[float
],Dict
[Union
[int
,str
],float
]]) – The (sparse) representation of the coefficients.
__init__
__init__(quadratic_program, 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
- quadratic_program (
Any
) – The parent QuadraticProgram. - coefficients (
Union
[ndarray
,spmatrix
,List
[float
],Dict
[Union
[int
,str
],float
]]) – The (sparse) representation of the coefficients.
Methods
__init__ (quadratic_program, 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, either using variable names or indices as keys. |
Attributes
coefficients | Returns the coefficients of the linear expression. |
quadratic_program | Returns the parent QuadraticProgram. |
coefficients
Returns the coefficients of the linear expression.
Return type
dok_matrix
Returns
The coefficients of the linear expression.
evaluate
evaluate(x)
Evaluate the linear expression for given variables.
Parameters
x (Union
[ndarray
, List
, Dict
[Union
[int
, str
], float
]]) – The values of the variables to be evaluated.
Return type
float
Returns
The value of the linear expression given the variable values.
evaluate_gradient
evaluate_gradient(x)
Evaluate the gradient of the linear expression for given variables.
Parameters
x (Union
[ndarray
, List
, Dict
[Union
[int
, str
], float
]]) – The values of the variables to be evaluated.
Return type
ndarray
Returns
The value of the gradient of the linear expression given the variable values.
quadratic_program
Returns the parent QuadraticProgram.
Return type
Any
Returns
The parent QuadraticProgram.
to_array
to_array()
Returns the coefficients of the linear expression as array.
Return type
ndarray
Returns
An array with the coefficients corresponding to the linear expression.
to_dict
to_dict(use_name=False)
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.
Return type
Dict
[Union
[int
, str
], float
]
Returns
An dictionary with the coefficients corresponding to the linear expression.