LinearExpression
Bases: QuadraticProgramElement
Representation of a linear expression by its coefficients.
Source code in q3as/quadratic/problems/linear_expression.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
bounds: ExpressionBounds
property
Returns the lower bound and the upper bound of the linear expression
Returns:
| Type | Description |
|---|---|
ExpressionBounds
|
The lower bound and the upper bound of the linear expression |
Raises:
| Type | Description |
|---|---|
QiskitOptimizationError
|
if the linear expression contains any unbounded variable |
coefficients: dok_matrix
property
writable
Returns the coefficients of the linear expression.
Returns:
| Type | Description |
|---|---|
dok_matrix
|
The coefficients of the linear expression. |
__getitem__(i)
Returns the i-th coefficient where i can be a variable name or index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i |
Union[int, str]
|
the index or name of the variable corresponding to the coefficient. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The coefficient corresponding to the addressed variable. |
Source code in q3as/quadratic/problems/linear_expression.py
__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:
| Name | Type | Description | Default |
|---|---|---|---|
quadratic_program |
Any
|
The parent QuadraticProgram. |
required |
coefficients |
Union[ndarray, spmatrix, List[float], Dict[Union[int, str], float]]
|
The (sparse) representation of the coefficients. |
required |
Source code in q3as/quadratic/problems/linear_expression.py
evaluate(x)
Evaluate the linear expression for given variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
Union[ndarray, List, Dict[Union[int, str], float]]
|
The values of the variables to be evaluated. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value of the linear expression given the variable values. |
Source code in q3as/quadratic/problems/linear_expression.py
evaluate_gradient(x)
Evaluate the gradient of the linear expression for given variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
Union[ndarray, List, Dict[Union[int, str], float]]
|
The values of the variables to be evaluated. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The value of the gradient of the linear expression given the variable values. |
Source code in q3as/quadratic/problems/linear_expression.py
to_array()
Returns the coefficients of the linear expression as array.
Returns:
| Type | Description |
|---|---|
ndarray
|
An array with the coefficients corresponding to the linear expression. |
Source code in q3as/quadratic/problems/linear_expression.py
to_dict(use_name=False)
Returns the coefficients of the linear expression as dictionary, either using variable names or indices as keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_name |
bool
|
Determines whether to use index or names to refer to variables. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[Union[int, str], float]
|
An dictionary with the coefficients corresponding to the linear expression. |