Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/calcunits/QC_hadamard.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, 8 months ago) by kaityo
File MIME type: text/x-c++src
File size: 2264 byte(s)
First commit
1 //---------------------------------------------------------------------------
2 // Hadamard gate
3 //---------------------------------------------------------------------------
4 #include <cmath>
5 #include "QC_hadamard.h"
6 //---------------------------------------------------------------------------
7 /**
8 * Constructor
9 */
10 QC_hadamard::QC_hadamard(int _Target) : QCalcUnit() {
11 Target = _Target;
12 }
13 //---------------------------------------------------------------------------
14 /**
15 * Calculate
16 */
17 void QC_hadamard::calc(int target, double R[], double I[], int NumberOfBits) {
18
19 unsigned int state = 1<< (NumberOfBits - 1);
20 const double sq2 = 1.0 / std::sqrt(2.0);
21
22 double r0,r1,i0,i1;
23 for (unsigned int i=0;i<state;i++) {
24 unsigned int ix0 = insert0(i,target);
25 unsigned int ix1 = insert1(i,target);
26
27 //Hadamard
28 r0 = R[ix0];
29 i0 = I[ix0];
30 r1 = R[ix1];
31 i1 = I[ix1];
32
33 R[ix0] = sq2*(r1+r0);
34 I[ix0] = sq2*(i1+i0);
35
36 R[ix1] = sq2*(-r1+r0);
37 I[ix1] = sq2*(-i1+i0);
38 }
39 }
40 //---------------------------------------------------------------------------
41 void
42 QC_hadamard::Calc(QBits *qBits) {
43 int N = qBits->GetNumberOfQBits();
44 double *R = qBits->GetBitsR();//Real Part
45 double *I = qBits->GetBitsI();//Imaginary Part
46
47 QC_hadamard::calc(Target, R, I, N);
48 }
49 //---------------------------------------------------------------------------
50 #ifdef __USE__MPI
51 void
52 QC_hadamard::calcmpi(int t1, double R[], double I[], int N) {
53 double r0 = 0.0;
54 double i0 = 0.0;
55 double r1 = 0.0;
56 double i1 = 0.0;
57 unsigned int ix0, ix1;
58 const double sq2 = 1.0 / std::sqrt(2.0);
59
60 for (int i = 0; i < (1 << (N - 1)); i++) {
61 // Obtain indices of state:
62 ix0 = QCalcUnit::insert0(i, t1);
63 ix1 = QCalcUnit::insert1(i, t1);
64
65 bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1);
66 if (bstore) {
67 double nr0 = sq2*(r1+r0);
68 double ni0 = sq2*(i1+i0);
69 double nr1 = sq2*(-r1+r0);
70 double ni1 = sq2*(-i1+i0);
71 // Store:
72 store(R, I, ix0, ix1, nr0, ni0, nr1, ni1);
73 }
74 }
75 }
76 #endif
77 //---------------------------------------------------------------------------
78

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