Skip to content

Commit e00cb41

Browse files
added C++ convenience overloads
so that C++ users can pass vectors directly to functions, even inline, rather than having to store qubit lists as local variables and pass pointers. This is not only concise, but prevents them from causing insidious seg-faults/dangling errors by attempting to pass inline vector pointers. Alas this adds quite a lot of new boilerplate; 2000 lines of it! :'( See PR #584 for a breakdown into smaller commits.
1 parent 29d343a commit e00cb41

26 files changed

+2114
-170
lines changed

quest/include/calculations.h

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121

2222

2323
/*
24-
* These signatures are divided into two partitions; those which are
25-
* natively C and C++ compatible (first partition) and those which are
24+
* These signatures are divided into three partitions; those which are
25+
* natively C and C++ compatible (first partition), then those which are
2626
* only exposed to C++ (second partition) because they return 'qcomp'
27-
* which cannot cross the C++-to-C ABI. The first partition defines the
28-
* doc groups, and the second partition functions are added into them.
27+
* which cannot cross the C++-to-C ABI, and then finally the C++-only
28+
* convenience overloads. The first partition defines the doc groups, and
29+
* the latter partition functions are added into them.
30+
*/
31+
32+
33+
34+
/*
35+
* C AND C++ AGNOSTIC FUNCTIONS
2936
*/
3037

