13. Example Programs

13.1. Hello World - Simple Bell State

#include <cudaq.h>

struct bell {
  int operator()(int num_iters) __qpu__ {
    cudaq::qarray<2> q;
    int nCorrect = 0;
    for (int i = 0; i < num_iters; i++) {
      h(q[0]);
      x<cudaq::ctrl>(q[0], q[1]);
      auto results = mz(q);
      if (results[0] == results[1])
        nCorrect++;

      reset(q[0]);
      reset(q[1]);
    }
    return nCorrect;
  }
};

int main() { printf("N Correct = %d\n", bell{}(100)); }

13.2. GHZ State Preparation and Sampling

#include <cudaq.h>

struct ghz {
  void operator()(const int n_qubits) __qpu__ {
    cudaq::qvector q(n_qubits);
    h(q[0]);
    for (int i = 0; i < n_qubits - 1; ++i)
      // note use of ctrl modifier
      x<cudaq::ctrl>(q[i], q[i+1]);

    mz(q);
  }
};

int main() {
  // Sample the state produced by the ghz kernel
  auto counts = cudaq::sample(ghz{}, 10);
  for (auto [bits, count] : counts) {
    printf("Observed %s %lu times.\n", bits.c_str(), count);
  }
  return 0;
}

13.3. Quantum Phase Estimation

// Compile and run with:
// ```
// nvq++ phase_estimation.cpp -o qpe.x && ./qpe.x
// ```

#include <cudaq.h>
#include <cudaq/algorithm.h>
#include <stdio.h>

#include <cmath>

// A pure device quantum kernel defined as a free function
// (cannot be called from host code).
__qpu__ void iqft(cudaq::qview<> q) {
  int N = q.size();
  // Swap qubits
  for (int i = 0; i < N / 2; ++i) {
    swap(q[i], q[N - i - 1]);
  }

  for (int i = 0; i < N - 1; ++i) {
    h(q[i]);
    int j = i + 1;
    for (int y = i; y >= 0; --y) {
      double denom = (1UL << (j - y));
      const double theta = -M_PI / denom;
      r1<cudaq::ctrl>(theta, q[j], q[y]);
    }
  }

  h(q[N - 1]);
}

// CUDA Quantum kernel call operators can be templated on
// input CUDA Quantum kernel expressions. Here we define a general
// Phase Estimation algorithm that is generic on the eigenstate
// preparation and unitary evolution steps.
struct qpe {

  // Define the CUDA Quantum call expression to take user-specified eigenstate
  // and unitary evolution kernels, as well as the number of qubits in the
  // counting register and in the eigenstate register.
  template <typename StatePrep, typename Unitary>
  void operator()(const int nCountingQubits, StatePrep &&state_prep,
                  Unitary &&oracle) __qpu__ {
    // Allocate a register of qubits
    cudaq::qvector q(nCountingQubits + 1);

    // Extract sub-registers, one for the counting qubits
    // another for the eigenstate register
    auto counting_qubits = q.front(nCountingQubits);
    auto &state_register = q.back();

    // Prepare the eigenstate
    state_prep(state_register);

    // Put the counting register into uniform superposition
    h(counting_qubits);

    // Perform `ctrl-U^j`
    for (int i = 0; i < nCountingQubits; ++i) {
      for (int j = 0; j < (1 << i); ++j) {
        cudaq::control(oracle, counting_qubits[i], state_register);
      }
    }

    // Apply inverse quantum Fourier transform
    iqft(counting_qubits);

    // Measure to gather sampling statistics
    mz(counting_qubits);

    return;
  }
};

struct r1PiGate {
  void operator()(cudaq::qubit &q) __qpu__ { r1(1., q); }
};

int main() {

  for (auto nQubits : std::vector<int>{2, 4, 6, 8}) {
    auto counts = cudaq::sample(
        qpe{}, nQubits, [](cudaq::qubit &q) __qpu__ { x(q); }, r1PiGate{});
    auto mostProbable = counts.most_probable();
    double theta = cudaq::to_integer(mostProbable) / (double)(1UL << nQubits);
    auto piEstimate = 1. / (2 * theta);
    printf("Pi Estimate(nQubits == %d) = %lf \n", nQubits, piEstimate);
  }
}

