dimod.BinaryQuadraticModel.to_serializable

BinaryQuadraticModel.to_serializable(use_bytes=False, bias_dtype=<class 'numpy.float32'>, bytes_type=<class 'bytes'>)[source]

Convert the binary quadratic model to a serializable object.

Parameters:
  • use_bytes (bool, optional, default=False) – If True, a compact representation representing the biases as bytes is used. Uses tobytes().
  • bias_dtype (data-type, optional, default=numpy.float32) – If use_bytes is True, this dtype will be used to represent the bias values in the serialized format.
  • bytes_type (class, optional, default=bytes) – This class will be used to wrap the bytes objects in the serialization if use_bytes is true.
Returns:

An object that can be serialized.

Return type:

dict

Examples

Encode using JSON

>>> import json
...
>>> bqm = dimod.BinaryQuadraticModel({'a': -1.0, 'b': 1.0}, {('a', 'b'): -1.0}, 0.0, dimod.SPIN)
>>> s = json.dumps(bqm.to_serializable())

Encode using BSON

>>> import bson
...
>>> bqm = dimod.BinaryQuadraticModel({'a': -1.0, 'b': 1.0}, {('a', 'b'): -1.0}, 0.0, dimod.SPIN)
>>> doc = bqm.to_serializable(use_bytes=True)
>>> b = bson.BSON.encode(doc)  # doctest: +SKIP

See also

from_serializable()

json.dumps(), json.dump() JSON encoding functions

bson.BSON.encode() BSON encoding method