NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
489 stars 181 forks source link

Incorrect QIR generated for some conditional measurements (qir_cond_for_loop-6.cpp) #1030

Closed bmhowe23 closed 2 months ago

bmhowe23 commented 9 months ago

Required prerequisites

Describe the bug

This issue was captured in #783, but #783 was closed because the local emulation was no longer failing. However, the underlying invalid QIR generation issue still exists.

Specifically, For certain unrolled-loop test cases (like the one in the title), the compiler is injecting __quantum__rt__result_record_output calls even if if checks prevent measurements from occurring.

The compiler should not inject those calls for cases like this.

Steps to reproduce the bug

See this gist for the resulting QIR coming from the compiler at the time of this issue creation (freshly regenerated at the time of the creation of this new issue).

The QIR can also be generated like this in the dev image like this:

$ nvq++ --target quantinuum --emulate test/NVQPP/qir_cond_for_loop-6.cpp
$ CUDAQ_DUMP_JIT_IR=1 ./a.out

You can also run it on the provider hardware in order to see a test actually fail:

$ nvq++ --target quantinuum --quantinuum-machine H1-1E test/NVQPP/qir_cond_for_loop-6.cpp
$ ./a.out
{ 
  q0result%03 : { 1:8 0:92 }
   q0result%04 : { 1:3 0:97 }
   q0result%10 : { 0:100 }
   q0result%02 : { 1:11 0:89 }
   q0result%07 : { 1:1 0:99 }
   q0result%14 : { 0:100 }
   q0result%08 : { 0:100 }
   q0result%01 : { 1:25 0:75 }
   q0result%12 : { 0:100 }
   q0result%13 : { 0:100 }
   q0result%09 : { 0:100 }
   q0result%11 : { 0:100 }
   q0result%00 : { 0:49 1:51 }
   q0result%06 : { 0:100 }
   q0result%05 : { 1:1 0:99 }
   q0result%18 : { 0:100 }
   q0result%17 : { 0:100 }
   __global__ : { 00000001000000000000:1 00100000000000000000:11 00000100000000000000:1 00001000000000000000:3 00010000000000000000:8 01000000000000000000:25 10000000000000000000:51 }
   q0result%16 : { 0:100 }
   q0result%15 : { 0:100 }
   q0result%19 : { 0:100 }
}
FAILURE: nIterRan (20) >= nIter (20)

Expected behavior

When run on H1-1E, the unit test should pass.

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

Not a regression

Environment

Suggestions

No response

schweitzpgi commented 9 months ago

Specifically, For certain unrolled-loop test cases (like the one in the title), the compiler is injecting quantumrt__result_record_output calls even if if checks prevent measurements from occurring.

That tracks. QIR lowering assumed straight-line code.

sacpis commented 2 months ago

@bmhowe23 Seems like you have already fixed this issue with PR. Is this issue safe to close now?

bmhowe23 commented 2 months ago

Yes, I agree that it seems to be fixed now. Closing now. Thanks!