Portuguese, Brazilian
Idiomas
English
Japanese
German
Korean
Portuguese, Brazilian
French
Shortcuts

qiskit.optimization.QuadraticProgram

class QuadraticProgram(name='')[código fonte]

Quadratically Constrained Quadratic Program representation.

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

Parâmetros

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

__init__(name='')[código fonte]
Parâmetros

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.

clear()

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.

export_as_lp_string()

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.

get_feasibility_info(x)

Returns whether a solution is feasible or not along with the violations.

get_linear_constraint(i)

Returns a linear constraint for a given name or index.

get_num_binary_vars()

Returns the total number of binary variables.

get_num_continuous_vars()

Returns the total number of continuous variables.

get_num_integer_vars()

Returns the total number of integer variables.

get_num_linear_constraints()

Returns the number of linear constraints.

get_num_quadratic_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.

get_quadratic_constraint(i)

Returns a quadratic constraint for a given name or index.

get_variable(i)

Returns a variable for a given name or index.

integer_var([lowerbound, upperbound, name])

Adds an integer variable to the quadratic program.

is_feasible(x)

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.

pprint_as_string()

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_linear_constraint(i)

Remove a linear constraint

remove_quadratic_constraint(i)

Remove a quadratic constraint

substitute_variables([constants, variables])

Substitutes variables with constants or other variables.

to_docplex()

Returns a docplex model corresponding to this quadratic program.

to_ising()

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

apelido de QuadraticProgramStatus

binary_var(name=None)[código fonte]

Adds a binary variable to the quadratic program.

Parâmetros

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

Tipo de retorno

Variable

Retorna

The added variable.

Levanta

QiskitOptimizationError – if the variable name is already occupied.

clear()[código fonte]

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

Tipo de retorno

None

continuous_var(lowerbound=0, upperbound=1e+20, name=None)[código fonte]

Adds a continuous variable to the quadratic program.

Parâmetros
  • 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.

Tipo de retorno

Variable

Retorna

The added variable.

Levanta

QiskitOptimizationError – if the variable name is already occupied.

export_as_lp_string()[código fonte]

Returns the quadratic program as a string of LP format.

Tipo de retorno

str

Retorna

A string representing the quadratic program.

from_docplex(model)[código fonte]

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

Parâmetros

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

Levanta

QiskitOptimizationError – if the model contains unsupported elements.

Tipo de retorno

None

from_ising(qubit_op, offset=0.0, linear=False)[código fonte]

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

Parâmetros
  • 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.

Levanta
Tipo de retorno

None

get_feasibility_info(x)[código fonte]

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.

Retorna

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

Tipo de retorno

feasible

Levanta

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

get_linear_constraint(i)[código fonte]

Returns a linear constraint for a given name or index.

Parâmetros

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

Tipo de retorno

LinearConstraint

Retorna

The corresponding constraint.

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

  • KeyError – if the name does not exist

get_num_binary_vars()[código fonte]

Returns the total number of binary variables.

Tipo de retorno

int

Retorna

The total number of binary variables.

get_num_continuous_vars()[código fonte]

Returns the total number of continuous variables.

Tipo de retorno

int

Retorna

The total number of continuous variables.

get_num_integer_vars()[código fonte]

Returns the total number of integer variables.

Tipo de retorno

int

Retorna

The total number of integer variables.

get_num_linear_constraints()[código fonte]

Returns the number of linear constraints.

Tipo de retorno

int

Retorna

The number of linear constraints.

get_num_quadratic_constraints()[código fonte]

Returns the number of quadratic constraints.

Tipo de retorno

int

Retorna

The number of quadratic constraints.

get_num_vars(vartype=None)[código fonte]

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

Parâmetros

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

Tipo de retorno

int

Retorna

The total number of variables.

get_quadratic_constraint(i)[código fonte]

Returns a quadratic constraint for a given name or index.

Parâmetros

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

Tipo de retorno

QuadraticConstraint

Retorna

The corresponding constraint.

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

  • KeyError – if the name does not exist

get_variable(i)[código fonte]

Returns a variable for a given name or index.

Parâmetros

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

Tipo de retorno

Variable

Retorna

The corresponding variable.

integer_var(lowerbound=0, upperbound=1e+20, name=None)[código fonte]

Adds an integer variable to the quadratic program.

Parâmetros
  • 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.

Tipo de retorno

Variable

Retorna

The added variable.

Levanta

QiskitOptimizationError – if the variable name is already occupied.

is_feasible(x)[código fonte]

Returns whether a solution is feasible or not.

Parâmetros

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

Tipo de retorno

bool

Retorna

True if the solution provided is feasible otherwise False.

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

linear * x sense rhs.

Parâmetros
  • 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.

Tipo de retorno

LinearConstraint

Retorna

The added constraint.

Levanta

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.

Tipo de retorno

List[LinearConstraint]

Retorna

List of linear constraints.

property linear_constraints_index

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

Tipo de retorno

Dict[str, int]

Retorna

The linear constraint index dictionary.

maximize(constant=0.0, linear=None, quadratic=None)[código fonte]

Sets a quadratic objective to be maximized.

Parâmetros
  • 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.

Tipo de retorno

None

Retorna

The created quadratic objective.

minimize(constant=0.0, linear=None, quadratic=None)[código fonte]

Sets a quadratic objective to be minimized.

Parâmetros
  • 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.

Tipo de retorno

None

Retorna

The created quadratic objective.

property name

Returns the name of the quadratic program.

Tipo de retorno

str

Retorna

The name of the quadratic program.

property objective

Returns the quadratic objective.

Tipo de retorno

QuadraticObjective

Retorna

The quadratic objective.

pprint_as_string()[código fonte]

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)[código fonte]

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

Parâmetros

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.

Tipo de retorno

None

quadratic_constraint(linear=None, quadratic=None, sense='<=', rhs=0.0, name=None)[código fonte]
Adds a quadratic equality constraint to the quadratic program of the form:

x * Q * x <= rhs.

Parâmetros
  • 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.

Tipo de retorno

QuadraticConstraint

Retorna

The added constraint.

Levanta

QiskitOptimizationError – if the constraint name already exists.

property quadratic_constraints

Returns the list of quadratic constraints of the quadratic program.

Tipo de retorno

List[QuadraticConstraint]

Retorna

List of quadratic constraints.

property quadratic_constraints_index

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

Tipo de retorno

Dict[str, int]

Retorna

The quadratic constraint index dictionary.

read_from_lp_file(filename)[código fonte]

Loads the quadratic program from a LP file.

Parâmetros

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

Levanta

Nota

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

Tipo de retorno

None

remove_linear_constraint(i)[código fonte]

Remove a linear constraint

Parâmetros

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

Levanta
  • KeyError – if name does not exist

  • IndexError – if index is out of range

Tipo de retorno

None

remove_quadratic_constraint(i)[código fonte]

Remove a quadratic constraint

Parâmetros

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

Levanta
  • KeyError – if name does not exist

  • IndexError – if index is out of range

Tipo de retorno

None

property status

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

Tipo de retorno

QuadraticProgramStatus

Retorna

The status of the quadratic program

substitute_variables(constants=None, variables=None)[código fonte]

Substitutes variables with constants or other variables.

Parâmetros
  • 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

Tipo de retorno

QuadraticProgram

Retorna

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.

Levanta

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

to_docplex()[código fonte]

Returns a docplex model corresponding to this quadratic program.

Tipo de retorno

Model

Retorna

The docplex model corresponding to this quadratic program.

Levanta

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

to_ising()[código fonte]

Return the Ising Hamiltonian of this problem.

Retorna

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

Tipo de retorno

qubit_op

Levanta
property variables

Returns the list of variables of the quadratic program.

Tipo de retorno

List[Variable]

Retorna

List of variables.

property variables_index

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

Tipo de retorno

Dict[str, int]

Retorna

The variable index dictionary.

write_to_lp_file(filename)[código fonte]

Writes the quadratic program to an LP file.

Parâmetros

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.

Levanta
  • OSError – If this cannot open a file.

  • DOcplexException – If filename is an empty string

Tipo de retorno

None