What is the recommended method to store and retrieve D-Wave objects? (i.e. BQMs, embeddings, samplesets)
I'm wondering if there is a recommended practice or something available in the D-Wave toolset to store and load complex objects for offline use, or if this is left to the user. i.e. BQMs, embeddings, samplesets
I noticed that `dimod` has multiple ways of exporting and importing BQMs and samplesets. However, once exported, the storage method seems to be left to the user.
I include embeddings because even though there are simple ways of reusing an embedding once it has been found, I don't see a standardized way of preserving that embedding for later use.
Comments
Hello,
We have lots of serialization in ocean.
BinaryQuadraticModel and SampeSet objects for example have a to_serializable() function that allows them to be json serialized like this:
Some objects can be serialized as-is:
In some cases objects are not serializable in Python, in which case "pickling" the object like this is possible:
This does not allow the same human-readability as json does, but for simple storage and retrieval, it's pretty handy.
For portability, GitHub is a great option to store a variety of data, and make it accessible from multiple locations via the internet.
I hope this was helpful!
Dear David,
I am running into problems when following any of the methods listed above. I am solving a 3x3 linear system using the QUBO sampler. There are no issues when I run the code on the Pegasus topology and I am able to retrieve my answer.
---
solver = EmbeddingComposite(DWaveSampler(solver={'topology__type': 'pegasus'}))
response = solver.sample_qubo(Q,num_reads=n_rep)
---
I then want to serialize the object 'response' so that it can be stored locally and retrieved at a future time for processing. However, when I try
---
s = json.dumps(response.to_serializable())
---
I get the error
---
Traceback (most recent call last):
File "<ipython-input-50-3555f0fb8473>", line 1, in <module>
s = json.dumps(response.to_serializable())
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type type is not JSON serializable
---
I also tried pickle,
---
s=pickle.dumps(response)
---
and that did not return an error. However, when I try to load
---
s_new=pickle.loads(s)
---
I get the following error
---
Traceback (most recent call last):
File "<ipython-input-54-fe98a5b8387b>", line 1, in <module>
s_new=pickle.loads(s)
File "/usr/local/lib/python3.8/site-packages/dwave/embedding/transforms.py", line 152, in _mutate_dict
raise TypeError("EmbeddedStructure is immutable")
TypeError: EmbeddedStructure is immutable
---
It is very important that I am able to store all the solver data I can from the quantum computer, as I am interesting in computing statistics regarding the annealing process, as well as retrieving individual solutions. Any help is much appreciated.
Thank you,
Juan S.
Just an update on a partial success using the following commands.
---
s=pickle.dumps(response.to_serializable())
s_new=dimod.SampleSet.from_serializable(pickle.loads(s))
---
This seems to work. At least I am able to save my data and then read it at a later time. Commands like
---
s_new.info
s_new.first.sample
dwave.inspector.show(s_new)
---
work. However,
---
s_new == response
False
---
As long as I am able to extract information from s_new and use dwave tools on this information, that's fine by me.
---
s_new.record==response.record
---
returns 'True' for every record.
However,
---
s_new.samples==response.samples
False
---
But
---
samples=response.samples()
samples_new=s_new.samples()
samples_new[0]==samples[0]
True
---
Hello,
I think what you wrote in the last section makes sense.
The individual samples will be the same, but the list object holding them will not, because they are separate objects, not references to the same object.
You are making a copy of the list of values, so comparing the lists to each other makes sense.
Comparing the list object itself should prove to return False.
For instance, if the reference to the list object was the same, and you edited an element in one list, the same element in the other list should change, because they wouldn't be two identical list, but rather two references to the same single list.
I hope this is helpful.
Please let us know if you have any questions.
What about CQMs (Constrained Quadratic Models)? Ho do you save and later retrieve them?
We now have a method that can be used save a CQM to file:
Take a look here for examples:
https://github.com/dwavesystems/dimod/issues/899
You might need to install the newest version of dwave-ocean-sdk library, which will install the newest dimod library.
Thank you David J!
My understanding, however, is that it does not actually write to disk/file, it only returns a temporary file. Am I missing something?
Hi again,
That's right, you will have to use the method outlined in the link.
To write to a file you would do something like this:
Here we are using the open function to open a writable binary file ("wb") named "my_file", and copying the contents of the cqm file to it using the shutil.copyfileobj function.
Similarly you could open and read the file something like this:
I hope this helps answer your question.
Please let us know if you need any further information.
Definitely it helps!
Thanks a lot David J!
Please sign in to leave a comment.