The Quantum Exact Simulation Toolkit v4.0.0
Loading...
Searching...
No Matches
Initialisation
 TEST_CASE ("initBlankState", TEST_CATEGORY)
 
 TEST_CASE ("initZeroState", TEST_CATEGORY)
 
 TEST_CASE ("initPlusState", TEST_CATEGORY)
 
 TEST_CASE ("initClassicalState", TEST_CATEGORY)
 
 TEST_CASE ("initDebugState", TEST_CATEGORY)
 
 TEST_CASE ("initRandomPureState", TEST_CATEGORY)
 
 TEST_CASE ("initRandomMixedState", TEST_CATEGORY)
 
 TEST_CASE ("initArbitraryPureState", TEST_CATEGORY)
 
 TEST_CASE ("setQuregAmps", TEST_CATEGORY)
 
 TEST_CASE ("setDensityQuregFlatAmps", TEST_CATEGORY)
 
 TEST_CASE ("setDensityQuregAmps", TEST_CATEGORY)
 
 TEST_CASE ("setQuregToRenormalized", TEST_CATEGORY)
 
 TEST_CASE ("setQuregToPauliStrSum", TEST_CATEGORY)
 

Detailed Description

Function Documentation

◆ TEST_CASE() [1/13]

TEST_CASE ( "initArbitraryPureState" ,
TEST_CATEGORY  )

Definition at line 224 of file initialisations.cpp.

224 {
225
226 SECTION( LABEL_CORRECTNESS ) {
227
228 // works for unnormalised states
229 qvector refVec = getRandomVector(getPow2(getNumCachedQubits()));
230 qmatrix refMat = getOuterProduct(refVec, refVec);
231
232 auto apiFunc = [&](Qureg qureg) { initArbitraryPureState(qureg, refVec.data()); };
233
234 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), apiFunc, refVec); }
235 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), apiFunc, refMat); }
236 }
237
238 /// @todo input validation
239}
void initArbitraryPureState(Qureg qureg, qcomp *amps)
Definition qureg.h:49

◆ TEST_CASE() [2/13]

TEST_CASE ( "initBlankState" ,
TEST_CATEGORY  )

TESTS

Definition at line 72 of file initialisations.cpp.

72 {
73
74 SECTION( LABEL_CORRECTNESS ) {
75
76 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), initBlankState, getRefStatevec()); }
77 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), initBlankState, getRefDensmatr()); }
78 }
79
80 /// @todo input validation
81}
void initBlankState(Qureg qureg)

◆ TEST_CASE() [3/13]

TEST_CASE ( "initClassicalState" ,
TEST_CATEGORY  )

Definition at line 118 of file initialisations.cpp.

118 {
119
120 SECTION( LABEL_CORRECTNESS ) {
121
122 int numQubits = getNumCachedQubits();
123 int numInds = (int) getPow2(numQubits);
124 int stateInd = GENERATE_COPY( range(0,numInds) );
125
126 qvector refVec = getRefStatevec(); refVec[stateInd] = 1; // |i> = {0, ..., 1, 0, ...}
127 qmatrix refMat = getRefDensmatr(); refMat[stateInd][stateInd] = 1; // |i><i|
128
129 auto apiFunc = [&](Qureg qureg) { initClassicalState(qureg, stateInd); };
130
131 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), apiFunc, refVec); }
132 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), apiFunc, refMat); }
133 }
134
135 /// @todo input validation
136}
void initClassicalState(Qureg qureg, qindex stateInd)

◆ TEST_CASE() [4/13]

TEST_CASE ( "initDebugState" ,
TEST_CATEGORY  )

Definition at line 139 of file initialisations.cpp.

139 {
140
141 SECTION( LABEL_CORRECTNESS ) {
142
143 qvector refVec = getRefStatevec(); setToDebugState(refVec); // |debug>
144 qmatrix refMat = getRefDensmatr(); setToDebugState(refMat); // ||debug>>
145
146 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), initDebugState, refVec); }
147 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), initDebugState, refMat); }
148 }
149
150 /// @todo input validation
151}
void initDebugState(Qureg qureg)

