From 536b0ff5697126fe75bcfb49da6e1000400ea14f Mon Sep 17 00:00:00 2001 From: Coull Date: Mon, 8 Apr 2024 11:18:27 -0700 Subject: [PATCH 1/6] fix: change shiftingFields to localDetuning --- src/braket/ir/ahs/hamiltonian.py | 31 ++++++-- src/braket/ir/ahs/local_detuning.py | 42 +++++++++++ src/braket/ir/ahs/program_v1.py | 2 +- src/braket/ir/ahs/shifting_field.py | 32 ++------- .../braket/ir/ahs/test_ahs_program_v1.py | 70 ++++++++++--------- .../braket/ir/ahs/test_hamiltonian.py | 17 ++++- .../unit_tests/braket/task_result/conftest.py | 8 ++- 7 files changed, 130 insertions(+), 72 deletions(-) create mode 100644 src/braket/ir/ahs/local_detuning.py diff --git a/src/braket/ir/ahs/hamiltonian.py b/src/braket/ir/ahs/hamiltonian.py index 6277e656..ccb6a469 100644 --- a/src/braket/ir/ahs/hamiltonian.py +++ b/src/braket/ir/ahs/hamiltonian.py @@ -13,25 +13,42 @@ from typing import List -from pydantic.v1 import BaseModel +from pydantic.v1 import BaseModel, Field from braket.ir.ahs.driving_field import DrivingField -from braket.ir.ahs.shifting_field import ShiftingField - +from braket.ir.ahs.local_detuning import LocalDetuning class Hamiltonian(BaseModel): """ Specifies the Hamiltonian Attributes: - driving_fileds: An externally controlled force + drivingFields: An externally controlled force that drives coherent transitions between selected levels of certain atom types - shifting_fields: An externally controlled polarizing force + localDetuning: An externally controlled polarizing force the effect of which is accurately described by a frequency shift of certain levels. Examples: - >>> Hamiltonian(driving_fields=[DrivingField],shifting_fields=[ShiftingField]) + >>> Hamiltonian(driving_fields=[DrivingField],local_detuning=[LocalDetuning]) """ drivingFields: List[DrivingField] - shiftingFields: List[ShiftingField] + localDetuning: List[LocalDetuning] = Field(alias="shiftingFields") + + def __getattr__(self, name): + if name == "shiftingFields": + name = "localDetuning" + return self.__dict__[name] + + def __setattr__(self, name, value): + if name == "shiftingFields": + name = "localDetuning" + self.__dict__[name] = value + + def __delattr__(self, name): + if name == "shiftingFields": + name = "localDetuning" + del self.__dict__[name] + + class Config: + allow_population_by_field_name = True diff --git a/src/braket/ir/ahs/local_detuning.py b/src/braket/ir/ahs/local_detuning.py new file mode 100644 index 00000000..570781c8 --- /dev/null +++ b/src/braket/ir/ahs/local_detuning.py @@ -0,0 +1,42 @@ +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"). You +# may not use this file except in compliance with the License. A copy of +# the License is located at +# +# http://aws.amazon.com/apache2.0/ +# +# or in the "license" file accompanying this file. This file is +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF +# ANY KIND, either express or implied. See the License for the specific +# language governing permissions and limitations under the License. + +from pydantic.v1 import BaseModel + +from braket.ir.ahs.physical_field import PhysicalField + + +class LocalDetuning(BaseModel): + r"""Specifies the local detuning, defined by the formula + + .. math:: + H_{shift} (t) := -\Delta(t) \sum_k h_k | r_k \rangle \langle r_k | + + where + + :math:`\Delta(t)` is the magnitude of the frequency shift in rad/s, + + :math:`h_k` is the site coefficient, + + :math:`|r_k \rangle` is the Rydberg state of atom k. + + with the sum :math:`\sum_k` taken over all target atoms. + + Attributes: + magnitude: PhysicalField + + Examples: + >>> LocalDetuning(magnitude=PhysicalField) + """ + + magnitude: PhysicalField diff --git a/src/braket/ir/ahs/program_v1.py b/src/braket/ir/ahs/program_v1.py index 5a83935e..af044a79 100644 --- a/src/braket/ir/ahs/program_v1.py +++ b/src/braket/ir/ahs/program_v1.py @@ -43,7 +43,7 @@ class Program(BraketSchemaBase): Examples: >>> Program( ... setup={"ahs_register":AtomArrangement}, - ... hamiltonian={"drivingFields":DrivingField,"shiftingFields":ShiftingField} + ... hamiltonian={"drivingFields":DrivingField,"localDetuning":LocalDetuning} ... ) """ diff --git a/src/braket/ir/ahs/shifting_field.py b/src/braket/ir/ahs/shifting_field.py index 37542f14..cd79172b 100644 --- a/src/braket/ir/ahs/shifting_field.py +++ b/src/braket/ir/ahs/shifting_field.py @@ -11,32 +11,10 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. -from pydantic.v1 import BaseModel +from braket.ir.ahs.local_detuning import LocalDetuning -from braket.ir.ahs.physical_field import PhysicalField - -class ShiftingField(BaseModel): - r"""Specifies the shifting field, defined by the formula - - .. math:: - H_{shift} (t) := -\Delta(t) \sum_k h_k | r_k \rangle \langle r_k | - - where - - :math:`\Delta(t)` is the magnitude of the frequency shift in rad/s, - - :math:`h_k` is the site coefficient, - - :math:`|r_k \rangle` is the Rydberg state of atom k. - - with the sum :math:`\sum_k` taken over all target atoms. - - Attributes: - magnitude: PhysicalField - - Examples: - >>> ShiftingField(magnitude=PhysicalField) - """ - - magnitude: PhysicalField +# The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead. +# This file and class will be removed in a future version. +# We are retaining this now to avoid breaking backwards compatibility for users already utilizing this nomenclature. +ShiftingField = LocalDetuning diff --git a/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py b/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py index 898f0b2c..9bf5bcbb 100644 --- a/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py +++ b/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py @@ -27,46 +27,50 @@ [3.0e-6, 0.0], [3.0e-6, 3.0e-6], [3.0e-6, 6.0e-6], - ], + ], "filling": [1, 1, 1, 1, 0, 0], } } -valid_hamiltonian_input = { - "drivingFields": [ - { - "amplitude": { - "time_series": { - "values": [0.0, 2.51327e7, 2.51327e7, 0.0], - "times": [0.0, 3.0e-7, 2.7e-6, 3.0e-6], +@pytest.fixture(params=[ + "shiftingFields", "localDetuning", +]) +def valid_hamiltonian_input(request): + return { + "drivingFields": [ + { + "amplitude": { + "time_series": { + "values": [0.0, 2.51327e7, 2.51327e7, 0.0], + "times": [0.0, 3.0e-7, 2.7e-6, 3.0e-6], + }, + "pattern": "uniform", + }, + "phase": { + "time_series": {"values": [0, 0], "times": [0.0, 3.0e-6]}, + "pattern": "uniform", }, - "pattern": "uniform", - }, - "phase": { - "time_series": {"values": [0, 0], "times": [0.0, 3.0e-6]}, - "pattern": "uniform", - }, - "detuning": { - "time_series": { - "values": [-1.25664e8, -1.25664e8, 1.25664e8, 1.25664e8], - "times": [0.0, 3.0e-7, 2.7e-6, 3.0e-6], + "detuning": { + "time_series": { + "values": [-1.25664e8, -1.25664e8, 1.25664e8, 1.25664e8], + "times": [0.0, 3.0e-7, 2.7e-6, 3.0e-6], + }, + "pattern": "uniform", }, - "pattern": "uniform", - }, - } - ], - "shiftingFields": [ - { - "magnitude": { - "time_series": {"values": [-1.25664e8, 1.25664e8], "times": [0.0, 3.0e-6]}, - "pattern": [0.5, 1.0, 0.5, 0.5, 0.5, 0.5], } - } - ], -} + ], + request.param: [ + { + "magnitude": { + "time_series": {"values": [-1.25664e8, 1.25664e8], "times": [0.0, 3.0e-6]}, + "pattern": [0.5, 1.0, 0.5, 0.5, 0.5, 0.5], + } + } + ], + } -def test_valid(): +def test_valid(valid_hamiltonian_input): program = Program( setup=valid_setup_input, hamiltonian=valid_hamiltonian_input, @@ -76,7 +80,7 @@ def test_valid(): @pytest.mark.xfail(raises=ValidationError) -def test__missing_setup(): +def test__missing_setup(valid_hamiltonian_input): Program( hamiltonian=valid_hamiltonian_input, ) @@ -89,7 +93,7 @@ def test__missing_hamiltonian(): ) -def test_correct_decimal_serialization(): +def test_correct_decimal_serialization(valid_hamiltonian_input): program = Program( setup=valid_setup_input, hamiltonian=valid_hamiltonian_input, diff --git a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py index db256d92..7f563ff1 100644 --- a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py +++ b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py @@ -26,6 +26,14 @@ def test_valid(): ) assert hamiltonian_field.drivingFields == valid_driving_fields assert hamiltonian_field.shiftingFields == valid_shifting_fields + assert hamiltonian_field.localDetuning == valid_shifting_fields + + hamiltonian_field = Hamiltonian( + drivingFields=valid_driving_fields, localDetuning=valid_shifting_fields + ) + assert hamiltonian_field.drivingFields == valid_driving_fields + assert hamiltonian_field.shiftingFields == valid_shifting_fields + assert hamiltonian_field.localDetuning == valid_shifting_fields @pytest.mark.xfail(raises=ValidationError) @@ -36,7 +44,14 @@ def test__missing_driving_fields(): @pytest.mark.xfail(raises=ValidationError) -def test__missing_shifting_fields(): +def test__missing_driving_fields_with_local_detuning(): + Hamiltonian( + localDetuning=valid_shifting_fields, + ) + + +@pytest.mark.xfail(raises=ValidationError) +def test__missing_local_detuning(): Hamiltonian( drivingFields=valid_driving_fields, ) diff --git a/test/unit_tests/braket/task_result/conftest.py b/test/unit_tests/braket/task_result/conftest.py index 880483d3..0f6b6d4a 100644 --- a/test/unit_tests/braket/task_result/conftest.py +++ b/test/unit_tests/braket/task_result/conftest.py @@ -139,8 +139,10 @@ def blackbird_program(): ) -@pytest.fixture -def ahs_program(): +@pytest.fixture(params=[ + "shiftingFields", "localDetuning" +]) +def ahs_program(request): return AHSProgram( setup={ "ahs_register": { @@ -178,7 +180,7 @@ def ahs_program(): }, } ], - "shiftingFields": [ + request.param: [ { "magnitude": { "time_series": {"values": [-1.25664e8, 1.25664e8], "times": [0.0, 3.0e-6]}, From ab1d30e7c112e3a515491964d00bc00a573972f9 Mon Sep 17 00:00:00 2001 From: Coull Date: Mon, 8 Apr 2024 11:19:44 -0700 Subject: [PATCH 2/6] tox fix --- src/braket/ir/ahs/hamiltonian.py | 9 +++++---- src/braket/ir/ahs/shifting_field.py | 4 ++-- test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py | 12 ++++++++---- test/unit_tests/braket/task_result/conftest.py | 4 +--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/braket/ir/ahs/hamiltonian.py b/src/braket/ir/ahs/hamiltonian.py index ccb6a469..6036662b 100644 --- a/src/braket/ir/ahs/hamiltonian.py +++ b/src/braket/ir/ahs/hamiltonian.py @@ -18,6 +18,7 @@ from braket.ir.ahs.driving_field import DrivingField from braket.ir.ahs.local_detuning import LocalDetuning + class Hamiltonian(BaseModel): """ Specifies the Hamiltonian @@ -34,21 +35,21 @@ class Hamiltonian(BaseModel): drivingFields: List[DrivingField] localDetuning: List[LocalDetuning] = Field(alias="shiftingFields") - + def __getattr__(self, name): if name == "shiftingFields": name = "localDetuning" return self.__dict__[name] - + def __setattr__(self, name, value): if name == "shiftingFields": name = "localDetuning" self.__dict__[name] = value - + def __delattr__(self, name): if name == "shiftingFields": name = "localDetuning" del self.__dict__[name] - + class Config: allow_population_by_field_name = True diff --git a/src/braket/ir/ahs/shifting_field.py b/src/braket/ir/ahs/shifting_field.py index cd79172b..eaffdb20 100644 --- a/src/braket/ir/ahs/shifting_field.py +++ b/src/braket/ir/ahs/shifting_field.py @@ -13,8 +13,8 @@ from braket.ir.ahs.local_detuning import LocalDetuning - # The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead. # This file and class will be removed in a future version. -# We are retaining this now to avoid breaking backwards compatibility for users already utilizing this nomenclature. +# We are retaining this now to avoid breaking backwards compatibility for users already +# utilizing this nomenclature. ShiftingField = LocalDetuning diff --git a/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py b/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py index 9bf5bcbb..d60cd5ad 100644 --- a/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py +++ b/test/unit_tests/braket/ir/ahs/test_ahs_program_v1.py @@ -27,14 +27,18 @@ [3.0e-6, 0.0], [3.0e-6, 3.0e-6], [3.0e-6, 6.0e-6], - ], + ], "filling": [1, 1, 1, 1, 0, 0], } } -@pytest.fixture(params=[ - "shiftingFields", "localDetuning", -]) + +@pytest.fixture( + params=[ + "shiftingFields", + "localDetuning", + ] +) def valid_hamiltonian_input(request): return { "drivingFields": [ diff --git a/test/unit_tests/braket/task_result/conftest.py b/test/unit_tests/braket/task_result/conftest.py index 0f6b6d4a..fde6b717 100644 --- a/test/unit_tests/braket/task_result/conftest.py +++ b/test/unit_tests/braket/task_result/conftest.py @@ -139,9 +139,7 @@ def blackbird_program(): ) -@pytest.fixture(params=[ - "shiftingFields", "localDetuning" -]) +@pytest.fixture(params=["shiftingFields", "localDetuning"]) def ahs_program(request): return AHSProgram( setup={ From 1fe95c91559a118206f86e51935c94d94b652b48 Mon Sep 17 00:00:00 2001 From: Coull Date: Mon, 8 Apr 2024 11:24:06 -0700 Subject: [PATCH 3/6] docs fix --- src/braket/ir/ahs/hamiltonian.py | 10 ++++---- .../braket/ir/ahs/test_hamiltonian.py | 24 ++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/braket/ir/ahs/hamiltonian.py b/src/braket/ir/ahs/hamiltonian.py index 6036662b..f60a6592 100644 --- a/src/braket/ir/ahs/hamiltonian.py +++ b/src/braket/ir/ahs/hamiltonian.py @@ -30,21 +30,21 @@ class Hamiltonian(BaseModel): the effect of which is accurately described by a frequency shift of certain levels. Examples: - >>> Hamiltonian(driving_fields=[DrivingField],local_detuning=[LocalDetuning]) + >>> Hamiltonian(drivingFields=[DrivingField],localDetuning=[LocalDetuning]) """ drivingFields: List[DrivingField] localDetuning: List[LocalDetuning] = Field(alias="shiftingFields") - def __getattr__(self, name): + def __setattr__(self, name, value): if name == "shiftingFields": name = "localDetuning" - return self.__dict__[name] + self.__dict__[name] = value - def __setattr__(self, name, value): + def __getattr__(self, name): if name == "shiftingFields": name = "localDetuning" - self.__dict__[name] = value + return self.__dict__[name] def __delattr__(self, name): if name == "shiftingFields": diff --git a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py index 7f563ff1..41fd8c4c 100644 --- a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py +++ b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py @@ -25,8 +25,8 @@ def test_valid(): drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields ) assert hamiltonian_field.drivingFields == valid_driving_fields - assert hamiltonian_field.shiftingFields == valid_shifting_fields assert hamiltonian_field.localDetuning == valid_shifting_fields + assert hamiltonian_field.shiftingFields == valid_shifting_fields hamiltonian_field = Hamiltonian( drivingFields=valid_driving_fields, localDetuning=valid_shifting_fields @@ -36,6 +36,28 @@ def test_valid(): assert hamiltonian_field.localDetuning == valid_shifting_fields +def test_set(): + hamiltonian_field = Hamiltonian( + drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields + ) + hamiltonian_field.localDetuning = [] + hamiltonian_field.shiftingFields = [] + + +def test_del_shifting_fields(): + hamiltonian_field = Hamiltonian( + drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields + ) + del hamiltonian_field.shiftingFields + + +def test_del_local_detuning(): + hamiltonian_field = Hamiltonian( + drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields + ) + del hamiltonian_field.localDetuning + + @pytest.mark.xfail(raises=ValidationError) def test__missing_driving_fields(): Hamiltonian( From aabdbf273403d84209aaec2917609b0bffd809f6 Mon Sep 17 00:00:00 2001 From: Coull Date: Mon, 8 Apr 2024 12:02:59 -0700 Subject: [PATCH 4/6] tox coverage --- src/braket/ir/ahs/hamiltonian.py | 8 +++----- test/unit_tests/braket/ir/ahs/test_hamiltonian.py | 7 +++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/braket/ir/ahs/hamiltonian.py b/src/braket/ir/ahs/hamiltonian.py index f60a6592..b1c23840 100644 --- a/src/braket/ir/ahs/hamiltonian.py +++ b/src/braket/ir/ahs/hamiltonian.py @@ -36,16 +36,14 @@ class Hamiltonian(BaseModel): drivingFields: List[DrivingField] localDetuning: List[LocalDetuning] = Field(alias="shiftingFields") + def __getattr__(self, name): + return self.__dict__[name] if name != "shiftingFields" else self.__dict__["localDetuning"] + def __setattr__(self, name, value): if name == "shiftingFields": name = "localDetuning" self.__dict__[name] = value - def __getattr__(self, name): - if name == "shiftingFields": - name = "localDetuning" - return self.__dict__[name] - def __delattr__(self, name): if name == "shiftingFields": name = "localDetuning" diff --git a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py index 41fd8c4c..28fd856c 100644 --- a/test/unit_tests/braket/ir/ahs/test_hamiltonian.py +++ b/test/unit_tests/braket/ir/ahs/test_hamiltonian.py @@ -36,6 +36,13 @@ def test_valid(): assert hamiltonian_field.localDetuning == valid_shifting_fields +def test_get_local_detuning(): + hamiltonian_field = Hamiltonian( + drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields + ) + assert hamiltonian_field.localDetuning == valid_shifting_fields + + def test_set(): hamiltonian_field = Hamiltonian( drivingFields=valid_driving_fields, shiftingFields=valid_shifting_fields From bdd9a445dd36bb9d2b92ecf566bbadd1b6d5c0ca Mon Sep 17 00:00:00 2001 From: Abe Coull <85974725+math411@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:25:30 -0700 Subject: [PATCH 5/6] Update shifting_field.py --- src/braket/ir/ahs/shifting_field.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/braket/ir/ahs/shifting_field.py b/src/braket/ir/ahs/shifting_field.py index eaffdb20..1c9d16aa 100644 --- a/src/braket/ir/ahs/shifting_field.py +++ b/src/braket/ir/ahs/shifting_field.py @@ -11,10 +11,12 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. +from warnings import deprecated from braket.ir.ahs.local_detuning import LocalDetuning # The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead. # This file and class will be removed in a future version. # We are retaining this now to avoid breaking backwards compatibility for users already # utilizing this nomenclature. +@deprecated("The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead.") ShiftingField = LocalDetuning From 1782ba4d444634948bb70e3382466c6a423e61be Mon Sep 17 00:00:00 2001 From: Abe Coull <85974725+math411@users.noreply.github.com> Date: Mon, 8 Apr 2024 14:27:40 -0700 Subject: [PATCH 6/6] Update shifting_field.py --- src/braket/ir/ahs/shifting_field.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/braket/ir/ahs/shifting_field.py b/src/braket/ir/ahs/shifting_field.py index 1c9d16aa..eaffdb20 100644 --- a/src/braket/ir/ahs/shifting_field.py +++ b/src/braket/ir/ahs/shifting_field.py @@ -11,12 +11,10 @@ # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. -from warnings import deprecated from braket.ir.ahs.local_detuning import LocalDetuning # The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead. # This file and class will be removed in a future version. # We are retaining this now to avoid breaking backwards compatibility for users already # utilizing this nomenclature. -@deprecated("The class `ShiftingField` is deprecated. Please use `LocalDetuning` instead.") ShiftingField = LocalDetuning