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

Function getPartialTrace

tests/utils/linalg.cpp:422–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

420
421
422qmatrix getPartialTrace(qmatrix in, vector<int> targets) {
423 DEMAND( in.size() > getPow2(targets.size()) );
424
425 auto numTargs = targets.size();
426 auto numQubits = getLog2(in.size());
427 auto numTargVals = getPow2(numTargs);
428
429 qmatrix out = getZeroMatrix(getPow2(numQubits - numTargs));
430
431 for (qindex v=0; v<numTargVals; v++) {
432
433 // prepare { |0>, I, I, |1>, ... }
434 vector<qmatrix> matrices(numQubits, getIdentityMatrix(2));
435 for (size_t t=0; t<numTargs; t++) {
436 int bit = getBitAt(v, t);
437 matrices[targets[t]] = {
438 {bit? 0.:1.},
439 {bit? 1.:0.}};
440 }
441
442 qmatrix ket = getKroneckerProduct(matrices);
443 qmatrix bra = getConjugateTranspose(ket);
444 out += bra * in * ket;
445 }
446
447 return out;
448}
449
450
451qmatrix getControlledMatrix(qmatrix matrix, int numCtrls) {

Callers 1

SECTIONFunction · 0.85

Calls 7

getPow2Function · 0.85
getLog2Function · 0.85
getBitAtFunction · 0.85
getZeroMatrixFunction · 0.70
getIdentityMatrixFunction · 0.70
getKroneckerProductFunction · 0.70
getConjugateTransposeFunction · 0.70

Tested by

no test coverage detected