Support verbatim boxes in `from_ir`
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.
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
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.
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?
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.
Thank you, it makes sense!
Hi @robotastray, - please let me know if you want to continue working on this issue! Happy to review if it'll be ready.
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!
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.
@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 👍
Completed in #1085