qilisdk.core.model

Classes

SlackCounter

A singleton class to generate a slack counter id that increments continuously within the user's active session.

ObjectiveSense

An Enumeration of the Objective sense options.

Constraint

Represent a symbolic constraint inside a Model.

Objective

Represent the scalar objective function optimized by a Model.

Model

Aggregate an objective and constraints into an optimization problem.

QUBO

Specialized Model constrained to Quadratic Unconstrained Binary Optimization form.

Module Contents

class SlackCounter[font]

A singleton class to generate a slack counter id that increments continuously within the user’s active session.

next() int[font]

Return the next counter value and increment the counter.

reset_counter() None[font]
class ObjectiveSense[font]

Bases: qilisdk.core.types.QiliEnum

An Enumeration of the Objective sense options.

Initialize self. See help(type(self)) for accurate signature.

MINIMIZE = 'minimize'[font]
MAXIMIZE = 'maximize'[font]
class Constraint(label: str, term: qilisdk.core.comparison.Comparison)[font]

Represent a symbolic constraint inside a Model.

Exemple

from qilisdk.core.model import Constraint
from qilisdk.core.variables import BinaryVariable, LEQ

x = BinaryVariable("x")
constraint = Constraint("limit", LEQ(x, 1))

Build a constraint defined by a comparison term such as x + y <= 2.

Paràmetres:
  • label (str) – The constraint’s label.

  • term (Comparison) – The comparison term that defines the constraint.

Llença:

ValueError – if the term provided is not a ConstraintTerm.

property label: str[font]

Returns: str: The label of the constraint object.

property term: qilisdk.core.comparison.Comparison[font]

Returns: Comparison: The comparison term of the constraint object.

variables() list[qilisdk.core.variables.BaseVariable][font]

Returns the list of variables in the constraint term.

Tipus de retorn:

list[BaseVariable]

Retorna:

the list of variables in the constraint term.

Tipus de retorn:

list[BaseVariable]

property lhs: qilisdk.core.expression.Expression[font]

Returns: Expression: The left hand side of the constraint term.

property rhs: qilisdk.core.expression.Expression[font]

Returns: Expression: The right hand side of the constraint term.

property degree: int[font]

Returns: int: The degree of the constraint term.

class Objective(label: str, term: qilisdk.core.variables.BaseVariable | qilisdk.core.expression.Expression, sense: ObjectiveSense = ObjectiveSense.MINIMIZE)[font]

Represent the scalar objective function optimized by a Model.

Exemple

from qilisdk.core.model import Objective, ObjectiveSense
from qilisdk.core.variables import BinaryVariable

x = BinaryVariable("x")
obj = Objective("profit", 3 * x, sense=ObjectiveSense.MAXIMIZE)

Build a new objective function.

Paràmetres:
  • label (str) – Objective label.

  • term (BaseVariable | Expression) – Expression to minimize or maximize.

  • sense (ObjectiveSense, optional) – Optimization sense. Defaults to ObjectiveSense.MINIMIZE.

Llença:
  • ValueError – if the term provided is not a Expression Object.

  • ValueError – if the optimization sense provided is not one that is defined by the ObjectiveSense Enum.

property label: str[font]

Returns: str: the label of the objective.

property term: qilisdk.core.expression.Expression[font]

Returns: Expression: the objective term.

property sense: ObjectiveSense[font]

Returns: ObjectiveSense: the objective optimization sense.

variables() list[qilisdk.core.variables.BaseVariable][font]

Gathers a list of all the variables in the objective term.

Retorna:

the list of variables in the objective term.

Tipus de retorn:

list[BaseVariable]

class Model(label: str)[font]

Aggregate an objective and constraints into an optimization problem.

Exemple

from qilisdk.core import BinaryVariable, LEQ, Model

num_items = 4
values = [1, 3, 5, 2]
weights = [3, 2, 4, 5]
max_weight = 6
bin_vars = [BinaryVariable(f"b{i}") for i in range(num_items)]
model = Model("Knapsack")
objective = sum(values[i] * bin_vars[i] for i in range(num_items))
model.set_objective(objective)
constraint = LEQ(sum(weights[i] * bin_vars[i] for i in range(num_items)), max_weight)
model.add_constraint("maximum weight", constraint)

