Table of Contents

Toffoli gate (CUDA-Q)

Toffoli gate implementation using CUDA-Q. The example prepares $\lvert 110\rangle$ (both controls $\lvert 1\rangle$, target $\lvert 0\rangle$) and applies the Toffoli gate, flipping the target to produce $\lvert 111\rangle$.

// Compile: nvq++ main.cpp -o main
// Run:     ./main
 
#include <cudaq.h>
 
struct kernel {
    __qpu__ void operator()() {
        cudaq::qvector<3> q;
        x(q[0]);                          // q[0] = |1>
        x(q[1]);                          // q[1] = |1>  => |110>
        x<cudaq::ctrl>(q[0], q[1], q[2]); // Toffoli: |110> -> |111>
        mz(q);
    }
};
 
int main() {
    auto counts = cudaq::sample(kernel{});
    counts.dump();  // 100% |111>
}