diff --git a/src/braket/experimental/autoqasm/program/program.py b/src/braket/experimental/autoqasm/program/program.py index 171e196c7..839a971b2 100644 --- a/src/braket/experimental/autoqasm/program/program.py +++ b/src/braket/experimental/autoqasm/program/program.py @@ -480,8 +480,12 @@ def verbatim_block(self) -> None: """Sets the program conversion context into a verbatim block context. Raises: - errors.VerbatimBlockNotAllowed: If the target device does not support verbatim blocks. + errors.VerbatimBlockNotAllowed: If a verbatim block is not allowed at this point in + the program; for example, if the target device does not support verbatim blocks. """ + if self._in_verbatim: + raise errors.VerbatimBlockNotAllowed("Verbatim blocks cannot be nested.") + device = self.get_target_device() if ( device diff --git a/test/unit_tests/braket/experimental/autoqasm/test_pragmas.py b/test/unit_tests/braket/experimental/autoqasm/test_pragmas.py index 240a920f8..49d326ff3 100644 --- a/test/unit_tests/braket/experimental/autoqasm/test_pragmas.py +++ b/test/unit_tests/braket/experimental/autoqasm/test_pragmas.py @@ -41,6 +41,17 @@ def program_func() -> None: assert program_func().to_ir() == expected +def test_nested_verbatim_box() -> None: + @aq.main + def program_func() -> None: + with aq.verbatim(): + with aq.verbatim(): + h(0) + + with pytest.raises(errors.VerbatimBlockNotAllowed): + program_func() + + def test_verbatim_box_invalid_target_qubit() -> None: @aq.main def program_func() -> None: