dimod.BinaryQuadraticModel.to_json¶
-
BinaryQuadraticModel.to_json(fp=None)[source]¶ Serialize the binary quadratic model using JSON.
Parameters: fp (file, optional) – A .write()-supporting file object to save the binary quadratic model to, formatted according to the current BQM schema. If not provided, returns a string. Examples
This example shows a serialized binary quadratic model in JSON encoding for schema version 1.0.0.
{ "linear_terms": [ {"bias": 1.0, "label": 0}, {"bias": -1.0, "label": 1} ], "info": {}, "offset": 0.5, "quadratic_terms": [ {"bias": 0.5, "label_head": 1, "label_tail": 0} ], "variable_labels": [0, 1], "variable_type": "SPIN", "version": { "bqm_schema": "1.0.0", "dimod": "0.6.3" } }
This is an example of writing a binary quadratic model to a JSON-format file.
>>> import dimod >>> bqm = dimod.BinaryQuadraticModel({'a': -1.0, 'b': 1.0}, {('a', 'b'): -1.0}, 0.0, dimod.SPIN) >>> with open('tmp.txt', 'w') as file: ... bqm.to_json(file)
This is an example of writing a binary quadratic model to a JSON-format string.
>>> import dimod >>> bqm = dimod.BinaryQuadraticModel({'a': -1.0, 'b': 1.0}, {('a', 'b'): -1.0}, 0.0, dimod.SPIN) >>> bqm.to_json() {"info": {}, "linear_terms": [{"bias": -1.0, "label": "a"}, {"bias": 1.0, "label": "b"}], "offset": 0.0, "quadratic_terms": [{"bias": -1.0, "label_head": "b", "label_tail": "a"}], "variable_labels": ["a", "b"], "variable_type": "SPIN", "version": {"bqm_schema": "1.0.0", "dimod": "0.6.3"}}