diff --git a/dimod/core/composite.py b/dimod/core/composite.py index af5532e71..5dfd7a0c2 100644 --- a/dimod/core/composite.py +++ b/dimod/core/composite.py @@ -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 @@ -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""" + 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`. diff --git a/dimod/core/sampler.py b/dimod/core/sampler.py index 932911f14..b866ffd0c 100644 --- a/dimod/core/sampler.py +++ b/dimod/core/sampler.py @@ -105,6 +105,7 @@ def parameters(self): return self._parameters """ +from __future__ import annotations import abc import typing @@ -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: + return self diff --git a/tests/test_composite.py b/tests/test_composite.py index cdf8b65e0..5c40cbf94 100644 --- a/tests/test_composite.py +++ b/tests/test_composite.py @@ -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)) + self.assertTrue(sampler.innermost_child() is innermost_child) + diff --git a/tests/test_sampler.py b/tests/test_sampler.py index 3906bf806..33c5d1467 100644 --- a/tests/test_sampler.py +++ b/tests/test_sampler.py @@ -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) \ No newline at end of file