@@ -444,6 +444,89 @@ TEST_CASE( "setAmps", "[state_initialisations]" ) {
444
444
445
445
446
446
447
+ /* * @sa setDensityAmps
448
+ * @ingroup unittest
449
+ * @author Tyson Jones
450
+ */
451
+ TEST_CASE ( " setDensityAmps" , " [state_initialisations]" ) {
452
+
453
+ Qureg matr = createDensityQureg (NUM_QUBITS, QUEST_ENV);
454
+ qreal reals[matr.numAmpsTotal ];
455
+ qreal imags[matr.numAmpsTotal ];
456
+ int maxInd = (1 << NUM_QUBITS);
457
+
458
+ SECTION ( " correctness" ) {
459
+
460
+ SECTION ( " density-matrix" ) {
461
+
462
+ // all valid number of amplitudes and offsets
463
+ int startRow = GENERATE_COPY ( range (0 ,maxInd) );
464
+ int startCol = GENERATE_COPY ( range (0 ,maxInd) );
465
+
466
+ int numPriorAmps = startRow + startCol*(1 << matr.numQubitsRepresented );
467
+ int maxNumAmps = matr.numAmpsTotal - numPriorAmps;
468
+ int numAmps = GENERATE_COPY ( range (0 ,maxNumAmps) ); // upper-bound allows all amps specified
469
+
470
+ // generate random amplitudes
471
+ for (int i=0 ; i<numAmps; i++) {
472
+ reals[i] = getRandomReal (-5 ,5 );
473
+ imags[i] = getRandomReal (-5 ,5 );
474
+ }
475
+
476
+ // check both specified and un-specified amplitudes are correctly handled
477
+ initDebugState (matr);
478
+ QMatrix matrRef = toQMatrix (matr);
479
+
480
+ setDensityAmps (matr, startRow, startCol, reals, imags, numAmps);
481
+
482
+ int r=startRow;
483
+ int c=startCol;
484
+ for (int i=0 ; i<numAmps; i++) {
485
+ qcomp amp = reals[i] + imags[i] * (qcomp) 1i;
486
+ matrRef[r][c] = amp;
487
+
488
+ r++;
489
+ if (r >= maxInd ) {
490
+ r=0 ;
491
+ c++;
492
+ }
493
+ }
494
+
495
+ REQUIRE ( areEqual (matr, matrRef) );
496
+ }
497
+ }
498
+ SECTION ( " input validation" ) {
499
+
500
+ SECTION ( " start index" ) {
501
+
502
+ int badInd = GENERATE_COPY ( -1 , maxInd );
503
+ int numAmps = 0 ;
504
+ REQUIRE_THROWS_WITH ( setDensityAmps (matr, badInd, 0 , reals, imags, numAmps), Contains (" Invalid amplitude index" ) );
505
+ REQUIRE_THROWS_WITH ( setDensityAmps (matr, 0 , badInd, reals, imags, numAmps), Contains (" Invalid amplitude index" ) );
506
+ }
507
+
508
+ SECTION ( " number of amplitudes" ) {
509
+
510
+ // independent
511
+ int numAmps = GENERATE_COPY ( -1 , matr.numAmpsTotal +1 );
512
+ REQUIRE_THROWS_WITH ( setDensityAmps (matr, 0 , 0 , reals, imags, numAmps), Contains (" Invalid number of amplitudes" ) );
513
+
514
+ // invalid considering start-index
515
+ REQUIRE_THROWS_WITH ( setDensityAmps (matr, maxInd-1 , maxInd-1 , reals, imags, 2 ), Contains (" More amplitudes given than exist" ) );
516
+ REQUIRE_THROWS_WITH ( setDensityAmps (matr, maxInd-1 , maxInd-2 , reals, imags, maxInd+2 ), Contains (" More amplitudes given than exist" ) );
517
+ }
518
+ SECTION ( " state-vector" ) {
519
+
520
+ Qureg vec = createQureg (NUM_QUBITS, QUEST_ENV);
521
+ REQUIRE_THROWS_WITH ( setDensityAmps (vec, 0 , 0 , reals, imags, 0 ), Contains (" valid only for density matrices" ) );
522
+ destroyQureg (vec, QUEST_ENV);
523
+ }
524
+ }
525
+ destroyQureg (matr, QUEST_ENV);
526
+ }
527
+
528
+
529
+
447
530
/* * @sa setWeightedQureg
448
531
* @ingroup unittest
449
532
* @author Tyson Jones
0 commit comments