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

Invalid QASM generated from Quake #641

Closed
2 tasks done
srulre opened this issue Sep 11, 2023 · 3 comments
Closed
2 tasks done

Invalid QASM generated from Quake #641

srulre opened this issue Sep 11, 2023 · 3 comments
Assignees

Comments

@srulre
Copy link

srulre commented Sep 11, 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

This is a continuation of #603, which has not been resolved entirely.
Some kernels don't translate to a valid QASM - specifically, gate definitions appear after their calls.
In this case, it has something to do with empty kernels (identity operators).

Steps to reproduce the bug

I ran the code below:

#include <cudaq.h>

#include <iostream>

int main() {
        auto [kernel1, qubit1] = cudaq::make_kernel<cudaq::qubit>();

        auto [kernel2, qubit2] = cudaq::make_kernel<cudaq::qubit>();
        kernel2.x(qubit2);
        kernel2.call(kernel1, qubit2);

        auto [kernel3, qubit3] = cudaq::make_kernel<cudaq::qubit>();
        kernel3.h(qubit3);
        kernel3.call(kernel2, qubit3);
        kernel3.h(qubit3);
        kernel3.mz(qubit3);

        auto kernel4 = cudaq::make_kernel();
        auto qubit4 = kernel4.qalloc();
        kernel4.call(kernel1, qubit4);
        kernel4.call(kernel3, qubit4);

        std::cout << kernel4.to_quake() << std::endl;
        return 0;
}

And it generated the following Quake code:

module {
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_361293194784() attributes {"cudaq-entrypoint"} {
    %0 = quake.alloca !quake.ref
    call @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%0) : (!quake.ref) -> ()
    call @__nvqpp__mlirgen____nvqppBuilderKernel_202375922897(%0) : (!quake.ref) -> ()
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0: !quake.ref) {
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_202375922897(%arg0: !quake.ref) {
    quake.h %arg0 : (!quake.ref) -> ()
    call @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%arg0) : (!quake.ref) -> ()
    quake.h %arg0 : (!quake.ref) -> ()
    %bits = quake.mz %arg0 name "" : (!quake.ref) -> i1
    return
  }
  func.func @__nvqpp__mlirgen____nvqppBuilderKernel_093606261879(%arg0: !quake.ref) {
    quake.x %arg0 : (!quake.ref) -> ()
    call @__nvqpp__mlirgen____nvqppBuilderKernel_367535629127(%arg0) : (!quake.ref) -> ()
    return
  }
}

Then, I ran cudaq-translate --convert-to=openqasm example.qke. The following QASM code has been generated, with the gate nvqpp__mlirgen____nvqppBuilderKernel_367535629127 illegally being called before being defined.

// Code generated by NVIDIA's nvq++ compiler
OPENQASM 2.0;

include "qelib1.inc";

gate nvqpp__mlirgen____nvqppBuilderKernel_093606261879 q0 {
  x q0;
  nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0;
}

gate nvqpp__mlirgen____nvqppBuilderKernel_202375922897 q0 {
  h q0;
  nvqpp__mlirgen____nvqppBuilderKernel_093606261879 q0;
  h q0;
  creg var1[1];
  measure q0 -> var1[0];
}

gate nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0 {
}

qreg var0[1];
nvqpp__mlirgen____nvqppBuilderKernel_367535629127 var0[0];
nvqpp__mlirgen____nvqppBuilderKernel_202375922897 var0[0];

Expected behavior

The code below or an equivalent:

// Code generated by NVIDIA's nvq++ compiler
OPENQASM 2.0;

include "qelib1.inc";

gate nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0 {
}

gate nvqpp__mlirgen____nvqppBuilderKernel_093606261879 q0 {
  x q0;
  nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0;
}

gate nvqpp__mlirgen____nvqppBuilderKernel_202375922897 q0 {
  h q0;
  nvqpp__mlirgen____nvqppBuilderKernel_093606261879 q0;
  h q0;
  creg var1[1];
  measure q0 -> var1[0];
}

qreg var0[1];
nvqpp__mlirgen____nvqppBuilderKernel_367535629127 var0[0];
nvqpp__mlirgen____nvqppBuilderKernel_202375922897 var0[0];

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

Not a regression

Environment

Cudaq Docker image ID: sha256:672fdd4393aab3b27162a5ede45bc5f651801442e372441bb1e3e516b643e0ea
Additional data:

"org.opencontainers.image.created": "2023-09-09T02:16:38.587Z",
"org.opencontainers.image.description": "CUDA Quantum toolkit for heterogeneous quantum-classical workflows",
"org.opencontainers.image.licenses": "NOASSERTION",
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.revision": "77cbcd1ab00ef597727858abb9e248602c53c27d",
"org.opencontainers.image.source": "https://github.com/NVIDIA/cuda-quantum",
"org.opencontainers.image.title": "cuda-quantum",
"org.opencontainers.image.url": "https://github.com/NVIDIA/cuda-quantum",
"org.opencontainers.image.version": "latest"

Suggestions

No response

@schweitzpgi
Copy link
Collaborator

Appears to be fixed. Please verify and close.

@srulre
Copy link
Author

srulre commented Sep 13, 2023

Thanks, I'll test and let you know after the new docker image is available.

@srulre
Copy link
Author

srulre commented Sep 14, 2023

Works now, thanks!

@srulre srulre closed this as completed Sep 14, 2023
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

No branches or pull requests

3 participants