Site Tools


cuda-q-quantum-kernels

Table of Contents

Quantum Kernels

Quantum kernels are C++ functions marked __qpu__ that contain quantum operations. They're compiled to quantum instructions and executed on quantum backends. Kernels can take classical parameters and return measurement results.

#include "cudaq.h"
 
// Simple kernel: no parameters
struct BellState {
  void operator()() __qpu__ {
    cudaq::qvector q(2);
    h(q[0]);
    cx(q[0], q[1]);
    mz(q);  // Measure both qubits
  }
};
 
// Parameterized kernel
struct RYGate {
  void operator()(double theta) __qpu__ {
    cudaq::qvector q(1);
    ry(theta, q[0]);
    mz(q[0]);
  }
};
 
// Multi-parameter kernel
struct TwoQubitGate {
  void operator()(double t1, double t2) __qpu__ {
    cudaq::qvector q(2);
    ry(t1, q[0]);
    ry(t2, q[1]);
    cx(q[0], q[1]);
    mz(q);
  }
};
 
// Execution
int main() {
  auto bell_result = cudaq::sample<BellState>(1000);
  auto ry_result = cudaq::sample<RYGate>(1000, 1.57);
  auto two_result = cudaq::sample<TwoQubitGate>(1000, 0.5, 1.0);
 
  return 0;
}

Kernels are stateless—each call is independent. CUDA-Q handles compilation, backend selection, and result marshaling. Think of kernels like CUDA kernels: they execute on accelerators (quantum hardware or GPU simulators), not on the CPU.

cuda-q-quantum-kernels.md · Last modified: by 127.0.0.1