print(model)
Paràmetres:

label (str) – Model label.

property lagrange_multipliers: dict[str, float][font]
set_lagrange_multiplier(constraint_label: str, lagrange_multiplier: float) None[font]

Sets the lagrange multiplier value for a given constraint.

Paràmetres:
  • constraint_label (str) – the constraint to which the lagrange multiplier value corresponds.

  • lagrange_multiplier (float) – the lagrange multiplier value.

Llença:

ValueError – if the constraint provided is not in the model.

property label: str[font]

Returns: str: The model label.

property constraints: list[Constraint][font]

Returns: list[Constraint]: a list of all the constraints in the model.

property encoding_constraints: list[Constraint][font]

Returns: list[Constraint]: a list of all variable encoding constraints in the model.

property objective: Objective[font]

Returns: Objective: The objective of the model.

variables() list[qilisdk.core.variables.BaseVariable][font]
Retorna:

a list of variables that are used in the model whether that is in the constraints or the objective.

Tipus de retorn:

list[BaseVariable]

add_constraint(label: str, term: qilisdk.core.comparison.Comparison, lagrange_multiplier: float = 100) None[font]

Add a constraint to the model.

Paràmetres:
  • label (str) – constraint label.

  • term (Comparison) – The constraint’s comparison term.

Llença:

ValueError – if the constraint label is already used in the model.

set_objective(term: qilisdk.core.expression.Expression, label: str = 'obj', sense: ObjectiveSense = ObjectiveSense.MINIMIZE) None[font]

Sets the model’s objective.

Paràmetres:
  • term (Expression) – the objective term.

  • label (str, optional) – the objective’s label. Defaults to «obj».

  • sense (ObjectiveSense, optional) – The optimization sense of the model’s objective. Defaults to ObjectiveSense.MINIMIZE.

evaluate(sample: Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.RealNumber | list[int]]) dict[str, qilisdk.core.types.Number][font]

Evaluates the objective and the constraints of the model given a set of values for the variables.

Paràmetres:

sample (Mapping[BaseVariable, Number  |  list[int]]) – The dictionary maps the variable to the value to be used during the evaluation. In case the variable is continuous (Not Binary or Spin) then the value could either be a number or a list of binary bits that correspond to the encoding of the variable. Note: All the model’s variables must be provided for the model to be evaluated.

Retorna:

a dictionary that maps the name of the objective/constraint to it’s evaluated value.

Note: For constraints, the value is equal to lagrange multiplier of that constraint if the constraint is not satisfied or 0 otherwise.

Tipus de retorn:

dict[str, float]

to_qubo(lagrange_multiplier_dict: dict[str, float] | None = None, penalization: Literal['unbalanced', 'slack'] = 'slack', parameters: list[float] | None = None, linearize: bool = True, linearization_lagrange_multiplier: float = 100) QUBO[font]

Export the model to a qubo model.

When linearize is True, any pseudo-Boolean monomial of degree greater than two, coming either from the objective or from the squared/slack penalty of a constraint, is automatically rewritten to quadratic form by introducing auxiliary binary variables and corresponding Rosenberg penalty constraints. See QUBO.from_model() for details on the reduction scheme.

Paràmetres:
  • lagrange_multiplier_dict (dict[str, float] | None, optional) – A dictionary with lagrange multiplier values to scale the model’s constraints. Defaults to None.

  • penalization (Literal[&quot;unbalanced&quot;, &quot;slack&quot;], optional) – the penalization used to handle inequality constraints. Defaults to «slack».

  • parameters (list[float] | None, optional) – the parameters used for the unbalanced penalization method. Defaults to None.

  • linearize (bool, optional) – Automatically reduce high-degree pseudo-Boolean monomials to quadratic form by introducing auxiliary binary variables. When False, exporting a model whose objective or constraints contain terms of degree three or higher raises a ValueError. Defaults to True.

  • linearization_lagrange_multiplier (float, optional) – The Lagrange multiplier applied to each Rosenberg penalty constraint added during linearization. Defaults to 100.

