rich7420 commented on code in PR #868: URL: https://github.com/apache/mahout/pull/868#discussion_r2715090633
########## qdp/qdp-kernels/src/iqp.cu: ########## @@ -0,0 +1,232 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// IQP (Instantaneous Quantum Polynomial) Encoding CUDA Kernels +// +// Creates entangled quantum states via diagonal phase gates: +// |psi> = H^n * U_phase(data) * H^n |0>^n +// +// The amplitude for basis state |z> is: +// amplitude[z] = (1/2^n) * sum_x exp(i*theta(x)) * (-1)^popcount(x AND z) +// +// Two variants: +// - enable_zz=0: theta(x) = sum_i x_i * data_i (n parameters) +// - enable_zz=1: theta(x) = sum_i x_i * data_i + sum_{i<j} x_i * x_j * data_ij +// (n + n*(n-1)/2 parameters) + +#include <cuda_runtime.h> +#include <cuComplex.h> +#include <math.h> + +// Compute phase theta(x) for a given basis state x +__device__ double compute_phase( + const double* __restrict__ data, + size_t x, + unsigned int num_qubits, + int enable_zz +) { + double phase = 0.0; + + // Single-qubit Z terms: sum_i x_i * data[i] + for (unsigned int i = 0; i < num_qubits; ++i) { + if ((x >> i) & 1U) { + phase += data[i]; + } + } + + // Two-qubit ZZ terms (if enabled): sum_{i<j} x_i * x_j * data[n + pair_index] + if (enable_zz) { + unsigned int pair_idx = num_qubits; + for (unsigned int i = 0; i < num_qubits; ++i) { + for (unsigned int j = i + 1; j < num_qubits; ++j) { + if (((x >> i) & 1U) && ((x >> j) & 1U)) { + phase += data[pair_idx]; + } + pair_idx++; + } + } + } + + return phase; +} + +__global__ void iqp_encode_kernel( + const double* __restrict__ data, + cuDoubleComplex* __restrict__ state, + size_t state_len, + unsigned int num_qubits, + int enable_zz +) { + size_t z = blockIdx.x * blockDim.x + threadIdx.x; + if (z >= state_len) return; + + double real_sum = 0.0; + double imag_sum = 0.0; + + // Sum over all input basis states x + for (size_t x = 0; x < state_len; ++x) { + double phase = compute_phase(data, x, num_qubits, enable_zz); + + // Compute (-1)^{popcount(x AND z)} using __popcll intrinsic + int parity = __popcll(x & z) & 1; + double sign = (parity == 0) ? 1.0 : -1.0; + + // Accumulate: sign * exp(i*phase) = sign * (cos(phase) + i*sin(phase)) + double cos_phase, sin_phase; + sincos(phase, &sin_phase, &cos_phase); + real_sum += sign * cos_phase; + imag_sum += sign * sin_phase; + } Review Comment: in this part, The core computation logic in both kernels is nearly identical: the calculation logic in `iqp_encode_kernel` (lines 80-92) and `iqp_encode_batch_kernel` (lines 123-134) is almost the same, with the batch version only adding index calculations. could you consider extracting it into a `__device__` function or something that both kernels call. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
