class QuadraticProgram(name='')[ソース]

This representation supports inequality and equality constraints, as well as continuous, binary, and integer variables.

パラメータ

name (str) – The name of the quadratic program.

__init__(name='')[ソース]
パラメータ

name (str) – The name of the quadratic program.

Methods

 __init__([name]) type name str binary_var([name]) Adds a binary variable to the quadratic program. binary_var_dict(keys[, name, key_format]) Uses 『var_dict』 to construct a dictionary of binary variables binary_var_list(keys[, name, key_format]) Uses 『var_list』 to construct a list of binary variables Clears the quadratic program, i.e., deletes all variables, constraints, the objective function as well as the name. continuous_var([lowerbound, upperbound, name]) Adds a continuous variable to the quadratic program. continuous_var_dict(keys[, lowerbound, …]) Uses 『var_dict』 to construct a dictionary of continuous variables continuous_var_list(keys[, lowerbound, …]) Uses 『var_list』 to construct a list of continuous variables Returns the quadratic program as a string of LP format. from_docplex(model) Loads this quadratic program from a docplex model. from_ising(qubit_op[, offset, linear]) Create a quadratic program from a qubit operator and a shift value. Returns whether a solution is feasible or not along with the violations. Returns a linear constraint for a given name or index. Returns the total number of binary variables. Returns the total number of continuous variables. Returns the total number of integer variables. Returns the number of linear constraints. Returns the number of quadratic constraints. get_num_vars([vartype]) Returns the total number of variables or the number of variables of the specified type. Returns a quadratic constraint for a given name or index. Returns a variable for a given name or index. integer_var([lowerbound, upperbound, name]) Adds an integer variable to the quadratic program. integer_var_dict(keys[, lowerbound, …]) Uses 『var_dict』 to construct a dictionary of integer variables integer_var_list(keys[, lowerbound, …]) Uses 『var_list』 to construct a dictionary of integer variables Returns whether a solution is feasible or not. linear_constraint([linear, sense, rhs, name]) Adds a linear equality constraint to the quadratic program of the form: maximize([constant, linear, quadratic]) Sets a quadratic objective to be maximized. minimize([constant, linear, quadratic]) Sets a quadratic objective to be minimized. DEPRECATED Returns the quadratic program as a string in Docplex’s pretty print format. prettyprint([out]) DEPRECATED Pretty prints the quadratic program to a given output stream (None = default). quadratic_constraint([linear, quadratic, …]) Adds a quadratic equality constraint to the quadratic program of the form: read_from_lp_file(filename) Loads the quadratic program from a LP file. Remove a linear constraint Remove a quadratic constraint substitute_variables([constants, variables]) Substitutes variables with constants or other variables. Returns a docplex model corresponding to this quadratic program. Return the Ising Hamiltonian of this problem. write_to_lp_file(filename) Writes the quadratic program to an LP file.

Attributes

 linear_constraints Returns the list of linear constraints of the quadratic program. linear_constraints_index Returns the dictionary that maps the name of a linear constraint to its index. name Returns the name of the quadratic program. objective Returns the quadratic objective. quadratic_constraints Returns the list of quadratic constraints of the quadratic program. quadratic_constraints_index Returns the dictionary that maps the name of a quadratic constraint to its index. status Status of the quadratic program. variables Returns the list of variables of the quadratic program. variables_index Returns the dictionary that maps the name of a variable to its index.
Status

QuadraticProgramStatus のエイリアス

binary_var(name=None)[ソース]

Adds a binary variable to the quadratic program.

パラメータ

