amazon-braket-sdk-python icon indicating copy to clipboard operation
amazon-braket-sdk-python copied to clipboard

Support verbatim boxes in `from_ir`

Open speller26 opened this issue 1 year ago • 9 comments

Describe the feature you'd like Right now, verbatim boxes are ignored altogether when translating OpenQASM to Circuit objects via the Circuit.from_ir method:

from braket.circuits import Circuit

oq = """
OPENQASM 3.0;
#pragma braket verbatim
box {
  h $0;
  cnot $0, $1;
}
"""
Circuit.from_ir(oq)
T  : │  0  │  1  │
      ┌───┐       
q0 : ─┤ H ├───●───
      └───┘   │   
            ┌─┴─┐ 
q1 : ───────┤ X ├─
            └───┘ 
T  : │  0  │  1  │

Verbatim boxes need to be supported for faithful translation of OpenQASM, particularly for programs with both verbatim and non-verbatim components.

Note: Implementing this feature will require also expanding the OpenQASM parsing functionality in the default simulator package by adding methods for verbatim box handling to the interpreter and program context.

How would this feature be used? Please describe. As mentioned above, this would allow OpenQASM programs with verbatim boxes to be translated correctly.

Describe alternatives you've considered Entirely verbatim circuits, can be wrapped manually

verbatim_circuit = Circuit().add_verbatim_box(Circuit.from_ir(openqasm))

but this fails for programs with verbatim boxes in between non-verbatim instructions.

speller26 avatar May 09 '24 07:05 speller26

Interested in tackling this. It seems like all the updates for this feature needs to be done at this default simulator repo openqasm_parser.py? https://github.com/amazon-braket/amazon-braket-default-simulator-python/blob/main/src/braket/default_simulator/openqasm/parser/openqasm_parser.py#L71

Hazarre avatar May 29 '25 23:05 Hazarre

The parser already supports box statements. The changes should be made one level up, in the openqasm directory (there should be no need to modify anything in the parser subdirectory.

speller26 avatar May 31 '25 19:05 speller26

There is a Box class as well as a visitBoxStatement() method in openqasm_parser.py. I noticed that in order to wrap the circuit properly, we would need to implement VerbatimBoxStart and VerbatimBoxEnd classes. They do not exist in the amazon-braket-simulator-python repo. Please correct me if I am wrong @speller26 , wouldn't we need an internal Instruction class to call something like Instruction(VerbatimBoxStart(), target=[])? I don't believe such a class exists in the codebase either.
Are we allowed to implement such a class or is it not necessary?

ghost avatar May 31 '25 20:05 ghost

The actual implementation of the box is up to the subclass; as long as there's some abstract method to enclose a block of instructions inside a box while keeping the context that the box belongs to a verbatim pragma, it'll be possible to support verbatim boxes.

The BeginVerbatimBox and EndVerbatimBox instructions are a Braket SDK implementation detail; you could totally implement the verbatim box wrapper to insert both instructions in the appropriate place.

speller26 avatar Jun 10 '25 02:06 speller26

Thank you, it makes sense!

ghost avatar Jun 10 '25 13:06 ghost

Hi @robotastray, - please let me know if you want to continue working on this issue! Happy to review if it'll be ready.

speller26 avatar Jun 10 '25 23:06 speller26

Hello, I encountered some issues after realising that some of the functions I was importing were causing a circular import error. I am now working on integrating the necessary classes into the amazon-braket-default-simulator-python repository. I will continue working on this even after the deadline if I am not able to finish on time. Thank you!

sim-eng-ii avatar Jun 11 '25 11:06 sim-eng-ii

Hello @speller26, I was able to get the code working locally with the test that I added. To do so, I made several adjustments to make sure changes both repositories (amazon-braket-sdk-python and amazon-braket-default-simulator-python were picked up. Once ready I will open a PR and request a review. Thank you.

update at 17:03 BTS: I have created two PRs addressing both repositories before the deadline. I am happy to continue working on this issue if further changes are required, even after UnitaryHack has concluded. Thank you.

sim-eng-ii avatar Jun 11 '25 14:06 sim-eng-ii

@speller26 I updated the PR as per what we discussed, let me know if the changes I made are suitable or I can make further revisions 👍

sim-eng-ii avatar Jun 28 '25 07:06 sim-eng-ii

Completed in #1085

speller26 avatar Aug 21 '25 23:08 speller26