dimod.Response.change_vartype

Response.change_vartype(**kwargs)[source]

Create a new response with the given vartype.

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

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

    • Vartype.SPIN, 'SPIN', {-1, 1}
    • Vartype.BINARY, 'BINARY', {0, 1}
  • data_vector_offsets (dict[field, numpy.array/list], optional, default=None) – Offsets to add to data_vectors of the response formatted as a dict containing per-sample offsets in vectors. Each vector is the same length as samples_matrix.
  • inplace (bool, optional, default=True) – If True, the response is updated in-place, otherwise a new response is returned.
Returns:

Response. New response with vartype matching input ‘vartype’.

Examples

This example converts the response of the dimod package’s ExactSolver sampler to binary and adds offsets.

>>> import dimod
>>> response = dimod.ExactSolver().sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1})
>>> response_binary = response.change_vartype('BINARY',
...                   data_vector_offsets={'energy': [0, 0.1, 0.2, 0.3]},
...                   inplace=False)
>>> response_binary.vartype
<Vartype.BINARY: frozenset([0, 1])>
>>> for datum in response_binary.data():    
...    print(datum)
...
Sample(sample={'a': 0, 'b': 0}, energy=-1.5)
Sample(sample={'a': 1, 'b': 0}, energy=-0.4)
Sample(sample={'a': 1, 'b': 1}, energy=-0.3)
Sample(sample={'a': 0, 'b': 1}, energy=2.8)

This example code snippet creates a response with spin variables from a response with binary variables while adding energy offsets to the new response.

import pandas as pd
samples = [[0, 1], [1, 0]]
energies = [0.0, 1.0]
response = dimod.Response.from_matrix(samples, {'energy': energies})
offsets = {'energy': [0.25, 0.75]}
response.change_vartype('SPIN',
                         data_vector_offsets = offsets)