dimod.BinaryQuadraticModel.to_coo

BinaryQuadraticModel.to_coo(fp=None, vartype_header=False)[source]

Serialize the binary quadratic model to a COOrdinate format encoding.

Parameters:
  • fp (file, optional) – .write()-supporting file object to save the linear and quadratic biases of a binary quadratic model to. The model is stored as a list of 3-tuples, (i, j, bias), where \(i=j\) for linear biases. If not provided, returns a string.
  • vartype_header (bool, optional, default=False) – If true, the binary quadratic model’s variable type as prepended to the string or file as a header.

Note

Variables must use index lables (numeric lables). Binary quadratic models saved to COOrdinate format encoding do not preserve offsets.

Examples

This is an example of a binary quadratic model encoded in COOrdinate format.

0 0 0.50000
0 1 0.50000
1 1 -1.50000

The Coordinate format with a header

# vartype=SPIN
0 0 0.50000
0 1 0.50000
1 1 -1.50000

This is an example of writing a binary quadratic model to a COOrdinate-format file.

>>> bqm = dimod.BinaryQuadraticModel({0: -1.0, 1: 1.0}, {(0, 1): -1.0}, 0.0, dimod.SPIN)
>>> with open('tmp.ising', 'w') as file:  # doctest: +SKIP
...     bqm.to_coo(file)

This is an example of writing a binary quadratic model to a COOrdinate-format string.

>>> bqm = dimod.BinaryQuadraticModel({0: -1.0, 1: 1.0}, {(0, 1): -1.0}, 0.0, dimod.SPIN)
>>> bqm.to_coo()  # doctest: +SKIP
0 0 -1.000000
0 1 -1.000000
1 1 1.000000