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

Parameters

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

__init__(name='')[source]
Parameters

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

Methods

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

binary_var(name=None)[source]

Parameters

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

Return type

Variable

Returns

Raises

QiskitOptimizationError – if the variable name is already occupied.

clear()[source]

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

Return type

None

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

Parameters
• 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.

Return type

Variable

Returns

Raises

QiskitOptimizationError – if the variable name is already occupied.

export_as_lp_string()[source]

Returns the quadratic program as a string of LP format.

Return type

str

Returns

A string representing the quadratic program.

from_docplex(model)[source]

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

Parameters

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

Raises

QiskitOptimizationError – if the model contains unsupported elements.

Return type

None

from_ising(qubit_op, offset=0.0, linear=False)[source]

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

Parameters
• 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.

Raises
Return type

None

get_feasibility_info(x)[source]

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.

Returns

Whether the solution provided is feasible or not. List[Variable]: List of variables which are violated. List[Constraint]: List of constraints which are violated.

Return type

feasible

Raises

QiskitOptimizationError – If the input x is not same len as total vars

get_linear_constraint(i)[source]

Returns a linear constraint for a given name or index.

Parameters

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

Return type

LinearConstraint

Returns

The corresponding constraint.

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

• KeyError – if the name does not exist

get_num_binary_vars()[source]

Returns the total number of binary variables.

Return type

int

Returns

The total number of binary variables.

get_num_continuous_vars()[source]

Returns the total number of continuous variables.

Return type

int

Returns

The total number of continuous variables.

get_num_integer_vars()[source]

Returns the total number of integer variables.

Return type

int

Returns

The total number of integer variables.

get_num_linear_constraints()[source]

Returns the number of linear constraints.

Return type

int

Returns

The number of linear constraints.

Returns the number of quadratic constraints.

Return type

int

Returns

get_num_vars(vartype=None)[source]

Returns the total number of variables or the number of variables of the specified type.

Parameters

vartype (Optional[VarType]) – The type to be filtered on. All variables are counted if None.

Return type

int

Returns

The total number of variables.

Returns a quadratic constraint for a given name or index.

Parameters

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

Return type

Returns

The corresponding constraint.

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

• KeyError – if the name does not exist

get_variable(i)[source]

Returns a variable for a given name or index.

Parameters

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

Return type

Variable

Returns

The corresponding variable.

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

Parameters
• 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.

Return type

Variable

Returns

Raises

QiskitOptimizationError – if the variable name is already occupied.

is_feasible(x)[source]

Returns whether a solution is feasible or not.

Parameters

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

Return type

bool

Returns

True if the solution provided is feasible otherwise False.

linear_constraint(linear=None, sense='<=', rhs=0.0, name=None)[source]
Adds a linear equality constraint to the quadratic program of the form:

linear * x sense rhs.

Parameters
• 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.

Return type

LinearConstraint

Returns

Raises

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.

Return type

List[LinearConstraint]

Returns

List of linear constraints.

property linear_constraints_index

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

Return type

Dict[str, int]

Returns

The linear constraint index dictionary.

Sets a quadratic objective to be maximized.

Parameters
• 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.

Return type

None

Returns

Sets a quadratic objective to be minimized.

Parameters
• 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.

Return type

None

Returns

property name

Returns the name of the quadratic program.

Return type

str

Returns

The name of the quadratic program.

property objective

Return type

Returns

pprint_as_string()[source]

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)[source]

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

Parameters

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.

Return type

None

x * Q * x <= rhs.

Parameters
• 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.

Return type

Returns

Raises

QiskitOptimizationError – if the constraint name already exists.

Return type

Returns

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

Return type

Dict[str, int]

Returns

Parameters

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

Raises

Note

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

Return type

None

remove_linear_constraint(i)[source]

Remove a linear constraint

Parameters

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

Raises
• KeyError – if name does not exist

• IndexError – if index is out of range

Return type

None

Parameters

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

Raises
• KeyError – if name does not exist

• IndexError – if index is out of range

Return type

None

property status

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

Return type

Returns

The status of the quadratic program

substitute_variables(constants=None, variables=None)[source]

Substitutes variables with constants or other variables.

Parameters
• 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

Return type

Returns

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.

Raises

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

to_docplex()[source]

Returns a docplex model corresponding to this quadratic program.

Return type

Model

Returns

The docplex model corresponding to this quadratic program.

Raises

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

to_ising()[source]

Return the Ising Hamiltonian of this problem.

Returns

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

Return type

qubit_op

Raises
property variables

Returns the list of variables of the quadratic program.

Return type

List[Variable]

Returns

List of variables.

property variables_index

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

Return type

Dict[str, int]

Returns

The variable index dictionary.

write_to_lp_file(filename)[source]

Writes the quadratic program to an LP file.

Parameters

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.

Raises
• OSError – If this cannot open a file.

• DOcplexException – If filename is an empty string

Return type

None