◆ TEST_CASE() [5/13]

TEST_CASE ( "initPlusState" ,
TEST_CATEGORY  )

Definition at line 99 of file initialisations.cpp.

99 {
100
101 SECTION( LABEL_CORRECTNESS ) {
102
103 int numQubits = getNumCachedQubits();
104 qreal vecElem = 1. / std::sqrt(getPow2(numQubits));
105 qreal matElem = 1. / getPow2(numQubits);
106
107 qvector refVec = getConstantVector(getPow2(numQubits), vecElem); // |+> = 1/sqrt(2^N) {1, ...}
108 qmatrix refMat = getConstantMatrix(getPow2(numQubits), matElem); // |+><+| = 1/2^N {{1, ...}, ...}
109
110 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), initPlusState, refVec); }
111 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), initPlusState, refMat); }
112 }
113
114 /// @todo input validation
115}
void initPlusState(Qureg qureg)

◆ TEST_CASE() [6/13]

TEST_CASE ( "initRandomMixedState" ,
TEST_CATEGORY  )

Definition at line 187 of file initialisations.cpp.

187 {
188
189 SECTION( LABEL_CORRECTNESS ) {
190
191 // this test does not use reference states
192
193 GENERATE( range(0,10) );
194 int numPureStates = GENERATE( 1, 2, 10 );
195
196 auto testFunc = [&](Qureg qureg) {
197
198 initRandomMixedState(qureg, numPureStates);
199
200 /// @todo
201 /// these not-all-same-amp checks can be made much more rigorous,
202 /// by e.g. asserting distinct nodes haven't generated all the same
203 /// amplitudes (we currently observe this by eye)
204 syncQuregFromGpu(qureg);
205 REQUIRE( qureg.cpuAmps[0] != qureg.cpuAmps[1] ); // performed on all nodes
206
207 qreal prob = calcTotalProb(qureg);
208 REQUIRE_AGREE( prob, 1 );
209
210 qreal purity = calcPurity(qureg);
211 if (numPureStates == 1)
212 REQUIRE_AGREE( purity, 1 );
213 else
214 REQUIRE( purity < 1 );
215 };
216
217 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), testFunc); }
218 }
219
220 /// @todo input validation
221}
qreal calcPurity(Qureg qureg)
qreal calcTotalProb(Qureg qureg)
void initRandomMixedState(Qureg qureg, qindex numPureStates)
void syncQuregFromGpu(Qureg qureg)
Definition qureg.cpp:399

◆ TEST_CASE() [7/13]

TEST_CASE ( "initRandomPureState" ,
TEST_CATEGORY  )

Definition at line 154 of file initialisations.cpp.

154 {
155
156 SECTION( LABEL_CORRECTNESS ) {
157
158 // this test does not use reference states
159 GENERATE( range(0,10) );
160
161 auto testFunc = [&](Qureg qureg) {
162
163 initRandomPureState(qureg);
164
165 /// @todo
166 /// these not-all-same-amp checks can be made much more rigorous,
167 /// by e.g. asserting distinct nodes haven't generated all the same
168 /// amplitudes (we currently observe this by eye)
169 syncQuregFromGpu(qureg);
170 REQUIRE( qureg.cpuAmps[0] != qureg.cpuAmps[1] );
171
172 qreal prob = calcTotalProb(qureg);
173 REQUIRE_AGREE( prob, 1 );
174
175 qreal purity = calcPurity(qureg);
176 REQUIRE_AGREE( purity, 1 );
177 };
178
179 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), testFunc); }
180 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), testFunc); }
181 }
182
183 /// @todo input validation
184}
void initRandomPureState(Qureg qureg)

◆ TEST_CASE() [8/13]

TEST_CASE ( "initZeroState" ,
TEST_CATEGORY  )

Definition at line 84 of file initialisations.cpp.

