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

Conversation

king-p3nguin
Copy link
Contributor

Issue #, if available:

Description of changes:

  • braket.ahs.AnalogHamiltonianSimulation.from_ir() was implemented.

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

  • I have added tests that prove my fix is effective or that my feature works (if appropriate)
  • I have checked that my tests are not configured for a specific region or account (if appropriate)

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

@king-p3nguin king-p3nguin requested a review from a team as a code owner June 2, 2024 17:41
@peterkomar-aws peterkomar-aws self-requested a review June 5, 2024 16:22
Copy link
Contributor

@peterkomar-aws peterkomar-aws left a comment

Choose a reason for hiding this comment

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

Very precise work, thank you for contributing this PR. I highlighted few changes that would make this ready for approval.

def test_from_ir(ir):
ahs = AnalogHamiltonianSimulation.from_ir(ir)
problem = ahs.to_ir()
assert Program.parse_raw(problem.json()) == problem
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of comparing the returned value of program with Program.parse_raw(program.json()), which tests the functionality of parse_raw and .json, it will be more reliable to compare AnalogHamiltonianSimulation.from_ir(ir).to_ir() with ir.

)
)
problem = ahs.to_ir()
assert Program.parse_raw(problem.json()) == problem
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of comparing the returned value of program with Program.parse_raw(program.json()), which tests the functionality of parse_raw and .json, it will be more reliable to compare AnalogHamiltonianSimulation.from_ir(ir).to_ir() with ir.

hamiltonian = Hamiltonian()
for term in source.hamiltonian.drivingFields:
amplitude = (
Field(
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic to construct this Field is repeated four times. It looks like there is a good need for adding a factory method to Field. Please create that, and use it here to make the code more maintainable.

@king-p3nguin
Copy link
Contributor Author

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

peterkomar-aws
peterkomar-aws previously approved these changes Jun 6, 2024
Copy link
Contributor

@peterkomar-aws peterkomar-aws left a comment

Choose a reason for hiding this comment

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

Only one minor suggestions. Looks good to me. Thanks for the quick turnaround. Great work!


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.

Copy link

codecov bot commented Jun 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (d5dfbf4) to head (8ccd0ac).
Report is 43 commits behind head on main.

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@peterkomar-aws
Copy link
Contributor

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
Copy link
Contributor Author

@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.

Copy link
Contributor

@shpface shpface left a comment

Choose a reason for hiding this comment

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

Nice work. Thanks for the contribution.

@peterkomar-aws peterkomar-aws merged commit 2f02107 into amazon-braket:main Jun 6, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants