Add lattice-utils to dwave-experimental - #44
Conversation
-move initialization of num_spins to subclass
thisac
left a comment
There was a problem hiding this comment.
Thanks @SebastianGitt! First pass of the lattice submodule.
- I'd recommend running a formatter (black) since all of these files are new and there are a few places with incorrect spacing, etc.
- I think the saving/loading of embeddings can be refined a bit, but I'll have a closer look at it later.
-added more comprehensive docstrings -fixed formatting issues
-added more comprehensive docstrings -fixed formatting issues
…dwave-experimental into add-lattice-utils
-removed spin reversal transform functionality -uses automorphism module already in dwave-experimental -uses data classes for experiment configs
-removed spin reversal transform functionality -uses automorphism module already in dwave-experimental -uses data classes for experiment configs -formatted using black
andrew-d-king
left a comment
There was a problem hiding this comment.
Looks generally good; I am not able to pore over it in detail but it all seems sensible.
thisac
left a comment
There was a problem hiding this comment.
Still have experiment module to look at. Re. the tests, it probably makes sense splitting them up into several files collected in a test_lattice_utils folder.
-added progress bars using TQDM -added docstrings to the evaluation methods in observable.py -misc cleanup
-added progress bars using TQDM -added docstrings to the evaluation methods in observable.py -misc cleanup
…dwave-experimental into add-lattice-utils
jackraymond
left a comment
There was a problem hiding this comment.
Nice job, especially the integration of new automorphism functionality to make the shimming tutorial generalizable.
Some of this code is not lattice specific, I'd prefer it weren't bundled up in a lattice utility. Like embed_bqm (which mirrors a dwave.system) and unembed_bqm (which perhaps is, or should be dwave.system method), bootstrapping, bitstring compression. Ideally we could think how to extract it for more general use.
Examples don't run well, or at all for me. Adding an interface and some additional help messages on what is going on would be great. Perhaps cite papers etc where statistics are used.
There are a lot of magic numbers/methods floating around, if these could be promoted to function arguments and justified that would be great.
Some other naming conventions I found a bit confusing.
-fix failing tests due to updated method signatures with explicit arguments -rename energy_scale to signed_energy_scale
-remove existing examples and replaced with a new 1D chain shimming example -restructure tests into separate files in a new folder -update optimize function to accept custom dimod solvers -expand docstring for ``embed_bqm`` method to explain difference from existing methods in dwave-system -promote ``optimize`` and ``make_networkx_graph`` to public methods
thisac
left a comment
There was a problem hiding this comment.
Looking good so far! A couple of comments and improvements before ready to merge.
One thing that we discussed already: tqdm should be made into an optional dependency. Currently it's required to install.
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Shimming example for 1D Ising chain.""" |
There was a problem hiding this comment.
Let's also rename this file to just 1D_Ising_chain_shim.py or maybe prefix it with lattice_utils or even put it in a lattice_utils subfolder since there are quite a few more lattice-utils examples incoming, no?
-make TQDM an optional import -order imports -add some missing docstrings -removed subscript unpacking for Python 3.10 compatibility -updated licenses to 2026
fd48b6e to
2b02cee
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #44 +/- ##
==========================================
- Coverage 95.38% 92.46% -2.92%
==========================================
Files 13 30 +17
Lines 931 2017 +1086
==========================================
+ Hits 888 1865 +977
- Misses 43 152 +109 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
thisac
left a comment
There was a problem hiding this comment.
A couple of things noticed, but otherwise happy to merge when approved by @andrew-d-king.
-import Experiment and Lattice types for proper type hints -fix typos -SamplerCall is now a proper data class
randomir
left a comment
There was a problem hiding this comment.
A few comments, after a first pass over the experiment and lattice modules.
| from pathlib import Path | ||
| import sys | ||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
| if str(REPO_ROOT) not in sys.path: | ||
| sys.path.insert(0, str(REPO_ROOT)) | ||
|
|
There was a problem hiding this comment.
This is not needed when example requirements are installed. Note that those requirements include the dwave-experimental package in the parent repo (-e .. line).
| from pathlib import Path | |
| import sys | |
| REPO_ROOT = Path(__file__).resolve().parents[1] | |
| if str(REPO_ROOT) not in sys.path: | |
| sys.path.insert(0, str(REPO_ROOT)) |
| ) | ||
|
|
||
|
|
||
| def _make_mock_sampler( |
There was a problem hiding this comment.
Why is dwave.system.testing.MockDWaveSampler not sufficient here? Could we rather extend the existing mock sampler if some functionality is needed here?
| if sampler is None: | ||
| return self._get_path(kind) | ||
|
|
||
| if type(sampler).__name__ == "MockDWaveSampler": |
| __all__ = ['optimize', 'ExponentialBackoffSimulatedAnnealingSampler'] | ||
|
|
||
|
|
||
| class ExponentialBackoffSimulatedAnnealingSampler(dimod.Sampler): |
There was a problem hiding this comment.
It's not backoff (timeout) that's exponential, but number of sweeps.
| class ExponentialBackoffSimulatedAnnealingSampler(dimod.Sampler): | |
| class ExponentialSweepsSimulatedAnnealingSampler(dimod.Sampler): |
| reference_sample = None | ||
|
|
||
| # If the lattice is embedded, we should optimize the logical lattice | ||
| if hasattr(lattice, "logical_lattice"): |
There was a problem hiding this comment.
Why do you prefer duck typing over explicit type check?
| if hasattr(lattice, "logical_lattice"): | |
| if isinstance(lattice, EmbeddedLattice): |
Here, and elsewhere.
There was a problem hiding this comment.
In this case it was to avoid circular imports
| def __init__( | ||
| self, | ||
| *, | ||
| inst: Lattice, |
There was a problem hiding this comment.
Better name:
| inst: Lattice, | |
| lattice: Lattice, |
| if result_fields is None: | ||
| result_fields = list(data.keys()) | ||
| if ignore_shim: | ||
| result_fields.remove("shim_data") |
There was a problem hiding this comment.
This fails with ValueError when "shim_data" is not in the list -- e.g. on second file (!), or when passed-in arg is not None, but omits the shim data value.
|
|
||
| # Strip spaces and replace other unswanted symbols with underscores. | ||
| pathstring = pathstring.replace(" ", "_") | ||
| for bad_symbol in ":;,": |
There was a problem hiding this comment.
Here, and elsewhere (security): use an allowlist for pathname construction, not a short denylist.
E.g. allow only [A-Za-z0-9._-], replace the rest with _.
Consider anneal_schedule or solver name containing /../../.
|
|
||
| try: | ||
| with lzma.open(filename, "rb") as f: | ||
| data = pickle.load(f) |
There was a problem hiding this comment.
The use of pickle (in a loop) is the biggest security/safety issue I see in this PR.
We can allow it for now in the experimental package, but we will have to replace it with np.savez/np.savez_compressed/orjson/JSON/similar serialization before moving this code to Ocean.
Unless you feel keen on rewriting the serialization in this PR, please add a warning to the experiment/lattice module docs.
There was a problem hiding this comment.
Thanks, I'll add the warning. It will be deprecated in favour of using databases before its merged to Ocean.
| else: | ||
| break | ||
|
|
||
| best_sampleset.info["num_sweeps_exit"] = num_sweeps |
There was a problem hiding this comment.
This fails if the while loop doesn't run!
-use an allowlist for pathname construction -add warning about the security risk of using pickled data
Add a minimal version of LatQA to dwave-experimental as a submodule named
lattice-utils. Currently supports examples of a 1D Ising Chain and a 2D dimerized triangular lattice. Tests still need to be added to the pull request.