- BinaryQuadraticModel.to_file(*, ignore_labels: bool = False, spool_size: int = 1000000000, version: Union[int, Tuple[int, int]] = 2) tempfile.SpooledTemporaryFile [source]¶
Serialize the binary quadratic model to a file-like object.
Note that BQMs with the
objectdata type are serialized as
ignore_labels – Treat the BQM as unlabeled. This is useful for large BQMs to save on space.
spool_size – Defines the
max_sizepassed to the constructor of
tempfile.SpooledTemporaryFile. Determines whether the returned file-like’s contents is kept on disk or in memory.
version – The serialization version to use. Either as an integer defining the major version, or as a tuple,
Format Specification (Version 1.0):
This format is inspired by the NPY format
The first 8 bytes are a magic string: exactly “DIMODBQM”.
The next 1 byte is an unsigned byte: the major version of the file format.
The next 1 byte is an unsigned byte: the minor version of the file format.
The next 4 bytes form a little-endian unsigned int, the length of the header data HEADER_LEN.
The next HEADER_LEN bytes form the header data. This is a json-serialized dictionary. The dictionary is exactly:
dict(shape=bqm.shape, dtype=bqm.dtype.name, itype=bqm.data.index_dtype.name, ntype=bqm.data.index_dtype.name, vartype=bqm.vartype.name, type=type(bqm).__name__, variables=list(bqm.variables), )
It is terminated by a newline character and padded with spaces to make the entire length of the entire header divisible by 64.
The binary quadratic model data comes after the header. The number of bytes can be determined by the data types and the number of variables and number of interactions (described in the
dtype.itemsizebytes are the offset. The next
dtype.itemsize) bytes are the linear data. The linear data includes the neighborhood starts and the biases. The final 2 *
dtype.itemsize) bytes are the quadratic data. Stored as
Format Specification (Version 2.0):
In order to make the header a more reasonable length, variable labels have been moved to the body. The
variablesfield of the header dictionary now has a boolean value, making the dictionary:
dict(shape=bqm.shape, dtype=bqm.dtype.name, itype=bqm.data.index_dtype.name, ntype=bqm.data.index_dtype.name, vartype=bqm.vartype.name, type=type(bqm).__name__, variables=any(v != i for i,v in enumerate(bqm.variables)), )
If the BQM is index-labeled, then no additional data is added. Otherwise, a new section is appended after the bias data.
The first 4 bytes are exactly “VARS”.
The next 4 bytes form a little-endian unsigned int, the length of the variables array
The next VARIABLES_LENGTH bytes are a json-serialized array. As constructed by json.dumps(list(bqm.variables)).