3138
// enable invocation by both C and C++ binaries
@@ -193,7 +200,7 @@ qreal calcProbOfMultiQubitOutcome(Qureg qureg, int* qubits, int* outcomes, int n
193200

194201
/// @notdoced
195202
/// @notvalidated
196-
void calcProbsOfAllMultiQubitOutcomes(qreal* outcomeProbs, Qureg qureg, int* qubits, int numQubits);
203+
void calcProbsOfAllMultiQubitOutcomes(qreal* outcomeProbs, Qureg qureg, int* qubits, int numQubits);
197204

198205

199206
/** @} */
@@ -279,8 +286,8 @@ Qureg calcReducedDensityMatrix(Qureg qureg, int* retainQubits, int numRetainQubi
279286
* below functions have a C-compatible wrapper defined in
280287
* wrappers.h which passes/receives the primitives by pointer;
281288
* a qcomp ptr can be safely passed from the C++ source binary
282-
* the user's C binary. We manually add these functions to the
283-
* Doxygen doc groups defined above
289+
* the user's C binary. We manually add these functions to their
290+
* respective Doxygen doc groups defined above
284291
*/
285292

286293

@@ -309,6 +316,54 @@ qcomp calcExpecNonHermitianFullStateDiagMatrPower(Qureg qureg, FullStateDiagMatr
309316

310317

311318

319+
/*
320+
* C++ OVERLOADS
321+
*
322+
* which are only accessible to C++ binaries, and accept
323+
* arguments more natural to C++ (e.g. std::vector). We
324+
* manually add these to their respective Doxygen doc groups.
325+
*/
326+
327+
#ifdef __cplusplus
328+
329+
#include <vector>
330+
331+
332+
/// @ingroup calc_prob
333+
/// @nottested
334+
/// @notdoced
335+
/// @notvalidated
336+
/// @cpponly
337+
qreal calcProbOfMultiQubitOutcome(Qureg qureg, std::vector<int> qubits, std::vector<int> outcomes);
338+
339+
340+
/// @ingroup calc_prob
341+
/// @nottested
342+
/// @notdoced
343+
/// @notvalidated
344+
/// @cpponly
345+
std::vector<qreal> calcProbsOfAllMultiQubitOutcomes(Qureg qureg, std::vector<int> qubits);
346+
347+
348+
/// @ingroup calc_partialtrace
349+
/// @nottested
350+
/// @notdoced
351+
/// @notvalidated
352+
/// @cpponly
353+
Qureg calcPartialTrace(Qureg qureg, std::vector<int> traceOutQubits);
354+
355+
356+
/// @ingroup calc_partialtrace
357+
/// @nottested
358+
/// @notdoced
359+
/// @notvalidated
360+
/// @cpponly
361+
Qureg calcReducedDensityMatrix(Qureg qureg, std::vector<int> retainQubits);
362+
363+
364+
#endif // __cplusplus
365+
366+
312367
#endif // CALCULATIONS_H
313368

314-
/** @} (end doxygen defgroup) */
369+
/** @} */ // (end file-wide doxygen defgroup)

quest/include/channels.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@
5656

5757

5858

59+
/*
60+
* unlike some other headers, we here intermix the C and C++-only
61+
* signatures, grouping them semantically & by their doc groups
62+
*/
63+
64+
65+
5966
/**
6067
* @defgroup channels_structs Structs
6168
* @brief Data structures for representing decoherence channels.
@@ -383,11 +390,13 @@ extern "C" {
383390

384391
/// @ingroup channels_setters
385392
/// @notdoced
393+
/// @cpponly
386394
void setInlineKrausMap(KrausMap map, int numQb, int numOps, std::vector<std::vector<std::vector<qcomp>>> matrices);
387395

388396

389397
/// @ingroup channels_setters
390398
/// @notdoced
399+
/// @cpponly
391400
void setInlineSuperOp(SuperOp op, int numQb, std::vector<std::vector<qcomp>> matrix);
392401

393402

@@ -458,11 +467,13 @@ extern "C" {
458467

459468
/// @ingroup channels_create
460469
/// @notdoced
470+
/// @cpponly
461471
KrausMap createInlineKrausMap(int numQubits, int numOperators, std::vector<std::vector<std::vector<qcomp>>> matrices);
462472

463473

464474
/// @ingroup channels_create
465475
/// @notdoced
476+
/// @cpponly
466477
SuperOp createInlineSuperOp(int numQubits, std::vector<std::vector<qcomp>> matrix);
467478

468479

@@ -521,4 +532,4 @@ extern "C" {
521532

522533
#endif // CHANNELS_H
523534

524-
/** @} (end doxygen defgroup) */
535+
/** @} */ // (end file-wide doxygen defgroup)

quest/include/debug.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
#include "quest/include/types.h"
1818

19+
20+
21+
/*
22+
* C AND C++ AGNOSTIC FUNCTIONS
23+
*/
24+
1925
// enable invocation by both C and C++ binaries
2026
#ifdef __cplusplus
2127
extern "C" {
@@ -159,6 +165,39 @@ void getEnvironmentString(char str[200]);
159165
}
160166
#endif
161167

168+
169+
170+
/*
171+
* C++ OVERLOADS
172+
*
173+
* which are only accessible to C++ binaries, and accept
174+
* arguments more natural to C++ (e.g. std::vector). We
175+
* manually add these to their respective Doxygen doc groups.
176+
*/
177+
178+
#ifdef __cplusplus
179+
180+
#include <vector>
181+
182+
183+
/// @ingroup debug_seed
184+
/// @nottested
185+
/// @notdoced
186+
/// @cpponly
187+
void setSeeds(std::vector<unsigned> seeds);
188+
189+
190+
/// @ingroup debug_seed
191+
/// @nottested
192+
/// @notdoced
193+
/// @cpponly
194+
std::vector<unsigned> getSeeds();
195+
196+
197+
#endif // __cplusplus
198+
199+
200+
162201
#endif // DEBUG_H
163202

164-
/** @} (end doxygen defgroup) */
203+
/** @} */ // (end file-wide doxygen defgroup)

quest/include/decoherence.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
#include "quest/include/qureg.h"
1818
#include "quest/include/channels.h"
1919

20+
21+
22+
/*
23+
* C AND C++ AGNOSTIC FUNCTIONS
24+
*/
25+
2026
// enable invocation by both C and C++ binaries
2127
#ifdef __cplusplus
2228
extern "C" {
2329
#endif
2430

25-
2631
/// @notdoced
2732
/// @notvalidated
2833
void mixDephasing(Qureg qureg, int qubit, qreal prob);
@@ -53,7 +58,7 @@ void mixQureg(Qureg qureg, Qureg other, qreal prob);
5358

5459
/// @notdoced
5560
/// @notvalidated
56-
void mixKrausMap(Qureg qureg, int* qubits, int numQubits, KrausMap map);
61+
void mixKrausMap(Qureg qureg, int* targets, int numTargets, KrausMap map);
5762

5863
/// @notdoced
5964
/// @notvalidated
@@ -65,6 +70,36 @@ void mixSuperOp(Qureg qureg, int* targets, int numTargets, SuperOp superop);
6570
}
6671
#endif
6772

73+
74+
75+
/*
76+
* C++ OVERLOADS
77+
*
78+
* which are only accessible to C++ binaries, and accept
79+
* arguments more natural to C++ (e.g. std::vector). These
80+
* are included in the file-wide doxygen group (no subgroups).
81+
*/
82+
83+
#ifdef __cplusplus
84+
85+
#include <vector>
86+
87+
/// @nottested
88+
/// @notdoced
89+
/// @notvalidated
90+
/// @cpponly
91+
void mixKrausMap(Qureg qureg, std::vector<int> targets, KrausMap map);
92+
93+
/// @nottested
94+
/// @notdoced
95+
/// @notvalidated
96+
/// @cpponly
97+
void mixSuperOp(Qureg qureg, std::vector<int> targets, SuperOp superop);
98+
99+
#endif // __cplusplus
100+
101+
102+
68103
#endif // DECOHERENCE_H
69104

70-
/** @} (end doxygen defgroup) */
105+
/** @} */ // (end file-wide doxygen defgroup)

quest/include/deprecated.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ refactor your code to v4, and should absolutely not continue to use the old v3 A
114114
*/
115115

116116

117-
// TODO:
118-
// we use placeholder message(...) below which only issues a warning,
119-
// and does not kill compilation; there alas seems to be no cross-platform
120-
// method of aborting compilation with a _Pragma (and aborting during
121-
// preprocessing via #error is too soon). We currently work around this by
122-
// referring to an undefined symbol. Ew!
117+
/// @todo:
118+
/// we use placeholder message(...) below which only issues a warning,
119+
/// and does not kill compilation; there alas seems to be no cross-platform
120+
/// method of aborting compilation with a _Pragma (and aborting during
121+
/// preprocessing via #error is too soon). We currently work around this by
122+
/// referring to an undefined symbol. Ew!
123123

124124

125125
#define _FORCE_COMPILATION_TO_FAIL() \

quest/include/environment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ QuESTEnv getQuESTEnv();
7575

7676
#endif // ENVIRONMENT_H
7777

78-
/** @} (end doxygen defgroup) */
78+
/** @} */ // (end file-wide doxygen defgroup)

quest/include/initialisations.h

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
#include "quest/include/qureg.h"
2020
#include "quest/include/paulis.h"
2121

22+
23+
24+
/*
25+
* C AND C++ AGNOSTIC FUNCTIONS
26+
*/
27+
2228
// enable invocation by both C and C++ binaries
2329
#ifdef __cplusplus
2430
extern "C" {
@@ -144,6 +150,65 @@ void setQuregToReducedDensityMatrix(Qureg out, Qureg in, int* retainQubits, int
144150
}
145151
#endif
146152

153+
154+
155+
/*
156+
* C++ OVERLOADS
157+
*
158+
* which are only accessible to C++ binaries, and accept
159+
* arguments more natural to C++ (e.g. std::vector). We
160+
* manually add these to their respective Doxygen doc groups.
161+
*/
162+
163+
#ifdef __cplusplus
164+
165+
#include <vector>
166+
167+
168+
/// @ingroup init_amps
169+
/// @nottested
170+
/// @notdoced
171+
/// @notvalidated
172+
/// @cpponly
173+
void setQuregAmps(Qureg qureg, qindex startInd, std::vector<qcomp> amps);
174+
175+
176+
/// @ingroup init_amps
177+
/// @nottested
178+
/// @notdoced
179+
/// @notvalidated
180+
/// @cpponly
181+
void setDensityQuregAmps(Qureg qureg, qindex startRow, qindex startCol, std::vector<std::vector<qcomp>> amps);
182+
183+
184+
/// @ingroup init_amps
185+
/// @nottested
186+
/// @notdoced
187+
/// @notvalidated
188+
/// @cpponly
189+
void setDensityQuregFlatAmps(Qureg qureg, qindex startInd, std::vector<qcomp> amps);
190+
191+
192+
/// @ingroup init_amps
193+
/// @nottested
194+
/// @notdoced
195+
/// @notvalidated
196+
/// @cpponly
197+
void setQuregToPartialTrace(Qureg out, Qureg in, std::vector<int> traceOutQubits);
198+
199+
200+
/// @ingroup init_amps
201+
/// @nottested
202+
/// @notdoced
203+
/// @notvalidated
204+
/// @cpponly
205+
void setQuregToReducedDensityMatrix(Qureg out, Qureg in, std::vector<int> retainQubits);
206+
207+
208+
#endif // __cplusplus
209+
210+
211+
147212
#endif // INITIALISATIONS_H
148213

149-
/** @} (end doxygen defgroup) */
214+
/** @} */ // (end file-wide doxygen defgroup)

0 commit comments

Comments
 (0)