bqm.to_coo() not storing the offset
Description bqm offsets are not appearing in .coo files
Steps To Reproduce
import itertools
import dimod
bqm = dimod.BQM.from_ising({}, {c: 1 for c in itertools.combinations(range(4), 2)}, offset=42)
with open("inputs/{}.coo".format("test_1"), "w") as f:
bqm.to_coo(f, vartype_header=True)
Expected Behavior the file should look like:
# vartype=SPIN
42
0 1 1.000000
0 2 1.000000
0 3 1.000000
1 2 1.000000
1 3 1.000000
2 3 1.000000
but it looks like:
# vartype=SPIN
0 1 1.000000
0 2 1.000000
0 3 1.000000
1 2 1.000000
1 3 1.000000
2 3 1.000000
Environment
- OS: MacOS 10.15.7
- Python version: Python 3.7.6
- dimod version: 0.9.9
Would be pretty straightforward to add another header line,
# vartype=SPIN
# offset=42
0 1 1.000000
0 2 1.000000
0 3 1.000000
1 2 1.000000
1 3 1.000000
2 3 1.000000
or
# vartype=SPIN, offset=42
0 1 1.000000
0 2 1.000000
0 3 1.000000
1 2 1.000000
1 3 1.000000
2 3 1.000000
unless there was a particular reason you wanted it on it's own line like above?
On it's own line it looks a bit more like it's part of the problem's weights. But I don't know if this could mess with the parsing in older dimod versions.
FWIW, I like @pau557's suggestion. The next logical step is to generalize it to store one polynomial term/coefficient per line:
42
a 1.0
a b 2.0
b c 3.0
a b c 4.0
Then BinaryPolynomial could use it as well.
Obviously, that would not be the COO anymore. :rofl:
If we want to keep calling it COO, the only option, IMO, is to store it in a header. In which case, header output should default to true if offset != 0. (Also if vartype != binary.)