13.4. Deuteron Binding Energy Parameter Sweep

#include <cudaq.h>
#include <cudaq/algorithm.h>

struct deuteron_n2_ansatz {
  void operator()(double theta) __qpu__ {
    cudaq::qarray<2> q;
    x(q[0]);
    ry(theta, q[1]);
    x<cudaq::ctrl>(q[1], q[0]);
  }
};

int main() {
  using namespace cudaq::spin;
  cudaq::spin_op h = 5.907 - 2.1433 * x(0) * x(1) - 2.1433 * y(0) * y(1) +
              .21829 * z(0) - 6.125 * z(1);

  // Perform parameter sweep for deuteron N=2 Hamiltonian
  const auto param_space = cudaq::linspace(-M_PI, M_PI, 25);
  for (const auto& param : param_space) {
    // KERNEL::observe(...) <==>
    // E(params...) = <psi(params...) | H | psi(params...)>
    double energy_at_param = cudaq::observe(deuteron_n2_ansatz{}, h, param);
    printf("<H>(%lf) = %lf\n", param, energy_at_param);
  }
}

13.5. Grover’s Algorithm

// Compile and run with:
// ```
// nvq++ grover.cpp -o grover.x && ./grover.x
// ```

#include <cudaq.h>

__qpu__ void reflect_about_uniform(cudaq::qview<> q) {
  auto ctrlQubits = q.front(q.size() - 1);
  auto &lastQubit = q.back();

  // Compute (U) Action (V) produces
  // U V U::Adjoint
  cudaq::compute_action(
      [&]() {
        h(q);
        x(q);
      },
      [&]() { z<cudaq::ctrl>(ctrlQubits, lastQubit); });
}

struct run_grover {
  template <typename CallableKernel>
  __qpu__ auto operator()(const int n_qubits, const int n_iterations,
                          CallableKernel &&oracle) {
    cudaq::qvector q(n_qubits);
    h(q);
    for (int i = 0; i < n_iterations; i++) {
      oracle(q);
      reflect_about_uniform(q);
    }
    mz(q);
  }
};

struct oracle {
  void operator()(cudaq::qvector<> &q) __qpu__ {
    z<cudaq::ctrl>(q[0], q[2]);
    z<cudaq::ctrl>(q[1], q[2]);
  }
};

int main() {
  auto counts = cudaq::sample(run_grover{}, 3, 1, oracle{});
  counts.dump();
}

13.6. Iterative Phase Estimation

// Compile and run with:
// ```
// nvq++ iterative_qpe.cpp -o qpe.x && ./qpe.x
// ```

#include <cudaq.h>

struct iqpe {
  void operator()() __qpu__ {
    cudaq::qarray<2> q;
    h(q[0]);
    x(q[1]);
    for (int i = 0; i < 8; i++)
      r1<cudaq::ctrl>(-5 * M_PI / 8., q[0], q[1]);

    h(q[0]);
    auto cr0 = mz(q[0]);
    reset(q[0]);

    h(q[0]);
    for (int i = 0; i < 4; i++)
      r1<cudaq::ctrl>(-5 * M_PI / 8., q[0], q[1]);

    if (cr0)
      rz(-M_PI / 2., q[0]);

    h(q[0]);
    auto cr1 = mz(q[0]);
    reset(q[0]);

    h(q[0]);
    for (int i = 0; i < 2; i++)
      r1<cudaq::ctrl>(-5 * M_PI / 8., q[0], q[1]);

    if (cr0)
      rz(-M_PI / 4., q[0]);

    if (cr1)
      rz(-M_PI / 2., q[0]);

    h(q[0]);
    auto cr2 = mz(q[0]);
    reset(q[0]);
    h(q[0]);
    r1<cudaq::ctrl>(-5 * M_PI / 8., q[0], q[1]);

    if (cr0)
      rz(-M_PI / 8., q[0]);

    if (cr1)
      rz(-M_PI_4, q[0]);

    if (cr2)
      rz(-M_PI_2, q[0]);

    h(q[0]);
    mz(q[0]);
  }
};

int main() {
  auto counts = cudaq::sample(/*shots*/ 10, iqpe{});
  counts.dump();

  return 0;
}