Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CUDA quantum fails to render nested kernel calls #332

Closed
2 tasks done
orclassiq opened this issue Jul 2, 2023 · 1 comment · Fixed by #334
Closed
2 tasks done

CUDA quantum fails to render nested kernel calls #332

orclassiq opened this issue Jul 2, 2023 · 1 comment · Fixed by #334
Assignees

Comments

@orclassiq
Copy link
Contributor

orclassiq commented Jul 2, 2023

Required prerequisites

  • 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.

Describe the bug

Calling cudaq::kernel_builder::to_quake with nested kernel calls fails because the serializer can't find the innermost kernel symbol.

Steps to reproduce the bug

The output of the following program:

#include <cudaq.h>

#include <iostream>

int main() {
  auto [kernel1, qubit1] = cudaq::make_kernel<cudaq::qubit>();
  std::cout << "kernel1.name " << kernel1.name() << std::endl;
  std::cout << kernel1.to_quake();

  auto [kernel2, qubit2] = cudaq::make_kernel<cudaq::qubit>();
  kernel2.call(kernel1, qubit2);
  std::cout << "kernel2.name " << kernel2.name() << std::endl;
  std::cout << kernel2.to_quake();

  auto kernel3 = cudaq::make_kernel();
  auto qreg3 = kernel3.qalloc(1);
  auto qubit3 = qreg3[0];
  kernel3.call(kernel2, qubit3);
  std::cout << "kernel3.name " << kernel3.name() << std::endl;
  std::cout << kernel3.to_quake();
}

Is:

kernel1.name __nvqppBuilderKernel_367535629127
module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
}
kernel2.name __nvqppBuilderKernel_093606261879
module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%arg0: !quake.ref) {
    call @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0) : (!quake.ref) -> ()
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
}
kernel3.name __nvqppBuilderKernel_202375922897
loc("-":3:5): error: 'func.call' op '__nvqpp__mlirgen____nvqppBuilderKernel_367535629127' does not reference a valid function
terminate called after throwing an instance of 'std::runtime_error'
  what():  cudaq::builder failed to JIT compile the Quake representation.
Aborted

The code successfully serializes the first two kernels, and but fails to serialize the third.
The missing symbol is the same symbol used by the other kernels to describe the innermost kernel.

Note, the code is compiled without any additional flags on the latest docker image (ghcr.io/nvidia/cuda-quantum@sha256:92e55d523b5124a22868472ef5bf532a7647d6173f5d12a371dd0f46f8570330).

Expected behavior

The above program shouldn't fail. Instead, the last "section" should result in an output similar to the following text:

kernel1.name __nvqppBuilderKernel_367535629127
module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
}
kernel2.name __nvqppBuilderKernel_093606261879
module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%arg0: !quake.ref) {
    call @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0) : (!quake.ref) -> ()
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
}
kernel3.name __nvqppBuilderKernel_202375922897
module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_202375922897() {
    %0 = quake.alloca !quake.veq<1>
    %1 = quake.extract_ref %0[0] : (!quake.veq<1>) -> !quake.ref
    call @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%1) : (!quake.ref) -> ()
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%arg0: !quake.ref) {
    call @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0) : (!quake.ref) -> ()
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
}

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

Not a regression

Environment

  • Docker image: I've used the latest version of the cuda-quantum image: ghcr.io/nvidia/cuda-quantum@sha256:92e55d523b5124a22868472ef5bf532a7647d6173f5d12a371dd0f46f8570330.
    I couldn't find the correct the exact CUDA Quantum version used in this image (either the version or the commit hash).
  • CUDA Quantum version: I've used the builtin version of the CUDA Quantum library in the docker image
  • C++ compiler: I've used the builtin nvq++ in the docker image
  • Operating system: I've used the docker image

Suggestions

No response

@amccaskey
Copy link
Collaborator

Thanks for filing this @orclassiq. I just issued a PR for the fix (#334).

amccaskey added a commit that referenced this issue Jul 7, 2023
* Fix kernel_builder nested function call bug, #332

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

* Update to also look for quake.apply

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

---------

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
MarkusPfundstein pushed a commit to fermioniq/cuda-quantum that referenced this issue Sep 23, 2024
* Fix kernel_builder nested function call bug, NVIDIA#332

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

* Update to also look for quake.apply

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>

---------

Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants