dimod.ConstrainedQuadraticModel.to_file#

ConstrainedQuadraticModel.to_file(*, spool_size: int = 1000000000, compress: bool = False) SpooledTemporaryFile[source]#

Serialize to a file-like object.

Parameters:
  • spool_size – 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.

  • compress – If True, the data will be compressed with zipfile.ZIP_DEFLATED.

Format Specification (Version 2.0):

This format is inspired by the NPY format

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

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=len(cqm.variables),
     num_constraints=len(cqm.constraints),
     num_biases=cqm.num_biases(),
     num_quadratic_variables=cqm.num_quadratic_variables(include_objective=False),
     num_quadratic_variables_real=cqm.num_quadratic_variables(Vartype.REAL, include_objective=True),
     num_linear_biases_real=cqm.num_biases(Vartype.REAL, linear_only=True),
     num_weighted_constraints=sum(comp.lhs.is_soft() for comp in cqm.constraints.values()),
     )

it is terminated by a newline character and padded with spaces to make the entire length of the entire header divisible by 64.

The constraint quadratic model data comes after the header. It is encoded as a zip file with the following structure

constraints/
    <label>/
        lhs
        rhs
        sense
        [discrete]
        [penalty]
        [weight]
    ...
objective
varinfo
[variable_labels.json]

The objective file encodes the objective. See Expression Format Specification below for details about the file format.

The varinfo file encodes the Vartype, lower bound, and upper bound of each variable in the model.

If the variable labels are not range(num_variables), the variable labels are encoded as a json-formatted string in variable_labels.json.

Each constraint/<label>/ directory encodes a constraint with the matching label. The lhs file encodes the left-hand-side of the constraint. See Expression Format Specification below for details about the file format. The rhs file stores a single float representing the roght-hand-side of the constraint. The sense file stores the sense as a string. The discrete file, if present, encodes whether the constraint is a discrete constraint. The penalty and weight files, if present, encode the weight and penalty type for the constraint.

Expression Format Specification (Version 2.0):

This format is inspired by the NPY format

The first 9 bytes are a magic string: exactly “DIMODEXPR”.

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:

data = dict(shape=expr.shape,
            dtype=expr.dtype.name,
            itype=expr.index_dtype.name,
            type=type(expr).__name__,
            )

it is terminated by a newline character and padded with spaces to make the entire length of the entire header divisible by 64.

The expression data comes after the header.

Examples

>>> cqm1 = dimod.ConstrainedQuadraticModel()
>>> x, y = dimod.Binaries(["x", "y"])
>>> cqm1.set_objective(2 * x * y - 2 * x)
>>> cqm_file = cqm1.to_file()
>>> cqm2 = dimod.ConstrainedQuadraticModel.from_file(cqm_file)
>>> print(cqm2.objective.to_polystring())
-2*x + 2*x*y