Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/calcunits/QC_crot.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Wed Aug 3 10:14:23 2011 UTC (12 years, 9 months ago) by kaityo
File MIME type: text/x-c++src
File size: 2701 byte(s)
First commit
1 //---------------------------------------------------------------------------
2 // Controlled Rotate Gate
3 //---------------------------------------------------------------------------
4 #include <cmath>
5 #include <math.h>
6 #include "QC_crot.h"
7 //---------------------------------------------------------------------------
8 /**
9 * Constructor
10 */
11 QC_crot::QC_crot(int _TargetBit,int _ControlBit, double degree) : QCalcUnit() {
12 TargetBit = _TargetBit;
13 ControlBit = _ControlBit;
14 Theta = M_PI * degree / 180.0;
15 }
16 //---------------------------------------------------------------------------
17 /**
18 * Calculation
19 */
20 void
21 QC_crot::calc(int Target, int Control, double theta,
22 double R[], double I[], int NumberOfBits) {
23 unsigned int states = 1<< (NumberOfBits - 2);
24
25 int Bit0 = Target;
26 int Bit1 = Control;
27 if (Bit0 > Bit1) swap(Bit0, Bit1);
28
29 const double c = std::cos(theta);
30 const double s = std::sin(theta);
31
32 double r1, i1;
33 for (unsigned int i = 0; i < states; i++) {
34 unsigned int ix1 = QCalcUnit::insert1(i, Bit0);
35 ix1 = QCalcUnit::insert1(ix1, Bit1);
36 double r1 = R[ix1];
37 double i1 = I[ix1];
38
39 R[ix1] = c*r1 - s*i1;
40 I[ix1] = s*r1 + c*i1;
41 }
42 }
43 //---------------------------------------------------------------------------
44 void
45 QC_crot::Calc(QBits *qBits) {
46 int N = qBits->GetNumberOfQBits();
47 double *R = qBits->GetBitsR();//Real Part
48 double *I = qBits->GetBitsI();//Imaginary Part
49
50 QC_crot::calc(TargetBit, ControlBit, Theta, R, I, N);
51 }
52 //---------------------------------------------------------------------------
53 #ifdef __USE__MPI
54 void
55 QC_crot::calcmpi(int t1, int c1, double theta, double R[], double I[], int N) {
56 const double c = std::cos(theta);
57 const double s = std::sin(theta);
58 double r0 = 0.0;
59 double i0 = 0.0;
60 double r1 = 0.0;
61 double i1 = 0.0;
62 unsigned int ix0, ix1;
63
64 // Sort BitsNumber to regular order
65 int Bit0 = t1;
66 int Bit1 = c1;
67 if (Bit0 > Bit1) {
68 swap(Bit0, Bit1);
69 }
70
71 for (int i = 0; i < (1 << (N - 2)); i++) {
72 // Obtain indices of state:
73 unsigned int ix1 = QCalcUnit::insert1(i, Bit0);
74 ix1 = QCalcUnit::insert1(ix1, Bit1);
75 unsigned int ix0 = ix1 & ~(1 << t1);
76
77 bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1);
78 if (bstore) {
79 double nr0 = r0;
80 double ni0 = i0;
81 double nr1 = c*r1 - s*i1;
82 double ni1 = s*r1 + c*i1;
83 // Store:
84 store(R, I, ix0, ix1, nr0, ni0, nr1, ni1);
85 }
86 }
87 }
88 #endif
89 //---------------------------------------------------------------------------

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26