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

Kernel is translated into an invalid QASM #603

Closed
2 of 3 tasks
srulre opened this issue Sep 5, 2023 · 1 comment
Closed
2 of 3 tasks

Kernel is translated into an invalid QASM #603

srulre opened this issue Sep 5, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@srulre
Copy link

srulre commented Sep 5, 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.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

This is a continuation of #380.

Two major issues prevent generating a valid QASM:

  • The gate name starts with an underscore (_). This is illegal in OpenQASM 2.0
  • OpenQASM does not support forward references. Gates must be defined before they are used.
    According to the OpenQASM specification:

gate name(params) qargs {body}
...
Only built-in gate statements, calls to previously defined gates, and barrier statements can appear in body

Steps to reproduce the bug

The following example uses two recursive calls:

#include <cudaq.h>

#include <numbers>
#include <iostream>

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

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

        auto kernel3 = cudaq::make_kernel();
        auto qubit3 = kernel3.qalloc();
        kernel3.call(kernel2, qubit3);

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

The code generates the following MLIR:

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

After running cudaq-translate --convert-to=openqasm example.qke, the resulting QASM output is given by:

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

include "qelib1.inc";

gate __nvqpp__mlirgen____nvqppBuilderKernel_093606261879 q0 {
  h q0;
  __nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0;
  h q0;
  creg var1[1];
  measure q0 -> var1[0];
}

gate __nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0 {
  x q0;
}

qreg var0[1];
__nvqpp__mlirgen____nvqppBuilderKernel_093606261879 var0[0];

Expected behavior

The result should be the QASM below or an equivalent. Note the removal of the leading underscores and the moving of the nvqpp__mlirgen____nvqppBuilderKernel_367535629127 gate definition to the beginning of the file:

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

include "qelib1.inc";

gate nvqpp__mlirgen____nvqppBuilderKernel_367535629127 q0 {
  x q0;
}

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

qreg var0[1];
nvqpp__mlirgen____nvqppBuilderKernel_093606261879 var0[0];

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

Not a regression

Environment

  • cuda-quantum docker image version:sha256:9ec8ff5e307ccb6dd5c71a2b45cc486cd42221c10d7f98c6147a807f69029f1b
    More details:
                "org.opencontainers.image.created": "2023-09-01T21:03:00.664Z",
                "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": "3fe9658a48ad6f2cca85253751367c2433a53d21",
                "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

@amccaskey amccaskey added the bug Something isn't working label Sep 6, 2023
@boschmitt
Copy link
Collaborator

Fixed by #604

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants