From e1564a42ac8b898f021bc7d2fbae370338f5ea50 Mon Sep 17 00:00:00 2001 From: Abe Coull <85974725+math411@users.noreply.github.com> Date: Wed, 10 Apr 2024 15:34:56 -0700 Subject: [PATCH] fix: remove shifting field from testing (#948) --- setup.py | 2 +- src/braket/ahs/__init__.py | 1 + .../ahs/analog_hamiltonian_simulation.py | 10 ++-- .../ahs/test_analog_hamiltonian_simulation.py | 20 ++++---- ...ifting_field.py => test_local_detuning.py} | 48 +++++++++---------- ...alog_hamiltonian_simulation_task_result.py | 2 +- 6 files changed, 40 insertions(+), 43 deletions(-) rename test/unit_tests/braket/ahs/{test_shifting_field.py => test_local_detuning.py} (73%) diff --git a/setup.py b/setup.py index 34d220161..821748ef3 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ packages=find_namespace_packages(where="src", exclude=("test",)), package_dir={"": "src"}, install_requires=[ - "amazon-braket-schemas>=1.21.0", + "amazon-braket-schemas>=1.21.3", "amazon-braket-default-simulator>=1.21.2", "oqpy~=0.3.5", "backoff", diff --git a/src/braket/ahs/__init__.py b/src/braket/ahs/__init__.py index 8a9fd2666..5bf3cc61e 100644 --- a/src/braket/ahs/__init__.py +++ b/src/braket/ahs/__init__.py @@ -17,5 +17,6 @@ from braket.ahs.driving_field import DrivingField # noqa: F401 from braket.ahs.field import Field # noqa: F401 from braket.ahs.hamiltonian import Hamiltonian # noqa: F401 +from braket.ahs.local_detuning import LocalDetuning # noqa: F401 from braket.ahs.pattern import Pattern # noqa: F401 from braket.ahs.shifting_field import ShiftingField # noqa: F401 diff --git a/src/braket/ahs/analog_hamiltonian_simulation.py b/src/braket/ahs/analog_hamiltonian_simulation.py index 8d8cce10f..af02471e2 100644 --- a/src/braket/ahs/analog_hamiltonian_simulation.py +++ b/src/braket/ahs/analog_hamiltonian_simulation.py @@ -21,12 +21,12 @@ from braket.ahs.discretization_types import DiscretizationError, DiscretizationProperties from braket.ahs.driving_field import DrivingField from braket.ahs.hamiltonian import Hamiltonian -from braket.ahs.shifting_field import ShiftingField +from braket.ahs.local_detuning import LocalDetuning from braket.device_schema import DeviceActionType class AnalogHamiltonianSimulation: - SHIFTING_FIELDS_PROPERTY = "shifting_fields" + LOCAL_DETUNING_PROPERTY = "local_detuning" DRIVING_FIELDS_PROPERTY = "driving_fields" def __init__(self, register: AtomArrangement, hamiltonian: Hamiltonian) -> None: @@ -74,7 +74,7 @@ def _hamiltonian_to_ir(self) -> ir.Hamiltonian: terms[term_type].append(term_ir) return ir.Hamiltonian( drivingFields=terms[AnalogHamiltonianSimulation.DRIVING_FIELDS_PROPERTY], - shiftingFields=terms[AnalogHamiltonianSimulation.SHIFTING_FIELDS_PROPERTY], + localDetuning=terms[AnalogHamiltonianSimulation.LOCAL_DETUNING_PROPERTY], ) def discretize(self, device: AwsDevice) -> AnalogHamiltonianSimulation: # noqa @@ -116,8 +116,8 @@ def _get_term_ir( @_get_term_ir.register -def _(term: ShiftingField) -> tuple[str, ir.ShiftingField]: - return AnalogHamiltonianSimulation.SHIFTING_FIELDS_PROPERTY, ir.ShiftingField( +def _(term: LocalDetuning) -> tuple[str, ir.LocalDetuning]: + return AnalogHamiltonianSimulation.LOCAL_DETUNING_PROPERTY, ir.LocalDetuning( magnitude=ir.PhysicalField( time_series=ir.TimeSeries( times=term.magnitude.time_series.times(), diff --git a/test/unit_tests/braket/ahs/test_analog_hamiltonian_simulation.py b/test/unit_tests/braket/ahs/test_analog_hamiltonian_simulation.py index aeb969869..65cc2a074 100644 --- a/test/unit_tests/braket/ahs/test_analog_hamiltonian_simulation.py +++ b/test/unit_tests/braket/ahs/test_analog_hamiltonian_simulation.py @@ -23,7 +23,7 @@ AtomArrangement, DiscretizationError, DrivingField, - ShiftingField, + LocalDetuning, SiteType, ) from braket.ahs.atom_arrangement import AtomArrangementItem @@ -61,8 +61,8 @@ def driving_field(): @pytest.fixture -def shifting_field(): - return ShiftingField( +def local_detuning(): + return LocalDetuning( Field( TimeSeries().put(0.0, -1.25664e8).put(3.0e-6, 1.25664e8), Pattern([0.5, 1.0, 0.5, 0.5, 0.5, 0.5]), @@ -78,8 +78,8 @@ def test_create(): assert mock1 == ahs.hamiltonian -def test_to_ir(register, driving_field, shifting_field): - hamiltonian = driving_field + shifting_field +def test_to_ir(register, driving_field, local_detuning): + hamiltonian = driving_field + local_detuning ahs = AnalogHamiltonianSimulation(register=register, hamiltonian=hamiltonian) problem = ahs.to_ir() assert Program.parse_raw(problem.json()) == problem @@ -123,8 +123,8 @@ def test_invalid_action_name(): AnalogHamiltonianSimulation(register=Mock(), hamiltonian=Mock()).discretize(device) -def test_discretize(register, driving_field, shifting_field): - hamiltonian = driving_field + shifting_field +def test_discretize(register, driving_field, local_detuning): + hamiltonian = driving_field + local_detuning ahs = AnalogHamiltonianSimulation(register=register, hamiltonian=hamiltonian) action = Mock() @@ -177,11 +177,7 @@ def test_discretize(register, driving_field, shifting_field): "values": ["-125664000.0", "-125664000.0", "125664000.0", "125664000.0"], }, } - local_detuning = ( - discretized_json["hamiltonian"]["shiftingFields"][0]["magnitude"] - if "shiftingFields" in discretized_json["hamiltonian"].keys() - else discretized_json["hamiltonian"]["localDetuning"][0]["magnitude"] - ) + local_detuning = discretized_json["hamiltonian"]["localDetuning"][0]["magnitude"] assert local_detuning == { "pattern": ["0.50", "1.00", "0.50", "0.50", "0.50", "0.50"], "time_series": { diff --git a/test/unit_tests/braket/ahs/test_shifting_field.py b/test/unit_tests/braket/ahs/test_local_detuning.py similarity index 73% rename from test/unit_tests/braket/ahs/test_shifting_field.py rename to test/unit_tests/braket/ahs/test_local_detuning.py index 0249b8758..4c4f94675 100644 --- a/test/unit_tests/braket/ahs/test_shifting_field.py +++ b/test/unit_tests/braket/ahs/test_local_detuning.py @@ -16,49 +16,49 @@ import pytest from braket.ahs.hamiltonian import Hamiltonian -from braket.ahs.shifting_field import ShiftingField +from braket.ahs.local_detuning import LocalDetuning from braket.timings.time_series import StitchBoundaryCondition @pytest.fixture -def default_shifting_field(): - return ShiftingField(Mock()) +def default_local_detuning(): + return LocalDetuning(Mock()) def test_create(): mock0 = Mock() - field = ShiftingField(magnitude=mock0) + field = LocalDetuning(magnitude=mock0) assert mock0 == field.magnitude -def test_add_hamiltonian(default_shifting_field): - expected = [default_shifting_field, Mock(), Mock(), Mock()] +def test_add_hamiltonian(default_local_detuning): + expected = [default_local_detuning, Mock(), Mock(), Mock()] result = expected[0] + Hamiltonian([expected[1], expected[2], expected[3]]) assert result.terms == expected -def test_add_to_hamiltonian(default_shifting_field): - expected = [Mock(), Mock(), Mock(), default_shifting_field] +def test_add_to_hamiltonian(default_local_detuning): + expected = [Mock(), Mock(), Mock(), default_local_detuning] result = Hamiltonian([expected[0], expected[1], expected[2]]) + expected[3] assert result.terms == expected def test_add_to_other(): - field0 = ShiftingField(Mock()) - field1 = ShiftingField(Mock()) + field0 = LocalDetuning(Mock()) + field1 = LocalDetuning(Mock()) result = field0 + field1 assert type(result) is Hamiltonian assert result.terms == [field0, field1] -def test_add_to_self(default_shifting_field): - result = default_shifting_field + default_shifting_field +def test_add_to_self(default_local_detuning): + result = default_local_detuning + default_local_detuning assert type(result) is Hamiltonian - assert result.terms == [default_shifting_field, default_shifting_field] + assert result.terms == [default_local_detuning, default_local_detuning] -def test_iadd_to_other(default_shifting_field): - expected = [Mock(), Mock(), Mock(), default_shifting_field] +def test_iadd_to_other(default_local_detuning): + expected = [Mock(), Mock(), Mock(), default_local_detuning] other = Hamiltonian([expected[0], expected[1], expected[2]]) other += expected[3] assert other.terms == expected @@ -69,7 +69,7 @@ def test_from_lists(): glob_amplitude = [0.5, 0.8, 0.9, 1.0] pattern = [0.3, 0.7, 0.6, -0.5, 0, 1.6] - sh_field = ShiftingField.from_lists(times, glob_amplitude, pattern) + sh_field = LocalDetuning.from_lists(times, glob_amplitude, pattern) assert sh_field.magnitude.time_series.values() == glob_amplitude assert sh_field.magnitude.pattern.series == pattern @@ -82,7 +82,7 @@ def test_from_lists_not_eq_length(): glob_amplitude = [0.5, 0.8, 0.9, 1.0] pattern = [0.3, 0.7, 0.6, -0.5, 0, 1.6] - ShiftingField.from_lists(times, glob_amplitude, pattern) + LocalDetuning.from_lists(times, glob_amplitude, pattern) def test_stitch(): @@ -94,8 +94,8 @@ def test_stitch(): glob_amplitude_2 = [0.5, 0.8, 0.9, 1.0] pattern_2 = pattern_1 - sh_field_1 = ShiftingField.from_lists(times_1, glob_amplitude_1, pattern_1) - sh_field_2 = ShiftingField.from_lists(times_2, glob_amplitude_2, pattern_2) + sh_field_1 = LocalDetuning.from_lists(times_1, glob_amplitude_1, pattern_1) + sh_field_2 = LocalDetuning.from_lists(times_2, glob_amplitude_2, pattern_2) new_sh_field = sh_field_1.stitch(sh_field_2, boundary=StitchBoundaryCondition.LEFT) @@ -116,8 +116,8 @@ def test_stitch_not_eq_pattern(): glob_amplitude_2 = [0.5, 0.8, 0.9, 1.0] pattern_2 = [-0.3, 0.7, 0.6, -0.5, 0, 1.6] - sh_field_1 = ShiftingField.from_lists(times_1, glob_amplitude_1, pattern_1) - sh_field_2 = ShiftingField.from_lists(times_2, glob_amplitude_2, pattern_2) + sh_field_1 = LocalDetuning.from_lists(times_1, glob_amplitude_1, pattern_1) + sh_field_2 = LocalDetuning.from_lists(times_2, glob_amplitude_2, pattern_2) sh_field_1.stitch(sh_field_2) @@ -125,7 +125,7 @@ def test_stitch_not_eq_pattern(): def test_discretize(): magnitude_mock = Mock() mock_properties = Mock() - field = ShiftingField(magnitude=magnitude_mock) + field = LocalDetuning(magnitude=magnitude_mock) discretized_field = field.discretize(mock_properties) magnitude_mock.discretize.assert_called_with( time_resolution=mock_properties.rydberg.rydbergLocal.timeResolution, @@ -137,5 +137,5 @@ def test_discretize(): @pytest.mark.xfail(raises=ValueError) -def test_iadd_to_itself(default_shifting_field): - default_shifting_field += Hamiltonian(Mock()) +def test_iadd_to_itself(default_local_detuning): + default_local_detuning += Hamiltonian(Mock()) diff --git a/test/unit_tests/braket/tasks/test_analog_hamiltonian_simulation_task_result.py b/test/unit_tests/braket/tasks/test_analog_hamiltonian_simulation_task_result.py index 23cca78be..be2d6fdbe 100644 --- a/test/unit_tests/braket/tasks/test_analog_hamiltonian_simulation_task_result.py +++ b/test/unit_tests/braket/tasks/test_analog_hamiltonian_simulation_task_result.py @@ -75,7 +75,7 @@ def additional_metadata(): }, } ], - "shiftingFields": [ + "localDetuning": [ { "magnitude": { "time_series": {