@@ -693,6 +693,9 @@ namespace report {
693693 string NEW_PAULI_STR_SUM_DIFFERENT_NUM_STRINGS_AND_COEFFS =
694694 " Given a different number of Pauli strings (${NUM_STRS}) and coefficients ${NUM_COEFFS}." ;
695695
696+ string NEW_PAULI_STR_SUM_CANNOT_FIT_INTO_CPU_MEM =
697+ " A PauliStrSum containing ${NUM_TERMS} terms cannot fit in the available RAM of ${NUM_BYTES} bytes." ;
698+
696699 string NEW_PAULI_STR_SUM_STRINGS_ALLOC_FAILED =
697700 " Attempted allocation of the PauliStrSum's ${NUM_TERMS}-term array of Pauli strings (${NUM_BYTES} bytes total) unexpectedly failed." ;
698701
@@ -3190,14 +3193,25 @@ void validate_controlAndPauliStrTargets(Qureg qureg, int ctrl, PauliStr str, con
31903193
31913194void validate_newPauliStrSumParams (qindex numTerms, const char * caller) {
31923195
3193- // note we do not bother checking whether RAM has enough memory to contain
3194- // the new Pauli sum, because the caller to this function has already
3195- // been passed data of the same size (and it's unlikely the user is about
3196- // to max RAM), and the memory requirements scale only linearly with the
3197- // parameters (e.g. numTerms), unlike the exponential scaling of the memory
3198- // of Qureg and CompMatr, for example
3199-
32003196 assertThat (numTerms > 0 , report::NEW_PAULI_STR_SUM_NON_POSITIVE_NUM_STRINGS, {{" ${NUM_TERMS}" , numTerms}}, caller);
3197+
3198+ // attempt to fetch RAM, and simply return if we fail; if we unknowingly
3199+ // didn't have enough RAM, then alloc validation will trigger later
3200+ size_t memPerNode = 0 ;
3201+ try {
3202+ memPerNode = mem_tryGetLocalRamCapacityInBytes ();
3203+ } catch (mem::COULD_NOT_QUERY_RAM e) {
3204+ return ;
3205+ }
3206+
3207+ // pedantically check whether the PauliStrSum fits in memory. This seems
3208+ // ridiculous/pointless because the user is expected to have already prepared
3209+ // data of an equivalent size (which is passed), but checking means we catch
3210+ // when the user has passed an erroneous 'numTerms' which is way too large,
3211+ // avoiding a seg fault
3212+
3213+ bool fits = mem_canPauliStrSumFitInMemory (numTerms, memPerNode);
3214+ assertThat (fits, report::NEW_PAULI_STR_SUM_CANNOT_FIT_INTO_CPU_MEM, {{" ${NUM_TERMS}" , numTerms}, {" ${NUM_BYTES}" , memPerNode}}, caller);
32013215}
32023216
32033217void validate_newPauliStrSumMatchingListLens (qindex numStrs, qindex numCoeffs, const char * caller) {
0 commit comments