dwavesystems / dwavesystems/dimod

BinaryQuadraticModel.to_serializable() unexpected behaviour

Open
#591 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
143
Forks
91
Avg merge
1h 24m
Merged PRs (30d)
3

Description

When using BinaryQuadraticModel.to_serializable() the parameter bias_dtype affects the result even when `use_bytes` is False. This is unexpected, because the description of bias_dtype is:
```
"""
...
bias_dtype (data-type, optional, default=numpy.float32):
If `use_bytes` is True, this :class:`~numpy.dtype` will be used
to represent the bias values in the serialized format.
...
"""
```
For example, the following code (Option A) fails, and the stored JSON file does not match the original object.

``` python
import json
import dimod
import numpy
from dimod.serialization.json import DimodEncoder, DimodDecoder

bqm = dimod.BinaryQuadraticModel(
{(0, 0): -0.3841300947500583, (0, 1): 0.03878295861060144},
{((0, 0), (0, 1)): -0.060900266513891695},
0.0, dimod.Vartype.SPIN)

# Option A:
with open('./test.json','w') as fp:
json.dump(bqm,fp,cls=DimodEncoder)

#Option B:
# with open('./test.json','w') as fp:
# ser = bqm.to_serializable(bias_dtype=numpy.float64)
# json.dump(ser,fp)

with open('./test.json','r') as fp:
copy = json.load(fp,cls=DimodDecoder)

assert(bqm==copy), "Mismatch"

```
Option B works.

This behaviour is, at least, unexpected and doesn't match the documentation.

DimodEncoder, doesn't take any arguments and therefore stores numpy.float32 by default without a warning that the data as changed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.