QuadraticProgram#

class QuadraticProgram(name='')[source]#

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

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.

রিটার্ন টাইপ:

Variable

binary_var_dict(keys, name=None, key_format='{}')[source]#

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.

রেইজেস:
রিটার্ন টাইপ:

Dict[str, Variable]

binary_var_list(keys, name=None, key_format='{}')[source]#

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.

রেইজেস:
রিটার্ন টাইপ:

List[Variable]

clear()[source]#

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

Adds a continuous variable to the quadratic program.

প্যারামিটার:
  • lowerbound (float | int) -- The lowerbound of the variable.

  • upperbound (float | int) -- The upperbound of the variable.

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

রিটার্ন টাইপ:

Variable

continuous_var_dict(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[source]#

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.

রেইজেস:
রিটার্ন টাইপ:

Dict[str, Variable]

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

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.

রেইজেস:
রিটার্ন টাইপ:

List[Variable]

export_as_lp_string()[source]#

Returns the quadratic program as a string of LP format.

রিটার্নস:

A string representing the quadratic program.

রিটার্ন টাইপ:

str

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

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.

রেইজেস:
get_feasibility_info(x)[source]#

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

Returns a linear constraint for a given name or index.

প্যারামিটার:

i (int | str) -- the index or name of the constraint.

রিটার্নস:

The corresponding constraint.

রেইজেস:
  • IndexError -- if the index is out of the list size

  • KeyError -- if the name does not exist

রিটার্ন টাইপ:

LinearConstraint

get_num_binary_vars()[source]#

Returns the total number of binary variables.

রিটার্নস:

The total number of binary variables.

রিটার্ন টাইপ:

int

get_num_continuous_vars()[source]#

Returns the total number of continuous variables.

রিটার্নস:

The total number of continuous variables.

রিটার্ন টাইপ:

int

get_num_integer_vars()[source]#

Returns the total number of integer variables.

রিটার্নস:

The total number of integer variables.

রিটার্ন টাইপ:

int

get_num_linear_constraints()[source]#

Returns the number of linear constraints.

রিটার্নস:

The number of linear constraints.

রিটার্ন টাইপ:

int

get_num_quadratic_constraints()[source]#

Returns the number of quadratic constraints.

রিটার্নস:

The number of quadratic constraints.

রিটার্ন টাইপ:

int

get_num_vars(vartype=None)[source]#

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.

রিটার্ন টাইপ:

int

get_quadratic_constraint(i)[source]#

Returns a quadratic constraint for a given name or index.

প্যারামিটার:

i (int | str) -- the index or name of the constraint.

রিটার্নস:

The corresponding constraint.

রেইজেস:
  • IndexError -- if the index is out of the list size

  • KeyError -- if the name does not exist

রিটার্ন টাইপ:

QuadraticConstraint

get_variable(i)[source]#

Returns a variable for a given name or index.

প্যারামিটার:

i (int | str) -- the index or name of the variable.

রিটার্নস:

The corresponding variable.

রিটার্ন টাইপ:

Variable

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

Adds an integer variable to the quadratic program.

প্যারামিটার:
  • lowerbound (float | int) -- The lowerbound of the variable.

  • upperbound (float | int) -- The upperbound of the variable.

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

রিটার্ন টাইপ:

Variable

integer_var_dict(keys, lowerbound=0, upperbound=1e+20, name=None, key_format='{}')[source]#

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.

রেইজেস:
রিটার্ন টাইপ:

Dict[str, Variable]

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

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.

রেইজেস:
রিটার্ন টাইপ:

List[Variable]

is_feasible(x)[source]#

Returns whether a solution is feasible or not.

প্যারামিটার:

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

রিটার্নস:

True if the solution provided is feasible otherwise False.

রিটার্ন টাইপ:

bool

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.

প্যারামিটার:
  • 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, 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 (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.

রিটার্ন টাইপ:

LinearConstraint

maximize(constant=0.0, linear=None, quadratic=None)[source]#

Sets a quadratic objective to be maximized.

প্যারামিটার:
রিটার্নস:

The created quadratic objective.

রিটার্ন টাইপ:

None

minimize(constant=0.0, linear=None, quadratic=None)[source]#

Sets a quadratic objective to be minimized.

প্যারামিটার:
রিটার্নস:

The created quadratic objective.

রিটার্ন টাইপ:

None

prettyprint(wrap=80)[source]#

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.

রিটার্ন টাইপ:

str

quadratic_constraint(linear=None, quadratic=None, sense='<=', rhs=0.0, name=None)[source]#
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, 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 (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.

রিটার্ন টাইপ:

QuadraticConstraint

read_from_lp_file(filename)[source]#

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

Remove a linear constraint

প্যারামিটার:

i (str | int) -- an index or a name of a linear constraint

রেইজেস:
remove_quadratic_constraint(i)[source]#

Remove a quadratic constraint

প্যারামিটার:

i (str | int) -- an index or a name of a quadratic constraint

রেইজেস:
substitute_variables(constants=None, variables=None)[source]#

Substitutes variables with constants or other variables.

প্যারামিটার:
  • constants (Dict[int | str, float] | None) -- replace variable by constant e.g., {'x': 2} means x is substituted with 2

  • variables (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)} means x is substituted with y * 2

রিটার্নস:

An optimization problem by substituting variables with constants or other variables. If the substitution is valid, QuadraticProgram.status is still QuadraticProgram.Status.VALID. 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.

রিটার্ন টাইপ:

QuadraticProgram

to_ising()[source]#

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

রেইজেস:
write_to_lp_file(filename)[source]#

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