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 delay and barrier for circuits #993

Conversation

Manvi-Agrawal
Copy link

@Manvi-Agrawal Manvi-Agrawal commented Jun 5, 2024

Issue , if available:

Fixes #974

Testing done:

  • Unit test coverage is 100%.
  • Running this code produces this QASM code
from braket.circuits import Circuit
circ = Circuit().barrier([0, 1, 2])
print(circ.to_ir("OPENQASM").source)

OUTPUT

OPENQASM 3.0;
bit[3] b;
qubit[3] q;
barrier q[0], q[1], q[2];
b[0] = measure q[0];
b[1] = measure q[1];
b[2] = measure q[2];
from braket.circuits import Circuit

circ = Circuit().delay([0, 1, 2], 30)

print(circ.to_ir("OPENQASM").source)

OUTPUT

OPENQASM 3.0;
bit[3] b;
qubit[3] q;
delay[30 s] q[0], q[1], q[2];
b[0] = measure q[0];
b[1] = measure q[1];
b[2] = measure q[2];

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.

@Manvi-Agrawal Manvi-Agrawal requested a review from a team as a code owner June 5, 2024 03:57
@Manvi-Agrawal Manvi-Agrawal changed the title Feature: Add delay and barrier for circuits [Unitary-Hack] Feature: Add delay and barrier for circuits Jun 5, 2024
@Manvi-Agrawal Manvi-Agrawal mentioned this pull request Jun 5, 2024
5 tasks
Copy link
Contributor

@rmshaffer rmshaffer left a comment

Choose a reason for hiding this comment

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

A few comments - also it looks like the unit tests are currently failing in CI, could you take a look and resolve this? Thank you!

src/braket/circuits/gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
src/braket/circuits/gates.py Outdated Show resolved Hide resolved
src/braket/circuits/gates.py Outdated Show resolved Hide resolved
src/braket/circuits/gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
@Manvi-Agrawal Manvi-Agrawal changed the title [Unitary-Hack] Feature: Add delay and barrier for circuits Feature: Add delay and barrier for circuits Jun 5, 2024
@rmshaffer rmshaffer changed the title Feature: Add delay and barrier for circuits feature: Add delay and barrier for circuits Jun 5, 2024
Copy link
Contributor

@jcjaskula-aws jcjaskula-aws left a comment

Choose a reason for hiding this comment

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

Good start. left comments throughout the code. Feel free to ask any question if it does not make sense and give your opinion about the brainstorming parts (don't resolve them though, I'd like other Braket members to think about them).

src/braket/circuits/gates.py Outdated Show resolved Hide resolved
@@ -3847,6 +3847,77 @@ def pulse_gate(
Gate.register_gate(PulseGate)


class Barrier(Gate):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sold yet on whether it should be a Gate or something else like a CompilerDirective.

Copy link
Author

Choose a reason for hiding this comment

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

@jcjaskula-aws , @rmshaffer , did you get a chance to brainstorm if its okay to proceed with Barrier as a Gate?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @jcjaskula-aws - I think CompilerDirective makes the most sense here, since these are not instructions which directly affect the state of the qubits.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Manvi-Agrawal were you able to try converting this to be a CompilerDirective instead of a Gate? Please let us know if you hit any issues with this.

src/braket/circuits/gates.py Outdated Show resolved Hide resolved
src/braket/circuits/gates.py Show resolved Hide resolved
src/braket/circuits/gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
test/unit_tests/braket/circuits/test_gates.py Show resolved Hide resolved
matrices.append(gate.to_matrix())
identity = np.eye(2**gate.qubit_count)
assert np.allclose(functools.reduce(lambda a, b: a @ b, matrices), identity)
if NoMatrixGeneration not in irsubclasses:
Copy link
Contributor

@jcjaskula-aws jcjaskula-aws Jun 5, 2024

Choose a reason for hiding this comment

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

This makes me think that we need to check if it works (or at least returns an understandable error message) with simulators.

test/unit_tests/braket/circuits/test_gates.py Outdated Show resolved Hide resolved
@Manvi-Agrawal
Copy link
Author

Manvi-Agrawal commented Jun 5, 2024

unit tests are currently failing in CI,

Fixed. that was because I renamed a class using refactor->rename but in test framework we need to provide value as strings too.

Copy link

codecov bot commented Jun 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (90f9395) to head (ebc96f0).
Report is 1 commits behind head on feature/delay-barrier-instructions.

Additional details and impacted files
@@                         Coverage Diff                          @@
##           feature/delay-barrier-instructions      #993   +/-   ##
====================================================================
  Coverage                              100.00%   100.00%           
====================================================================
  Files                                     135       136    +1     
  Lines                                    8949      9038   +89     
  Branches                                 2011      2029   +18     
====================================================================
+ Hits                                     8949      9038   +89     

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

Comment on lines +42 to +43
class DurationGate(Gate, Parameterizable):
"""Class `DurationGate` represents a quantum gate that operates on N qubits and a duration."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note, there is quite a bit of duplication between this DurationGate class and the existing AngledGate class. Did you consider refactoring to make a common base class for instructions with a single float parameter? I guess this would also ideally extend to the DoubleAngledGate and TripleAngledGate classes - so maybe outside the scope of this PR and could be tracked by a separate issue.

@@ -3847,6 +3847,77 @@ def pulse_gate(
Gate.register_gate(PulseGate)


class Barrier(Gate):
Copy link
Contributor

Choose a reason for hiding this comment

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

@Manvi-Agrawal were you able to try converting this to be a CompilerDirective instead of a Gate? Please let us know if you hit any issues with this.

@rmshaffer
Copy link
Contributor

Hi @Manvi-Agrawal - we still have 2 days to wrap up unitaryHACK PRs and get them merged. Are you going to be able to address the open comments on this PR? Please let us know so that we know how to proceed with this. Thank you!

@Manvi-Agrawal
Copy link
Author

Hi @rmshaffer , actually I wouldnt be able to get to this issue until a couple of weeks due to personal commitments. I am happy to take a look at the open comments after that, even if it doesn't count as progress towards Unitary Hack, if it's not a time pressing issue.

Thank you for all your help with this issue and review so far. Past week had been a bit crazy due to finals and other stuff for quarter wrap up.

@rmshaffer rmshaffer changed the base branch from main to feature/delay-barrier-instructions June 17, 2024 21:28
@rmshaffer
Copy link
Contributor

@Manvi-Agrawal thank you for all of your work on this PR! We are going to merge the PR into a feature branch and close the unitaryHACK issue to ensure that you get credit for your work. We will then open a new PR to finish the remaining work for merging to main.

@rmshaffer rmshaffer merged commit b1c414a into amazon-braket:feature/delay-barrier-instructions Jun 17, 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.

Support for adding barriers and delays to Circuit
3 participants