-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathdeprecated.h
1302 lines (911 loc) · 45.5 KB
/
deprecated.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
* Backwards-compatible definitions of deprecated v3 functions which
* work as expected, but issue a user-toggleable warning during
* compilation. The deprecated functions are necessarily instantiated
* here as macros so that they are only resolved (and the compiler
* warnings therein triggered) when a user actually calls them.
*/
#ifndef DEPRECATED_H
#define DEPRECATED_H
#include "quest/include/quest.h"
#include "stdlib.h"
/*
* INITIAL WARNING
*/
#warning \
"Deprecated functions have been included in compilation. The QuEST v3 API will be attemptedly \
automatically substituted for the v4 API, although some uses of the old API will still fail to \
compile. For example, access to v3 struct fields (e.g. 'ComplexMatrixN.real[0][0]') must be \
manually replaced with v4 struct fields (e.g. 'CompMatr.cpuElems[0][0]'), though new v4 functions \
make struct field access mostly redundant (e.g. via 'setCompMatr()'). Even when successfully \
compiling, use of the deprecated v3 functions is dangerous; these are not unit-tested, and the \
auto-porting to v4 may introduce new bugs. As such, you should only use this facility to help \
refactor your code to v4, and should absolutely not continue to use the old v3 API for simulation."
#if DISABLE_DEPRECATION_WARNINGS
#warning "Deprecation warnings are silenced."
#else
#warning "Deprecation warnings are enabled."
#endif
/*
* TOGGLEABLE WARNING MESSAGES
*
* users can define precompiler constant DISABLE_DEPRECATION_WARNINGS=1
* in order to disable compile-time deprecation warnings. This will
* make most of the QuEST v3 API silently work by casting to the
* v4 API at compile-time. Note that _Pragma() are resolved at
* compile-time, AFTER pre-processing; some compilers (like gcc) will
* ergo treat them like statements which can fill an 'if() (token)', so
* that (e.g.) 'if(..) oldFunc()' will issue a warning but execute
* 'oldFunc()' outside of the conditional. That won't compile if the
* function depends on control-flow params like a for-loop index. Yuck!
*/
#if DISABLE_DEPRECATION_WARNINGS
#define _WARN_TYPE_RENAMED(oldname, newname)
#define _WARN_FUNC_RENAMED(oldname, newname)
#define _WARN_FUNC_NOW_HAS_FEWER_ARGS(oldsig, newsig)
#define _WARN_GENERAL_MSG(msg)
#else
#define _WARN_TYPE_RENAMED(oldname, newname) \
_EFFECT_PRAGMA(message( \
"The QuEST type '" oldname "' is deprecated. " \
"Please instead use '" newname "' which has been automatically invoked."))
#define _WARN_FUNC_RENAMED(oldname, newname) \
_EFFECT_PRAGMA(message( \
"The QuEST function '" oldname "' is deprecated. " \
"Please instead use '" newname "' which has been automatically invoked."))
#define _WARN_FUNC_NOW_HAS_FEWER_ARGS(oldsig, newsig) \
_EFFECT_PRAGMA(message( \
"The QuEST function '" oldsig "' now accepts fewer arguments, and has " \
"new signature '" newsig "' which has been automatically invoked."))
#define _WARN_GENERAL_MSG(msg) \
_EFFECT_PRAGMA(message(msg))
#endif
#define _EFFECT_PRAGMA(cmd) _Pragma(#cmd)
/*
* NON-TOGGLEABLE ERRORS
*
* which cannot be disabled, and after which compilation cannot continue.
*/
// TODO:
// we use placeholder message(...) below which only issues a warning,
// and does not kill compilation; there alas seems to be no cross-platform
// method of aborting compilation with a _Pragma (and aborting during
// preprocessing via #error is too soon). We currently work around this by
// referring to an undefined symbol. Ew!
#define _FORCE_COMPILATION_TO_FAIL() \
_FORCING_UNKNOWN_SYMBOL_ERROR_TO_STOP_COMPILATION
#define _ERROR_GENERAL_MSG(msg) \
_EFFECT_PRAGMA(message(msg)) \
_FORCE_COMPILATION_TO_FAIL()
#define _ERROR_FUNC_RENAMED(oldname, newfunc) \
_ERROR_GENERAL_MSG( \
"The QuEST function '" oldname "' is deprecated. " \
"Please instead use '" newfunc "' which could not here be automatically invoked.") \
_FORCE_COMPILATION_TO_FAIL()
#define _ERROR_FUNC_REMOVED(oldname) \
_ERROR_GENERAL_MSG( \
"The QuEST function '" oldname "' is deprecated, and has no replacement.") \
_FORCE_COMPILATION_TO_FAIL()
#define _ERROR_PREPROCESSOR_RENAMED(oldname, newname) \
_ERROR_GENERAL_MSG( \
"The QuEST preprocessor '" oldname "' is deprecated. " \
"Please instead use '" newname "' which could not here be automatically invoked.") \
_FORCE_COMPILATION_TO_FAIL()
#define _ERROR_PREPROCESSOR_REMOVED(oldname) \
_ERROR_GENERAL_MSG( \
"The QuEST preprocessor '" oldname "' is deprecated, and has no replacement.") \
_FORCE_COMPILATION_TO_FAIL()
#define _ERROR_PHASE_FUNC_REMOVED(oldname) \
_ERROR_GENERAL_MSG( \
"The QuEST function '" oldname "' is deprecated. Please instead create a 'DiagMatr' or 'FullStateDiagMatr', initialise it " \
"via functions like 'setDiagMatrFromMultiVarFunc()' or 'setDiagMatrFromMultiDimLists()', and apply it via 'applyDiagMatr() or " \
"'applyFullStateDiagMatr()'. This procedure cannot be automatically performed here." ) \
_FORCE_COMPILATION_TO_FAIL()
/*
* PREPROCESSOR CONSTANTS
*
* We cannot simply replace format specifiers like 'REAL_STRING_FORMAT' because our
* warning code interrupts the string joining done by the preprocessor. For example,
* '"x = " REAL_STRING_FORMAT' would not compile due to the warning inside REAL_*.
*/
#define REAL_STRING_FORMAT \
_ERROR_PREPROCESSOR_RENAMED("REAL_STRING_FORMAT", "QREAL_FORMAT_SPECIFIER")
#define REAL_QASM_FORMAT \
_ERROR_PREPROCESSOR_REMOVED("REAL_QASM_FORMAT")
#define MPI_QuEST_REAL \
_ERROR_PREPROCESSOR_REMOVED("MPI_QuEST_REAL")
#define MPI_MAX_AMPS_IN_MSG \
_ERROR_PREPROCESSOR_REMOVED("MPI_MAX_AMPS_IN_MSG")
#define REAL_EPS \
_ERROR_PREPROCESSOR_REMOVED("REAL_EPS")
#define REAL_SPECIFIER \
_ERROR_PREPROCESSOR_REMOVED("REAL_SPECIFIER")
#define absReal(...) \
_ERROR_PREPROCESSOR_REMOVED("absReal()")
/*
* STACK-MEMORY STRUCT TYPES
*
* which involves us defining entirely the old v3 structs, and
* warning users to migrate to the new v4 versions. We avoided
* just replacing the v3 structs with the v4 equivalents
* because then direct access of v3 fields (.real[0][0]) would
* cause a compiler error with no error messages. We accept
* that only for heap-memory structs which always have a
* proceeding create() function which will dispatch the warning.
*/
typedef struct ComplexMatrix2
{
qreal real[2][2];
qreal imag[2][2];
} ComplexMatrix2;
typedef struct ComplexMatrix4
{
qreal real[4][4];
qreal imag[4][4];
} ComplexMatrix4;
// enable referencess to ComplexMatrix2/ 4 inside this header without
// causing a warning unrelated to the user's code (must define these
// before below macro which would then seek to replace the values)
typedef ComplexMatrix2 _NoWarnComplexMatrix2;
typedef ComplexMatrix4 _NoWarnComplexMatrix4;
// warn about use of ComplexMatrix1/2, but allow it
#define ComplexMatrix2 \
_WARN_GENERAL_MSG( \
"The QuEST type 'ComplexMatrix2' is deprecated in favour of 'CompMatr1' which has a different memory layout. We will attempt to " \
"automatically replace your 'ComplexMatrix2' with a 'CompMatr1' instance in subsequently invoked functions.") \
ComplexMatrix2
#define ComplexMatrix4 \
_WARN_GENERAL_MSG( \
"The QuEST type 'ComplexMatrix4' is deprecated in favour of 'CompMatr2' which has a different memory layout. We will attempt to " \
"automatically replace your 'ComplexMatrix4' with a 'CompMatr2' instance in subsequently invoked functions.") \
ComplexMatrix4
// automatically convert ComplexMatrix2/4 to CompMatr1/2 when
// passed to a v3 function which has a v4 port
#define _GET_COMP_MATR_1_FROM_COMPLEX_MATRIX_2(m) \
getCompMatr1( (qcomp[2][2]) { \
{getQcomp(m.real[0][0], m.imag[0][0]), getQcomp(m.real[0][1], m.imag[0][1])}, \
{getQcomp(m.real[1][0], m.imag[1][0]), getQcomp(m.real[1][1], m.imag[1][1])}})
#define _GET_COMP_MATR_2_FROM_COMPLEX_MATRIX_4(m) \
getCompMatr2( (qcomp[4][4]) { \
{getQcomp(m.real[0][0], m.imag[0][0]), getQcomp(m.real[0][1], m.imag[0][1]), getQcomp(m.real[0][2], m.imag[0][2]), getQcomp(m.real[0][3], m.imag[0][3])}, \
{getQcomp(m.real[1][0], m.imag[1][0]), getQcomp(m.real[1][1], m.imag[1][1]), getQcomp(m.real[1][2], m.imag[1][2]), getQcomp(m.real[1][3], m.imag[1][3])}, \
{getQcomp(m.real[2][0], m.imag[2][0]), getQcomp(m.real[2][1], m.imag[2][1]), getQcomp(m.real[2][2], m.imag[2][2]), getQcomp(m.real[2][3], m.imag[2][3])}, \
{getQcomp(m.real[3][0], m.imag[3][0]), getQcomp(m.real[3][1], m.imag[3][1]), getQcomp(m.real[3][2], m.imag[3][2]), getQcomp(m.real[3][3], m.imag[3][3])}})
/*
* PAULI OPERATOR TYPES
*/
enum pauliOpType {PAULI_I=0, PAULI_X=1, PAULI_Y=2, PAULI_Z=3};
#define _WARN_DEPRECATED_PAULI_ENUM(enum, intcode, char1, char2, char3) \
_WARN_GENERAL_MSG( \
"The QuEST enum '" #enum "' is deprecated, although has been defined in the deprecation header for convenience. " \
"Please instead use integer " #intcode " or characters '" #char1 "', '" #char2 "', '" #char3 "'.") \
typedef enum pauliOpType _NoWarnPauliOpType;
#define pauliOpType \
_WARN_GENERAL_MSG("The QuEST type 'enum pauliOpType' is deprecated, although it is still defined in the deprecation header for convenience.") \
pauliOpType
#define PAULI_I \
_WARN_DEPRECATED_PAULI_ENUM(PAULI_I, 0, I, i, 0) \
PAULI_I
#define PAULI_X \
_WARN_DEPRECATED_PAULI_ENUM(PAULI_X, 1, X, x, 1) \
PAULI_X
#define PAULI_Y \
_WARN_DEPRECATED_PAULI_ENUM(PAULI_Y, 2, Y, y, 2) \
PAULI_Y
#define PAULI_Z \
_WARN_DEPRECATED_PAULI_ENUM(PAULI_Z, 3, Z, z, 3) \
PAULI_Z
/*
* REMAINING TYPES
*/
#define _GET_QCOMP_FROM_COMPLEX_STRUCT(x) \
getQcomp((x).real, (x).imag)
#define Complex \
_WARN_GENERAL_MSG( \
"The QuEST type 'Complex' is deprecated, and replaced with complex scalar primitive 'qcomp' which can be instantiated " \
"with literals and modified with overloaded arithmetic operators. References to 'Complex' will be automatically replaced " \
"with an anonymous inline struct, though the QuEST will only ever return 'qcomp' instances.") \
struct { qreal real; qreal imag; }
#define ComplexMatrixN \
_WARN_TYPE_RENAMED("ComplexMatrixN", "CompMatr") \
CompMatr
#define PauliHamil \
_WARN_TYPE_RENAMED("PauliHamil", "PauliStrSum") \
PauliStrSum
#define DiagonalOp \
_WARN_TYPE_RENAMED("DiagonalOp", "FullStateDiagMatr") \
FullStateDiagMatr
#define SubDiagonalOp \
_WARN_TYPE_RENAMED("SubDiagonalOp", "DiagMatr") \
DiagMatr
#define Vector \
_WARN_GENERAL_MSG("The QuEST type 'Vector' is deprecated, and will be automatically replaced with an inline struct.") \
struct { qreal x; qreal y; qreal z; }
#define phaseFunc \
_ERROR_GENERAL_MSG( \
"The QuEST type 'enum phaseFunc' is deprecated. The functions which replace the 'applyPhaseFunc' family, such as " \
"'setDiagMatrFromMultiVarFunc()' and 'setDiagMatrFromMultiDimArray()', do not use pre-defined enums." )
#define bitEncoding \
_ERROR_GENERAL_MSG( \
"The QuEST type 'enum bitEncoding' is deprecated. The new v4 function 'setDiagMatrFromMultiVarFunc()' instead accepts " \
"an int flag to indicate whether (1) or not (0) to interpret the basis state bits under two's complement singed encoding." )
/*
* REMOVED FUNCTIONS WITH NO REPLACEMENT
*/
#define toComplex(...) \
_ERROR_FUNC_REMOVED("toComplex()")
#define fromComplex(...) \
_ERROR_FUNC_REMOVED("fromComplex()")
#define applyMultiControlledMatrixN(...) \
_ERROR_FUNC_REMOVED("applyMultiControlledMatrixN()") // our new multiplyCompMatr doesn't accept controls
#define syncQuESTSuccess(...) \
_ERROR_FUNC_REMOVED("syncQuESTSuccess()")
#define startRecordingQASM(...) \
_ERROR_FUNC_REMOVED("startRecordingQASM()")
#define stopRecordingQASM(...) \
_ERROR_FUNC_REMOVED("stopRecordingQASM()")
#define clearRecordedQASM(...) \
_ERROR_FUNC_REMOVED("clearRecordedQASM()")
#define printRecordedQASM(...) \
_ERROR_FUNC_REMOVED("printRecordedQASM()")
#define writeRecordedQASMToFile(...) \
_ERROR_FUNC_REMOVED("writeRecordedQASMToFile()")
#define bindArraysToStackComplexMatrixN(...) \
_ERROR_FUNC_REMOVED("bindArraysToStackComplexMatrixN()")
#define getStaticComplexMatrixN(...) \
_ERROR_FUNC_REMOVED("getStaticComplexMatrixN()")
/*
* FUNCTIONS WITH REPLACEMENTS WHICH CANNOT BE AUTOMATICALLY CALLED
*/
#define initComplexMatrixN(...) \
_ERROR_FUNC_RENAMED("initComplexMatrixN(ComplexMatrixN, qreal[][], qreal[][])", "setCompMatr(CompMatr, qcomp[][])")
#define createPauliHamil(...) \
_ERROR_FUNC_RENAMED( \
"createPauliHamil(int numQubits, int numTerms)", \
"createPauliStrSum(PauliStr* strings, qcomp* coeffs, qindex numTerms)")
#define initPauliHamil(...) \
_ERROR_FUNC_RENAMED( \
"initPauliHamil(PauliHamil hamil, qreal* coeffs, enum pauliOpType* codes)", \
"createPauliStrSum(PauliStr* strings, qcomp* coeffs, qindex numTerms)")
#define initDiagonalOp(...) \
_ERROR_FUNC_RENAMED( \
"initDiagonalOp(DiagonalOp obj, qreal* allElemsRe, qreal* allElemsIm)", \
"setFullStateDiagMatr(FullStateDiagMatr obj, qindex startInd, qcomp* someElems, qindex numElems)")
#define setDiagonalOpElems(...) \
_ERROR_FUNC_RENAMED( \
"setDiagonalOpElems(DiagonalOp, qindex, qreal*, qreal*, qindex)", \
"setFullStateDiagMatr(FullStateDiagMatr, qindex, qcomp*, qindex)")
#define initStateFromAmps(...) \
_ERROR_FUNC_RENAMED("initStateFromAmps(Qureg, qreal*, qreal*)", "initArbitraryPureState(Qureg, qcomp*)")
#define setAmps(...) \
_ERROR_FUNC_RENAMED("setAmps(Qureg, qindex, qreal*, qreal*, qindex)", "setQuregAmps(Qureg, qindex, qcomp*, qindex)")
#define setDensityAmps(...) \
_ERROR_FUNC_RENAMED( \
"setDensityAmps(Qureg, qindex startRow, qindex startCol, qreal* flatAmpsRe, qreal* flatAmpsIm, qindex numFlatAmps)", \
"setDensityQuregAmps(Qureg, qindex startRow, qindex startCol, qcomp** amps, qindex numRows, qindex numCols)")
#define reportState(qureg) \
_ERROR_FUNC_RENAMED("reportState(qureg)", "reportQuregToFile(qureg, char* fn)")
#define getQuESTSeeds(...) \
_ERROR_GENERAL_MSG( \
"The QuEST function 'getQuESTSeeds(QuESTEnv env, unsigned long int* out, int numOut)' has been deprecated. " \
"Please instead use 'getSeeds(unsigned* out)' which accepts a pointer to pre-allocated memory of length " \
"equal to that returned by 'getNumSeeds()'. We cannot automatically invoke this replacement routine." )
#define applyPhaseFunc(...) \
_ERROR_PHASE_FUNC_REMOVED("applyPhaseFunc")
#define applyPhaseFuncOverrides(...) \
_ERROR_PHASE_FUNC_REMOVED("applyPhaseFuncOverrides")
#define applyMultiVarPhaseFunc(...) \
_ERROR_PHASE_FUNC_REMOVED("applyMultiVarPhaseFunc")
#define applyMultiVarPhaseFuncOverrides(...) \
_ERROR_PHASE_FUNC_REMOVED("applyMultiVarPhaseFuncOverrides")
#define applyNamedPhaseFunc(...) \
_ERROR_PHASE_FUNC_REMOVED("applyNamedPhaseFunc")
#define applyNamedPhaseFuncOverrides(...) \
_ERROR_PHASE_FUNC_REMOVED("applyNamedPhaseFuncOverrides")
#define applyParamNamedPhaseFunc(...) \
_ERROR_PHASE_FUNC_REMOVED("applyParamNamedPhaseFunc")
#define applyParamNamedPhaseFuncOverrides(...) \
_ERROR_PHASE_FUNC_REMOVED("applyParamNamedPhaseFuncOverrides")
/*
* FUNCTIONS WITH THE SAME NAME BUT 1 INSTEAD OF 2 ARGS
*
* can be redirected to the new single-arg form, issuing a warning
* when given the second, superfluous parameter. Note that deprecated
* functions which can accept C99 inline temporary arrays, e.g.
* f((qcomp[]) {1,2,3}), can not be overloaded in this way, because
* the array elements confuse variadic macros
*/
#define _GET_MACRO_WITH_1_OR_2_ARGS(_1, _2, macroname, ...) macroname
#define _CALL_MACRO_WITH_1_OR_2_ARGS(prefix, ...) \
_GET_MACRO_WITH_1_OR_2_ARGS(__VA_ARGS__, prefix##_2, prefix##_1)(__VA_ARGS__)
#define _CREATE_QUREG_1(n) \
createQureg(n)
#define _CREATE_QUREG_2(n, env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("createQureg(int, QuESTEnv)", "createQureg(int)") \
_CREATE_QUREG_1(n)
#define createQureg(...) \
_CALL_MACRO_WITH_1_OR_2_ARGS(_CREATE_QUREG, __VA_ARGS__)
#define _CREATE_DENSITY_QUREG_1(n) \
createDensityQureg(n)
#define _CREATE_DENSITY_QUREG_2(n, env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("createDensityQureg(int, QuESTEnv)", "createDensityQureg(int)") \
_CREATE_DENSITY_QUREG_1(n)
#define createDensityQureg(...) \
_CALL_MACRO_WITH_1_OR_2_ARGS(_CREATE_DENSITY_QUREG, __VA_ARGS__)
#define _CREATE_CLONE_QUREG_1(n) \
createCloneQureg(n)
#define _CREATE_CLONE_QUREG_2(n, env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("createCloneQureg(Qureg, QuESTEnv)", "createCloneQureg(Qureg)") \
_CREATE_CLONE_QUREG_1(n)
#define createCloneQureg(...) \
_CALL_MACRO_WITH_1_OR_2_ARGS(_CREATE_CLONE_QUREG, __VA_ARGS__)
#define _DESTROY_QUREG_1(qureg) \
destroyQureg(qureg)
#define _DESTROY_QUREG_2(n, env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("destroyQureg(Qureg, QuESTEnv)", "destroyQureg(Qureg)") \
_DESTROY_QUREG_1(n)
#define destroyQureg(...) \
_CALL_MACRO_WITH_1_OR_2_ARGS(_DESTROY_QUREG, __VA_ARGS__)
#define _GET_ENVIRONMENT_STRING_1(str) \
getEnvironmentString(str)
#define _GET_ENVIRONMENT_STRING_2(str) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("getEnvironmentString(QuESTEnv, char[200])", "getEnvironmentString(char[200])") \
_GET_ENVIRONMENT_STRING_1(str)
#define getEnvironmentString(...) \
_CALL_MACRO_WITH_1_OR_2_ARGS(_GET_ENVIRONMENT_STRING, __VA_ARGS__)
/*
* FUNCTIONS WITH THE SAME NAME BUT 0 INSTEAD OF 1 ARGS
*
* which are handled similar to above, but require more
* pre-processor trickery to handle the no-arg case
*/
#define _COUNT_ZERO_OR_ONE_ARGS(...) _COUNT_ZERO_OR_ONE_ARGS_INNER(__VA_ARGS__, 1, 0,)
#define _COUNT_ZERO_OR_ONE_ARGS_INNER(_0, _1, X, ...) X
#define _CONCAT_SYMBOLS(A, B) _CONCAT_SYMBOLS_INNER(A, B)
#define _CONCAT_SYMBOLS_INNER(A, B) A ## B
#define _COMMA_SEPERATOR ,
#define _SYNC_QUEST_ENV_0(_) \
syncQuESTEnv()
#define _SYNC_QUEST_ENV_1(env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("syncQuESTEnv(QuESTEnv)", "syncQuESTEnv()") \
syncQuESTEnv()
#define syncQuESTEnv(X) \
_CONCAT_SYMBOLS(_SYNC_QUEST_ENV_, _COUNT_ZERO_OR_ONE_ARGS(_COMMA_SEPERATOR ## X))(X)
#define _REPORT_QUEST_ENV_0(_) \
reportQuESTEnv()
#define _REPORT_QUEST_ENV_1(env) \
_WARN_FUNC_NOW_HAS_FEWER_ARGS("reportQuESTEnv(QuESTEnv)", "reportQuESTEnv()") \
reportQuESTEnv()
#define reportQuESTEnv(X) \
_CONCAT_SYMBOLS(_REPORT_QUEST_ENV_, _COUNT_ZERO_OR_ONE_ARGS(_COMMA_SEPERATOR ## X))(X)
/*
* KRAUS MAPS
*
* which we handle explicitly, because the name 'mixKrausMap' is retained
* (in v3, it is a 1-qubit Kraus map, but in v4, it has any-qubit) with
* the same number of arguments, albeit with different types. This requires
* us to use a C11 _Generic which must dispatch to compile-time (NOT
* preprocessor) functions. We re-use an inner macro for all Kraus map funcs,
* which accepts the variable number of target qubits at the end.
*/
#define _MIX_KRAUS_MAP_INNER(qureg, ops, numOps, targs, numTargs) \
int dim = 1 << numTargs; \
qcomp*** ptrs = (qcomp***) malloc(numOps * sizeof *ptrs); \
for (int n=0; n<numOps; n++) { \
ptrs[n] = (qcomp**) malloc(dim * sizeof **ptrs); \
for (int r=0; r<dim; r++) { \
ptrs[n][r] = (qcomp*) malloc(dim * sizeof ***ptrs); \
for (int c=0; c<dim; c++) \
ptrs[n][r][c] = getQcomp(ops[n].real[r][c], ops[n].imag[r][c]); \
} \
} \
\
KrausMap map = createKrausMap(numTargs, numOps); \
setKrausMap(map, ptrs); \
(mixKrausMap)(qureg, (targs), numTargs, map); /* calls below macro, wrapped to avoid warning */ \
destroyKrausMap(map); \
\
for (int n=0; n<numOps; n++) { \
for (int r=0; r<dim; r++) \
free(ptrs[n][r]); \
free(ptrs[n]); \
} \
free(ptrs);
static inline void _mixKrausMap(Qureg qureg, int targ, _NoWarnComplexMatrix2 *ops, int numOps) {
_MIX_KRAUS_MAP_INNER(qureg, ops, numOps, &targ, 1);
}
#define _OPT_WARN_ABOUT_MIX_KRAUS_MAP_DEP() \
_WARN_FUNC_RENAMED( \
"mixKrausMap(Qureg, int targ, ComplexMatrix2* ops, int numOps)", \
"mixKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
#define mixKrausMap(qureg, targOrQubits, ...) \
_Generic((targOrQubits), \
int* : mixKrausMap, \
int : _OPT_WARN_ABOUT_MIX_KRAUS_MAP_DEP() _mixKrausMap \
)(qureg, targOrQubits, __VA_ARGS__)
static inline void _mixNonTPKrausMap(Qureg qureg, int targ, _NoWarnComplexMatrix2 *ops, int numOps) {
qreal eps = getValidationEpsilon();
setValidationEpsilon(0);
_MIX_KRAUS_MAP_INNER(qureg, ops, numOps, &targ, 1);
setValidationEpsilon(eps);
}
#define mixNonTPKrausMap(...) \
_WARN_FUNC_RENAMED( \
"mixNonTPKrausMap(Qureg, int targ, ComplexMatrix2* ops, int numOps)", \
"mixUnnormalizedKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
_mixNonTPKrausMap(__VA_ARGS__)
static inline void _mixTwoQubitKrausMap(Qureg qureg, int targ1, int targ2, _NoWarnComplexMatrix4 *ops, int numOps, int isNonCPTP) {
int targs[] = {targ1, targ2};
qreal eps = getValidationEpsilon();
if (isNonCPTP) setValidationEpsilon(0);
_MIX_KRAUS_MAP_INNER(qureg, ops, numOps, targs, 2);
setValidationEpsilon(eps);
}
#define mixTwoQubitKrausMap(...) \
_WARN_FUNC_RENAMED( \
"mixTwoQubitKrausMap(Qureg, int targ1, int targ2, ComplexMatrix4* ops, int numOps)", \
"mixKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
_mixTwoQubitKrausMap(__VA_ARGS__, 0)
#define mixNonTPTwoQubitKrausMap(...) \
_WARN_FUNC_RENAMED( \
"mixNonTPTwoQubitKrausMap(Qureg, int targ1, int targ2, ComplexMatrix4* ops, int numOps)", \
"mixUnnormalizedKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
_mixTwoQubitKrausMap(__VA_ARGS__, 1)
static inline void _mixMultiQubitKrausMap(Qureg qureg, int* targs, int numTargs, CompMatr *ops, int numOps, int isNonCPTP) {
qcomp*** ptrs = (qcomp***) malloc(numOps * sizeof *ptrs);
for (int n=0; n<numOps; n++)
ptrs[n] = ops[n].cpuElems;
KrausMap map = createKrausMap(numTargs, numOps);
setKrausMap(map, ptrs);
free(ptrs);
qreal eps = getValidationEpsilon();
if (isNonCPTP) setValidationEpsilon(0);
(mixKrausMap)(qureg, targs, numTargs, map); // calls above macro, wrapped to avoid warning */
destroyKrausMap(map);
setValidationEpsilon(eps);
}
#define mixMultiQubitKrausMap(...) \
_WARN_FUNC_RENAMED( \
"mixMultiQubitKrausMap(Qureg, int* targs, int numTargs, ComplexMatrixN* ops, int numOps)", \
"mixKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
_mixMultiQubitKrausMap(__VA_ARGS__, 0)
#define mixNonTPMultiQubitKrausMap(...) \
_WARN_FUNC_RENAMED( \
"mixNonTPMultiQubitKrausMap(Qureg, int* targs, int numTargs, ComplexMatrixN* ops, int numOps)", \
"mixUnnormalisedKrausMap(Qureg, int* targs, int numTargs, KrausMap)") \
_mixMultiQubitKrausMap(__VA_ARGS__, 1)
/*
* FUNCTIONS WITH CHANGED NAMES (AND POSSIBLY NUM ARGS)
*
* We use variadic args wherever possible so that users passing
* the wrong number of args will trigger a function-related
* compiler error, rather than the less-readable macro one. This
* also enables users to pass C99 inline compound literal arrays,
* which otherwise confuse the macros.
*/
static inline QuESTEnv _createQuESTEnv() {
initQuESTEnv();
return getQuESTEnv();
}
#define createQuESTEnv() \
_WARN_GENERAL_MSG( \
"The QuEST function 'createQuESTEnv()' is deprecated in favour of " \
"'initQuESTEnv()' which returns nothing, and 'getQuESTEnv()' which " \
"returns an immutable copy of the internally-managed environment. " \
"This second function is for convenience; the API no longer needs " \
"to receive the environment instance as a parameter. The above " \
"mentioned functions have been automatically invoked.") \
_createQuESTEnv()
#define destroyQuESTEnv(...) \
_WARN_FUNC_RENAMED("destroyQuESTEnv(QuESTEnv)", "finalizeQuESTEnv()") \
finalizeQuESTEnv()
#define createComplexMatrixN(...) \
_WARN_FUNC_RENAMED("createComplexMatrixN()", "createCompMatr()") \
createCompMatr(__VA_ARGS__)
#define destroyComplexMatrixN(...) \
_WARN_FUNC_RENAMED("destroyComplexMatrixN()", "destroyCompMatr()") \
destroyCompMatr(__VA_ARGS__)
#define destroyPauliHamil(...) \
_WARN_FUNC_RENAMED("destroyPauliHamil()", "destroyPauliStrSum()") \
destroyPauliStrSum(__VA_ARGS__)
#define createPauliHamilFromFile(...) \
_WARN_FUNC_RENAMED("createPauliHamilFromFile()", "createPauliStrSumFromReversedFile()") \
createPauliStrSumFromReversedFile(__VA_ARGS__)
#define reportPauliHamil(...) \
_WARN_FUNC_RENAMED("reportPauliHamil()", "reportPauliStrSum()") \
reportPauliStrSum(__VA_ARGS__)
#define createDiagonalOp(numQb, env) \
_WARN_FUNC_RENAMED("createDiagonalOp(int, QuESTEnv)", "createFullStateDiagMatr(int)") \
createFullStateDiagMatr(numQb)
#define destroyDiagonalOp(diagOp, env) \
_WARN_FUNC_RENAMED("destroyDiagonalOp(DiagonalOp, QuESTEnv)", "destroyFullStateDiagMatr(FullStateDiagMatr)") \
destroyFullStateDiagMatr(diagOp)
#define syncDiagonalOp(...) \
_WARN_FUNC_RENAMED("syncDiagonalOp()", "syncFullStateDiagMatr()") \
syncFullStateDiagMatr(__VA_ARGS__)
#define initDiagonalOpFromPauliHamil(...) \
_WARN_FUNC_RENAMED("initDiagonalOpFromPauliHamil()", "setFullStateDiagMatrFromPauliStrSum()") \
setFullStateDiagMatrFromPauliStrSum(__VA_ARGS__)
#define createDiagonalOpFromPauliHamilFile(fn, env) \
_WARN_FUNC_RENAMED("createDiagonalOpFromPauliHamilFile(char*, QuESTEnv)", "createFullStateDiagMatrFromPauliStrSumFile(char*)") \
createFullStateDiagMatrFromPauliStrSumFile(fn)
#define applyDiagonalOp(...) \
_WARN_FUNC_RENAMED("applyDiagonalOp()", "multiplyFullStateDiagMatr()") \
multiplyFullStateDiagMatr(__VA_ARGS__)
#define calcExpecDiagonalOp(...) \
_WARN_FUNC_RENAMED("calcExpecDiagonalOp()", "calcExpecNonHermitianFullStateDiagMatr()") \
calcExpecNonHermitianFullStateDiagMatr(__VA_ARGS__)
#define createSubDiagonalOp(...) \
_WARN_FUNC_RENAMED("createSubDiagonalOp()", "createDiagMatr()") \
createDiagMatr(__VA_ARGS__)
#define destroySubDiagonalOp(...) \
_WARN_FUNC_RENAMED("destroySubDiagonalOp()", "destroyDiagMatr()") \
destroyDiagMatr(__VA_ARGS__)
#define diagonalUnitary(...) \
_WARN_FUNC_RENAMED("diagonalUnitary()", "applyDiagMatr()") \
applyDiagMatr(__VA_ARGS__)
#define applySubDiagonalOp(...) \
_WARN_FUNC_RENAMED("applySubDiagonalOp()", "multiplyDiagMatr()") \
multiplyDiagMatr(__VA_ARGS__)
static inline void _applyGateSubDiagonalOp(Qureg qureg, int* targets, int numTargets, DiagMatr op) {
qreal eps = getValidationEpsilon();
setValidationEpsilon(0);
applyDiagMatr(qureg, targets, numTargets, op);
setValidationEpsilon(eps);
}
#define applyGateSubDiagonalOp(...) \
_WARN_GENERAL_MSG( \
"The QuEST function 'applyGateSubDiagonalOp()' is deprecated. To achieve the same thing, disable " \
"numerical validation via 'setValidationEpsilon(0)' before calling 'applyDiagMatr()'. You can " \
"save the existing epsilon via 'getValidationEpsilon()' to thereafter restore. This procedure " \
"has been performed here automatically.") \
_applyGateSubDiagonalOp(__VA_ARGS__)
#define reportStateToScreen(qureg, env, rank) \
_WARN_FUNC_RENAMED("reportStateToScreen(qureg, env, rank)", "reportQureg(qureg)") \
reportQureg(qureg)
#define getNumQubits(qureg) \
_WARN_FUNC_RENAMED("getNumQubits(qureg)", "qureg.numQubits") \
(qureg).numQubits
#define getNumAmps(qureg) \
_WARN_FUNC_RENAMED("getNumAmps(qureg)", "qureg.numAmps") \
(qureg).numAmps
#define setQuregToPauliHamil(...) \
_WARN_FUNC_RENAMED("setQuregToPauliHamil()", "setQuregToPauliStrSum()") \
setQuregToPauliStrSum(__VA_ARGS__)
#define cloneQureg(...) \
_WARN_FUNC_RENAMED("cloneQureg()", "setQuregToClone()") \
setQuregToClone(__VA_ARGS__)
#define setWeightedQureg(f1, q1, f2, q2, fOut, qOut) \
_WARN_GENERAL_MSG( \
"The QuEST function 'setWeightedQureg(f1,q1, f2,q2, fOut,qOut)' is deprecated, and replaced with " \
"'setQuregToSuperposition(fOut,qOut, f1,q1, f2,q2)' which has been automatically invoked. The new " \
"fucntion however accepts only statevectors, not density matrices, so may error at runtime. Beware " \
"that the order of the arguments has changed, so that the first supplied Qureg is modified." ) \
setQuregToSuperposition( \
getQcomp(fOut.real, fOut.imag), qOut, \
getQcomp(f1.real, f1.imag), q1, \
getQcomp(f2.real, f2.imag), q2)
#define phaseShift(...) \
_WARN_FUNC_RENAMED("phaseShift()", "applyPhaseShift()") \
applyPhaseShift(__VA_ARGS__)
#define controlledPhaseShift(...) \
_WARN_FUNC_RENAMED("controlledPhaseShift()", "applyTwoQubitPhaseShift()") \
applyTwoQubitPhaseShift(__VA_ARGS__)
#define multiControlledPhaseShift(...) \
_WARN_FUNC_RENAMED("multiControlledPhaseShift()", "applyMultiQubitPhaseShift()") \
applyMultiQubitPhaseShift(__VA_ARGS__)
#define controlledPhaseFlip(...) \
_WARN_FUNC_RENAMED("controlledPhaseFlip()", "applyTwoQubitPhaseFlip()") \
applyTwoQubitPhaseFlip(__VA_ARGS__)
#define multiControlledPhaseFlip(...) \
_WARN_FUNC_RENAMED("multiControlledPhaseFlip()", "applyMultiQubitPhaseFlip()") \
applyMultiQubitPhaseFlip(__VA_ARGS__)
#define sGate(...) \
_WARN_FUNC_RENAMED("sGate()", "applyS()") \
applyS(__VA_ARGS__)
#define tGate(...) \
_WARN_FUNC_RENAMED("tGate()", "applyT()") \
applyT(__VA_ARGS__)
#define copyStateToGPU(...) \
_WARN_FUNC_RENAMED("copyStateToGPU()", "syncQuregToGpu()") \
syncQuregToGpu(__VA_ARGS__)
#define copyStateFromGPU(...) \
_WARN_FUNC_RENAMED("copyStateFromGPU()", "syncQuregFromGpu()") \
syncQuregFromGpu(__VA_ARGS__)
#define copySubstateToGPU(...) \
_WARN_FUNC_RENAMED("copySubstateToGPU()", "syncSubQuregToGpu()") \
syncSubQuregToGpu(__VA_ARGS__)
#define copySubstateFromGPU(...) \
_WARN_FUNC_RENAMED("copySubstateFromGPU()", "syncSubQuregFromGpu()") \
syncSubQuregFromGpu(__VA_ARGS__)
#define getAmp(...) \
_WARN_FUNC_RENAMED("getAmp()", "getQuregAmp()") \
getQuregAmp(__VA_ARGS__)
#define getDensityAmp(...) \
_WARN_FUNC_RENAMED("getDensityAmp()", "getDensityQuregAmp()") \
getDensityQuregAmp(__VA_ARGS__)
#define getRealAmp(...) \
_WARN_FUNC_RENAMED("getRealAmp()", "real(getQuregAmp())") \
real(getQuregAmp(__VA_ARGS__))
#define getImagAmp(...) \
_WARN_FUNC_RENAMED("getImagAmp()", "imag(getImagAmp())") \
imag(getQuregAmp(__VA_ARGS__))
#define getProbAmp(...) \
_WARN_FUNC_RENAMED("getProbAmp()", "calcProbOfBasisState()") \
calcProbOfBasisState(__VA_ARGS__)
#define calcProbOfOutcome(...) \
_WARN_FUNC_RENAMED("calcProbOfOutcome()", "calcProbOfQubitOutcome()") \
calcProbOfQubitOutcome(__VA_ARGS__)
#define calcProbOfAllOutcomes(...) \
_WARN_FUNC_RENAMED("calcProbOfAllOutcomes()", "calcProbsOfAllMultiQubitOutcomes()") \
calcProbsOfAllMultiQubitOutcomes(__VA_ARGS__)
#define calcDensityInnerProduct(...) \
_WARN_FUNC_RENAMED("calcDensityInnerProduct()", "calcInnerProduct()") \
calcInnerProduct(__VA_ARGS__)
#define calcHilbertSchmidtDistance(...) \
_WARN_FUNC_RENAMED("calcHilbertSchmidtDistance()", "calcDistance()") \
calcDistance(__VA_ARGS__)
#define calcExpecPauliHamil(qureg, hamil, workspace) \
_WARN_FUNC_RENAMED("calcExpecPauliHamil(Qureg, PauliHamil, Qureg)", "calcExpecPauliStrSum(Qureg, PauliStrSum)") \
calcExpecPauliStrSum(qureg, hamil)
static inline qreal _calcExpecPauliStr(Qureg qureg, int* targs, _NoWarnPauliOpType* enums, int numTargs) {
int codes[100];
for (int i=0; i<numTargs && i<100; i++)
codes[i] = enums[i];
return calcExpecPauliStr(qureg, getPauliStr(codes, targs, numTargs));
}
#define calcExpecPauliProd(qureg, targs, paulis, numTargs, workspace) \
_WARN_FUNC_RENAMED( \
"calcExpecPauliProd(qureg, targs, paulis, numTargs, workspace)", \
"calcExpecPauliStr(qureg, getPauliStr(paulis, targs, numTargs))") \
_calcExpecPauliStr(qureg, targs, paulis, numTargs)
static inline PauliStrSum _createPauliStrSumFromCodes(int numQubits, _NoWarnPauliOpType* allPauliCodes, qreal* termCoeffs, int numTerms) {
int* targs = (int*) malloc(numQubits * sizeof *targs);
for (int i=0; i<numQubits; i++)
targs[i] = i;
PauliStr* strings = (PauliStr*) malloc(numTerms * sizeof *strings);
for (int i=0; i<numTerms; i++) {
int codes[100];
for (int j=0; j<numQubits && j<100; j++)
codes[i] = (int) allPauliCodes[i*numQubits+j];
strings[i] = getPauliStr(codes, targs, numQubits);
}
qcomp* coeffs = (qcomp*) malloc(numTerms * sizeof *coeffs);
for (int i=0; i<numTerms; i++)
coeffs[i] = termCoeffs[i];
PauliStrSum sum = createPauliStrSum(strings, coeffs, numTerms);
free(targs);
free(strings);
free(coeffs);
return sum;
}
static inline qreal _calcExpecPauliSum(Qureg qureg, _NoWarnPauliOpType* allPauliCodes, qreal* termCoeffs, int numTerms) {
PauliStrSum sum = _createPauliStrSumFromCodes(qureg.numQubits, allPauliCodes, termCoeffs, numTerms);
qreal out = calcExpecPauliStrSum(qureg, sum);
destroyPauliStrSum(sum);
return out;
}
#define calcExpecPauliSum(qureg, paulis, coeffs, numTerms, workspace) \
_WARN_FUNC_RENAMED("calcExpecPauliSum(Qureg, ...)", "calcExpecPauliStrSum(Qureg, PauliStrSum)") \
_calcExpecPauliSum(qureg, paulis, coeffs, numTerms)
static inline void _applyPauliSum(Qureg inQureg, _NoWarnPauliOpType* allPauliCodes, qreal* termCoeffs, int numSumTerms, Qureg outQureg) {
PauliStrSum sum = _createPauliStrSumFromCodes(inQureg.numQubits, allPauliCodes, termCoeffs, numSumTerms);
setQuregToClone(outQureg, inQureg);
multiplyPauliStrSum(outQureg, sum);
destroyPauliStrSum(sum);