dimod.QuadraticModel.relabel_variables

QuadraticModel.relabel_variables(mapping: Mapping[Hashable, Hashable], inplace: bool = True) dimod.quadratic.quadratic_model.QuadraticModel[source]

Relabel the variables according to the given mapping.

Parameters
  • mapping – Mapping of current variable labels to new ones. If an incomplete mapping is provided, unmapped variables retain their current labels.

  • inplace – If set to False, returns a new binary quadratic model mapped to the new labels.

Returns

The original or new quadratic model with updated variable labels.

Examples

>>> from dimod import QuadraticModel, BinaryQuadraticModel, generators
>>> bqm = generators.ran_r(1, 5)
>>> qm = QuadraticModel.from_bqm(bqm)
>>> qm_new = qm.relabel_variables({0: 'a', 1: 'b', 2: 'c'}, inplace=False)
>>> qm_new.variables
Variables(['a', 'b', 'c', 3, 4])