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

Implement braket.ahs.AnalogHamiltonianSimulation.from_ir() #983

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
61 changes: 61 additions & 0 deletions src/braket/ahs/analog_hamiltonian_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@
from __future__ import annotations

from collections import defaultdict
from decimal import Decimal
from functools import singledispatch
from typing import Union

import braket.ir.ahs as ir
from braket.ahs.atom_arrangement import AtomArrangement, SiteType
from braket.ahs.discretization_types import DiscretizationError, DiscretizationProperties
from braket.ahs.driving_field import DrivingField
from braket.ahs.field import Field
from braket.ahs.hamiltonian import Hamiltonian
from braket.ahs.local_detuning import LocalDetuning
from braket.device_schema import DeviceActionType
from braket.timings.time_series import TimeSeries


class AnalogHamiltonianSimulation:
Expand All @@ -49,6 +53,63 @@ def hamiltonian(self) -> Hamiltonian:
"""Hamiltonian: The hamiltonian to simulate."""
return self._hamiltonian

@staticmethod
def from_ir(source: ir.Program) -> AnalogHamiltonianSimulation:
"""Converts the canonical intermediate representation into
the Analog Hamiltonian Simulation.

Args:
source (ir.Program): The IR representation of the circuit.

Returns:
AnalogHamiltonianSimulation: The Analog Hamiltonian Simulation.
"""
atom_arrangement = AtomArrangement()
for site, fill in zip(source.setup.ahs_register.sites, source.setup.ahs_register.filling):
atom_arrangement.add(
coordinate=site, site_type=SiteType.FILLED if fill == 1 else SiteType.VACANT
)
hamiltonian = Hamiltonian()
for term in source.hamiltonian.drivingFields:
amplitude = AnalogHamiltonianSimulation._field_or_time_series_from_ir(
times=term.amplitude.time_series.times,
values=term.amplitude.time_series.values,
pattern=term.amplitude.pattern,
)
phase = AnalogHamiltonianSimulation._field_or_time_series_from_ir(
times=term.phase.time_series.times,
values=term.phase.time_series.values,
pattern=term.phase.pattern,
)
detuning = AnalogHamiltonianSimulation._field_or_time_series_from_ir(
times=term.detuning.time_series.times,
values=term.detuning.time_series.values,
pattern=term.detuning.pattern,
)
hamiltonian += DrivingField(
amplitude=amplitude,
phase=phase,
detuning=detuning,
)
for term in source.hamiltonian.localDetuning:
hamiltonian += LocalDetuning.from_lists(
times=term.magnitude.time_series.times,
values=term.magnitude.time_series.values,
pattern=term.magnitude.pattern,
)
return AnalogHamiltonianSimulation(
register=atom_arrangement,
hamiltonian=hamiltonian,
)

@staticmethod
def _field_or_time_series_from_ir(
times: list[Decimal], values: list[Decimal], pattern: Union[str, list[Decimal]]
) -> Union[Field, TimeSeries]:
if not isinstance(pattern, str):
return Field.from_lists(times=times, values=values, pattern=pattern)
return TimeSeries.from_lists(times, values)

def to_ir(self) -> ir.Program:
"""Converts the Analog Hamiltonian Simulation into the canonical intermediate
representation.
Expand Down
27 changes: 27 additions & 0 deletions src/braket/ahs/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,30 @@ def discretize(
discretized_pattern = self.pattern.discretize(pattern_resolution)
discretized_field = Field(time_series=discretized_time_series, pattern=discretized_pattern)
return discretized_field

@staticmethod
def from_lists(times: list[Decimal], values: list[Decimal], pattern: list[Decimal]) -> Field:
"""Builds Field from lists of time points, values and pattern.

Args:
times (list[Decimal]): The time points of the field
values (list[Decimal]): The values of the field
pattern (list[Decimal]): The pattern of the field

Raises:
ValueError: If the length of times and values differs.

