QuadraticProgram#
- class QuadraticProgram(name='')[ソース]#
ベースクラス:
object
Quadratically Constrained Quadratic Program representation.
This representation supports inequality and equality constraints, as well as continuous, binary, and integer variables.
- パラメータ:
name (str) – The name of the quadratic program.
Attributes
- linear_constraints#
Returns the list of linear constraints of the quadratic program.
- 戻り値:
List of linear constraints.
- linear_constraints_index#
Returns the dictionary that maps the name of a linear constraint to its index.
- 戻り値:
The linear constraint index dictionary.
- name#
Returns the name of the quadratic program.
- 戻り値:
The name of the quadratic program.
- objective#
Returns the quadratic objective.
- 戻り値:
The quadratic objective.
- quadratic_constraints#
Returns the list of quadratic constraints of the quadratic program.
- 戻り値:
List of quadratic constraints.
- quadratic_constraints_index#
Returns the dictionary that maps the name of a quadratic constraint to its index.
- 戻り値:
The quadratic constraint index dictionary.
- status#
Status of the quadratic program. It can be infeasible due to variable substitution.
- 戻り値:
The status of the quadratic program
- variables#
Returns the list of variables of the quadratic program.
- 戻り値:
List of variables.
- variables_index#
Returns the dictionary that maps the name of a variable to its index.
- 戻り値:
The variable index dictionary.
Methods
- binary_var(name=None)[ソース]#
Adds a binary variable to the quadratic program.
- パラメータ:
name (str | None) – The name of the variable. If it’s
None
or empty""
, the default name, e.g.,x0
, is used.- 戻り値:
The added variable.
- 例外:
QiskitOptimizationError – if the variable name is already occupied.
- 戻り値の型:
- binary_var_dict(keys, name=None, key_format='{}')[ソース]#
Uses 『var_dict』 to construct a dictionary of binary variables
- パラメータ:
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A dictionary mapping the variable names to variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- binary_var_list(keys, name=None, key_format='{}')[ソース]#
Uses 『var_list』 to construct a list of binary variables
- パラメータ:
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A list of variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- clear()[ソース]#
Clears the quadratic program, i.e., deletes all variables, constraints, the objective function as well as the name.
- continuous_var(lowerbound=0, upperbound=1e+20, name=None)[ソース]#
Adds a continuous variable to the quadratic program.
- パラメータ:
- 戻り値:
The added variable.
- 例外:
QiskitOptimizationError – if the variable name is already occupied.
- 戻り値の型:
- continuous_var_dict(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]#
Uses 『var_dict』 to construct a dictionary of continuous variables
- パラメータ:
lowerbound (float | int) – The lower bound of the variable(s).
upperbound (float | int) – The upper bound of the variable(s).
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A dictionary mapping the variable names to variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- continuous_var_list(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]#
Uses 『var_list』 to construct a list of continuous variables
- パラメータ:
lowerbound (float | int) – The lower bound of the variable(s).
upperbound (float | int) – The upper bound of the variable(s).
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A list of variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- export_as_lp_string()[ソース]#
Returns the quadratic program as a string of LP format.
- 戻り値:
A string representing the quadratic program.
- 戻り値の型:
- from_ising(qubit_op, offset=0.0, linear=False)[ソース]#
Create a quadratic program from a qubit operator and a shift value.
Variables are mapped to qubits in the same order, i.e., i-th variable is mapped to i-th qubit. See https://github.com/Qiskit/qiskit-terra/issues/1148 for details.
- パラメータ:
qubit_op (BaseOperator) – The qubit operator of the problem.
offset (float) – The constant value in the Ising Hamiltonian.
linear (bool) – If linear is True, \(x^2\) is treated as a linear term since \(x^2 = x\) for \(x \in \{0,1\}\). Else, \(x^2\) is treated as a quadratic term. The default value is False.
- 例外:
QiskitOptimizationError – If there are Pauli Xs in any Pauli term
QiskitOptimizationError – If there are more than 2 Pauli Zs in any Pauli term
NotImplementedError – If the input operator is a ListOp
- get_feasibility_info(x)[ソース]#
Returns whether a solution is feasible or not along with the violations. :param x: a solution value, such as returned in an optimizer result.
- 戻り値:
Whether the solution provided is feasible or not. List[Variable]: List of variables which are violated. List[Constraint]: List of constraints which are violated.
- 戻り値の型:
feasible
- 例外:
QiskitOptimizationError – If the input x is not same len as total vars
- get_linear_constraint(i)[ソース]#
Returns a linear constraint for a given name or index.
- パラメータ:
- 戻り値:
The corresponding constraint.
- 例外:
IndexError – if the index is out of the list size
KeyError – if the name does not exist
- 戻り値の型:
- get_num_binary_vars()[ソース]#
Returns the total number of binary variables.
- 戻り値:
The total number of binary variables.
- 戻り値の型:
- get_num_continuous_vars()[ソース]#
Returns the total number of continuous variables.
- 戻り値:
The total number of continuous variables.
- 戻り値の型:
- get_num_integer_vars()[ソース]#
Returns the total number of integer variables.
- 戻り値:
The total number of integer variables.
- 戻り値の型:
- get_num_linear_constraints()[ソース]#
Returns the number of linear constraints.
- 戻り値:
The number of linear constraints.
- 戻り値の型:
- get_num_quadratic_constraints()[ソース]#
Returns the number of quadratic constraints.
- 戻り値:
The number of quadratic constraints.
- 戻り値の型:
- get_num_vars(vartype=None)[ソース]#
Returns the total number of variables or the number of variables of the specified type.
- パラメータ:
vartype (VarType | None) – The type to be filtered on. All variables are counted if None.
- 戻り値:
The total number of variables.
- 戻り値の型:
- get_quadratic_constraint(i)[ソース]#
Returns a quadratic constraint for a given name or index.
- パラメータ:
- 戻り値:
The corresponding constraint.
- 例外:
IndexError – if the index is out of the list size
KeyError – if the name does not exist
- 戻り値の型:
- integer_var(lowerbound=0, upperbound=1e+20, name=None)[ソース]#
Adds an integer variable to the quadratic program.
- パラメータ:
- 戻り値:
The added variable.
- 例外:
QiskitOptimizationError – if the variable name is already occupied.
- 戻り値の型:
- integer_var_dict(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]#
Uses 『var_dict』 to construct a dictionary of integer variables
- パラメータ:
lowerbound (float | int) – The lower bound of the variable(s).
upperbound (float | int) – The upper bound of the variable(s).
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A dictionary mapping the variable names to variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- integer_var_list(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]#
Uses 『var_list』 to construct a list of integer variables
- パラメータ:
lowerbound (float | int) – The lower bound of the variable(s).
upperbound (float | int) – The upper bound of the variable(s).
name (str | None) – The name(s) of the variable(s). If it’s
None
or empty""
, the default name, e.g.,x0
, is used.key_format (str) – The format used to name/index the variable(s).
keys (int | Sequence) – If keys: int, it is interpreted as the number of variables to construct. Otherwise, the elements of the sequence are converted to strings via 『str』 and substituted into key_format.
- 戻り値:
A list of variable instances.
- 例外:
QiskitOptimizationError – if the variable name is already taken.
QiskitOptimizationError – if less than one variable instantiation is attempted.
QiskitOptimizationError – if key_format has more than one substitution or a nested substitution.
- 戻り値の型:
- linear_constraint(linear=None, sense='<=', rhs=0.0, name=None)[ソース]#
- Adds a linear equality constraint to the quadratic program of the form:
(linear * x) sense rhs
.
- パラメータ:
linear (ndarray | spmatrix | List[float] | Dict[int | str, float]) – The linear coefficients of the left-hand side of the constraint.
sense (str | ConstraintSense) –
The sense of the constraint,
==
,=
,E
, andEQ
denote 『equal to』.>=
,>
,G
, andGE
denote 『greater-than-or-equal-to』.<=
,<
,L
, andLE
denote 『less-than-or-equal-to』.
rhs (float) – The right-hand side of the constraint.
name (str | None) – The name of the constraint. If it’s
None
or empty""
, the default name, e.g.,c0
, is used.
- 戻り値:
The added constraint.
- 例外:
QiskitOptimizationError – if the constraint name already exists or the sense is not valid.
- 戻り値の型:
- maximize(constant=0.0, linear=None, quadratic=None)[ソース]#
Sets a quadratic objective to be maximized.
- パラメータ:
constant (float) – the constant offset of the objective.
linear (ndarray | spmatrix | List[float] | Dict[int | str, float]) – the coefficients of the linear part of the objective.
quadratic (ndarray | spmatrix | List[List[float]] | Dict[Tuple[int | str, int | str], float]) – the coefficients of the quadratic part of the objective.
- 戻り値:
The created quadratic objective.
- 戻り値の型:
None
- minimize(constant=0.0, linear=None, quadratic=None)[ソース]#
Sets a quadratic objective to be minimized.
- パラメータ:
constant (float) – the constant offset of the objective.
linear (ndarray | spmatrix | List[float] | Dict[int | str, float]) – the coefficients of the linear part of the objective.
quadratic (ndarray | spmatrix | List[List[float]] | Dict[Tuple[int | str, int | str], float]) – the coefficients of the quadratic part of the objective.
- 戻り値:
The created quadratic objective.
- 戻り値の型:
None
- prettyprint(wrap=80)[ソース]#
Returns a pretty printed string of this problem.
- パラメータ:
wrap (int) – The text width to wrap the output strings. It is disabled by setting 0. Note that some strings might exceed this value, for example, a long variable name won’t be wrapped. The default value is 80.
- 戻り値:
A pretty printed string representing the problem.
- 例外:
QiskitOptimizationError – if there is a non-printable name.
- 戻り値の型:
- quadratic_constraint(linear=None, quadratic=None, sense='<=', rhs=0.0, name=None)[ソース]#
- Adds a quadratic equality constraint to the quadratic program of the form:
(x * quadratic * x + linear * x) sense rhs
.
- パラメータ:
linear (ndarray | spmatrix | List[float] | Dict[int | str, float]) – The linear coefficients of the constraint.
quadratic (ndarray | spmatrix | List[List[float]] | Dict[Tuple[int | str, int | str], float]) – The quadratic coefficients of the constraint.
sense (str | ConstraintSense) –
The sense of the constraint,
==
,=
,E
, andEQ
denote 『equal to』.>=
,>
,G
, andGE
denote 『greater-than-or-equal-to』.<=
,<
,L
, andLE
denote 『less-than-or-equal-to』.
rhs (float) – The right-hand side of the constraint.
name (str | None) – The name of the constraint. If it’s
None
or empty""
, the default name, e.g.,q0
, is used.
- 戻り値:
The added constraint.
- 例外:
QiskitOptimizationError – if the constraint name already exists.
- 戻り値の型:
- read_from_lp_file(filename)[ソース]#
Loads the quadratic program from a LP file.
- パラメータ:
filename (str) – The filename of the file to be loaded.
- 例外:
FileNotFoundError – If the file does not exist.
注釈
This method requires CPLEX to be installed and present in
PYTHONPATH
.
- remove_linear_constraint(i)[ソース]#
Remove a linear constraint
- パラメータ:
- 例外:
KeyError – if name does not exist
IndexError – if index is out of range
- remove_quadratic_constraint(i)[ソース]#
Remove a quadratic constraint
- パラメータ:
i (str | int) – an index or a name of a quadratic constraint
- 例外:
KeyError – if name does not exist
IndexError – if index is out of range
- substitute_variables(constants=None, variables=None)[ソース]#
Substitutes variables with constants or other variables.
- パラメータ:
constants (Dict[int | str, float] | None) – replace variable by constant e.g.,
{'x': 2}
meansx
is substituted with 2variables (Dict[str | int, Tuple[str | int, float]] | None) – replace variables by weighted other variable need to copy everything using name reference to make sure that indices are matched correctly. The lower and upper bounds are updated accordingly. e.g.,
{'x': ('y', 2)}
meansx
is substituted withy * 2
- 戻り値:
An optimization problem by substituting variables with constants or other variables. If the substitution is valid,
QuadraticProgram.status
is stillQuadraticProgram.Status.VALID
. Otherwise, it getsQuadraticProgram.Status.INFEASIBLE
.- 例外:
QiskitOptimizationError – if the substitution is invalid as follows. - Same variable is substituted multiple times. - Coefficient of variable substitution is zero.
- 戻り値の型:
- to_ising()[ソース]#
Return the Ising Hamiltonian of this problem.
Variables are mapped to qubits in the same order, i.e., i-th variable is mapped to i-th qubit. See https://github.com/Qiskit/qiskit-terra/issues/1148 for details.
- 戻り値:
The qubit operator for the problem offset: The constant value in the Ising Hamiltonian.
- 戻り値の型:
qubit_op
- 例外:
QiskitOptimizationError – If a variable type is not binary.
QiskitOptimizationError – If constraints exist in the problem.