dimod.DiscreteQuadraticModel.to_file

DiscreteQuadraticModel.to_file(compress=False, compressed=None, ignore_labels=False, spool_size=1000000000)[source]

Convert the DQM to a file-like object.

Parameters
  • compress (bool, optional default=False) – If True, most of the data will be compressed.

  • compressed (bool, optional default=None) – Deprecated; please use compress instead.

  • ignore_labels (bool, optional, default=False) – Treat the DQM as unlabeled. This is useful for large DQMs to save on space.

  • spool_size (int, optional, default=int(1e9)) – Defines the max_size passed to the constructor of tempfile.SpooledTemporaryFile. Determines whether the returned file-like’s contents will be kept on disk or in memory.

Returns

A file-like object that can be used to construct a copy of the DQM. The class is a thin wrapper of tempfile.SpooledTemporaryFile that includes some methods from io.IOBase

Format Specification (Version 1.0):

This format is inspired by the NPY format

Header

The first 8 bytes are a magic string: exactly “DIMODDQM”.

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(num_variables=dqm.num_variables(),
     num_cases=dqm.num_cases(),
     num_case_interactions=dqm.num_case_interactions(),
     num_variable_interactions=dqm.num_variable_interactions(),
     variables=not (ignore_labels or dqm.variables.is_range),
     )

it is padded with spaces to make the entire length of the header divisible by 64.

DQM Data

The first 4 bytes are exactly “BIAS”

The next 4 bytes form a little-endian unsigned int, the length of the DQM data DATA_LEN.

The next DATA_LEN bytes are the vectors as returned by DiscreteQuadraticModel.to_numpy_vectors() saved using numpy.save().

Variable Data

The first 4 bytes are exactly “VARS”.

The next 4 bytes form a little-endian unsigned int, the length of the variables array VARIABLES_LENGTH.

The next VARIABLES_LENGTH bytes are a json-serialized array. As constructed by `json.dumps(list(bqm.variables)).