Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support all structured samplers in SubproblemCliqueEmbedder #271

Merged
merged 6 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ cache:
- '%AppData%\pip-cache'

build_script:
- "%PYTHON%\\python.exe -m pip install -r requirements.txt -r tests\\requirements.txt --cache-dir %AppData%\\pip-cache"
- "%PYTHON%\\python.exe -m pip install -r requirements.txt --cache-dir %AppData%\\pip-cache"
- "%PYTHON%\\python.exe -m pip install -r tests\\requirements.txt --cache-dir %AppData%\\pip-cache"

before_test:
- "%PYTHON%\\python.exe -m pip install ."
Expand Down
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ jobs:
command: |
. env/bin/activate
pip install -U pip
pip install -r requirements.txt -r tests/requirements.txt
pip install -r requirements.txt
pip install -r tests/requirements.txt
pip install wheel twine

- save_cache: &save-cache-env
Expand Down
26 changes: 6 additions & 20 deletions hybrid/samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import dimod
from dwave.system.samplers import DWaveSampler
from dwave.system.composites import AutoEmbeddingComposite, FixedEmbeddingComposite
from dwave.embedding.chimera import find_clique_embedding as find_chimera_clique_embedding
from dwave.embedding.pegasus import find_clique_embedding as find_pegasus_clique_embedding

from tabu import TabuSampler
from neal import SimulatedAnnealingSampler
from greedy import SteepestDescentSolver
from minorminer.busclique import busgraph_cache

from hybrid.core import Runnable, SampleSet
from hybrid.flow import Loop
Expand Down Expand Up @@ -114,8 +113,7 @@ class SubproblemCliqueEmbedder(traits.SubproblemIntaking,
Args:
sampler (:class:`dimod.Structured`):
Structured :class:`dimod.Sampler` such as a
:class:`~dwave.system.samplers.DWaveSampler`. The sampler has to
have a Chimera or Pegasus topology.
:class:`~dwave.system.samplers.DWaveSampler`.

Example:
To replace :class:`.QPUSubproblemAutoEmbeddingSampler` with a sampler
Expand All @@ -141,29 +139,17 @@ def __repr__(self):

@staticmethod
def find_clique_embedding(variables, sampler):
"""Given a Chimera- or Pegasus-based ``sampler``, and a list of
"""Given a :class:`dimod.Structured` ``sampler``, and a list of
variable labels, return a clique embedding.

Returns:
dict:
Clique embedding map with source variables from ``variables``
and target edges taken from ``sampler``.
and target graph taken from ``sampler``.

"""
topology_type = sampler.properties['topology']['type']
topology_shape = sampler.properties['topology']['shape']

clique_embedders = dict(chimera=find_chimera_clique_embedding,
pegasus=find_pegasus_clique_embedding)
try:
find_clique_embedding = clique_embedders[topology_type]
except KeyError:
raise ValueError('unknown sampler topology')

embedding = find_clique_embedding(
variables, m=topology_shape[0], target_edges=sampler.edgelist)

return embedding
g = sampler.to_networkx_graph()
return busgraph_cache(g).find_clique_embedding(variables)

def next(self, state, **runopts):
embedding = self.find_clique_embedding(
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage
codecov
parameterized
dwave-system>=1.13.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this creates a conflict with main requirements file. We'll have to update it there instead.

Copy link
Author

@seatim seatim May 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested locally, I ran pip install -r requirements.txt and pip install -r test/requirements.txt sequentially. And that worked. I see that CircleCI (.circleci/config.yml) is doing pip install -r requirements.txt -r test/requirements.txt in one command. So, alternatively, I suppose we could change .circleci/config.yml to run those commands sequentially. Should I try that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that should work!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✔️

17 changes: 11 additions & 6 deletions tests/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@
from operator import attrgetter

import numpy as np
from parameterized import parameterized_class
from parameterized import parameterized, parameterized_class

import dimod
from neal import SimulatedAnnealingSampler
from dwave.system import DWaveSampler
from dwave.system.testing import MockDWaveSampler

from hybrid.samplers import *
from hybrid.core import State
from hybrid.testing import mock
from hybrid.utils import random_sample

MockDWaveSampler.to_networkx_graph = DWaveSampler.to_networkx_graph


class MockDWaveReverseAnnealingSampler(MockDWaveSampler):
"""Extend the `dwave.system.testing.MockDWaveSampler` with mock support for
Expand Down Expand Up @@ -109,12 +112,12 @@ def test_external_embedding_sampler(self):
# verify mock sampler received custom kwargs
self.assertEqual(res.subsamples.first.energy, -1)

def test_clique_embedder(self):
@parameterized.expand([['chimera', 2], ['pegasus', 1], ['zephyr', 1]])
def test_clique_embedder(self, topology_type, expected_chain_length):
bqm = dimod.BinaryQuadraticModel.from_ising({}, {'ab': 1, 'bc': 1, 'ca': 1})
init = State.from_subproblem(bqm)

sampler = MockDWaveSampler()

sampler = MockDWaveSampler(topology_type=topology_type)
workflow = SubproblemCliqueEmbedder(sampler=sampler)

# run embedding
Expand All @@ -123,8 +126,10 @@ def test_clique_embedder(self):
# verify mock sampler received custom kwargs
self.assertIn('embedding', res)
self.assertEqual(len(res.embedding.keys()), 3)
# embedding a triangle onto a chimera produces 3 x 2-qubit chains
self.assertTrue(all(len(e) == 2 for e in res.embedding.values()))

# embedding a triangle onto a chimera produces 3 x 2-qubit chains;
# if embedding onto pegasus the chains have length 1.
self.assertTrue(all(len(e) == expected_chain_length for e in res.embedding.values()))

def test_reverse_annealing_sampler(self):
sampler = MockDWaveReverseAnnealingSampler()
Expand Down