Retorna:

A QUBO model that is generated from the model object.

Tipus de retorn:

QUBO

classmethod knapsack(values: list[float], weights: list[float], max_weight: float, label: str = 'Knapsack', lagrange_multiplier: float = 100) Model[font]

Factory method to generate a knapsack model.

Binary variable b_i = 1 if item i is selected. The objective maximises total value subject to a single weight inequality:

\[ \begin{align}\begin{aligned}\text{maximise} \quad \sum_i v_i b_i\\\text{subject to} \quad \sum_i w_i b_i \leq W\end{aligned}\end{align} \]
Paràmetres:
  • values (list[float]) – the value of each item.

  • weights (list[float]) – the non-negative weight of each item.

  • max_weight (float) – the maximum weight the knapsack can carry.

  • label (str, optional) – the model label. Defaults to «Knapsack».

  • lagrange_multiplier (float, optional) – penalty scale for the weight constraint when converting to QUBO. Defaults to 100.

Retorna:

a model of the knapsack problem with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if the number of values and weights differ.

  • ValueError – if the number of items is zero.

classmethod random_knapsack(num_items: int, value_range: tuple[float, float] = (1, 10), weight_range: tuple[float, float] = (1, 10), capacity_ratio: float = 0.5, label: str = 'Random Knapsack', seed: int = 1, lagrange_multiplier: float = 100) Model[font]

Factory method to generate a random knapsack model.

Values and weights are drawn uniformly at random from the given ranges, and the capacity of the knapsack is set to capacity_ratio times the total weight of all items, so that the instance is neither trivially feasible nor infeasible.

Paràmetres:
  • num_items (int) – the number of items to generate.

  • value_range (tuple[float, float], optional) – the range from which item values are drawn uniformly at random. Defaults to (1, 10).

  • weight_range (tuple[float, float], optional) – the range from which item weights are drawn uniformly at random. Defaults to (1, 10).

  • capacity_ratio (float, optional) – the fraction of the total weight that the knapsack can carry. Defaults to 0.5.

  • label (str, optional) – the model label. Defaults to «Random Knapsack».

  • seed (int, optional) – the seed for the random number generator. Defaults to 1.

  • lagrange_multiplier (float, optional) – penalty scale for the weight constraint when converting to QUBO. Defaults to 100.

Retorna:

a model of a random knapsack problem with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if the number of items is not greater than zero.

  • ValueError – if a range is not a well-ordered (low, high) pair.

  • ValueError – if capacity_ratio is negative.

classmethod ising(edges: list[tuple[int, int]], couplings: list[float] | None = None, fields: Mapping[int, float] | list[float] | None = None, label: str = 'Ising') Model[font]

Factory method to generate an Ising model from a weighted graph.

\[\text{minimise} \quad \sum_{(u,v) \in E} J_{uv}\, x_u x_v + \sum_i h_i x_i\]
Paràmetres:
  • edges (list[tuple[int, int]]) – the edges of the graph as (u, v) pairs.

  • couplings (list[float] | None, optional) – the coupling J of each edge, parallel to edges. Defaults to 1 for all edges.

  • fields (Mapping[int, float] | list[float] | None, optional) – the local field h of each node, either as a mapping from node to field (which may introduce nodes that are not part of any edge) or as a list parallel to the sorted nodes of the graph. Defaults to no local fields.

  • label (str, optional) – the model label. Defaults to «Ising».

Retorna:

a model of the Ising problem for the given graph.

Tipus de retorn:

Model

Llença:
  • ValueError – if couplings are provided and their number is different from the number of edges.

  • ValueError – if fields are provided as a list whose length differs from the number of nodes.

  • ValueError – if the graph contains a self-loop or a duplicate (undirected) edge.

  • ValueError – if the resulting model has no variables.

