NVIDIA / cuda-quantum

C++ and Python support for the CUDA Quantum programming model for heterogeneous quantum-classical workflows
https://nvidia.github.io/cuda-quantum/
Other
473 stars 172 forks source link

Translation to openqasm2 is missing a few optimizations #2176

Closed annagrin closed 3 days ago

annagrin commented 2 weeks ago

Required prerequisites

Describe the bug

See example and answer in: https://github.com/NVIDIA/cuda-quantum/discussions/2005

Optimizations to add:

cudaq-quake iqm_simple.cpp | cudaq-opt --pass-pipeline="builtin.module(func.func(memtoreg{quantum=0}),cc-loop-unroll)" | cudaq-translate --convert-to=openqasm2

Steps to reproduce the bug

#include <cudaq.h>
#include <fstream>

struct adonis_kernel {
  void operator()() __qpu__ {
    cudaq::qvector q(5);
    x(q[0]);

    for (int i = 0; i < 4; i++) {
      x<cudaq::ctrl>(q[i], q[i + 1]);
    }
    x<cudaq::ctrl>(q[0], q[2], q[1]);
    auto result = mz(q);
  }
};

int main() {
  // auto counts = cudaq::sample(adonis_kernel{});
  // counts.dump();
}

Expected behavior

The following command fails:

cudaq-quake iqm_simple.cpp  | cudaq-translate --convert-to=openqasm2

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

Not a regression

Environment

Suggestions

Add mem-to-reg and loop-unroll optimizations to addPipelineTranslateToOpenQASM

schweitzpgi commented 1 week ago

You'll want to run loop normalization before loop unrolling.