| 1 |
//--------------------------------------------------------------------------- |
| 2 |
// Rotate Gate |
| 3 |
//--------------------------------------------------------------------------- |
| 4 |
#include <cmath> |
| 5 |
#include <math.h> |
| 6 |
#include "QC_rot.h" |
| 7 |
//--------------------------------------------------------------------------- |
| 8 |
/** |
| 9 |
* |
| 10 |
*/ |
| 11 |
QC_rot::QC_rot(int _TargetBit,double degree) : QCalcUnit() { |
| 12 |
TargetBit = _TargetBit; |
| 13 |
Theta = M_PI * degree / 180.0; |
| 14 |
} |
| 15 |
//--------------------------------------------------------------------------- |
| 16 |
/** |
| 17 |
* Calculation |
| 18 |
*/ |
| 19 |
void |
| 20 |
QC_rot::calc(int target, double theta, |
| 21 |
double R[], double I[], int NumberOfBits) { |
| 22 |
const double c = std::cos(theta); |
| 23 |
const double s = std::sin(theta); |
| 24 |
double r1, i1; |
| 25 |
unsigned int states = (1 << (NumberOfBits - 1)); |
| 26 |
for (unsigned int i = 0; i < states; i++) { |
| 27 |
unsigned int ix1 = QCalcUnit::insert1(i, target); |
| 28 |
double r1 = R[ix1]; |
| 29 |
double i1 = I[ix1]; |
| 30 |
|
| 31 |
R[ix1] = c*r1 - s*i1; |
| 32 |
I[ix1] = s*r1 + c*i1; |
| 33 |
} |
| 34 |
} |
| 35 |
//--------------------------------------------------------------------------- |
| 36 |
void QC_rot::Calc(QBits *qBits) |
| 37 |
{ |
| 38 |
int N = qBits->GetNumberOfQBits(); |
| 39 |
double *R = qBits->GetBitsR();//Real Part |
| 40 |
double *I = qBits->GetBitsI();//Imaginary Part |
| 41 |
QC_rot::calc(TargetBit, Theta, R, I, N); |
| 42 |
} |
| 43 |
//--------------------------------------------------------------------------- |
| 44 |
#ifdef __USE__MPI |
| 45 |
void |
| 46 |
QC_rot::calcmpi(int t1, double theta, double R[], double I[], int N) { |
| 47 |
const double c = std::cos(theta); |
| 48 |
const double s = std::sin(theta); |
| 49 |
double r0 = 0.0; |
| 50 |
double i0 = 0.0; |
| 51 |
double r1 = 0.0; |
| 52 |
double i1 = 0.0; |
| 53 |
unsigned int ix0, ix1; |
| 54 |
|
| 55 |
for (int i = 0; i < (1 << (N - 1)); i++) { |
| 56 |
// Obtain indices of state: |
| 57 |
ix0 = QCalcUnit::insert0(i, t1); |
| 58 |
ix1 = QCalcUnit::insert1(i, t1); |
| 59 |
|
| 60 |
bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1); |
| 61 |
|
| 62 |
if (bstore) { |
| 63 |
double nr0 = r0; |
| 64 |
double ni0 = i0; |
| 65 |
double nr1 = c*r1 - s*i1; |
| 66 |
double ni1 = s*r1 + c*i1; |
| 67 |
// Store: |
| 68 |
store(R, I, ix0, ix1, nr0, ni0, nr1, ni1); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
#endif |
| 73 |
//--------------------------------------------------------------------------- |