# Phase gate (cuStateVec) [[p-gate|Phase gate]] implementation using cuStateVec. The example applies $P(\pi/2) = S$ to $\lvert +\rangle$, rotating it to $\lvert i\rangle = \frac{1}{\sqrt{2}}(\lvert 0\rangle + i\lvert 1\rangle)$. ```c // Compile: nvcc main.cu -o main -lcustatevec // Run: ./main #include #include #include #include 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); int32_t targets[] = {0}; // Apply H: |0> -> |+> double s = M_SQRT1_2; cuDoubleComplex h_gate[4] = {{s,0},{s,0},{s,0},{-s,0}}; custatevecApplyMatrix( handle, d_sv, CUDA_C_64F, nQubits, h_gate, CUDA_C_64F, CUSTATEVEC_MATRIX_LAYOUT_ROW, 0, targets, 1, NULL, NULL, 0, CUSTATEVEC_COMPUTE_64F, NULL, 0); // Apply P(pi/2) = S: |+> -> |i> cuDoubleComplex p_gate[4] = {{1,0},{0,0},{0,0},{0,1}}; custatevecApplyMatrix( handle, d_sv, CUDA_C_64F, nQubits, p_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) <- |i> = (|0> + i|1>) / sqrt(2) custatevecDestroy(handle); cudaFree(d_sv); return 0; } ```