dimod.SampleSet.change_vartype

SampleSet.change_vartype(vartype, energy_offset=0.0, inplace=True)[source]

Return the SampleSet with the given vartype.

Parameters:
  • vartype (Vartype/str/set) –

    Variable type to use for the new SampleSet. Accepted input values:

    • Vartype.SPIN, 'SPIN', {-1, 1}
    • Vartype.BINARY, 'BINARY', {0, 1}
  • energy_offset (number, optional, defaul=0.0) – Constant value applied to the ‘energy’ field of SampleSet.record.
  • inplace (bool, optional, default=True) – If True, the instantiated SampleSet is updated; otherwise, a new SampleSet is returned.
Returns:

SampleSet with changed vartype. If inplace is True, returns itself.

Return type:

SampleSet

Notes

This function is non-blocking unless inplace==True, in which case the sample set is resolved.

Examples

This example creates a binary copy of a spin-valued SampleSet.

>>> sampleset = dimod.ExactSolver().sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1})
>>> sampleset_binary = sampleset.change_vartype(dimod.BINARY, energy_offset=1.0, inplace=False)
>>> sampleset_binary.vartype is dimod.BINARY
True
>>> sampleset_binary.first.sample
{'a': 0, 'b': 0}