dimod.SampleSet.change_vartype¶
-
SampleSet.change_vartype(**kwargs)[source]¶ Return the
SampleSetwith 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
SampleSetis updated; otherwise, a newSampleSetis returned.
Returns: SampleSet with changed vartype. If inplace is True, returns itself.
Return type: Examples
This example creates a binary copy of a spin-valued
SampleSet.>>> import dimod ... >>> 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 >>> for datum in sampleset_binary.data(fields=['sample', 'energy', 'num_occurrences']): # doctest: +SKIP ... print(datum) Sample(sample={'a': 0, 'b': 0}, energy=-0.5, num_occurrences=1) Sample(sample={'a': 1, 'b': 0}, energy=0.5, num_occurrences=1) Sample(sample={'a': 1, 'b': 1}, energy=0.5, num_occurrences=1) Sample(sample={'a': 0, 'b': 1}, energy=3.5, num_occurrences=1)
- vartype (