Using static_cast on measurement results can cause the nvq++ compiler to crash despite the compiler knowing how to do that cast.
bmhowe23 opened this issue · 0 comments
bmhowe23 commented
Required prerequisites
- 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.
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
- If possible, make a PR with a failing test to give us a starting point to work on!
Describe the bug
Performing static_cast<bool>(measure_result)
causes cudaq-quake
(part of nvq++
) to crash with an unhelpful error message.
Currently the problem only happens when using a target that uses MLIR mode (hardware backends, NVQC, and any time the user runs with --enable-mlir
).
The problem is technically twofold:
- The compiler should accept the static_cast() operation, especially since the compiler can convert between measure_result and bool if the
static_cast
is not performed. (See below for example.) - The compiler gives an unhelpful error message
Steps to reproduce the bug
The following code reproduces the problem:
#include "cudaq.h"
bool xor_kernel() __qpu__ {
cudaq::qvector qvec(7);
x(qvec);
auto res = mz(qvec);
bool xor_result = false;
// This fails
for (auto x : res)
xor_result ^= static_cast<bool>(x);
// This works
// for (auto x : res)
// xor_result ^= x;
return xor_result;
}
int main(int argc, char *argv[]) {
bool result = xor_kernel();
printf("Result = %d\n", static_cast<int>(result));
return 0;
}
$ nvq++ --enable-mlir issue.cpp
cudaq-quake: /workspaces/cuda-quantum/lib/Frontend/nvqpp/ConvertExpr.cpp:615: bool cudaq::details::QuakeBridgeVisitor::TraverseImplicitCastExpr(clang::ImplicitCastExpr*, clang::RecursiveASTVisitor<cudaq::details::QuakeBridgeVisitor>::DataRecursionQueue*): Assertion `typeStack.size() == typeStackDepth && "must be original depth"' failed.
PLEASE submit a bug report to https://github.com/NVIDIA/cuda-quantum and include the crash backtrace.
Another example that produces the problem is:
#include "cudaq.h"
bool xor_kernel() __qpu__ {
cudaq::qubit q;
x(q);
auto res = mz(q);
bool xor_result = false;
xor_result ^= static_cast<bool>(res); // This fails
// xor_result ^= res; // This works
return xor_result;
}
int main(int argc, char *argv[]) {
bool result = xor_kernel();
printf("Result = %d\n", static_cast<int>(result));
return 0;
}
Expected behavior
The compiler should behave similar to the "This works" section of the above example. That is - it should correctly perform the static_cast operation.
Is this a regression? If it is, put the last known working version (or commit) here.
Not a regression
Environment
- CUDA Quantum version: latest (f798375)
- Python version:
- C++ compiler:
- Operating system:
Suggestions
No response