diff --git a/src/braket/ahs/local_detuning.py b/src/braket/ahs/local_detuning.py index 51fbcdd28..8d049e476 100644 --- a/src/braket/ahs/local_detuning.py +++ b/src/braket/ahs/local_detuning.py @@ -13,7 +13,7 @@ from __future__ import annotations -from braket.ahs.discretization_types import DiscretizationProperties +from braket.ahs.discretization_types import DiscretizationError, DiscretizationProperties from braket.ahs.field import Field from braket.ahs.hamiltonian import Hamiltonian from braket.ahs.pattern import Pattern @@ -149,12 +149,19 @@ def discretize(self, properties: DiscretizationProperties) -> LocalDetuning: Returns: LocalDetuning: A new discretized LocalDetuning. + + Raises: + DiscretizationError: If local detuning parameters are not available from + the device properties. """ local_detuning_parameters = properties.rydberg.rydbergLocal - time_resolution = ( - local_detuning_parameters.timeResolution if local_detuning_parameters else None - ) + if not local_detuning_parameters: + raise DiscretizationError( + "Local detuning parameters (rydbergLocal) are not available from the device " + "properties. Ensure that the device supports local detuning and that the " + "rydbergLocal capability data is returned by GetDevice." + ) discretized_magnitude = self.magnitude.discretize( - time_resolution=time_resolution, + time_resolution=local_detuning_parameters.timeResolution, ) return LocalDetuning(discretized_magnitude) diff --git a/test/unit_tests/braket/ahs/test_local_detuning.py b/test/unit_tests/braket/ahs/test_local_detuning.py index 8768dce64..ecd395f67 100644 --- a/test/unit_tests/braket/ahs/test_local_detuning.py +++ b/test/unit_tests/braket/ahs/test_local_detuning.py @@ -15,6 +15,7 @@ import pytest +from braket.ahs.discretization_types import DiscretizationError from braket.ahs.hamiltonian import Hamiltonian from braket.ahs.local_detuning import LocalDetuning from braket.timings.time_series import StitchBoundaryCondition @@ -134,6 +135,15 @@ def test_discretize(): assert discretized_field.magnitude == magnitude_mock.discretize.return_value +def test_discretize_raises_when_rydberg_local_missing(): + magnitude_mock = Mock() + mock_properties = Mock() + mock_properties.rydberg.rydbergLocal = None + field = LocalDetuning(magnitude=magnitude_mock) + with pytest.raises(DiscretizationError, match="rydbergLocal"): + field.discretize(mock_properties) + + @pytest.mark.xfail(raises=ValueError) def test_iadd_to_itself(default_local_detuning): default_local_detuning += Hamiltonian(Mock())