84 {
85
86 SECTION( LABEL_CORRECTNESS ) {
87
88 qvector refVec = getRefStatevec(); refVec[0] = 1; // |0> = {1, 0...}
89 qmatrix refMat = getRefDensmatr(); refMat[0][0] = 1; // |0><0| = {{1,0...},{0...}...}
90
91 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), initZeroState, refVec); }
92 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), initZeroState, refMat); }
93 }
94
95 /// @todo input validation
96}
void initZeroState(Qureg qureg)

◆ TEST_CASE() [9/13]

TEST_CASE ( "setDensityQuregAmps" ,
TEST_CATEGORY  )

Definition at line 313 of file initialisations.cpp.

313 {
314
315 //void setDensityQuregAmps(Qureg qureg, qindex startRow, qindex startCol, qcomp** amps, qindex numRows, qindex numCols);
316
317
318 SECTION( LABEL_CORRECTNESS ) {
319
320 int numTotalRowsCols = getPow2(getNumCachedQubits());
321
322 // systematic iteration is WAY too slow
323 GENERATE( range(0,1000) );
324 int numSetRows = getRandomInt(1, numTotalRowsCols+1);
325 int numSetCols = getRandomInt(1, numTotalRowsCols+1);
326 int startRow = getRandomInt(0, numTotalRowsCols - numSetRows);
327 int startCol = getRandomInt(0, numTotalRowsCols - numSetCols);
328
329 // caution that amps is 'qmatrix' despite not being square
330 qmatrix amps = getRandomNonSquareMatrix(numSetRows, numSetCols);
331
332 auto testFunc = [&](Qureg qureg) {
333
334 // initialise qureg randomly
335 qmatrix refMat = getRandomMatrix(numTotalRowsCols);
336 setQuregToReference(qureg, refMat);
337
338 // API needs nested pointers
339 std::vector<qcomp*> rowPtrs(numSetRows);
340 for (size_t r=0; r<numSetRows; r++)
341 rowPtrs[r] = amps[r].data();
342
343 // overwrite a sub-matrix of refMat and Qureg
344 setSubMatrix(refMat, amps, startRow, startCol);
345 setDensityQuregAmps(qureg, startRow, startCol, rowPtrs.data(), numSetRows, numSetCols);
346
347 // check that both targeted and non-targeted amps agree
348 REQUIRE_AGREE( qureg, refMat );
349 };
350
351 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), testFunc); }
352
353 }
354
355 /// @todo input validation
356}
void setDensityQuregAmps(Qureg qureg, qindex startRow, qindex startCol, qcomp **amps, qindex numRows, qindex numCols)
void setSubMatrix(qmatrix &dest, qmatrix sub, size_t r, size_t c)
Definition qmatrix.cpp:203
int getRandomInt(int min, int maxExcl)
Definition random.cpp:90

◆ TEST_CASE() [10/13]

TEST_CASE ( "setDensityQuregFlatAmps" ,
TEST_CATEGORY  )

Definition at line 275 of file initialisations.cpp.

275 {
276
277 SECTION( LABEL_CORRECTNESS ) {
278
279 int numTotalRows = getPow2(getNumCachedQubits());
280 int numTotalAmps = numTotalRows * numTotalRows;
281
282 // systematic iteration is WAY too slow
283 GENERATE( range(0,1000) );
284 int numSetAmps = getRandomInt(0, numTotalAmps + 1);
285 int startInd = getRandomInt(0, numTotalAmps - numSetAmps);
286 qvector amps = getRandomVector(numSetAmps);
287
288 auto testFunc = [&](Qureg qureg) {
289
290 // initialise qureg randomly
291 qmatrix refMat = getRandomMatrix(numTotalRows);
292 setQuregToReference(qureg, refMat);
293
294 // overwrite a contiguous region of row-major refMat, column-wise
295 refMat = getTranspose(refMat);
296 setSubMatrix(refMat, amps, startInd);
297 refMat = getTranspose(refMat);
298
299 // modify the same contiguous region of column-major qureg
300 setDensityQuregFlatAmps(qureg, startInd, amps.data(), numSetAmps);
301
302 // check that both targeted and non-targeted amps agree
303 REQUIRE_AGREE( qureg, refMat );
304 };
305
306 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), testFunc); }
307 }
308
309 /// @todo input validation
310}
void setDensityQuregFlatAmps(Qureg qureg, qindex startInd, qcomp *amps, qindex numAmps)

