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 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
7 changes: 6 additions & 1 deletion dimod/core/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
For more examples, see the source code for the composed
documented in :ref:`quadratic_composites`.
"""

import abc

from dimod.core.sampler import Sampler
Expand Down Expand Up @@ -74,8 +75,12 @@ def child(self):
except IndexError:
raise RuntimeError("A Composite must have at least one child Sampler")

def innermost_child(self) -> Sampler:
"""Returns the inner-most child sampler"""
Copy link
Contributor

@JoelPasvolsky JoelPasvolsky Mar 9, 2022

Choose a reason for hiding this comment

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

Suggested change
"""Returns the inner-most child sampler"""
"""Return the innermost child sampler."""

Copy link
Member

Choose a reason for hiding this comment

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

Also shouldn't it be Return?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, updated, thanks

return self.child.innermost_child()


class ComposedSampler(Sampler, Composite):
class ComposedSampler(Composite, Sampler):
"""Abstract base class for dimod composed samplers.

Inherits from :class:`.Sampler` and :class:`.Composite`.
Expand Down
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
9 changes: 9 additions & 0 deletions tests/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ def children(self):
sampler = Dummy()
with self.assertRaises(RuntimeError):
sampler.child


class TestInnermostChild(unittest.TestCase):

def test_composed_sampler(self):
innermost_child = dimod.ExactSolver()
sampler = dimod.ClipComposite(dimod.ScaleComposite(innermost_child))
Copy link
Member

Choose a reason for hiding this comment

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

I would use TrackingComposite and StructureComposite since those are staying in dimod through #1127 so the tests stay valid.

self.assertTrue(sampler.innermost_child() is innermost_child)

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

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

def test_innermost_child(self):
sampler = dimod.ExactSolver()
self.assertTrue(sampler.innermost_child() is sampler)