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

expose innermost_child sampler #1145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion dimod/core/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
For more examples, see the source code for the composed
documented in :ref:`quadratic_composites`.
"""
from __future__ import annotations
pau557 marked this conversation as resolved.
Show resolved Hide resolved

import abc

from dimod.core.sampler import Sampler
Expand Down Expand Up @@ -75,10 +77,14 @@ def child(self):
raise RuntimeError("A Composite must have at least one child Sampler")



class ComposedSampler(Sampler, Composite):
pau557 marked this conversation as resolved.
Show resolved Hide resolved
"""Abstract base class for dimod composed samplers.

Inherits from :class:`.Sampler` and :class:`.Composite`.

"""
pass

def innermost_child(self) -> Sampler:
"""Returns the inner-most child sampler"""
return self.child.innermost_child()
4 changes: 4 additions & 0 deletions dimod/core/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def parameters(self):
return self._parameters

"""
from __future__ import annotations

import abc
import typing
Expand Down Expand Up @@ -317,3 +318,6 @@ def remove_unknown_kwargs(self, **kwargs) -> typing.Dict[str, typing.Any]:
kwargs.pop(kw)

return kwargs

def innermost_child(self) -> Sampler:
Copy link
Member

Choose a reason for hiding this comment

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

needs a docstring.

return self
31 changes: 31 additions & 0 deletions tests/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,34 @@ def children(self):
sampler = Dummy()
with self.assertRaises(RuntimeError):
sampler.child


class TestInnermostChildProperties(unittest.TestCase):

class Dummy(dimod.Sampler):
def __init__(self, annealing_time_range):
self.annealing_time_range = annealing_time_range
@property
def properties(self):
return {"annealing_time_range": self.annealing_time_range}
@property
def parameters(self):
pass
def sample(**kwargs):
pass
sample_ising = sample_qubo = sample

def test_sampler(self):
# not a composed sampler
annealing_time_range = [1, 1000]
sampler = self.Dummy(annealing_time_range)
innermost_child = sampler.innermost_child()
self.assertEqual(innermost_child.properties["annealing_time_range"], annealing_time_range)

def test_composed_sampler(self):
annealing_time_range = [1, 1000]
sampler = dimod.ClipComposite(dimod.ScaleComposite(self.Dummy(annealing_time_range)))
innermost_child = sampler.innermost_child()
print(innermost_child.properties)
self.assertEqual(innermost_child.properties["annealing_time_range"], annealing_time_range)

17 changes: 17 additions & 0 deletions tests/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,20 @@ def properties(self):

# Check that known kwargs are kept and unknown kwargs are removed
self.assertDictEqual(kwargs, {'a': 1})

def test_innermost_child(self):
class Dummy(dimod.Sampler):
def sample(self, **kwargs):
kwargs = self.remove_unknown_kwargs(**kwargs)
return kwargs

@property
def parameters(self):
return {'a':[]}

@property
def properties(self):
return {'b':[]}

sampler = Dummy()
self.assertDictEqual(sampler.innermost_child().properties, {'b':[]})