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

Function getFullOperatorMatrix

tests/deprecated/test_utilities.cpp:417–483  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

415}
416
417QMatrix getFullOperatorMatrix(
418 int* ctrls, int numCtrls, int *targs, int numTargs, QMatrix op, int numQubits
419) {
420 DEMAND( numCtrls >= 0 );
421 DEMAND( numTargs >= 0 );
422 DEMAND( numQubits >= (numCtrls+numTargs) );
423 DEMAND( op.size() == (1u << numTargs) );
424
425 // copy {ctrls} and {targs}to restore at end
426 vector<int> ctrlsCopy(ctrls, ctrls+numCtrls);
427 vector<int> targsCopy(targs, targs+numTargs);
428
429 // full-state matrix of qubit swaps
430 QMatrix swaps = getIdentityMatrix(1 << numQubits);
431 QMatrix unswaps = getIdentityMatrix(1 << numQubits);
432 QMatrix matr;
433
434 // swap targs to {0, ..., numTargs-1}
435 for (int i=0; i<numTargs; i++) {
436 if (i != targs[i]) {
437 matr = getSwapMatrix(i, targs[i], numQubits);
438 swaps = matr * swaps;
439 unswaps = unswaps * matr;
440
441 // even if this is the last targ, ctrls might still need updating
442 updateIndices(
443 i, targs[i], (i < numTargs-1)? &targs[i+1] : NULL,
444 numTargs-i-1, ctrls, numCtrls);
445 }
446 }
447
448 // swap ctrls to {numTargs, ..., numTargs+numCtrls-1}
449 for (int i=0; i<numCtrls; i++) {
450 int newInd = numTargs+i;
451 if (newInd != ctrls[i]) {
452 matr = getSwapMatrix(newInd, ctrls[i], numQubits);
453 swaps = matr * swaps;
454 unswaps = unswaps * matr;
455
456 // update remaining ctrls (if any exist)
457 if (i < numCtrls-1)
458 updateIndices(newInd, ctrls[i], NULL, 0, &ctrls[i+1], numCtrls-i-1);
459 }
460 }
461
462 // construct controlled-op matrix for qubits {0, ..., numCtrls+numTargs-1}
463 size_t dim = 1 << (numCtrls+numTargs);
464 QMatrix fullOp = getIdentityMatrix(dim);
465 setSubMatrix(fullOp, op, dim-op.size(), dim-op.size());
466
467 // create full-state controlled-op matrix (left-pad identities)
468 if (numQubits > numCtrls+numTargs) {
469 size_t pad = 1 << (numQubits - numCtrls - numTargs);
470 fullOp = getKroneckerProduct(getIdentityMatrix(pad), fullOp);
471 }
472
473 // apply swap to either side (to swap qubits back and forth)
474 fullOp = unswaps * fullOp * swaps;

Callers 4

test_unitaries.cppFile · 0.85
applyReferenceOpFunction · 0.85
applyReferenceMatrixFunction · 0.85

Calls 5

updateIndicesFunction · 0.85
getIdentityMatrixFunction · 0.70
getSwapMatrixFunction · 0.70
setSubMatrixFunction · 0.70
getKroneckerProductFunction · 0.70

Tested by

no test coverage detected