classmethod random_ising(num_variables: int, edge_probability: float = 1.0, coefficient_range: tuple[float, float] = (-1, 1), label: str = 'Random Ising', seed: int = 1) Model[font]

Factory method to generate a random Ising model.

Every node carries a local field, and the nodes are coupled according to a random connected graph, with all coefficients drawn uniformly at random. By default the graph is fully connected, so every pair of nodes is coupled.

Paràmetres:
  • num_variables (int) – the number of variables in the Ising model.

  • edge_probability (float, optional) – the probability of coupling each pair of nodes that is not part of the random spanning tree that keeps the graph connected. Defaults to 1.0, i.e. a fully connected model.

  • coefficient_range (tuple[float, float], optional) – the range from which the coefficients of the Ising model are drawn uniformly at random. Defaults to (-1, 1).

  • label (str, optional) – the model label. Defaults to «Random Ising».

  • seed (int, optional) – the seed for the random number generator. Defaults to 1.

Retorna:

a model of a random Ising problem with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if the number of variables is not greater than zero.

  • ValueError – if edge_probability is outside [0, 1].

  • ValueError – if coefficient_range is not a well-ordered (low, high) pair.

classmethod factoring(number: int, label: str = 'Factoring', lagrange_multiplier: float = 100) Model[font]

Factory method to generate a factoring model.

Binary variables x_i and y_j encode two factors whose product must equal number. The problem is a constraint satisfaction instance with no objective:

\[\text{subject to} \quad \sum_{i,j} 2^{i+j}\, x_i y_j = N\]
Paràmetres:
  • number (int) – the number to factor.

  • label (str, optional) – the model label. Defaults to «Factoring».

  • lagrange_multiplier (float, optional) – penalty scale for the factoring constraint when converting to QUBO. Defaults to 100.

Retorna:

a model of the factoring problem for the given number.

Tipus de retorn:

Model

classmethod max_cut(edges: list[tuple[int, int]], weights: list[float] | None = None, label: str = 'Max-Cut') Model[font]

Factory method to generate a max-cut model.

Paràmetres:
  • edges (list[tuple[int, int]]) – the edges of the graph as (u, v) pairs.

  • weights (list[float] | None, optional) – a weight for each edge. Defaults to 1 for all edges.

  • label (str, optional) – the model label. Defaults to «Max-Cut».

Retorna:

a model of the max-cut problem for the given graph.

Tipus de retorn:

Model

Llença:
  • ValueError – if weights are provided and their number is different from the number of edges.

  • ValueError – if the graph contains a self-loop or a duplicate (undirected) edge.

classmethod random_max_cut(num_nodes: int, edge_probability: float = 0.5, weight_range: tuple[float, float] | None = None, label: str = 'Random Max-Cut', seed: int = 1) Model[font]

Factory method to generate a max-cut model on a random connected graph.

Paràmetres:
  • num_nodes (int) – the number of nodes in the graph. Must be greater than one.

  • edge_probability (float, optional) – the probability of adding each edge that is not part of the random spanning tree that keeps the graph connected. Defaults to 0.5.

  • weight_range (tuple[float, float] | None, optional) – the range from which the edge weights are drawn uniformly at random. Defaults to unweighted (all weights equal to 1).

  • label (str, optional) – the model label. Defaults to «Random Max-Cut».

  • seed (int, optional) – the seed for the random number generator. Defaults to 1.

Retorna:

a model of the max-cut problem for a random graph with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if num_nodes is smaller than two.

  • ValueError – if edge_probability is outside [0, 1].

  • ValueError – if weight_range is not a well-ordered (low, high) pair.

classmethod graph_coloring(edges: list[tuple[int, int]], num_colors: int, label: str = 'Graph Coloring', lagrange_multiplier: float = 100) Model[font]

Factory method to generate a graph coloring model.

Binary variable x_{v,k} = 1 if vertex v has color k. Each vertex must have exactly one color (an equality constraint), while the objective minimises the number of edges whose endpoints share a color:

