From ae07aab1442a45bbddb79e066f15eaf252a4254a Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 13 Sep 2026 08:07:10 +0200 Subject: wire simd into runtime execution --- src/core/kernel.c | 7 +++++++ src/core/runtime.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'src/core') diff --git a/src/core/kernel.c b/src/core/kernel.c index 0a92ee1..f8f7dfd 100644 --- a/src/core/kernel.c +++ b/src/core/kernel.c @@ -286,6 +286,13 @@ void psi_execute_kernel_batch(struct PsiKernelBatch batch, struct PsiVector *sta } } +void psi_apply_kernel(struct PsiVector *state, struct PsiKernel kernel, size_t num_qubits) +{ + struct PsiComplex *next = apply_kernel(state->data, kernel, num_qubits); + free(state->data); + state->data = next; +} + static void push_kernel(struct PsiKernel **kernels, size_t *count, size_t *capacity, struct PsiKernel kernel) { if (*count == *capacity) diff --git a/src/core/runtime.c b/src/core/runtime.c index 0e5cf9d..950ec7c 100644 --- a/src/core/runtime.c +++ b/src/core/runtime.c @@ -4,6 +4,7 @@ #include "core/gates.h" #include "core/kernel.h" #include "maths/complex.h" +#include "maths/simd.h" struct PsiRuntimeConfig psi_new_runtime_config(void) { @@ -121,6 +122,31 @@ static struct PsiVector new_zero_state(size_t num_qubits) return state; } +static void execute_kernels(struct PsiVector *state, const struct PsiKernel *kernels, size_t count, size_t num_qubits, struct PsiRuntimeConfig config) +{ + bool use_parallel = config.parallel && num_qubits >= config.parallel_threshold; + + for (size_t i = 0; i < count; i++) + { + struct PsiKernel kernel = kernels[i]; + + if (config.simd && kernel.target_count == 1) + { + struct PsiComplex gate[2][2] = { + { kernel.matrix.data[0], kernel.matrix.data[1] }, + { kernel.matrix.data[2], kernel.matrix.data[3] }, + }; + + if (use_parallel) + psi_apply_single_qubit_gate_simd_parallel(state->data, gate, kernel.targets[0], num_qubits); + else + psi_apply_single_qubit_gate_simd(state->data, gate, kernel.targets[0], num_qubits); + } + else + psi_apply_kernel(state, kernel, num_qubits); + } +} + struct PsiVector psi_compute_runtime_config(struct PsiRuntimeConfig config, size_t num_qubits, const struct PsiGateOp *operations, size_t op_count) { struct PsiVector state = new_zero_state(num_qubits); @@ -136,7 +162,7 @@ struct PsiVector psi_compute_runtime_config(struct PsiRuntimeConfig config, size } psi_optimize_structure_aware_batch(&batch); - psi_execute_structure_aware_batch(batch, &state); + execute_kernels(&state, batch.kernels, batch.count, num_qubits, config); psi_free_structure_aware_batch(&batch); return state; @@ -153,7 +179,7 @@ struct PsiVector psi_compute_runtime_config(struct PsiRuntimeConfig config, size if (config.batched) psi_optimize_kernel_batch(&batch); - psi_execute_kernel_batch(batch, &state); + execute_kernels(&state, batch.kernels, batch.count, num_qubits, config); psi_free_kernel_batch(&batch); return state; -- cgit v1.3