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

feature: Add support for SerializableProgram abstraction to Device interface #976

Merged
merged 157 commits into from
May 21, 2024

Conversation

rmshaffer
Copy link
Contributor

@rmshaffer rmshaffer commented May 13, 2024

Issue #, if available:

Description of changes:
The scenario is that an external library like AutoQASM wants to create program objects which can be executed on Braket devices, including local simulators and AWS devices, but also wants the freedom to return (simulated) results in a format that does not necessarily match the Braket schema for gate model task results.

This change adds a minimal abstraction, SerializableProgram, which represents a program that can be serialized to OpenQASM. It adds support for this in the Device class so that these programs can be run on Braket, while also supporting a local simulator which wants to return a GateModelQuantumTaskResult directly rather than being forced to match the schema of GateModelTaskResult.

For example implementations of this abstraction, see:

Testing done:
Tests added to verify new code and maintain 100% code coverage.

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.

ajberdy and others added 30 commits July 5, 2023 12:03
AutoQASM is an experimental module offering a new quantum-imperative programming experience embedded in Python for developing quantum programs.

Co-authored-by: Lauren Capelluto <107005333+laurencap@users.noreply.github.com>
Co-authored-by: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com>
Co-authored-by: Tim (Yi-Ting) <yitchen@amazon.com>
… programs (#600)

* Move mcm=True argument into device.run

* Add comments explaining mcm=True kwarg
* Begin improving transpiler code coverage

* Code coverage for transpiler to 100%

* Remove unnecessary line

* Improve exception handling and test cases
* Update local simulator commit

* update simulator commit hash in tox

---------

Co-authored-by: Tim <yitchen@amazon.com>
* Enable multiprocessing for aq.function calls

* Add rx gate
* Change: update how num_qubits get supplied by autoqasm functions

* Update README

* Add test for mismatched num_qubits args
* add gates for notebook examples

* autoqasm example notebooks

* update example in readme

* update notebooks and readme

* minor change in mentioning qubit number

* update text

* update text for merge and for comments
* Handle unnamed return values properly

* Combine retval variable names

* Fix handling of recursive function return values

* Fix handling of return values from non-recursive functions

* Move new tests to test_types.py
* Raise error if the user puts a break in their autoqasm loop

* Update src/braket/experimental/autoqasm/converters/break_statements.py

Co-authored-by: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com>

---------

Co-authored-by: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com>
… point function (#661)

* Add error message for using 'return' from the main autoqasm function.

* Respond to CR
* change: improving autoqasm api.py coverage to 100%

* Update src/braket/experimental/autoqasm/api.py

---------

Co-authored-by: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com>
* Improve error message for tuple parameters

* Update src/braket/experimental/autoqasm/types/conversions.py

Co-authored-by: Ryan Shaffer <3620100+rmshaffer@users.noreply.github.com>
Copy link

codecov bot commented May 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (ec5edaf) to head (677d810).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #976   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          135       135           
  Lines         8903      8917   +14     
  Branches      1998      2002    +4     
=========================================
+ Hits          8903      8917   +14     

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

@rmshaffer rmshaffer self-assigned this May 13, 2024
@rmshaffer rmshaffer marked this pull request as ready for review May 14, 2024 17:59
@rmshaffer rmshaffer requested a review from a team as a code owner May 14, 2024 17:59
laurencap
laurencap previously approved these changes May 14, 2024
@yitchen-tim
Copy link
Contributor

It adds support for this in the Device class so that these programs can be run on Braket, while also supporting a local simulator which wants to return a GateModelQuantumTaskResult directly rather than being forced to match the schema of GateModelTaskResult.

If the vision is to support any simulator that can execute OpenQASM, do you think it's worth creating a new result class instead of GateModelQuantumTaskResult? Currently, the MCM simulator writes results in the measurements field of GateModelQuantumTaskResult. Although GateModelQuantumTaskResult does not validate the field type, it does have type hint and doc that does not agree with the value written into the measurements field.

@rmshaffer
Copy link
Contributor Author

If the vision is to support any simulator that can execute OpenQASM, do you think it's worth creating a new result class instead of GateModelQuantumTaskResult? Currently, the MCM simulator writes results in the measurements field of GateModelQuantumTaskResult. Although GateModelQuantumTaskResult does not validate the field type, it does have type hint and doc that does not agree with the value written into the measurements field.

You're right, using the measurements field for arbitrary output values is not really intended. To support arbitrary OpenQASM programs with output variables, eventually we would need a task result object which is more generic than the current GateModelTaskResult and GateModelQuantumTaskResult, both of which assume that the result of a task is a single bitstring. However, doing this correctly would involve defining a new task result schema (in the schemas repo) which I believe is beyond the scope of this PR.

One possible option for now would be to define some kind of generic QuantumTaskResult object which does not have any particular schema. But this feels like it would be a temporary solution and I'd be hesitant to add that now. Curious to get other thoughts on this.

krneta
krneta previously approved these changes May 20, 2024
@rmshaffer rmshaffer dismissed stale reviews from krneta and laurencap via 677d810 May 21, 2024 13:55
@rmshaffer rmshaffer merged commit 7ba54ce into main May 21, 2024
24 checks passed
@rmshaffer rmshaffer deleted the mcm-sim branch May 21, 2024 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants