r-gate-custatevec
Table of Contents
Rotation gates (cuStateVec)
Rotation gates implementation using cuStateVec. The example applies $R_x(\pi/2)$ to $\lvert 0\rangle$, producing $\frac{1}{\sqrt{2}}(\lvert 0\rangle - i\lvert 1\rangle)$.
// Compile: nvcc main.cu -o main -lcustatevec // Run: ./main #include <stdio.h> #include <math.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); // R_x(pi/2): cos(pi/4)I - i*sin(pi/4)X double c = cos(M_PI / 4.0); double s = sin(M_PI / 4.0); cuDoubleComplex gate[4] = {{c,0},{0,-s},{0,-s},{c,0}}; 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.7071, 0.0000) // |1>: ( 0.0000, -0.7071) <- (|0> - i|1>) / sqrt(2) custatevecDestroy(handle); cudaFree(d_sv); return 0; }
r-gate-custatevec.txt ยท Last modified: by 127.0.0.1