\[ \begin{align}\begin{aligned}\text{minimise} \quad \sum_{(u,v)\in E} \sum_k x_{u,k}\, x_{v,k}\\\text{subject to} \quad \sum_k x_{v,k} = 1 \quad \forall v\end{aligned}\end{align} \]

A valid num_colors-coloring exists if and only if the optimal objective value is zero.

Paràmetres:
  • edges (list[tuple[int, int]]) – the edges of the graph as (u, v) pairs.

  • num_colors (int) – the number of colors available.

  • label (str, optional) – the model label. Defaults to «Graph Coloring».

  • lagrange_multiplier (float, optional) – penalty scale for the one-color constraints when converting to QUBO. Defaults to 100.

Retorna:

a model of the graph coloring problem for the given graph.

Tipus de retorn:

Model

Llença:

ValueError – if the graph contains a self-loop or a duplicate (undirected) edge.

classmethod random_graph_coloring(num_nodes: int, num_colors: int, edge_probability: float = 0.5, label: str = 'Random Graph Coloring', seed: int = 1, lagrange_multiplier: float = 100) Model[font]

Factory method to generate a graph coloring model on a random connected graph.

Paràmetres:
  • num_nodes (int) – the number of nodes in the graph. Must be greater than one.

  • num_colors (int) – the number of colors available.

  • edge_probability (float, optional) – the probability of adding each edge that is not part of the random spanning tree that keeps the graph connected. Defaults to 0.5.

  • label (str, optional) – the model label. Defaults to «Random Graph Coloring».

  • seed (int, optional) – the seed for the random number generator. Defaults to 1.

  • lagrange_multiplier (float, optional) – penalty scale for the one-color constraints when converting to QUBO. Defaults to 100.

Retorna:

a model of the graph coloring problem for a random graph with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if num_nodes is smaller than two.

  • ValueError – if num_colors is not greater than zero.

  • ValueError – if edge_probability is outside [0, 1].

classmethod travelling_salesman(edges: list[tuple[int, int]], distances: list[float], label: str = 'Travelling Salesman', lagrange_multiplier: float = 100) Model[font]

Factory method to generate a travelling salesman model.

Binary variable x_{i,t} = 1 if city i is at tour position t. The objective minimises total travel distance; two sets of equality constraints enforce a valid tour:

\[ \begin{align}\begin{aligned}\text{minimise} \quad \sum_{(u,v)\in E} W_{uv} \sum_t \bigl(x_{u,t}\,x_{v,t+1} + x_{v,t}\,x_{u,t+1}\bigr)\\\text{subject to} \quad \sum_t x_{i,t} = 1 \quad \forall i\\\sum_i x_{i,t} = 1 \quad \forall t\end{aligned}\end{align} \]

where index arithmetic on positions is modulo n.

Paràmetres:
  • edges (list[tuple[int, int]]) – list of undirected edges as (city_i, city_j) pairs.

  • distances (list[float]) – travel cost for each edge, parallel to edges.

  • label (str, optional) – the model label. Defaults to «Travelling Salesman».

  • lagrange_multiplier (float, optional) – penalty scale for the tour validity constraints when converting to QUBO. Defaults to 100.

Retorna:

a model of the travelling salesman problem for the given graph.

Tipus de retorn:

Model

Llença:
  • ValueError – if edges and distances have different lengths.

  • ValueError – if the graph has no edges.

  • ValueError – if the graph contains a self-loop or a duplicate (undirected) edge.

classmethod random_travelling_salesman(num_cities: int, edge_probability: float = 1.0, distance_range: tuple[float, float] = (1, 10), label: str = 'Random Travelling Salesman', seed: int = 1, lagrange_multiplier: float = 100) Model[font]

Factory method to generate a travelling salesman model on a random graph.

The distance of each edge is drawn uniformly at random from distance_range. By default the graph is complete, so every pair of cities is connected.

Note that a sparser graph is only guaranteed to be connected, not Hamiltonian, and that the objective charges nothing for travelling between two cities that are not connected by an edge, so the optimal tour of a sparse instance may use city pairs that have no edge.

