Skip to content

Commit

Permalink
Minor tweak to effectcrep
Browse files Browse the repository at this point in the history
Slightly more efficient parity implementation (fewer operations), and add the compiler hint to inline the parity function. In profiling this makes a surprisingly big difference.
  • Loading branch information
Corey Ostrove committed Sep 30, 2024
1 parent 5f34a9a commit 81ced99
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pygsti/evotypes/densitymx/effectcreps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace CReps_densitymx {
finalIndx += ((finds >> k) & 1) * 3 * base;
base = base >> 2; // /= 4 so base == 4**(N-1-k)
}

//Apply result
if(parity(finds & _zvals_int))
ret -= _abs_elval * state->_dataptr[finalIndx]; // minus sign
Expand All @@ -157,15 +157,25 @@ namespace CReps_densitymx {
return ret;
}

INT EffectCRep_Computational::parity(INT x) {
// int64-bit specific
x = (x & 0x00000000FFFFFFFF)^(x >> 32);
x = (x & 0x000000000000FFFF)^(x >> 16);
x = (x & 0x00000000000000FF)^(x >> 8);
x = (x & 0x000000000000000F)^(x >> 4);
x = (x & 0x0000000000000003)^(x >> 2);
x = (x & 0x0000000000000001)^(x >> 1);
return x & 1; // return the last bit (0 or 1)
// INT EffectCRep_Computational::parity(INT x) {
// // int64-bit specific
// x = (x & 0x00000000FFFFFFFF)^(x >> 32);
// x = (x & 0x000000000000FFFF)^(x >> 16);
// x = (x & 0x00000000000000FF)^(x >> 8);
// x = (x & 0x000000000000000F)^(x >> 4);
// x = (x & 0x0000000000000003)^(x >> 2);
// x = (x & 0x0000000000000001)^(x >> 1);
// return x & 1; // return the last bit (0 or 1)
// }

inline INT EffectCRep_Computational::parity(INT x) {
x ^= (x >> 32);
x ^= (x >> 16);
x ^= (x >> 8);
x ^= (x >> 4);
x ^= (x >> 2);
x ^= (x >> 1);
return x & 1; // Return the last bit
}


Expand Down

0 comments on commit 81ced99

Please sign in to comment.