-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathtest_unitaries.cpp
2902 lines (2219 loc) · 105 KB
/
test_unitaries.cpp
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
* Unit testing for QuEST's 'unitaries' API.
* The tests are in alphabetical order of the API doc.
*
* These tests work by constructing, from the unitary specification (e.g.
* control and target qubits), a full-Hilbert space complex matrix. This is
* then multiplied onto statevectors, or multiplied and it's conjugate-transpose
* right-multiplied onto density matrices.
*
* QuEST's user validation handling is unit tested by redefining exitWithError
* (a weak C symbol) to throw a C++ exception, caught by the Catch2 library.
*
* @author Tyson Jones
*/
#include "catch.hpp"
#include "QuEST.h"
#include "utilities.hpp"
/** Prepares the needed data structures for unit testing unitaries.
* This creates a statevector and density matrix of the size NUM_QUBITS,
* and corresponding QVector and QMatrix instances for analytic comparison.
*/
#define PREPARE_TEST(quregVec, quregMatr, refVec, refMatr) \
Qureg quregVec = createQureg(NUM_QUBITS, QUEST_ENV); \
Qureg quregMatr = createDensityQureg(NUM_QUBITS, QUEST_ENV); \
initDebugState(quregVec); \
initDebugState(quregMatr); \
QVector refVec = toQVector(quregVec); \
QMatrix refMatr = toQMatrix(quregMatr); \
assertQuregAndRefInDebugState(quregVec, refVec); \
assertQuregAndRefInDebugState(quregMatr, refMatr);
/** Destroys the data structures made by PREPARE_TEST */
#define CLEANUP_TEST(quregVec, quregMatr) \
destroyQureg(quregVec, QUEST_ENV); \
destroyQureg(quregMatr, QUEST_ENV);
/* allows concise use of Contains in catch's REQUIRE_THROWS_WITH */
using Catch::Matchers::Contains;
/** @sa compactUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "compactUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qcomp a = getRandomReal(-1,1) * expI(getRandomReal(0,2*M_PI));
qcomp b = sqrt(1-abs(a)*abs(a)) * expI(getRandomReal(0,2*M_PI));
Complex alpha; alpha.real = real(a); alpha.imag = imag(a);
Complex beta; beta.real = real(b); beta.imag = imag(b);
QMatrix op = toQMatrix(alpha, beta);
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
SECTION( "state-vector" ) {
compactUnitary(quregVec, target, alpha, beta);
applyReferenceOp(refVec, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
compactUnitary(quregMatr, target, alpha, beta);
applyReferenceOp(refMatr, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "qubit indices" ) {
int target = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( compactUnitary(quregVec, target, alpha, beta), Contains("Invalid target") );
}
SECTION( "unitarity" ) {
// unitary when |alpha|^2 + |beta|^2 = 1
alpha.real=1; alpha.imag=2;
beta.real=3; beta.imag=4;
REQUIRE_THROWS_WITH( compactUnitary(quregVec, 0, alpha, beta), Contains("unitary") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa diagonalUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "diagonalUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
SECTION( "correctness" ) {
// generate all possible targets
int numTargs = GENERATE( range(1,NUM_QUBITS+1) );
int* targs = GENERATE_COPY( sublists(range(0,NUM_QUBITS), numTargs) );
// initialise a random unitary diagonal op
SubDiagonalOp op = createSubDiagonalOp(numTargs);
for (long long int i=0; i<op.numElems; i++) {
qcomp elem = getRandomComplex();
elem /= abs(elem);
op.real[i] = real(elem);
op.imag[i] = imag(elem);
}
QMatrix opMatr = toQMatrix(op);
SECTION( "state-vector" ) {
diagonalUnitary(quregVec, targs, numTargs, op);
applyReferenceOp(refVec, targs, numTargs, opMatr);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
diagonalUnitary(quregMatr, targs, numTargs, op);
applyReferenceOp(refMatr, targs, numTargs, opMatr);
REQUIRE( areEqual(quregMatr, refMatr, 100*REAL_EPS) );
}
destroySubDiagonalOp(op);
}
SECTION( "input validation" ) {
SECTION( "diagonal dimension" ) {
int numTargs = 3;
SubDiagonalOp op = createSubDiagonalOp(numTargs);
int badNumTargs = GENERATE_COPY( numTargs-1, numTargs+1 );
int badTargs[NUM_QUBITS+1];
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, badTargs, badNumTargs, op), Contains("incompatible dimension") );
destroySubDiagonalOp(op);
}
SECTION( "number of targets" ) {
// make too many targets (which are otherwise valid)
SubDiagonalOp badOp = createSubDiagonalOp(NUM_QUBITS + 1);
int targs[NUM_QUBITS + 1];
for (int t=0; t<badOp.numQubits; t++)
targs[t] = t;
for (int i=0; i<badOp.numElems; i++)
badOp.real[i] = 1;
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, targs, badOp.numQubits, badOp), Contains("Invalid number of target qubits") );
destroySubDiagonalOp(badOp);
}
SECTION( "repetition in targets" ) {
// make a valid unitary diagonal op
SubDiagonalOp op = createSubDiagonalOp(3);
for (int i=0; i<op.numElems; i++)
op.real[i] = 1;
// make a repetition in the target list
int targs[] = {2,1,2};
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, targs, op.numQubits, op), Contains("target qubits must be unique") );
destroySubDiagonalOp(op);
}
SECTION( "qubit indices" ) {
// make a valid unitary diagonal op
SubDiagonalOp op = createSubDiagonalOp(3);
for (int i=0; i<op.numElems; i++)
op.real[i] = 1;
int targs[] = {0,1,2};
// make each target in-turn invalid
int badIndex = GENERATE( range(0,3) );
int badValue = GENERATE( -1, NUM_QUBITS );
targs[badIndex] = badValue;
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, targs, op.numQubits, op), Contains("Invalid target qubit") );
destroySubDiagonalOp(op);
}
SECTION( "unitarity" ) {
// make a valid unitary diagonal op
SubDiagonalOp op = createSubDiagonalOp(3);
int targs[] = {0,1,2};
for (int i=0; i<op.numElems; i++)
op.real[i] = 1;
// break unitarity via reals
op.real[2] = -.1;
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, targs, op.numQubits, op), Contains("unitary") );
// restore reals and break unitarity via imag
op.real[2] = 1;
op.imag[3] = -3.5;
REQUIRE_THROWS_WITH( diagonalUnitary(quregVec, targs, op.numQubits, op), Contains("unitary") );
destroySubDiagonalOp(op);
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledCompactUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledCompactUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qcomp a = getRandomReal(-1,1) * expI(getRandomReal(0,2*M_PI));
qcomp b = sqrt(1-abs(a)*abs(a)) * expI(getRandomReal(0,2*M_PI));
Complex alpha; alpha.real = real(a); alpha.imag = imag(a);
Complex beta; beta.real = real(b); beta.imag = imag(b);
QMatrix op = toQMatrix(alpha, beta);
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledCompactUnitary(quregVec, control, target, alpha, beta);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledCompactUnitary(quregMatr, control, target, alpha, beta);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = 0;
REQUIRE_THROWS_WITH( controlledCompactUnitary(quregVec, qb, qb, alpha, beta), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledCompactUnitary(quregVec, qb, 0, alpha, beta), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledCompactUnitary(quregVec, 0, qb, alpha, beta), Contains("Invalid target") );
}
SECTION( "unitarity" ) {
// unitary when |a|^2 + |b^2 = 1
alpha.real=1; alpha.imag=2;
beta.real=3; beta.imag=4;
REQUIRE_THROWS_WITH( controlledCompactUnitary(quregVec, 0, 1, alpha, beta), Contains("unitary") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledMultiQubitUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledMultiQubitUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
// figure out max-num targs (inclusive) allowed by hardware backend
int maxNumTargs = calcLog2(quregVec.numAmpsPerChunk);
if (maxNumTargs >= NUM_QUBITS)
maxNumTargs = NUM_QUBITS - 1; // make space for control qubit
SECTION( "correctness" ) {
// generate all possible qubit arrangements
int ctrl = GENERATE( range(0,NUM_QUBITS) );
int numTargs = GENERATE_COPY( range(1,maxNumTargs+1) );
int* targs = GENERATE_COPY( sublists(range(0,NUM_QUBITS), numTargs, ctrl) );
// for each qubit arrangement, use a new random unitary
QMatrix op = getRandomUnitary(numTargs);
ComplexMatrixN matr = createComplexMatrixN(numTargs);
toComplexMatrixN(op, matr);
SECTION( "state-vector" ) {
controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr);
applyReferenceOp(refVec, ctrl, targs, numTargs, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledMultiQubitUnitary(quregMatr, ctrl, targs, numTargs, matr);
applyReferenceOp(refMatr, ctrl, targs, numTargs, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
destroyComplexMatrixN(matr);
}
SECTION( "input validation" ) {
SECTION( "number of targets" ) {
// there cannot be more targets than qubits in register
// (numTargs=NUM_QUBITS is caught elsewhere, because that implies ctrl is invalid)
int numTargs = GENERATE( -1, 0, NUM_QUBITS+1 );
int targs[NUM_QUBITS+1]; // prevents seg-fault if validation doesn't trigger
ComplexMatrixN matr = createComplexMatrixN(NUM_QUBITS+1); // prevent seg-fault
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, 0, targs, numTargs, matr), Contains("Invalid number of target"));
destroyComplexMatrixN(matr);
}
SECTION( "repetition in targets" ) {
int ctrl = 0;
int numTargs = 3;
int targs[] = {1,2,2};
ComplexMatrixN matr = createComplexMatrixN(numTargs); // prevents seg-fault if validation doesn't trigger
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr), Contains("target") && Contains("unique"));
destroyComplexMatrixN(matr);
}
SECTION( "control and target collision" ) {
int numTargs = 3;
int targs[] = {0,1,2};
int ctrl = targs[GENERATE_COPY( range(0,numTargs) )];
ComplexMatrixN matr = createComplexMatrixN(numTargs); // prevents seg-fault if validation doesn't trigger
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr), Contains("Control") && Contains("target"));
destroyComplexMatrixN(matr);
}
SECTION( "qubit indices" ) {
int ctrl = 0;
int numTargs = 3;
int targs[] = {1,2,3};
ComplexMatrixN matr = createComplexMatrixN(numTargs); // prevents seg-fault if validation doesn't trigger
int inv = GENERATE( -1, NUM_QUBITS );
ctrl = inv;
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr), Contains("Invalid control") );
ctrl = 0; // restore valid ctrl
targs[GENERATE_COPY( range(0,numTargs) )] = inv; // make invalid target
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr), Contains("Invalid target") );
destroyComplexMatrixN(matr);
}
SECTION( "unitarity" ) {
int ctrl = 0;
int numTargs = GENERATE_COPY( range(1,maxNumTargs+1) );
ComplexMatrixN matr = createComplexMatrixN(numTargs); // initially zero, hence not-unitary
VLA(int, targs, numTargs);
for (int i=0; i<numTargs; i++)
targs[i] = i+1;
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, numTargs, matr), Contains("unitary") );
destroyComplexMatrixN(matr);
}
SECTION( "unitary creation" ) {
int numTargs = 3;
int targs[] = {1,2,3};
/* compilers don't auto-initialise to NULL; the below circumstance
* only really occurs when 'malloc' returns NULL in createComplexMatrixN,
* which actually triggers its own validation. Hence this test is useless
* currently.
*/
ComplexMatrixN matr;
matr.real = NULL;
matr.imag = NULL;
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, 0, targs, numTargs, matr), Contains("created") );
}
SECTION( "unitary dimensions" ) {
int ctrl = 0;
int targs[2] = {1,2};
ComplexMatrixN matr = createComplexMatrixN(3);
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, ctrl, targs, 2, matr), Contains("matrix size"));
destroyComplexMatrixN(matr);
}
SECTION( "unitary fits in node" ) {
// pretend we have a very limited distributed memory (judged by matr size)
quregVec.numAmpsPerChunk = 1;
int qb[] = {1,2};
ComplexMatrixN matr = createComplexMatrixN(2); // prevents seg-fault if validation doesn't trigger
REQUIRE_THROWS_WITH( controlledMultiQubitUnitary(quregVec, 0, qb, 2, matr), Contains("targets too many qubits"));
destroyComplexMatrixN(matr);
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledNot
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledNot", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
QMatrix op{{0,1},{1,0}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledNot(quregVec, control, target);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledNot(quregMatr, control, target);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledNot(quregVec, qb, qb), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledNot(quregVec, qb, 0), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledNot(quregVec, 0, qb), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledPauliY
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledPauliY", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
QMatrix op{{0,-qcomp(0,1)},{qcomp(0,1),0}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledPauliY(quregVec, control, target);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledPauliY(quregMatr, control, target);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledPauliY(quregVec, qb, qb), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledPauliY(quregVec, qb, 0), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledPauliY(quregVec, 0, qb), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledPhaseFlip
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledPhaseFlip", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
QMatrix op{{1,0},{0,-1}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledPhaseFlip(quregVec, control, target);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledPhaseFlip(quregMatr, control, target);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledPhaseFlip(quregVec, qb, qb), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledPhaseFlip(quregVec, qb, 0), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledPhaseFlip(quregVec, 0, qb), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledPhaseShift
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledPhaseShift", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qreal param = getRandomReal(-2*M_PI, 2*M_PI);
QMatrix op{{1,0},{0,expI(param)}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledPhaseShift(quregVec, control, target, param);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledPhaseShift(quregMatr, control, target, param);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledPhaseShift(quregVec, qb, qb, param), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledPhaseShift(quregVec, qb, 0, param), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledPhaseShift(quregVec, 0, qb, param), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledRotateAroundAxis
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledRotateAroundAxis", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
// each test will use a random parameter and axis vector
qreal param = getRandomReal(-4*M_PI, 4*M_PI);
Vector vec;
vec.x=getRandomReal(-1,1);
vec.y=getRandomReal(-1,1);
vec.z=getRandomReal(-1,1);
// Rn(a) = cos(a/2)I - i sin(a/2) n . paulivector
// (pg 24 of vcpc.univie.ac.at/~ian/hotlist/qc/talks/bloch-sphere-rotations.pdf)
qreal c = cos(param/2);
qreal s = sin(param/2);
qreal m = sqrt(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
QMatrix op{{c - qcomp(0,1)*vec.z*s/m, -(vec.y + qcomp(0,1)*vec.x)*s/m},
{(vec.y - qcomp(0,1)*vec.x)*s/m, c + qcomp(0,1)*vec.z*s/m}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledRotateAroundAxis(quregVec, control, target, param, vec);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledRotateAroundAxis(quregMatr, control, target, param, vec);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledRotateAroundAxis(quregVec, qb, qb, param, vec), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledRotateAroundAxis(quregVec, qb, 0, param, vec), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledRotateAroundAxis(quregVec, 0, qb, param, vec), Contains("Invalid target") );
}
SECTION( "zero rotation axis" ) {
vec.x=0; vec.y=0; vec.z=0;
REQUIRE_THROWS_WITH( controlledRotateAroundAxis(quregVec, 0, 1, param, vec), Contains("Invalid axis") && Contains("zero") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledRotateX
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledRotateX", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qreal param = getRandomReal(-4*M_PI, 4*M_PI);
QMatrix op{
{cos(param/2), -sin(param/2)*qcomp(0,1)},
{-sin(param/2)*qcomp(0,1), cos(param/2)}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledRotateX(quregVec, control, target, param);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledRotateX(quregMatr, control, target, param);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledRotateX(quregVec, qb, qb, param), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledRotateX(quregVec, qb, 0, param), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledRotateX(quregVec, 0, qb, param), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledRotateY
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledRotateY", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qreal param = getRandomReal(-4*M_PI, 4*M_PI);
QMatrix op{{cos(param/2), -sin(param/2)},{sin(param/2), cos(param/2)}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledRotateY(quregVec, control, target, param);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledRotateY(quregMatr, control, target, param);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 4*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledRotateY(quregVec, qb, qb, param), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledRotateY(quregVec, qb, 0, param), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledRotateY(quregVec, 0, qb, param), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledRotateZ
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledRotateZ", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qreal param = getRandomReal(-4*M_PI, 4*M_PI);
QMatrix op{{expI(-param/2.),0},{0,expI(param/2.)}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledRotateZ(quregVec, control, target, param);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledRotateZ(quregMatr, control, target, param);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledRotateZ(quregVec, qb, qb, param), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledRotateZ(quregVec, qb, 0, param), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledRotateZ(quregVec, 0, qb, param), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledTwoQubitUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledTwoQubitUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
// in distributed mode, each node must be able to fit all amps modified by unitary
REQUIRE( quregVec.numAmpsPerChunk >= 4 );
// every test will use a unique random matrix
QMatrix op = getRandomUnitary(2);
ComplexMatrix4 matr = toComplexMatrix4(op);
SECTION( "correctness" ) {
int targ1 = GENERATE( range(0,NUM_QUBITS) );
int targ2 = GENERATE_COPY( filter([=](int t){ return t!=targ1; }, range(0,NUM_QUBITS)) );
int control = GENERATE_COPY( filter([=](int c){ return c!=targ1 && c!=targ2; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledTwoQubitUnitary(quregVec, control, targ1, targ2, matr);
applyReferenceOp(refVec, control, targ1, targ2, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledTwoQubitUnitary(quregMatr, control, targ1, targ2, matr);
applyReferenceOp(refMatr, control, targ1, targ2, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "repetition of targets" ) {
int targ = 0;
int ctrl = 1;
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, ctrl, targ, targ, matr), Contains("target") && Contains("unique") );
}
SECTION( "control and target collision" ) {
int targ1 = 1;
int targ2 = 2;
int ctrl = GENERATE( 1,2 ); // catch2 bug; can't do GENERATE_COPY( targ1, targ2 )
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, ctrl, targ1, targ2, matr), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
// valid config
int ctrl = 0;
int targ1 = 1;
int targ2 = 2;
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, qb, targ1, targ2, matr), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, ctrl, qb, targ2, matr), Contains("Invalid target") );
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, ctrl, targ1, qb, matr), Contains("Invalid target") );
}
SECTION( "unitarity" ) {
matr.real[0][0] = 0; // break matr unitarity
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, 0, 1, 2, matr), Contains("unitary") );
}
SECTION( "unitary fits in node" ) {
// pretend we have a very limited distributed memory
quregVec.numAmpsPerChunk = 1;
REQUIRE_THROWS_WITH( controlledTwoQubitUnitary(quregVec, 0, 1, 2, matr), Contains("targets too many qubits"));
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa controlledUnitary
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "controlledUnitary", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
QMatrix op = getRandomUnitary(1);
ComplexMatrix2 matr = toComplexMatrix2(op);
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
int control = GENERATE_COPY( filter([=](int c){ return c!=target; }, range(0,NUM_QUBITS)) );
SECTION( "state-vector" ) {
controlledUnitary(quregVec, control, target, matr);
applyReferenceOp(refVec, control, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
controlledUnitary(quregMatr, control, target, matr);
applyReferenceOp(refMatr, control, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "control and target collision" ) {
int qb = GENERATE( range(0,NUM_QUBITS) );
REQUIRE_THROWS_WITH( controlledUnitary(quregVec, qb, qb, matr), Contains("Control") && Contains("target") );
}
SECTION( "qubit indices" ) {
int qb = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( controlledUnitary(quregVec, qb, 0, matr), Contains("Invalid control") );
REQUIRE_THROWS_WITH( controlledUnitary(quregVec, 0, qb, matr), Contains("Invalid target") );
}
SECTION( "unitarity" ) {
matr.real[0][0] = 0; // break unitarity
REQUIRE_THROWS_WITH( controlledUnitary(quregVec, 0, 1, matr), Contains("unitary") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa hadamard
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "hadamard", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
qreal a = 1/sqrt(2);
QMatrix op{{a,a},{a,-a}};
SECTION( "correctness" ) {
int target = GENERATE( range(0,NUM_QUBITS) );
SECTION( "state-vector ") {
hadamard(quregVec, target);
applyReferenceOp(refVec, target, op);
REQUIRE( areEqual(quregVec, refVec) );
}
SECTION( "density-matrix" ) {
hadamard(quregMatr, target);
applyReferenceOp(refMatr, target, op);
REQUIRE( areEqual(quregMatr, refMatr, 10*REAL_EPS) );
}
}
SECTION( "input validation" ) {
SECTION( "qubit indices" ) {
int target = GENERATE( -1, NUM_QUBITS );
REQUIRE_THROWS_WITH( hadamard(quregVec, target), Contains("Invalid target") );
}
}
CLEANUP_TEST( quregVec, quregMatr );
}
/** @sa multiControlledMultiQubitNot
* @ingroup unittest
* @author Tyson Jones
*/
TEST_CASE( "multiControlledMultiQubitNot", "[unitaries]" ) {
PREPARE_TEST( quregVec, quregMatr, refVec, refMatr );
SECTION( "correctness" ) {
// try all possible numbers of targets and controls
int numTargs = GENERATE_COPY( range(1,NUM_QUBITS) ); // leave space for 1 ctrl
int maxNumCtrls = NUM_QUBITS - numTargs;
int numCtrls = GENERATE_COPY( range(1,maxNumCtrls+1) );
// generate all possible valid qubit arrangements
int* targs = GENERATE_COPY( sublists(range(0,NUM_QUBITS), numTargs) );
int* ctrls = GENERATE_COPY( sublists(range(0,NUM_QUBITS), numCtrls, targs, numTargs) );
// for each qubit arrangement, use a new random unitary
QMatrix notOp{{0, 1},{1,0}};
SECTION( "state-vector" ) {
multiControlledMultiQubitNot(quregVec, ctrls, numCtrls, targs, numTargs);
for (int t=0; t<numTargs; t++)