name (Optional[str]) – The name of the variable.

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 (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

Dict[str, Variable]

A dictionary mapping the variable names to variable instances.

binary_var_list(keys, name=None, key_format='{}')[ソース]

Uses 『var_list』 to construct a list of binary variables

パラメータ
• name (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

List[Variable]

A list of variable instances.

clear()[ソース]

Clears the quadratic program, i.e., deletes all variables, constraints, the objective function as well as the name.

None

continuous_var(lowerbound=0, upperbound=1e+20, name=None)[ソース]

Adds a continuous variable to the quadratic program.

パラメータ
• lowerbound (Union[float, int]) – The lowerbound of the variable.

• upperbound (Union[float, int]) – The upperbound of the variable.

• name (Optional[str]) – The name of the variable.

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 (Union[float, int]) – The lower bound of the variable(s).

• upperbound (Union[float, int]) – The upper bound of the variable(s).

• name (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

Dict[str, Variable]

A dictionary mapping the variable names to variable instances.

continuous_var_list(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]

Uses 『var_list』 to construct a list of continuous variables

パラメータ
• lowerbound (Union[float, int]) – The lower bound of the variable(s).

• upperbound (Union[float, int]) – The upper bound of the variable(s).

• name (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

List[Variable]

A list of variable instances.

export_as_lp_string()[ソース]

Returns the quadratic program as a string of LP format.

str

A string representing the quadratic program.

from_docplex(model)[ソース]

Loads this quadratic program from a docplex model.

Note that this supports only basic functions of docplex as follows: - quadratic objective function - linear / quadratic constraints - binary / integer / continuous variables

パラメータ

model (Model) – The docplex model to be loaded.

QiskitOptimizationError – if the model contains unsupported elements.

None

from_ising(qubit_op, offset=0.0, linear=False)[ソース]

Create a quadratic program from a qubit operator and a shift value.

パラメータ
• qubit_op (Union[OperatorBase, WeightedPauliOperator]) – 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 treat as a quadratic term. The default value is False.

None

get_feasibility_info(x)[ソース]

Returns whether a solution is feasible or not along with the violations. :type x: Union[List[float], ndarray] :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.

パラメータ

i (Union[int, str]) – the index or name of the constraint.

LinearConstraint

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.

int

The total number of binary variables.

get_num_continuous_vars()[ソース]

Returns the total number of continuous variables.

int

The total number of continuous variables.

get_num_integer_vars()[ソース]

Returns the total number of integer variables.

int

The total number of integer variables.

get_num_linear_constraints()[ソース]

Returns the number of linear constraints.

int

The number of linear constraints.

get_num_quadratic_constraints()[ソース]

Returns the number of quadratic constraints.

int

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 (Optional[VarType]) – The type to be filtered on. All variables are counted if None.

int

The total number of variables.

get_quadratic_constraint(i)[ソース]

Returns a quadratic constraint for a given name or index.

パラメータ

i (Union[int, str]) – the index or name of the constraint.

QuadraticConstraint

The corresponding constraint.

• IndexError – if the index is out of the list size

• KeyError – if the name does not exist

get_variable(i)[ソース]

Returns a variable for a given name or index.

パラメータ

i (Union[int, str]) – the index or name of the variable.

Variable

The corresponding variable.

integer_var(lowerbound=0, upperbound=1e+20, name=None)[ソース]

Adds an integer variable to the quadratic program.

パラメータ
• lowerbound (Union[float, int]) – The lowerbound of the variable.

• upperbound (Union[float, int]) – The upperbound of the variable.

• name (Optional[str]) – The name of the variable.

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 (Union[float, int]) – The lower bound of the variable(s).

• upperbound (Union[float, int]) – The upper bound of the variable(s).

• name (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

Dict[str, Variable]

A dictionary mapping the variable names to variable instances.

integer_var_list(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[ソース]

Uses 『var_list』 to construct a dictionary of integer variables

パラメータ
• lowerbound (Union[float, int]) – The lower bound of the variable(s).

• upperbound (Union[float, int]) – The upper bound of the variable(s).

• name (Optional[str]) – The name(s) of the variable(s).

• key_format (str) – The format used to name/index the variable(s).

• keys (Union[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.

List[Variable]

A list of variable instances.

is_feasible(x)[ソース]

Returns whether a solution is feasible or not.

パラメータ

x (Union[List[float], ndarray]) – a solution value, such as returned in an optimizer result.

bool

True if the solution provided is feasible otherwise False.

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 (Union[ndarray, spmatrix, List[float], Dict[Union[int, str], float], None]) – The linear coefficients of the left-hand-side of the constraint.

• sense (Union[str, ConstraintSense]) – The sense of the constraint, - 『==』, 『=』, 『E』, and 『EQ』 denote 『equal to』. - 『>=』, 『>』, 『G』, and 『GE』 denote 『greater-than-or-equal-to』. - 『<=』, 『<』, 『L』, and 『LE』 denote 『less-than-or-equal-to』.

• rhs (float) – The right hand side of the constraint.

• name (Optional[str]) – The name of the constraint.

LinearConstraint

QiskitOptimizationError – if the constraint name already exists or the sense is not valid.

property linear_constraints

Returns the list of linear constraints of the quadratic program.

List[LinearConstraint]

List of linear constraints.

property linear_constraints_index

Returns the dictionary that maps the name of a linear constraint to its index.

Dict[str, int]

The linear constraint index dictionary.

maximize(constant=0.0, linear=None, quadratic=None)[ソース]

Sets a quadratic objective to be maximized.

パラメータ
• constant (float) – the constant offset of the objective.

• linear (Union[ndarray, spmatrix, List[float], Dict[Union[int, str], float], None]) – the coefficients of the linear part of the objective.

• quadratic (Union[ndarray, spmatrix, List[List[float]], Dict[Tuple[Union[int, str], Union[int, str]], float], None]) – the coefficients of the quadratic part of the objective.

None

The created quadratic objective.

minimize(constant=0.0, linear=None, quadratic=None)[ソース]

Sets a quadratic objective to be minimized.

パラメータ
• constant (float) – the constant offset of the objective.

• linear (Union[ndarray, spmatrix, List[float], Dict[Union[int, str], float], None]) – the coefficients of the linear part of the objective.

• quadratic (Union[ndarray, spmatrix, List[List[float]], Dict[Tuple[Union[int, str], Union[int, str]], float], None]) – the coefficients of the quadratic part of the objective.

None

The created quadratic objective.

property name

Returns the name of the quadratic program.

str

The name of the quadratic program.

property objective

Returns the quadratic objective.

QuadraticObjective

pprint_as_string()[ソース]

DEPRECATED Returns the quadratic program as a string in Docplex’s pretty print format. :rtype: str :returns: A string representing the quadratic program.

prettyprint(out=None)[ソース]

DEPRECATED Pretty prints the quadratic program to a given output stream (None = default).

パラメータ

out (Optional[str]) – The output stream or file name to print to. if you specify a file name, the output file name is has 『.mod』 as suffix.

None

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 * Q * x <= rhs.

パラメータ
• linear (Union[ndarray, spmatrix, List[float], Dict[Union[int, str], float], None]) – The linear coefficients of the constraint.

• quadratic (Union[ndarray, spmatrix, List[List[float]], Dict[Tuple[Union[int, str], Union[int, str]], float], None]) – The quadratic coefficients of the constraint.

• sense (Union[str, ConstraintSense]) – The sense of the constraint, - 『==』, 『=』, 『E』, and 『EQ』 denote 『equal to』. - 『>=』, 『>』, 『G』, and 『GE』 denote 『greater-than-or-equal-to』. - 『<=』, 『<』, 『L』, and 『LE』 denote 『less-than-or-equal-to』.

• rhs (float) – The right hand side of the constraint.

• name (Optional[str]) – The name of the constraint.

QuadraticConstraint

QiskitOptimizationError – if the constraint name already exists.

property quadratic_constraints

Returns the list of quadratic constraints of the quadratic program.

List[QuadraticConstraint]

List of quadratic constraints.

property quadratic_constraints_index

Returns the dictionary that maps the name of a quadratic constraint to its index.

Dict[str, int]

The quadratic constraint index dictionary.

read_from_lp_file(filename)[ソース]

Loads the quadratic program from a LP file.

パラメータ

filename (str) – The filename of the file to be loaded.

This method requires CPLEX to be installed and present in PYTHONPATH.

None

remove_linear_constraint(i)[ソース]

Remove a linear constraint

パラメータ

i (Union[str, int]) – an index or a name of a linear constraint

• KeyError – if name does not exist

• IndexError – if index is out of range

None

remove_quadratic_constraint(i)[ソース]

Remove a quadratic constraint

パラメータ

i (Union[str, int]) – an index or a name of a quadratic constraint

• KeyError – if name does not exist

• IndexError – if index is out of range

None

property status

Status of the quadratic program. It can be infeasible due to variable substitution.

QuadraticProgramStatus

The status of the quadratic program

substitute_variables(constants=None, variables=None)[ソース]

Substitutes variables with constants or other variables.

パラメータ
• constants (Optional[Dict[Union[int, str], float]]) – replace variable by constant e.g., {『x』: 2} means 『x』 is substituted with 2

• variables (Optional[Dict[Union[str, int], Tuple[Union[str, int], float]]]) – 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)} means 『x』 is substituted with 『y』 * 2

QuadraticProgram

An optimization problem by substituting variables with constants or other variables. If the substitution is valid, QuadraticProgram.status is still QuadraticProgram.Status.VALIAD. Otherwise, it gets QuadraticProgram.Status.INFEASIBLE.

QiskitOptimizationError – if the substitution is invalid as follows. - Same variable is substituted multiple times. - Coefficient of variable substitution is zero.

to_docplex()[ソース]

Returns a docplex model corresponding to this quadratic program.

Model

The docplex model corresponding to this quadratic program.

QiskitOptimizationError – if non-supported elements (should never happen).

to_ising()[ソース]

Return the Ising Hamiltonian of this problem.

The qubit operator for the problem offset: The constant value in the Ising Hamiltonian.

qubit_op

property variables

Returns the list of variables of the quadratic program.

List[Variable]

List of variables.

property variables_index

Returns the dictionary that maps the name of a variable to its index.

Dict[str, int]

The variable index dictionary.

write_to_lp_file(filename)[ソース]

Writes the quadratic program to an LP file.

パラメータ

filename (str) – The filename of the file the model is written to. If filename is a directory, file name 『my_problem.lp』 is appended. If filename does not end with 『.lp』, suffix 『.lp』 is appended.

• OSError – If this cannot open a file.

• DOcplexException – If filename is an empty string

None