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

fix: remove shifting field from testing #948

Merged
merged 4 commits into from
Apr 10, 2024
Merged
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/braket/ahs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions src/braket/ahs/analog_hamiltonian_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
AtomArrangement,
DiscretizationError,
DrivingField,
ShiftingField,
LocalDetuning,
SiteType,
)
from braket.ahs.atom_arrangement import AtomArrangementItem
Expand Down Expand Up @@ -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]),
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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():
Expand All @@ -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)

Expand All @@ -116,16 +116,16 @@ 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)


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,
Expand All @@ -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())
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def additional_metadata():
},
}
],
"shiftingFields": [
"localDetuning": [
{
"magnitude": {
"time_series": {
Expand Down