Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions src/pyqasm/modules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -754,6 +755,8 @@ def unroll(self, **kwargs: Any) -> None:
if not kwargs:
kwargs = {}

self._is_unrolled = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? Wouldn't we already have this attribute as False if we are doing unrolling for the first time?

@ShiveshDragon ShiveshDragon Nov 30, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct about that but it is redundant only during the first unroll, since the flag is already False initially. I added it so that it can handle the next unroll attempts, where the flag might still be True from previous successful unroll. Resetting it to False will ensure that a failed attempt doesn't leave the module incorrectly marked as unrolled. The Error Handler resets the AST but not the flag _is_unrolled which stays True because of previous successful attempt. But i will make sure to check the intended use and change it if necessary .


try:
self.num_qubits, self.num_clbits = 0, 0
if ext_gates := kwargs.get("external_gates"):
Expand All @@ -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
Expand Down Expand Up @@ -889,9 +893,13 @@ 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)

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."""
Expand Down
3 changes: 2 additions & 1 deletion src/pyqasm/modules/qasm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
3 changes: 2 additions & 1 deletion src/pyqasm/modules/qasm3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
12 changes: 12 additions & 0 deletions tests/qasm3/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading