Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Saving BinaryQuadraticModel with save_qubo() not preserve variables defined as Python tuples #20

Open
vsergeyev opened this issue Apr 21, 2021 · 0 comments

Comments

@vsergeyev
Copy link
Contributor

This issue occurs when using save_qubo function to save BinaryQuadraticModel.

It saves model good, except for a Python tuples converted to JSON arrays. Need to note, this is normal behavior for JSON format.

One gotcha maybe when it will be required to use tuple format for BQM variables.

Below is a slightly modified example from dimod's BinaryQuadraticModel documentation with demo of issue:

>>> bqm = dimod.BinaryQuadraticModel({(0, 0): 1, (1, 0): -1, (2, 0): .5}, {((0, 0), (1, 0)): .5, ((1, 0), (2, 0)): 1.5}, 1.4, dimod.Vartype.SPIN)
>>> bqm
BinaryQuadraticModel({(0, 0): 1.0, (1, 0): -1.0, (2, 0): 0.5}, {((0, 0), (1, 0)): 0.5, ((1, 0), (2, 0)): 1.5}, 1.4, 'SPIN')
>>> save_qubo(bqm, '42.json')
>>> load_qubo('42.json')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vova/z-quantum-qubo/src/python/zquantum/qubo/io.py", line 68, in load_qubo
    return bqm_from_serializable(qubo_dict)
  File "/home/vova/z-quantum-qubo/src/python/zquantum/qubo/io.py", line 53, in bqm_from_serializable
    {i: coef for i, coef in serializable["linear"]},
  File "/home/vova/z-quantum-qubo/src/python/zquantum/qubo/io.py", line 53, in <dictcomp>
    {i: coef for i, coef in serializable["linear"]},
TypeError: unhashable type: 'list'
>>> with open('42.json') as f:
...     print(f.read())
... 
{"linear": [[[0, 0], 1.0], [[1, 0], -1.0], [[2, 0], 0.5]], "quadratic": [[[0, 0], [1, 0], 0.5], [[1, 0], [2, 0], 1.5]], "offset": 1.4, "vartype": "SPIN", "schema": "zapata-v1-qubo"}
vsergeyev pushed a commit that referenced this issue Apr 21, 2021
vsergeyev pushed a commit that referenced this issue Apr 21, 2021
vsergeyev pushed a commit that referenced this issue Apr 29, 2021
mstechly pushed a commit that referenced this issue May 3, 2021
* Fixing #20: list cann't be a BQM variable, converting it to tuple in bqm_from_serializable()

* Fixing #20: added tests

* Fixing #20: addressed review comments

Co-authored-by: Vova Sergeyev <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant