Table of Contents

Quantum Algorithms Library

CUDA-Q provides a standard library of quantum algorithms: VQE, QAOA, Grover's, phase estimation, and others. These are implemented as reusable C++ templates.

#include "cudaq.h"
#include "cudaq/algorithm/vqe.h"
#include "cudaq/algorithm/qaoa.h"
 
// VQE: find ground state of a Hamiltonian
struct SimpleHamiltonian {
  void operator()(std::vector<double> params) __qpu__ {
    cudaq::qvector q(2);
    ry(params[0], q[0]);
    ry(params[1], q[1]);
    cx(q[0], q[1]);
    mz(q);
  }
};
 
// Grover's algorithm: search for marked state
struct GroverOracle {
  void operator()(std::string marked_state) __qpu__ {
    // Oracle marks the desired state
    // (Implementation depends on marked_state)
  }
};
 
// Custom algorithm
struct CustomAlgorithm {
  void operator()(std::vector<double> params) __qpu__ {
    cudaq::qvector q(params.size());
 
    // Problem-specific circuit
    for (size_t i = 0; i < params.size(); i++) {
      h(q[i]);
      ry(params[i], q[i]);
    }
 
    for (size_t i = 0; i < params.size() - 1; i++) {
      cx(q[i], q[i+1]);
    }
 
    mz(q);
  }
};
 
int main() {
  // Use built-in VQE
  // cudaq::vqe<SimpleHamiltonian>(hamiltonian, optimizer, initial_params);
 
  // Use built-in QAOA
  // cudaq::qaoa<CostFunction>(cost_func, mixer, optimizer, depth);
 
  // Or implement custom algorithm
  std::vector<double> params(5, 0.1);
  auto result = cudaq::sample<CustomAlgorithm>(1000, params);
 
  return 0;
}

The algorithm library accelerates development—use standard algorithms without reimplementing them. For custom algorithms, write your kernel and integrate with CUDA-Q's optimization framework.