Skip to content

Commit 9b39fe4

Browse files
added (some) operator validation tests
since they arguably constitute the majority of functions interfaced by users, so are worth checking before v4 release! The in-development more-doc branch has removed the corresponding `@nottested` tags from the checked functions. Additionally: - added C++-macro guard (to defend from `__cplusplus=0`) - ensured pre-processor checks are performed first when `quest.h` is included - removed some defunct validation-related comments - added post-sample validation to test utility RNG - I'm suspicious of it!
1 parent 0799d14 commit 9b39fe4

File tree

6 files changed

+488
-82
lines changed

6 files changed

+488
-82
lines changed

quest/include/modes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@
6363

6464

6565

66+
// ensure C++ macro is valid (API headers use #ifdef, not #if)
67+
68+
#ifdef __cplusplus
69+
#if !__cplusplus
70+
#error "Preprocessor __cplusplus was 0 and should instead be undefined"
71+
#endif
72+
#endif
73+
74+
75+
6676
// define optional-macro defaults (mostly to list them)
6777

6878
#ifndef PERMIT_NODES_TO_SHARE_GPU

quest/include/quest.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@
1515
#define QUEST_H
1616

1717

18+
// include version first so it is accessible to
19+
// debuggers in case a subsequent include fails
20+
#include "quest/include/version.h"
21+
22+
// include before API headers since it validates
23+
// preprocessor configuration, and affirms macro
24+
// preconditions assumed by subsequent header
25+
#include "quest/include/modes.h"
26+
27+
#include "quest/include/precision.h"
28+
#include "quest/include/types.h"
1829
#include "quest/include/calculations.h"
1930
#include "quest/include/debug.h"
2031
#include "quest/include/decoherence.h"
2132
#include "quest/include/environment.h"
2233
#include "quest/include/initialisations.h"
2334
#include "quest/include/channels.h"
24-
#include "quest/include/modes.h"
2535
#include "quest/include/operations.h"
2636
#include "quest/include/paulis.h"
27-
#include "quest/include/precision.h"
2837
#include "quest/include/qureg.h"
2938
#include "quest/include/matrices.h"
30-
#include "quest/include/types.h"
31-
#include "quest/include/version.h"
3239
#include "quest/include/wrappers.h"
3340

3441

