dimod.BinaryQuadraticModel.to_serializable¶
-
BinaryQuadraticModel.to_serializable(use_bytes=False, bias_dtype=<type 'numpy.float32'>, bytes_type=<type 'str'>)[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
dtypewill 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. Useful for when using Python 2 and using BSON encoding, which will not accept the raw bytes type, so bson.Binary can be used instead.
Returns: An object that can be serialized.
Return type: Examples
Encode using JSON
>>> import dimod >>> 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 in python 3.5+
>>> import dimod >>> 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
Encode using BSON in python 2.7. Because
bytesis an alias forstr, we need to signal to the encoder that it should encode the biases and labels as binary data.>>> import dimod >>> 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, bytes_type=bson.Binary) >>> b = bson.BSON.encode(doc) # doctest: +SKIP
- use_bytes (bool, optional, default=False) – If True, a compact representation representing the biases as
bytes is used. Uses