Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bool to int casting issue in Python #1654

Merged
merged 8 commits into from
May 13, 2024

Conversation

1tnguyen
Copy link
Collaborator

Description

When we need to check if var == 1, where var is a bool type and 1 is an integer type, we need to do an unsigned extension of i1 since a signed extension of bool True will result in -1 rather than 1.

Also, add the type casting logic to NotEq as well since it should be equivalent to Eq.

Resolve: #1620

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request May 10, 2024
Comment on lines +2723 to +2725
if F64Type.isinstance(left.type) and IntegerType.isinstance(
comparator.type):
left = arith.FPToSIOp(comparator.type, left).result
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not following why the LHS, if it's a floating point value, is integer truncated here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is existing code from the isinstance(op, ast.Eq) below, which, in principle, should also be handled in the ast.NotEq case here.

I don't know the context of it so didn't change it or migrate it to cc.CastOp yet.
@khalatepradnya Do you know the intent of this float -> int conversion in equal check?

Copy link
Collaborator

Choose a reason for hiding this comment

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

nope, sorry. Tagging @amccaskey for details.
I see that the comparator is of integer type and that the integer comparator is being used (arith.cmpi) on line#2740

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if this isn't a bug. Consider this python code

>>> a=1
>>> b=1.2
>>> c= b==a
>>> print(c)
False

python/cudaq/kernel/ast_bridge.py Outdated Show resolved Hide resolved
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request May 10, 2024
Copy link
Collaborator

@schweitzpgi schweitzpgi left a comment

Choose a reason for hiding this comment

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

LGTM

python/cudaq/kernel/ast_bridge.py Outdated Show resolved Hide resolved
python/cudaq/kernel/ast_bridge.py Outdated Show resolved Hide resolved
github-actions bot pushed a commit that referenced this pull request May 13, 2024
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@1tnguyen 1tnguyen merged commit 7ae6830 into NVIDIA:main May 13, 2024
127 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 13, 2024
@bettinaheim bettinaheim changed the title [Python AST] Always perform unsigned extension for i1 (bool) type when doing integer compare Fix bool to int casting issue in Python Jul 29, 2024
@bettinaheim bettinaheim added the bug fix To be listed under Bug Fixes in the release notes label Jul 29, 2024
@bettinaheim bettinaheim added this to the release 0.8.0 milestone Jul 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug fix To be listed under Bug Fixes in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Python Kernel] bool to int type casting issue
4 participants