Skip to content

Fixed dumping after single-statement unrolling (#493) - #495

Open
ShiveshDragon wants to merge 4 commits into
qBraid:mainfrom
ShiveshDragon:fix-dumps-single-statement
Open

ShiveshDragon wants to merge 4 commits into
qBraid:mainfrom
ShiveshDragon:fix-dumps-single-statement

Conversation

@ShiveshDragon

Copy link
Copy Markdown

Summary

Fixes #493

Previously, QasmModule.str() used the count of statements in the unrolled AST to find if unrolling had occurred or not.

This has caused incorrect output when unrolling gave zero or one statement, because the method returned the original program instead of giving the unrolled AST.

The changes done are as follows:

  • Introduces an _is_unrolled flag to see if unrolling has occured or not.
  • The flag gets undated during unroll() .
  • We use the flag in str() to get the correct AST.
  • Also added a regression test covering a program whose unrolled AST with only one statement.

Testing

  • pytest tests/qasm3/test_printer.py -v — 16 passed.

@argus-eye

argus-eye Bot commented Nov 26, 2026

Copy link
Copy Markdown

Argus review

Auto-review is off for this repo. Tick the box below to run a review on this PR.

  • Trigger Argus review

Estimated cost

  • Files changed: 2
  • Diff lines (±): 18
  • Historical avg: ~337.1k tokens · ~$1.17 · across last 18 review(s)

Tip: you can also comment @argus-eye review at any time.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread src/pyqasm/modules/base.py Outdated
curr_module._has_barriers = False
curr_module._statements = stmts_without_barriers
curr_module._unrolled_ast.statements = stmts_without_barriers
curr_module._is_unrolled = True

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 unrolled? Barriers could be removed from a program even if it is not unrolled

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.

I added that flag because remove_barriers() updates _unrolled_ast, but str() uses original program when _is_unrolled is False. This has caused the removed barriers to still appear in output.

I set the flag to True to make sure that the updated AST was used. However, I understand your point . The removal of barriers doesn't necessarily mean the program has been unrolled. I'll try to find better way to handle this issue.

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 .

@TheGupta2012

Copy link
Copy Markdown
Member

Hi @ShiveshDragon , thanks for contributing to the project and fixing #493 ! I wanted to discuss about whether the self._unrolled_ast is an attribute which can be used for this checking? Adding self._is_unrolled seems redundant if an existing attribute can be used for fixing this bug.

@ShiveshDragon

ShiveshDragon commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thank you for taking the time to review my changes and for the guidance. And I have tried comparing _unrolled_ast before and after unrolling, but the AST might remain the same if the program doesn't have anything that needs expansion. So an unchanged AST wouldn't necessarily mean that unrolling hasn't occurred. That's why I thought of using an explicit flag would be more reliable . I would be happy to hear your suggestions.

@ShiveshDragon

ShiveshDragon commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

I have updated the implementation so that remove_barriers() doesn't change _is_unrolled . Instead of that, when the module hasn't been unrolled, str() creates a shallow copy of original_program and replaces its statements with the current _statements. I have used shallow copy and not deep copy to save memory .

As for the issue of redundancy of the flag _is_unrolled , I think its better to keep it just in case the unroll attempt is unsuccessful and instead of showing False at the start , it could show True from previous successful attempt because Resetting didn't reset the flag.

If there are any other ways to correct it , please guide me .

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dumps() prints the original program when unroll() leaves at most one statement

3 participants