cuda-quantum icon indicating copy to clipboard operation
cuda-quantum copied to clipboard

While loop within kernel fails if there are two conditions

Open mawolf2023 opened this issue 10 months ago • 4 comments

Required prerequisites

  • [x] Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • [x] Make sure you've read the documentation. Your issue may be addressed there.
  • [x] Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • [x] If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

If you use a while loop inside a kernel with two conditions, the code fails and produces the following error.

python: /usr/local/llvm/include/llvm/Support/Casting.h:579: decltype(auto) llvm::cast(From*) [with To = mlir::arith::CmpIOp; From = mlir::Operation]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed. Aborted (core dumped)

Steps to reproduce the bug

import cudaq

@cudaq.kernel
def test():
    reg = cudaq.qvector(5)

    y=1
    x=0
    while x < 4 and y==1:
        h(reg)
        x+=1

print(cudaq.sample(test))

Expected behavior

Expect the while loop to execute with the given logic.

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA-Q version: CUDA-Q Version 0.9.1 (https://github.com/NVIDIA/cuda-quantum a3ab722eab7ba39ec81424278789fabca726c436)
  • Python version:
  • C++ compiler:
  • Operating system:

Suggestions

No response

mawolf2023 avatar Feb 19 '25 17:02 mawolf2023

I tried this on my machine and didn't repro the issue. The code generated is the correct short-circuit condition and while loop.

    cc.loop while {
      %3 = cc.load %2 : !cc.ptr<i64>
      %4 = arith.cmpi slt, %3, %c4_i64 : i64
      %5 = arith.cmpi eq, %4, %false : i1
      %6 = cc.if(%5) -> i1 {
        cc.continue %false : i1
      } else {
        %7 = cc.load %1 : !cc.ptr<i64>
        %8 = arith.cmpi eq, %7, %c1_i64 : i64
        cc.continue %8 : i1
      }
      cc.condition %6
    } do {

schweitzpgi avatar Feb 20 '25 19:02 schweitzpgi

I can reproduce the issue while using docker images cu12-0.9.1 (stable) and cu12-latest (nightly). The error can be bypassed by rewriting the loop condition, the following is equivalent but arguably a bit uglier:

-while x < 4 and y==1:
+while (x < 4) * (y==1):

bebora avatar Feb 21 '25 10:02 bebora

I tried running the above code with following images and could not reproduce the error.

cu12-latest
cu12-latest-base
cu12-0.10.0

sacpis avatar Apr 03 '25 03:04 sacpis

I can confirm that the issue is not present anymore, even when using a plain Python venv instead of the provided container images.

bebora avatar Apr 04 '25 16:04 bebora