Skip to content

Feature/Added explicit casting support#205

Merged
TheGupta2012 merged 9 commits intoqBraid:mainfrom
vinayswamik:feature/add-explicit-casting-support
Jun 14, 2025
Merged

Feature/Added explicit casting support#205
TheGupta2012 merged 9 commits intoqBraid:mainfrom
vinayswamik:feature/add-explicit-casting-support

Conversation

@vinayswamik
Copy link
Copy Markdown
Collaborator

Summary of changes

This PR adds initial support for Explicit Casting in both QasmVisitor and Qasm3ExprEvaluator, in accordance with the OpenQASM 3 casting rules.

With this change, we introduce logic to:

  • Convert values by reusing the existing implicit‐casting mechanism, then return the correctly cast result.
  • Validate declaration vs. explicit cast, ensuring that the target type and size exactly match the variable’s original declaration; otherwise emits a ValidationError.
  • Extended the test suite to cover all newly legal explicit‐cast scenarios as well as illegal‐cast error cases.

Closes #10

Added new test cases for explicit casting
Added explicit casting logic in evaluate_expression function
Added explicit casting logic flow and cleanup the repeated code
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 11, 2025

Codecov Report

Attention: Patch coverage is 97.05882% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pyqasm/expressions.py 95.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!


return openqasm_bits

def _check_type_size(
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.

This can be moved to the Qasm3Validator class as it has minimal dependency on the visitor and is only validating the size

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we can do that, but the problem is circular import.
because, _check_type_size is in expressions.py, which is trying to import Qasm3Validator from validator.py, But validator.py is also importing from expressions.py before it's fully loaded.

one simple way is, local import inside the function. can i do that..?

Copy link
Copy Markdown
Member

@TheGupta2012 TheGupta2012 Jun 13, 2025

Choose a reason for hiding this comment

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

Local imports for circumventing circular imports aren't really good practice. Usually it is possible to find a way to re-factor code to avoid circular imports.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah, instead we can move them to expressions.py as a static methods. can i do that...?

return base_size

# pylint: disable-next=too-many-arguments
def _check_type_for_cast(
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.

Similar comment as above, can be moved to the validator class

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

same as above, because _check_type_for_cast is calling _check_type_size. we can place a local import inside _check_type_for_cast to avoid circular import problem.

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.

It'll be great if you can find a way to resolve this (and above) before Monday , June 16th (when the final submissions for unitary hack are due). But since it is a code re-factor issue, and not related to core functionality, we can tackle it later as well in a separate issue.

Copy link
Copy Markdown
Member

@TheGupta2012 TheGupta2012 left a comment

Choose a reason for hiding this comment

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

Hi @vinayswamik , thank you for the great work! I have left some comments regarding organization of the code but overall it looks good.

Can you please also add a note in the CHANGELOG and an entry for the Cast in pyqasm supported instructions ?

@vinayswamik vinayswamik deleted the feature/add-explicit-casting-support branch June 13, 2025 03:13
@vinayswamik vinayswamik restored the feature/add-explicit-casting-support branch June 13, 2025 03:13
@TheGupta2012
Copy link
Copy Markdown
Member

Hi @vinayswamik , any reasons why you closed the PR?

@vinayswamik vinayswamik reopened this Jun 13, 2025
@vinayswamik
Copy link
Copy Markdown
Collaborator Author

Hi @vinayswamik , any reasons why you closed the PR?

oops!, sorry that was by mistake, i deleted wrong branch from my side, so its auto closed. Now i reopened it.

Added Cast to pyqasm supported operations
Performed minor cleanups to improve readability and maintainability.
Performed minor cleanups to improve readability and maintainability.
Added new and all possible test cases.
@vinayswamik vinayswamik requested a review from TheGupta2012 June 14, 2025 02:42
Copy link
Copy Markdown
Member

@TheGupta2012 TheGupta2012 left a comment

Choose a reason for hiding this comment

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

Thank you @vinayswamik for the changes, LGTM!

@TheGupta2012 TheGupta2012 merged commit 42bae60 into qBraid:main Jun 14, 2025
20 checks passed
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.

[FEATURE] Add support for explicit casting

3 participants