X gate implementation using cuStateVec.
// Compile: nvcc main.cu -o main -lcustatevec // Run: ./main #include <stdio.h> #include <cuda_runtime.h> #include <custatevec.h> int main() { const int nQubits = 1; const int dim = 1 << nQubits; cuDoubleComplex h_sv[2] = {{1,0},{0,0}}; // |0> cuDoubleComplex *d_sv; cudaMalloc(&d_sv, dim * sizeof(cuDoubleComplex)); cudaMemcpy(d_sv, h_sv, dim * sizeof(cuDoubleComplex), cudaMemcpyHostToDevice); custatevecHandle_t handle; custatevecCreate(&handle); cuDoubleComplex gate[4] = {{0,0},{1,0},{1,0},{0,0}}; // X gate int32_t targets[] = {0}; custatevecApplyMatrix( handle, d_sv, CUDA_C_64F, nQubits, gate, CUDA_C_64F, CUSTATEVEC_MATRIX_LAYOUT_ROW, 0, targets, 1, NULL, NULL, 0, CUSTATEVEC_COMPUTE_64F, NULL, 0); cudaMemcpy(h_sv, d_sv, dim * sizeof(cuDoubleComplex), cudaMemcpyDeviceToHost); for (int i = 0; i < dim; i++) printf("|%d>: (%.4f, %.4f)\n", i, cuCreal(h_sv[i]), cuCimag(h_sv[i])); // |0>: (0.0000, 0.0000) // |1>: (1.0000, 0.0000) custatevecDestroy(handle); cudaFree(d_sv); return 0; }