Returns:
Field: Field.
"""
if not (len(times) == len(values)):
raise ValueError(
f"The lengths of the lists for times({len(times)}) and values({len(values)})\
are not equal"
)

time_series = TimeSeries.from_lists(times=times, values=values)

drive = Field(time_series=time_series, pattern=Pattern(pattern))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name the variable field instead of drive to be more descriptive.


return drive
166 changes: 166 additions & 0 deletions test/unit_tests/braket/ahs/test_analog_hamiltonian_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,140 @@ def local_detuning():
)


@pytest.fixture
def ir():
return Program.parse_raw_schema(
"""
{
"braketSchemaHeader": {
"name": "braket.ir.ahs.program",
"version": "1"
},
"setup": {
"ahs_register": {
"sites": [
[
"0.0",
"0.0"
],
[
"0.0",
"0.000003"
],
[
"0.0",
"0.000006"
],
[
"0.000003",
"0.0"
],
[
"0.000003",
"0.000003"
],
[
"0.000003",
"0.000003"
],
[
"0.000003",
"0.000006"
]
],
"filling": [
1,
1,
1,
1,
1,
0,
0
]
}
},
"hamiltonian": {
"drivingFields": [
{
"amplitude": {
"time_series": {
"values": [
"0.0",
"25132700.0",
"25132700.0",
"0.0"
],
"times": [
"0.0",
"3E-7",
"0.0000027",
"0.000003"
]
},
"pattern": "uniform"
},
"phase": {
"time_series": {
"values": [
"0",
"0"
],
"times": [
"0.0",
"0.000003"
]
},
"pattern": "uniform"
},
"detuning": {
"time_series": {
"values": [
"-125664000.0",
"-125664000.0",
"125664000.0",
"125664000.0"
],
"times": [
"0.0",
"3E-7",
"0.0000027",
"0.000003"
]
},
"pattern": "uniform"
}
}
],
"localDetuning": [
{
"magnitude": {
"time_series": {
"values": [
"-125664000.0",
"125664000.0"
],
"times": [
"0.0",
"0.000003"
]
},
"pattern": [
"0.5",
"1.0",
"0.5",
"0.5",
"0.5",
"0.5"
]
}
}
]
}
}
"""
)


def test_create():
mock0 = Mock()
mock1 = Mock()
Expand All @@ -95,6 +229,38 @@ def test_to_ir_empty():
assert problem == Program.parse_raw_schema(problem.json())


def test_from_ir(ir):
problem = AnalogHamiltonianSimulation.from_ir(ir).to_ir()
assert problem == ir
assert problem == Program.parse_raw_schema(problem.json())


def test_from_ir_empty():
empty_ir = Program.parse_raw_schema(
"""
{
"braketSchemaHeader": {
"name": "braket.ir.ahs.program",
"version": "1"
},
"setup": {
"ahs_register": {
"sites": [],
"filling": []
}
},
"hamiltonian": {
"drivingFields": [],
"localDetuning": []
}
}
"""
)
problem = AnalogHamiltonianSimulation.from_ir(empty_ir).to_ir()
assert problem == empty_ir
assert problem == Program.parse_raw_schema(problem.json())


@pytest.mark.xfail(raises=TypeError)
def test_to_ir_invalid_hamiltonian(register):
hamiltonian = Mock()
Expand Down
20 changes: 20 additions & 0 deletions test/unit_tests/braket/ahs/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,23 @@ def test_uniform_field(
) or expected.pattern.series == actual.pattern.series
assert expected.time_series.times() == actual.time_series.times()
assert expected.time_series.values() == actual.time_series.values()


def test_from_lists():
times = [0, 0.1, 0.2, 0.3]
values = [0.5, 0.8, 0.9, 1.0]
pattern = [0.3, 0.7, 0.6, -0.5, 0, 1.6]

sh_field = Field.from_lists(times, values, pattern)
assert sh_field.time_series.times() == times
assert sh_field.time_series.values() == values
assert sh_field.pattern.series == pattern


@pytest.mark.xfail(raises=ValueError)
def test_from_lists_not_eq_length():
times = [0, 0.1, 0.2]
values = [0.5, 0.8, 0.9, 1.0]
pattern = [0.3, 0.7, 0.6, -0.5, 0, 1.6]

Field.from_lists(times, values, pattern)