dimod.BinaryQuadraticModel.to_coo¶
-
BinaryQuadraticModel.to_coo(fp=None)[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. 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
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: ... 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() 0 0 -1.000000 0 1 -1.000000 1 1 1.000000