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 1 commit
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
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
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]])
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for Zephyr as well?

Copy link
Member

Choose a reason for hiding this comment

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

Now I see your comment above. You can up the lower bound in tests/requirements.txt only.

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