From 0c34e118e87344ac4191a45e38f5192a7ad9b11f Mon Sep 17 00:00:00 2001 From: hjh Date: Thu, 11 Jun 2026 13:12:52 +0800 Subject: [PATCH] Use compensated summation in dense matrix CPU path --- quest/src/cpu/cpu_subroutines.cpp | 34 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/quest/src/cpu/cpu_subroutines.cpp b/quest/src/cpu/cpu_subroutines.cpp index 59df946e9..61ad96f4c 100644 --- a/quest/src/cpu/cpu_subroutines.cpp +++ b/quest/src/cpu/cpu_subroutines.cpp @@ -41,6 +41,7 @@ #include #include +#include using std::vector; @@ -607,7 +608,8 @@ void cpu_statevec_anyCtrlAnyTargDenseMatr_sub(Qureg qureg, ConstList64 ctrls, Co // i = nth local index where ctrls are active and targs form value k qindex i = setBits(i0, targs.data(), numTargBits, k); // loop may be unrolled - amps[i] = getCpuQcomp(0, 0); + cpu_qcomp sum = getCpuQcomp(0, 0); + cpu_qcomp compensation = getCpuQcomp(0, 0); // loop may be unrolled for (qindex j=0; j= std::abs(term.re)) + compensation.re += (sum.re - tRe) + term.re; + else + compensation.re += (term.re - tRe) + sum.re; + sum.re = tRe; + + qreal tIm = sum.im + term.im; + if (std::abs(sum.im) >= std::abs(term.im)) + compensation.im += (sum.im - tIm) + term.im; + else + compensation.im += (term.im - tIm) + sum.im; + sum.im = tIm; } + + amps[i] = sum + compensation; } } }