2025-08-15
·Adding Verbatim Box Support to from_ir() in Amazon Braket Python SDK
·Quantum Computing
·5 min read
1. About Amazon Braket Python SDK
Amazon Braket Software Development Kit (SDK) is an open source python library that allows users to interact with quantum processing units (QPUs). Currently available devices include:
- IonQ
- IQM
- QuEra
- Rigetti
The SDK also includes quantum simulators available for debugging and running circuits in parallel, reducing the runtime of your quantum programs.
2. About OpenQASM
OpenQASM provides an intermediate representation enabling communication with quantum hardware via a compiler. The latest version, 3.0, extends earlier versions by introducing features such as gate timing, expanded classical logic (including control flow and new data types), and integrated pulse-level definitions.
This contribution adds support to the Circuit.from_ir() method in the Amazon Braket SDK to translate OpenQASM 3.0 programs without ignoring verbatim boxes.
2.1 OpenQASM Verbatim Pragma
When we refer to verbatim in the context of OpenQASM programs, we mean a circuit or subcircuits that the compiler must execute exactly as given, without modification. Verbatim compilation is a feature available in Rigetti and IonQ, albeit the latter allows verbatim compilation only for the whole circuit. In OpenQASM, the verbatim pragma is defined in a Box{} block of code preceded by the #pragma braket verbatim directive.
For example
OPENQASM 3;
bit[2] c;
#pragma braket verbatim
box{
rx(0.314159) $0;
rz(0.628318) $0, $1;
cz $0, $1;
}
c[0] = measure $0;
c[1] = measure $1;
3. Adding Verbatim Support to Circuit.from_ir() in Amazon Braket SDK
Issue #972 requests support for faithful translation of OpenQASM 3.0 verbatim circuits. This contribution addresses this issue by modifying both the Amazon Braket Python SDK and the Amazon Braket Default Simulator.
The from_ir() method is defined in circuit.py. The function takes two input arguments (i.e. OpenQASM string, and Inputs to the circuit) and returns a Braket Circuit implementing the OpenQASM program via
Interpreter(BraketProgramContext()).build_circuit(
source=source,
inputs=inputs,
is_file=False
)
The .build_circuit() method called on line 1275 refers to the Interpreter class method imported from default_simulator.openqasm.interpreter. interpreter.py is the file in which the main changes were applied. Examining the run() function called in build_circuit(), we see that self.visit() is the relevant method.
The @visit.register decorator is used to call the appropriate methods routed by the self.visit() dispatcher function. When the command starts with braket verbatim, the system sets self.context.in_verbatim_box = True.
The self.in_verbatim_box flag, with a default value of False, is defined in the __init__(self) function of the AbstractProgramContext() class in program_context.py.
If the verbatim pragma directive is not followed immediately by a Box{} statement, a ValueError is raised via _(self, node: Program) -> None.
raise ValueError('braket verbatim pragma must be followed by a box statement')
However, if the OpenQASM verbatim program syntax is correctly provided, the following function is called.
@visit.register
def _(self, node: Box) -> None:
if self.context.in_verbatim_box:
self.context.add_verbatim_marker(VerbatimBoxDelimiter.START_VERBATIM)
for instr_node in node.body:
self.visit(instr_node)
self.context.add_verbatim_marker(VerbatimBoxDelimiter.END_VERBATIM)
self.context.in_verbatim_box = False
else:
for instr_node in node.body:
self.visit(instr_node)
When the in_verbatim_box flag is set to true, verbatim markers are added to outline the start and end of the verbatim section, and upon completion, the flag is reset to its initial value. Otherwise, the standard path
for instr_node in node.body:
self.visit(instr_node)
is followed.
The Box class was already present in the openqasm_ast; therefore, no changes were required in this file.
@dataclass
class Box(QuantumStatement):
"""
Timing box
Example::
box[maxdur] {
delay[start_stretch] $0
x $0
}
"""
The add_verbatim_marker function is a method of the AbstractProgramContext that takes a marker as an argument, which in this case is a VerbatimBoxDelimiter class.
class VerbatimBoxDelimiter(str, enum):
START_VERBATIM = 'StartVerbatim'
END_VERBATIM = 'EndVerbatim'
@property
def qubit_count(self) -> int:
return 0
3.1 Technical Issues & Resolution
Initially there was a timing issue whereby the in_verbatim_box flag was checked in the condition before it was set.
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
=================================== FAILURES ===================================
def test_verbatim_wo_box():
qasm_without_box = """
OPENQASM 3.0;
#pragma braket verbatim
h $0;
"""
with pytest.raises(ValueError, match="braket verbatim pragma must be followed by a box statement"):
> Interpreter().run(qasm_without_box)
E Failed: DID NOT RAISE <class 'ValueError'>
test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py:2303: Failed
=========================== short test summary info ============================
FAILED test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py::test_verbatim_wo_box - Failed: DID NOT RAISE <class 'ValueError'>
============================== 1 failed in 7.08s ===============================
Consequently, the ValueError was not raised when the pragma directive was not followed by Box{}.
To fix this issue, the condition needed to check if the command starts with “pragma verbatim” and if the next statement is a Box. If not, a ValueError is raised.
@visit.register
def _(self, node: Program) -> None:
for i, stmt in enumerate(node.statements):
if (
isinstance(stmt, Pragma)
and stmt.command.startswith("braket verbatim")
):
if i + 1 < len(node.statements) and not isinstance(node.statements[i + 1], Box):
raise ValueError("braket verbatim pragma must be followed by a box statement")
self.visit(node.statements)
4. Testing
Tests were written to verify the feature behaved as expected in both the Amazon Braket SDK and the Amazon Braket Default Python Simulator.
4.1. Testing Amazon Braket SDK
test_from_ir_with_verbatim_box()test_from_ir_with_mixed_verbatim_non_verbatim_instr()test_add_start_marker()test_add_end_marker()test_add_verbatim_invalid_marker()
4.2. Testing Amazon Braket Default Python Simulator
test_verbatim_box_start()test_verbatim_box_end()test_verbatim_box()test_verbatim_wo_box()
5. Conclusions
Issue #972 was successfully resolved through PRs #1085 and #285, allowing for correct translation of OpenQASM programs containing verbatim circuits.
References
[1] Amazon Braket Python SDK, https://aws.amazon.com/braket/getting-started/
[2] OpenQASM Live Specification, https://openqasm.com/index.html
Topics
- Quantum Computing
- Amazon Braket
- Verbatim Circuits
- OpenQASM Program with Verbatim Boxes
Tech Stack
- Python
- Amazon Braket