quest/src/api/operations.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ void applyDiagMatrPower(Qureg qureg, int* targets, int numTargets, DiagMatr matr
312312
validate_quregFields(qureg, __func__);
313313
validate_targets(qureg, targets, numTargets, __func__);
314314
validate_matrixDimMatchesTargets(matrix, numTargets, __func__); // also checks fields and is-synced
315-
validate_matrixIsUnitary(matrix, __func__); // checks abs=1 (so no risk of 0^(-neg exp)) and rechecks fields/synced
316-
validate_matrixExpIsNonDiverging(matrix, exponent, __func__); // harmlessly re-validates fields and is-sync
317-
validate_unitaryExponentIsReal(exponent, __func__); // checks matrix^exponent is approx unitary (abs=1)
315+
validate_matrixIsUnitary(matrix, __func__);
316+
validate_matrixExpIsNonDiverging(matrix, exponent, __func__);
317+
validate_unitaryExponentIsReal(exponent, __func__);
318318

319319
// notice exponent is a 'qcomp' not a 'qreal' despite validation; see applyMultiStateControlledDiagMatrPower()
320320

@@ -326,9 +326,9 @@ void applyControlledDiagMatrPower(Qureg qureg, int control, int* targets, int nu
326326
validate_quregFields(qureg, __func__);
327327
validate_controlAndTargets(qureg, control, targets, numTargets, __func__);
328328
validate_matrixDimMatchesTargets(matrix, numTargets, __func__); // also checks fields and is-synced
329-
validate_matrixIsUnitary(matrix, __func__); // checks abs=1 (so no risk of 0^(-neg exp)) and rechecks fields/synced
330-
validate_matrixExpIsNonDiverging(matrix, exponent, __func__); // harmlessly re-validates fields and is-sync
331-
validate_unitaryExponentIsReal(exponent, __func__); // checks matrix^exponent is approx unitary (abs=1)
329+
validate_matrixIsUnitary(matrix, __func__);
330+
validate_matrixExpIsNonDiverging(matrix, exponent, __func__);
331+
validate_unitaryExponentIsReal(exponent, __func__);
332332

333333
// notice exponent is a 'qcomp' not a 'qreal' despite validation; see applyMultiStateControlledDiagMatrPower()
334334

@@ -340,9 +340,9 @@ void applyMultiControlledDiagMatrPower(Qureg qureg, int* controls, int numContro
340340
validate_quregFields(qureg, __func__);
341341
validate_controlsAndTargets(qureg, controls, numControls, targets, numTargets, __func__);
342342
validate_matrixDimMatchesTargets(matrix, numTargets, __func__); // also checks fields and is-synced
343-
validate_matrixIsUnitary(matrix, __func__); // checks abs=1 (so no risk of 0^(-neg exp)) and rechecks fields/synced
344-
validate_matrixExpIsNonDiverging(matrix, exponent, __func__); // harmlessly re-validates fields and is-sync
345-
validate_unitaryExponentIsReal(exponent, __func__); // checks matrix^exponent is approx unitary (abs=1)
343+
validate_matrixIsUnitary(matrix, __func__);
344+
validate_matrixExpIsNonDiverging(matrix, exponent, __func__);
345+
validate_unitaryExponentIsReal(exponent, __func__);
346346

347347
// notice exponent is a 'qcomp' not a 'qreal' despite validation; see applyMultiStateControlledDiagMatrPower()
348348

@@ -354,10 +354,10 @@ void applyMultiStateControlledDiagMatrPower(Qureg qureg, int* controls, int* sta
354354
validate_quregFields(qureg, __func__);
355355
validate_controlsAndTargets(qureg, controls, numControls, targets, numTargets, __func__);
356356
validate_controlStates(states, numControls, __func__); // can be nullptr, ignoring numControls
357-
validate_matrixDimMatchesTargets(matrix, numTargets, __func__); // also checks fields and is-synced
358-
validate_matrixIsUnitary(matrix, __func__); // checks abs=1 (so no risk of 0^(-neg exp)) and rechecks fields/synced
359-
validate_matrixExpIsNonDiverging(matrix, exponent, __func__); // harmlessly re-validates fields and is-sync
360-
validate_unitaryExponentIsReal(exponent, __func__); // checks matrix^exponent is approx unitary (abs=1)
357+
validate_matrixDimMatchesTargets(matrix, numTargets, __func__);
358+
validate_matrixIsUnitary(matrix, __func__);
359+
validate_matrixExpIsNonDiverging(matrix, exponent, __func__);
360+
validate_unitaryExponentIsReal(exponent, __func__);
361361

362362
// unlike calcExpecDiagMatrPower which accepts a 'qreal' exponent,
363363
// this function accepts a 'qcomp'. This is because it always
@@ -422,8 +422,8 @@ void applyFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr matrix, qcomp ex
422422
validate_quregFields(qureg, __func__);
423423
validate_matrixFields(matrix, __func__);
424424
validate_matrixAndQuregAreCompatible(matrix, qureg, false, __func__);
425-
validate_matrixIsUnitary(matrix, __func__); // checks abs=1 (so no risk of 0^(-neg exp))
426-
validate_unitaryExponentIsReal(exponent, __func__); // checks matrix^exponent is approx unitary (abs=1)
425+
validate_matrixIsUnitary(matrix, __func__);
426+
validate_unitaryExponentIsReal(exponent, __func__);
427427
validate_matrixExpIsNonDiverging(matrix, exponent, __func__);
428428

429429
bool onlyMultiply = false;

quest/src/core/validation.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,9 @@ void assertMatrixIsSynced(T matr, string errMsg, const char* caller) {
22722272
if (!mem_isAllocated(util_getGpuMemPtr(matr)))
22732273
return;
22742274

2275-
// check if GPU amps have EVER been overwritten; we sadly cannot check the LATEST changes were pushed though
2275+
// check if GPU amps have EVER been overwritten; we sadly cannot check the LATEST changes were pushed though.
2276+
// note we check this whenever the matrix has GPU memory, even if it is being applied upon a Qureg which is
2277+
// NOT GPU-accelerated and ergo the GPU memory is not consulted. It's best to build the habit in the user!
22762278
assertThat(*(matr.wasGpuSynced) == 1, errMsg, caller);
22772279
}
22782280
void validate_matrixIsSynced(CompMatr matr, const char* caller) { assertMatrixIsSynced(matr, report::COMP_MATR_NOT_SYNCED_TO_GPU, caller);}

0 commit comments

Comments
 (0)