◆ TEST_CASE() [11/13]

TEST_CASE ( "setQuregAmps" ,
TEST_CATEGORY  )

Definition at line 244 of file initialisations.cpp.

244 {
245
246 SECTION( LABEL_CORRECTNESS ) {
247
248 int numTotalAmps = getPow2(getNumCachedQubits());
249 int numSetAmps = GENERATE_COPY( range(0,numTotalAmps+1) );
250 int startInd = GENERATE_COPY( range(0,numTotalAmps-numSetAmps) );
251 qvector amps = getRandomVector(numSetAmps);
252
253 auto testFunc = [&](Qureg qureg) {
254
255 // initialise qureg randomly
256 qvector refVec = getRandomVector(numTotalAmps);
257 setQuregToReference(qureg, refVec);
258
259 // modify only subset of refVec amps and qureg...
260 setSubVector(refVec, amps, startInd);
261 setQuregAmps(qureg, startInd, amps.data(), numSetAmps);
262
263 // so that we simultaneously check targeted amps
264 // are modified while non-targeted are not
265 REQUIRE_AGREE( qureg, refVec );
266 };
267
268 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), testFunc); }
269 }
270
271 /// @todo input validation
272}
void setQuregAmps(Qureg qureg, qindex startInd, qcomp *amps, qindex numAmps)

◆ TEST_CASE() [12/13]

TEST_CASE ( "setQuregToPauliStrSum" ,
TEST_CATEGORY  )

Definition at line 390 of file initialisations.cpp.

390 {
391
392 SECTION( LABEL_CORRECTNESS ) {
393
394 GENERATE( range(0,10) );
395 int numQubits = getNumCachedQubits();
396 int numTerms = GENERATE_COPY( 1, numQubits, getPow2(2*numQubits) );
397 PauliStrSum sum = createRandomPauliStrSum(numQubits, numTerms);
398 qmatrix refMat = getMatrix(sum, numQubits);
399
400 auto apiFunc = [&](Qureg qureg) { setQuregToPauliStrSum(qureg, sum); };
401
402 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), apiFunc, refMat); }
403 }
404
405 /// @todo input validation
406}
void setQuregToPauliStrSum(Qureg qureg, PauliStrSum sum)

◆ TEST_CASE() [13/13]

TEST_CASE ( "setQuregToRenormalized" ,
TEST_CATEGORY  )

Definition at line 359 of file initialisations.cpp.

359 {
360
361 SECTION( LABEL_CORRECTNESS ) {
362
363 GENERATE( range(0,10) );
364 qindex dim = getPow2(getNumCachedQubits());
365 qvector refVec = getRandomVector(dim);
366 qmatrix refMat = getRandomMatrix(dim);
367
368 // eliminate random chance of tr(refMat)=0, triggering validation
369 if (doScalarsAgree(getTrace(refMat), 0))
370 refMat[0][0] += 1/(qreal) dim;
371
372 // [=] stores current (pre-normalised) reference objects
373 auto funcVec = [=](Qureg qureg) { setQuregToReference(qureg, refVec); setQuregToRenormalized(qureg); };
374 auto funcMat = [=](Qureg qureg) { setQuregToReference(qureg, refMat); setQuregToRenormalized(qureg); };
375
376 // setQuregToRenormalized() makes statevectors become valid
377 refVec = getNormalised(refVec);
378
379 // but it only divides density matrices by the sum of the real-elems of their diagonals
380 refMat /= getReferenceProbability(refMat);
381
382 SECTION( LABEL_STATEVEC ) { TEST_ON_CACHED_QUREGS(getCachedStatevecs(), funcVec, refVec); }
383 SECTION( LABEL_DENSMATR ) { TEST_ON_CACHED_QUREGS(getCachedDensmatrs(), funcMat, refMat); }
384 }
385
386 /// @todo input validation
387}
qreal setQuregToRenormalized(Qureg qureg)