dimod.BinaryQuadraticModel.from_coo¶
-
classmethod
BinaryQuadraticModel.from_coo(obj, vartype=None)[source]¶ Deserialize a binary quadratic model from a COOrdinate format encoding.
Parameters: - obj – (str/file): Either a string or a .read()-supporting file object that represents linear and quadratic biases for a binary quadratic model. This data is stored as a list of 3-tuples, (i, j, bias), where \(i=j\) for linear biases.
- vartype (
Vartype/str/set, optional) –Variable type for the binary quadratic model. Accepted input values:
Vartype.SPIN,'SPIN',{-1, 1}Vartype.BINARY,'BINARY',{0, 1}
If not provided, the vartype must be specified with a header in the file.
Note
Variables must use index lables (numeric lables). Binary quadratic models created from COOrdinate format encoding have offsets set to zero.
Examples
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 example saves a binary quadratic model to a COOrdinate-format file and creates a new model by reading the saved file.
>>> import dimod >>> bqm = dimod.BinaryQuadraticModel({0: -1.0, 1: 1.0}, {(0, 1): -1.0}, 0.0, dimod.BINARY) >>> with open('tmp.qubo', 'w') as file: # doctest: +SKIP ... bqm.to_coo(file) >>> with open('tmp.qubo', 'r') as file: # doctest: +SKIP ... new_bqm = dimod.BinaryQuadraticModel.from_coo(file, dimod.BINARY) >>> any(new_bqm) # doctest: +SKIP True