KEMBAR78
Fix `bool` to `int` casting issue in Python by 1tnguyen · Pull Request #1654 · NVIDIA/cuda-quantum · GitHub
Skip to content

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

@github-actions
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

@github-actions
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

github-actions bot pushed a commit that referenced this pull request May 13, 2024
@github-actions
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
@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