From ccd9c3d52546ec2454c848f70e41171f1285a108 Mon Sep 17 00:00:00 2001 From: Shivesh Raja Date: Thu, 26 Nov 2026 23:38:33 +0530 Subject: [PATCH 1/4] Fixed dumping after single-statement unrolling --- src/pyqasm/modules/base.py | 6 +++++- tests/qasm3/test_printer.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pyqasm/modules/base.py b/src/pyqasm/modules/base.py index cafd509f..ca3fc5a9 100644 --- a/src/pyqasm/modules/base.py +++ b/src/pyqasm/modules/base.py @@ -199,6 +199,7 @@ def __init__(self, name: str, program: Program): self._has_barriers: Optional[bool] = None self._validated_program = False self._unrolled_ast = Program(statements=[]) + self._is_unrolled = False self._external_gates: list[str] = [] # declared `opaque` in the source, so unroll() never flushes these self._opaque_gates: set[str] = set() @@ -754,6 +755,8 @@ def unroll(self, **kwargs: Any) -> None: if not kwargs: kwargs = {} + self._is_unrolled = False + try: self.num_qubits, self.num_clbits = 0, 0 if ext_gates := kwargs.get("external_gates"): @@ -764,6 +767,7 @@ def unroll(self, **kwargs: Any) -> None: self._consolidate_qubits = consolidate_qbts visitor = QasmVisitor(module=self, scope_manager=ScopeManager(), **kwargs) self.accept(visitor) + self._is_unrolled = True except (ValidationError, UnrollError) as err: # reset the unrolled ast and qasm self.num_qubits, self.num_clbits = -1, -1 @@ -889,7 +893,7 @@ def __str__(self) -> str: str: The string representation of the module. """ - if len(self._unrolled_ast.statements) > 1: + if self._is_unrolled: return self._qasm_ast_to_str(self.unrolled_ast) return self._qasm_ast_to_str(self.original_program) diff --git a/tests/qasm3/test_printer.py b/tests/qasm3/test_printer.py index a8b6f1e5..32c391df 100644 --- a/tests/qasm3/test_printer.py +++ b/tests/qasm3/test_printer.py @@ -83,3 +83,15 @@ def test_compact_gate_arguments_qasm2(): qasm3_header = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nqubit[1] q;\n' check_unrolled_qasm(module.to_qasm3(as_str=True), qasm3_header + "rx(pi/2) q[0];") assert module.to_qasm3().compact_gate_arguments is True + + +def test_dumps_after_unroll_with_single_statement(): + qasm = "OPENQASM 3.0;\ngate g a { }\nqubit[1] q;\ng q[0];" + expected = "OPENQASM 3.0;\nqubit[1] q;\n" + + module = loads(qasm) + module.unroll() + + assert len(module.unrolled_ast.statements) == 1 + check_unrolled_qasm(dumps(module), expected) + check_unrolled_qasm(str(module), expected) From eaa0d63dd917ff45e1e24b5c89517a7be53d636a Mon Sep 17 00:00:00 2001 From: Shivesh Raja Date: Fri, 27 Nov 2026 00:07:18 +0530 Subject: [PATCH 2/4] Update changelog for single-statement unroll fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e50acb8..5cf2503a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Types of changes: ### Fixed - `unroll()` accepts `inv @` on a custom gate whose body contains `nop`, and keeps each `nop` in the inverted body. Before, it raised `ValidationError: Unsupported / undeclared QASM operation: nop` ([#485](https://github.com/qBraid/pyqasm/pull/485), [#477](https://github.com/qBraid/pyqasm/issues/477)). - `publish-lab-env.yml` files a publish request before it approves the environment. Since qBraid/qbraid-api#1334 the server approves only an environment with an open request, so every run failed at approve with `Environment must have status REQUESTED or PENDING to publish`. The request counts toward the API key owner's publish quota and notifies the platform admins ([#491](https://github.com/qBraid/pyqasm/pull/491), [#490](https://github.com/qBraid/pyqasm/issues/490)). +- Fixed `dumps()` and `str()` returning the original QASM program when unrolling produces exactly one statement. ([#495](https://github.com/qBraid/pyqasm/pull/495)) ### Dependencies From 66663d2718857515870fd5883fb4da29c27d2c1d Mon Sep 17 00:00:00 2001 From: Shivesh Raja Date: Fri, 27 Nov 2026 12:19:15 +0530 Subject: [PATCH 3/4] Fix unrolled AST state handling --- src/pyqasm/modules/base.py | 1 + src/pyqasm/modules/qasm2.py | 3 ++- src/pyqasm/modules/qasm3.py | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pyqasm/modules/base.py b/src/pyqasm/modules/base.py index ca3fc5a9..736bec39 100644 --- a/src/pyqasm/modules/base.py +++ b/src/pyqasm/modules/base.py @@ -406,6 +406,7 @@ def remove_barriers(self, in_place: bool = True) -> Optional["QasmModule"]: curr_module._has_barriers = False curr_module._statements = stmts_without_barriers curr_module._unrolled_ast.statements = stmts_without_barriers + curr_module._is_unrolled = True return curr_module diff --git a/src/pyqasm/modules/qasm2.py b/src/pyqasm/modules/qasm2.py index 3bb366d2..51cb3985 100644 --- a/src/pyqasm/modules/qasm2.py +++ b/src/pyqasm/modules/qasm2.py @@ -227,4 +227,5 @@ def accept(self, visitor: QasmVisitor) -> None: unrolled_stmt_list = visitor.visit_basic_block(self._statements) final_stmt_list = visitor.finalize(unrolled_stmt_list) - self.unrolled_ast.statements = self.finalize(final_stmt_list) # type: ignore[assignment] + if not visitor._check_only: + self.unrolled_ast.statements = self.finalize(final_stmt_list) # type: ignore[assignment] diff --git a/src/pyqasm/modules/qasm3.py b/src/pyqasm/modules/qasm3.py index f9bb0b59..3ff60883 100644 --- a/src/pyqasm/modules/qasm3.py +++ b/src/pyqasm/modules/qasm3.py @@ -162,4 +162,5 @@ def accept(self, visitor: QasmVisitor) -> None: unrolled_stmt_list = visitor.visit_basic_block(self._statements) final_stmt_list = visitor.finalize(unrolled_stmt_list) - self._unrolled_ast.statements = self.finalize(final_stmt_list) # type: ignore[assignment] + if not visitor._check_only: + self._unrolled_ast.statements = self.finalize(final_stmt_list) # type: ignore[assignment] From 4ca97ee16df6345fbc81761ba212117fa8f6da4a Mon Sep 17 00:00:00 2001 From: Shivesh Raja Date: Mon, 30 Nov 2026 19:39:08 +0530 Subject: [PATCH 4/4] Fix string output after barrier removal --- src/pyqasm/modules/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/pyqasm/modules/base.py b/src/pyqasm/modules/base.py index 736bec39..279c99be 100644 --- a/src/pyqasm/modules/base.py +++ b/src/pyqasm/modules/base.py @@ -406,7 +406,6 @@ def remove_barriers(self, in_place: bool = True) -> Optional["QasmModule"]: curr_module._has_barriers = False curr_module._statements = stmts_without_barriers curr_module._unrolled_ast.statements = stmts_without_barriers - curr_module._is_unrolled = True return curr_module @@ -896,7 +895,11 @@ def __str__(self) -> str: if self._is_unrolled: return self._qasm_ast_to_str(self.unrolled_ast) - return self._qasm_ast_to_str(self.original_program) + + program = copy(self.original_program) + program.statements = self._statements + + return self._qasm_ast_to_str(program) def copy(self) -> QasmModule: """Return a deep copy of the module."""