From f7b0946da6e3711a166770c1d4deaa963929752b Mon Sep 17 00:00:00 2001 From: LubuSeb <187313664+LubuSeb@users.noreply.github.com> Date: Fri, 5 Jun 2026 17:12:14 +0200 Subject: [PATCH] Improve applyCompMatr summation accuracy --- quest/src/cpu/cpu_subroutines.cpp | 26 +++++++++++---------- tests/unit/operations.cpp | 38 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/quest/src/cpu/cpu_subroutines.cpp b/quest/src/cpu/cpu_subroutines.cpp index 59df946e9..3ae78942a 100644 --- a/quest/src/cpu/cpu_subroutines.cpp +++ b/quest/src/cpu/cpu_subroutines.cpp @@ -607,7 +607,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 correction = getCpuQcomp(0, 0); // loop may be unrolled for (qindex j=0; j +#include #include using std::tuple; @@ -1315,6 +1317,42 @@ TEST_ALL_CTRL_OPERATIONS( PauliGadget, any, pauligad, nullptr ); TEST_ALL_CTRL_OPERATIONS( CompMatr1, one, compmatr, nullptr ); TEST_ALL_CTRL_OPERATIONS( CompMatr2, two, compmatr, nullptr ); TEST_ALL_CTRL_OPERATIONS( CompMatr, any, compmatr, nullptr ); + +TEST_CASE( "applyCompMatr uses compensated summation", TEST_CATEGORY_OPS ) { + + int numTargs = 4; + qindex numAmps = getPow2(numTargs); + int targets[] = {0,1,2,3}; + + Qureg qureg = createQureg(numTargs); + CompMatr matrix = createCompMatr(numTargs); + + qvector amps(numAmps, qcomp(1, 0)); + setQuregAmps(qureg, 0, amps.data(), amps.size()); + + for (qindex i=0; i::digits); + matrix.cpuElems[0][0] = qcomp( large, large); + for (qindex j=1; j