MCPcopy Create free account
hub / github.com/QuEST-Kit/QuEST / getSwapMatrix

Function getSwapMatrix

tests/deprecated/test_utilities.cpp:343–389  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341}
342
343QMatrix getSwapMatrix(int qb1, int qb2, int numQb) {
344 DEMAND( numQb > 1 );
345 DEMAND( (qb1 >= 0 && qb1 < numQb) );
346 DEMAND( (qb2 >= 0 && qb2 < numQb) );
347
348 if (qb1 > qb2)
349 std::swap(qb1, qb2);
350
351 if (qb1 == qb2)
352 return getIdentityMatrix(1 << numQb);
353
354 QMatrix swap;
355
356 if (qb2 == qb1 + 1) {
357 // qubits are adjacent
358 swap = QMatrix{{1,0,0,0},{0,0,1,0},{0,1,0,0},{0,0,0,1}};
359
360 } else {
361 // qubits are distant
362 int block = 1 << (qb2 - qb1);
363 swap = getZeroMatrix(block*2);
364 QMatrix iden = getIdentityMatrix(block/2);
365
366 // Lemma 3.1 of arxiv.org/pdf/1711.09765.pdf
367 QMatrix p0{{1,0},{0,0}};
368 QMatrix l0{{0,1},{0,0}};
369 QMatrix l1{{0,0},{1,0}};
370 QMatrix p1{{0,0},{0,1}};
371
372 /* notating a^(n+1) = identity(1<<n) (otimes) a, we construct the matrix
373 * [ p0^(N) l1^N ]
374 * [ l0^(N) p1^N ]
375 * where N = qb2 - qb1 */
376 setSubMatrix(swap, getKroneckerProduct(iden, p0), 0, 0);
377 setSubMatrix(swap, getKroneckerProduct(iden, l0), block, 0);
378 setSubMatrix(swap, getKroneckerProduct(iden, l1), 0, block);
379 setSubMatrix(swap, getKroneckerProduct(iden, p1), block, block);
380 }
381
382 // pad swap with outer identities
383 if (qb1 > 0)
384 swap = getKroneckerProduct(swap, getIdentityMatrix(1<<qb1));
385 if (qb2 < numQb-1)
386 swap = getKroneckerProduct(getIdentityMatrix(1<<(numQb-qb2-1)), swap);
387
388 return swap;
389}
390
391/* (don't generate doxygen doc)
392 *

Callers 1

getFullOperatorMatrixFunction · 0.70

Calls 4

getIdentityMatrixFunction · 0.70
getZeroMatrixFunction · 0.70
setSubMatrixFunction · 0.70
getKroneckerProductFunction · 0.70

Tested by

no test coverage detected