Paràmetres:
  • num_cities (int) – the number of cities. Must be greater than one.

  • edge_probability (float, optional) – the probability of connecting each pair of cities that is not part of the random spanning tree that keeps the graph connected. Defaults to 1.0, i.e. a complete graph.

  • distance_range (tuple[float, float], optional) – the range from which the distances are drawn uniformly at random. Defaults to (1, 10).

  • label (str, optional) – the model label. Defaults to «Random Travelling Salesman».

  • seed (int, optional) – the seed for the random number generator. Defaults to 1.

  • lagrange_multiplier (float, optional) – penalty scale for the tour validity constraints when converting to QUBO. Defaults to 100.

Retorna:

a model of the travelling salesman problem for a random graph with the given parameters.

Tipus de retorn:

Model

Llença:
  • ValueError – if num_cities is smaller than two.

  • ValueError – if edge_probability is outside [0, 1].

  • ValueError – if distance_range is not a well-ordered (low, high) pair.

class QUBO(label: str)[font]

Bases: Model

Specialized Model constrained to Quadratic Unconstrained Binary Optimization form.

Exemple

from qilisdk.core.model import QUBO
from qilisdk.core.variables import BinaryVariable

x0, x1 = BinaryVariable("x0"), BinaryVariable("x1")
qubo = QUBO("Example")
qubo.set_objective((x0 + x1) ** 2)
Paràmetres:

label (str) – QUBO model label.

continuous_vars: dict[str, qilisdk.core.variables.Variable][font]
property qubo_objective: Objective | None[font]

Returns: Objective | None: The QUBO objective (factoring in the constraints and objective of the model). If the objective and constraints are not defined in the model, this property returns None.

add_constraint(label: str, term: qilisdk.core.comparison.Comparison, lagrange_multiplier: float = 100, penalization: Literal['unbalanced', 'slack'] = 'slack', parameters: list[float] | None = None, transform_to_qubo: bool = True, linearize: bool = True) None[font]

Adds a constraint to the QUBO model.

Paràmetres:
  • label (str) – the constraint label.

  • term (Comparison) – the constraint’s comparison term.

  • lagrange_multiplier (float, optional) – the lagrange multiplier used to scale this constraint. Defaults to 100.

  • penalization (Literal[&quot;unbalanced&quot;, &quot;slack&quot;], optional) – the penalization used to handel inequality constraints. Defaults to «slack».

  • parameters (list[float] | None, optional) – the parameters used for the unbalanced penalization method. Defaults to None.

  • transform_to_qubo (bool, optional) – Automatically transform a given constraint to QUBO format. Defaults to True.

  • linearize (bool, optional) – linearize the constraints if they are above degree 2.

Llença:
  • ValueError – if constraint label already exists in the model.

  • ValueError – if a penalization method is provided that is not (&quot;unbalanced&quot;, &quot;slack&quot;)

  • ValueError – if unbalanced penalization method is used and not enough parameters are provided.

  • ValueError – if the degree of the provided term is larger than 2.

  • ValueError – if the constraint term contains variables that are not from the binary, positive integer, integer or real domains.

set_objective(term: qilisdk.core.expression.Expression, label: str = 'obj', sense: ObjectiveSense = ObjectiveSense.MINIMIZE) None[font]

Set the QUBO objective.

If a _Linearizer has been attached to this QUBO instance (via from_model() with linearize=True), the binary-encoded objective is additionally rewritten so that every monomial has degree at most two. Auxiliary variables introduced by the rewrite are registered on the linearizer.

Paràmetres:
  • term (Expression) – The objective’s term.

  • label (str, optional) – the objective’s label. Defaults to «obj».

  • sense (ObjectiveSense, optional) – The optimization sense of the model’s objective. Defaults to ObjectiveSense.MINIMIZE.

Llença:

ValueError – if the degree of the provided term is larger than 2.

set_lagrange_multiplier(constraint_label: str, lagrange_multiplier: float) None[font]

