amazon-braket / amazon-braket-sdk-python

A Python SDK for interacting with quantum devices on Amazon Braket
https://aws.amazon.com/braket/
Apache License 2.0
294 stars 118 forks source link

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

Closed king-p3nguin closed 1 month ago

king-p3nguin commented 1 month ago

Issue #, if available:

Description of changes:

Testing done:

Input:

from braket.timings.time_series import TimeSeries
from braket.ahs.analog_hamiltonian_simulation import AnalogHamiltonianSimulation
from braket.ahs.atom_arrangement import AtomArrangement
from braket.ahs.atom_arrangement import SiteType
from braket.ahs.driving_field import DrivingField
from braket.ahs.local_detuning import LocalDetuning
from braket.ahs.field import Field
from braket.ahs.pattern import Pattern

register = (
    AtomArrangement()
    .add((0.0, 0.0))
    .add((0.0, 3.0e-6))
    .add((0.0, 6.0e-6))
    .add((3.0e-6, 0.0))
    .add((3.0e-6, 3.0e-6))
    .add((3.0e-6, 3.0e-6), SiteType.VACANT)
    .add((3.0e-6, 6.0e-6), SiteType.VACANT)
)

driving_field = DrivingField(
    TimeSeries().put(0.0, 0.0).put(3.0e-7, 2.51327e7).put(2.7e-6, 2.51327e7).put(3.0e-6, 0.0),
    TimeSeries().put(0.0, 0).put(3.0e-6, 0),
    TimeSeries()
    .put(0.0, -1.25664e8)
    .put(3.0e-7, -1.25664e8)
    .put(2.7e-6, 1.25664e8)
    .put(3.0e-6, 1.25664e8),
)

local_detuning = 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]),
    )
)

hamiltonian = driving_field + local_detuning
ahs = AnalogHamiltonianSimulation(register=register, hamiltonian=hamiltonian)

print(ahs.to_ir() == AnalogHamiltonianSimulation.from_ir(ahs.to_ir()).to_ir())

Output:

True

Merge Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your pull request.

General

Tests

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

king-p3nguin commented 1 month ago

@peterkomar-aws Thank you for reviewing! I made the change based on your review.

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (d5dfbf4) to head (8ccd0ac).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #983 +/- ## ========================================= Coverage 100.00% 100.00% ========================================= Files 135 135 Lines 8919 8941 +22 Branches 2002 2008 +6 ========================================= + Hits 8919 8941 +22 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

peterkomar-aws commented 1 month ago

Hi @king-p3nguin , all tests are passing, there is only a minor test coverage degradation (see the coverage report). Can you add a test that covers L110 of the analog_hamiltonian_simulation module?

king-p3nguin commented 1 month ago

@peterkomar-aws Thank you for reporting. It seems like the pattern in DrivingField is always "uniform", so I removed the static method _field_or_time_series_from_ir() and just set TimeSeries for the DrivingField. Let me know if it is okay or not.