pauli-gate-custatevec
Table of Contents
Pauli gates (cuStateVec)
Pauli gates implementation using cuStateVec.
// Compile: nvcc main.cu -o main -lcustatevec // Run: ./main #include <stdio.h> #include <math.h> #include <cuda_runtime.h> #include <custatevec.h> static void apply_and_print(custatevecHandle_t handle, cuDoubleComplex *d_sv, cuDoubleComplex gate[4], const char *label) { cuDoubleComplex h_sv[2] = {{1,0},{0,0}}; // reset to |0> cudaMemcpy(d_sv, h_sv, 2 * sizeof(cuDoubleComplex), cudaMemcpyHostToDevice); int32_t targets[] = {0}; custatevecApplyMatrix( handle, d_sv, CUDA_C_64F, 1, gate, CUDA_C_64F, CUSTATEVEC_MATRIX_LAYOUT_ROW, 0, targets, 1, NULL, NULL, 0, CUSTATEVEC_COMPUTE_64F, NULL, 0); cudaMemcpy(h_sv, d_sv, 2 * sizeof(cuDoubleComplex), cudaMemcpyDeviceToHost); printf("%s|0>: |0>=(%.4f,%.4f) |1>=(%.4f,%.4f)\n", label, cuCreal(h_sv[0]), cuCimag(h_sv[0]), cuCreal(h_sv[1]), cuCimag(h_sv[1])); } int main() { cuDoubleComplex *d_sv; cudaMalloc(&d_sv, 2 * sizeof(cuDoubleComplex)); custatevecHandle_t handle; custatevecCreate(&handle); cuDoubleComplex i_gate[4] = {{1,0},{0,0},{0,0},{1,0}}; cuDoubleComplex x_gate[4] = {{0,0},{1,0},{1,0},{0,0}}; cuDoubleComplex y_gate[4] = {{0,0},{0,-1},{0,1},{0,0}}; cuDoubleComplex z_gate[4] = {{1,0},{0,0},{0,0},{-1,0}}; apply_and_print(handle, d_sv, i_gate, "I"); apply_and_print(handle, d_sv, x_gate, "X"); apply_and_print(handle, d_sv, y_gate, "Y"); apply_and_print(handle, d_sv, z_gate, "Z"); // I|0>: |0>=(1.0000,0.0000) |1>=(0.0000,0.0000) // X|0>: |0>=(0.0000,0.0000) |1>=(1.0000,0.0000) // Y|0>: |0>=(0.0000,0.0000) |1>=(0.0000,1.0000) // Z|0>: |0>=(1.0000,0.0000) |1>=(0.0000,0.0000) custatevecDestroy(handle); cudaFree(d_sv); return 0; }
pauli-gate-custatevec.txt ยท Last modified: by 127.0.0.1
