qiskit-documentation/docs/api/qiskit/0.26/qiskit.optimization.problem...

150 lines
6.8 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: LinearExpression
description: API reference for qiskit.optimization.problems.LinearExpression
in_page_toc_min_heading_level: 1
python_api_type: class
python_api_name: qiskit.optimization.problems.LinearExpression
---
# qiskit.optimization.problems.LinearExpression
<Class id="qiskit.optimization.problems.LinearExpression" isDedicatedPage={true} github="https://github.com/qiskit-community/qiskit-aqua/tree/stable/0.9/qiskit/optimization/problems/linear_expression.py" signature="LinearExpression(quadratic_program, coefficients)" modifiers="class">
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\_\_
<Function id="qiskit.optimization.problems.LinearExpression.__init__" signature="__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.
</Function>
## Methods
| | |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
| [`__init__`](#qiskit.optimization.problems.LinearExpression.__init__ "qiskit.optimization.problems.LinearExpression.__init__")(quadratic\_program, coefficients) | Creates a new linear expression. |
| [`evaluate`](#qiskit.optimization.problems.LinearExpression.evaluate "qiskit.optimization.problems.LinearExpression.evaluate")(x) | Evaluate the linear expression for given variables. |
| [`evaluate_gradient`](#qiskit.optimization.problems.LinearExpression.evaluate_gradient "qiskit.optimization.problems.LinearExpression.evaluate_gradient")(x) | Evaluate the gradient of the linear expression for given variables. |
| [`to_array`](#qiskit.optimization.problems.LinearExpression.to_array "qiskit.optimization.problems.LinearExpression.to_array")() | Returns the coefficients of the linear expression as array. |
| [`to_dict`](#qiskit.optimization.problems.LinearExpression.to_dict "qiskit.optimization.problems.LinearExpression.to_dict")(\[use\_name]) | Returns the coefficients of the linear expression as dictionary, either using variable names or indices as keys. |
## Attributes
| | |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| [`coefficients`](#qiskit.optimization.problems.LinearExpression.coefficients "qiskit.optimization.problems.LinearExpression.coefficients") | Returns the coefficients of the linear expression. |
| [`quadratic_program`](#qiskit.optimization.problems.LinearExpression.quadratic_program "qiskit.optimization.problems.LinearExpression.quadratic_program") | Returns the parent QuadraticProgram. |
### coefficients
<Attribute id="qiskit.optimization.problems.LinearExpression.coefficients">
Returns the coefficients of the linear expression.
**Return type**
`dok_matrix`
**Returns**
The coefficients of the linear expression.
</Attribute>
### evaluate
<Function id="qiskit.optimization.problems.LinearExpression.evaluate" signature="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.
</Function>
### evaluate\_gradient
<Function id="qiskit.optimization.problems.LinearExpression.evaluate_gradient" signature="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.
</Function>
### quadratic\_program
<Attribute id="qiskit.optimization.problems.LinearExpression.quadratic_program">
Returns the parent QuadraticProgram.
**Return type**
`Any`
**Returns**
The parent QuadraticProgram.
</Attribute>
### to\_array
<Function id="qiskit.optimization.problems.LinearExpression.to_array" signature="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.
</Function>
### to\_dict
<Function id="qiskit.optimization.problems.LinearExpression.to_dict" signature="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.
</Function>
</Class>