NVIDIA / NVIDIA/cuda-quantum

Cannot reuse a std::vector<cudaq::measure_result> variable in __qpu__ kernels

Open
#1,730 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

c++ bridge stale-notified
Dominant language
C++
Stars
1.1k
Forks
456
Avg merge
1d 22h
Merged PRs (30d)
165

Description

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

The compiler reports a fatal casting error when trying to save a measurement to a pre-existing variable in a kernel. This operation (and other similar operations) are needed to implement quantum error correction algorithms in CUDA-Q, and these operations don't appear to be supported when --enable-mlir is selected.

Steps to reproduce the bug

The following file demonstrates the problem:

#include "cudaq.h"

void kernel() __qpu__ {
  cudaq::qvector q(2);
  std::vector<cudaq::measure_result> mz_res;
  mz_res = mz(q);
  // std::vector<cudaq::measure_result> mz_res = mz(q);   // This works but the above does not.
}

int main(int argc, char *argv[]) {
  auto counts = cudaq::sample(kernel);
  counts.dump();
  return 0;
}

Compiling produces this error:

$ nvq++ --enable-mlir issue.cpp
cudaq-quake: /opt/llvm/include/llvm/Support/Casting.h:567: decltype(auto) llvm::cast(const From&) [with To = mlir::FunctionType; From = mlir::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/NVIDIA/cuda-quantum and include the crash backtrace.
Stack dump:
<snip>

We ultimately want to get this level of support working:

void kernel() __qpu__ {
  cudaq::qvector q(2);
  std::vector<bool> syndrome;
  std::vector<cudaq::measure_result> last_mz = mz(q);
  for (std::size_t iter = 0; iter < 10; iter++) {
    // Perform some operations

    // Then measure some qubits and compare them to the last time we measured them.
    auto new_mz = mz(q);

    // We want to either perform this:
    syndrome = last_mz ^ new_mz;  // error: invalid operands to binary expression 
    last_mz = new_mz;             // error: not yet implemented: unhandled std::vector member function

    // Or this:
    for (std::size_t i = 0; i < new_mz.size(); i++) {
      syndrome[i] = last_mz[i] ^ new_mz[i]; // error: vector dereference is not yet supported
      last_mz[i] = new_mz[i];               // This appears to compile without issues
    }

    // But neither of those paths work yet, due to the aforementioned errors.
  }
}

int main(int argc, char *argv[]) {
  auto counts = cudaq::sample(kernel);
  counts.dump();
  return 0;
}
Expected behavior

We need all of the aforementioned examples to compile and run without errors.

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

Not a regression

Environment
  • CUDA Quantum version: latest (f7983754b)
  • Python version:
  • C++ compiler:
  • Operating system:
Suggestions

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in issue.cpp and run nvq++ --enable-mlir to observe the fatal casting error. Trace the compiler path for assigning std::vectorcudaq::measure_result values in qpu kernels; done means the listed measurement, assignment, indexing, and loop examples compile and run without errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.