dimod.ConstrainedQuadraticModel.num_quadratic_variables#

ConstrainedQuadraticModel.num_quadratic_variables(vartype: Vartype | str | frozenset | None = None, *, include_objective: bool | None = None) int[source]#

Number of variables with at least one quadratic interaction in the constrained quadratic model.

Includes interactions in both the objective and any constraints.

Parameters:
  • vartype – Count only variables of the specified Vartype.

  • include_objective – Count also variables in the objective. Currently defaults to false.

Returns:

The number of variables.

Examples

This example counts the two variables participating in interaction 3*i*k in constraint Constraint1 but not variable j’s interaction 2*i*j in the objective.

>>> cqm = dimod.ConstrainedQuadraticModel()
>>> i, j, k = dimod.Integers(["i", "j", "k"])
>>> cqm.set_objective(i - 2*i*j)
>>> cqm.add_constraint_from_comparison(3*i*k - 2*j <= 4, label="Constraint1")
'Constraint1'
>>> cqm.num_quadratic_variables()
2

Deprecated since version 0.10.14: In dimod 0.12.0 include_objective will default to true.