Sets the lagrange multiplier value for a given constraint.

Paràmetres:
  • constraint_label (str) – the constraint to which the lagrange multiplier value corresponds.

  • lagrange_multiplier (float) – the lagrange multiplier value.

Llença:

ValueError – if the constraint provided is not in the model.

evaluate(sample: Mapping[qilisdk.core.variables.BaseVariable, qilisdk.core.types.RealNumber | list[int]]) dict[str, qilisdk.core.types.Number][font]

Evaluates the objective and the constraints of the model given a set of values for the variables.

Paràmetres:

sample (Mapping[BaseVariable, RealNumber  |  list[int]]) – The dictionary maps the variable to the value to be used during the evaluation. In case the variable is continuous (Not Binary or Spin) then the value could either be a number or a list of binary bits that correspond to the encoding of the variable. Note: All the model’s variables must be provided for the model to be evaluated.

Retorna:

a dictionary that maps the name of the objective/constraint to it’s evaluated value.

Note: For constraints, the value is equal to the value of the evaluated constraint term multiplied by the lagrange multiplier of that constraint.

Tipus de retorn:

dict[str, float]

classmethod from_model(model: Model, lagrange_multiplier_dict: dict[str, float] | None = None, penalization: Literal['unbalanced', 'slack'] = 'slack', parameters: list[float] | None = None, linearize: bool = True, linearization_lagrange_multiplier: float = 100) QUBO[font]

A class method that constructs a QUBO model from a regular model if possible.

When linearize is True (default), any pseudo-Boolean monomial of degree greater than two that appears in the objective or in a transformed constraint penalty is rewritten using auxiliary binary variables via the Rosenberg penalty

\[P(a, b, w) = a \cdot b - 2 \cdot a \cdot w - 2 \cdot b \cdot w + 3 \cdot w,\]

so w is forced to equal the product a * b at the optimum. One such penalty is added as an equality QUBO constraint for every unique pair substitution. Setting linearize=False restores the previous behaviour, where a ValueError is raised if the model contains terms of degree three or higher.

Paràmetres:
  • model (Model) – the model to be used to construct the QUBO model.

  • lagrange_multiplier_dict (dict[str, float] | None, optional) – A dictionary with lagrange multiplier values to scale the model’s constraints. Defaults to None.

  • penalization (Literal[&quot;unbalanced&quot;, &quot;slack&quot;], optional) – the penalization used to handel inequality constraints. Defaults to «slack».

  • parameters (list[float] | None, optional) – the parameters used for the unbalanced penalization method. Defaults to None.

  • linearize (bool, optional) – Automatically reduce high-degree pseudo-Boolean monomials to quadratic form by introducing auxiliary binary variables. Defaults to True.

  • linearization_lagrange_multiplier (float, optional) – The Lagrange multiplier applied to each Rosenberg penalty constraint added as part of the linearization. Must be large enough to dominate any incentive to violate the auxiliary equalities w = a * b. Defaults to 100.

Retorna:

a QUBO model equivalent to the input model, with any high-degree terms rewritten via auxiliary binary variables when linearize is enabled.

Tipus de retorn:

QUBO

to_hamiltonian() qilisdk.analog.hamiltonian.Hamiltonian[font]

Construct an ising hamiltonian from the current QUBO model.

Llença:
  • ValueError – if the QUBO model is empty (doesn’t have an objective nor constraints.)

  • ValueError – if the QUBO model uses operations that are not addition or multiplications.

Retorna:

An ising hamiltonian that represents the QUBO model.

Tipus de retorn:

Hamiltonian

to_qubo(lagrange_multiplier_dict: dict[str, float] | None = None, penalization: Literal['unbalanced', 'slack'] = 'slack', parameters: list[float] | None = None, linearize: bool = True, linearization_lagrange_multiplier: float = 100) QUBO[font]

Return a copy of this QUBO model.

QUBO models are already in quadratic form, so the linearization arguments are accepted for signature compatibility with Model.to_qubo() but have no effect. A warning is emitted noting that no conversion was performed.