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

alias of QuadraticProgramStatus

binary_var(name=None)[소스]

매개변수

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)[소스]

매개변수
• 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)[소스]

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

반환값

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)[소스]

매개변수
• 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

반환값

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

반환값

property name

Returns the name of the quadratic program.

반환 형식

str

반환값

The name of the quadratic program.

property 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)[소스]

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

반환 형식

List[QuadraticConstraint]

반환값

property quadratic_constraints_index

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

반환 형식

Dict[str, int]

반환값

read_from_lp_file(filename)[소스]

매개변수

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)[소스]

매개변수

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