OSDN Git Service

Initial Commit
[qcad/qcad.git] / calcunits / QC_ccnot.cpp
1 //---------------------------------------------------------------------------
2 #include "QC_ccnot.h"
3 //---------------------------------------------------------------------------
4 /**
5  * Constructor
6 **/
7 QC_ccnot::QC_ccnot(int _target, int _control1, int _control2) : QCalcUnit() {
8     TargetBit = _target;
9     ControlBit1 = _control1;
10     ControlBit2 = _control2;
11 }
12 //---------------------------------------------------------------------------
13 /**
14  *
15 **/
16 void
17 QC_ccnot::calc(int target, int control1, int control2,
18                double R[], double I[], int NumberOfBits) {
19 // Sort Bits to reglar order of bit number
20     int Bit0 = target;
21     int Bit1 = control1;
22     int Bit2 = control2;
23     if (Bit0 > Bit1) swap(Bit0, Bit1);
24     if (Bit1 > Bit2) swap(Bit1, Bit2);
25     if (Bit0 > Bit1) swap(Bit0, Bit1);
26
27     unsigned int states = 1 << (NumberOfBits - 3);
28
29     for (unsigned int i = 0; i < states; i++) {
30         unsigned int ix = i;
31         ix = insert1(ix,Bit0);
32         ix = insert1(ix,Bit1);
33         ix = insert1(ix,Bit2);
34
35         unsigned int ix_1 = ix;
36         unsigned int ix_0 = ix & ~(1 << target); //Set TargetBit 0
37         swap(R[ix_0], R[ix_1]);
38         swap(I[ix_0], I[ix_1]);
39     }
40
41 }
42 //---------------------------------------------------------------------------
43 void
44 QC_ccnot::Calc(QBits *qBits) {
45     double *R = qBits->GetBitsR();//Real Part
46     double *I = qBits->GetBitsI();//Imaginary Part
47     int N = qBits->GetNumberOfQBits();
48
49     QC_ccnot::calc(TargetBit, ControlBit1, ControlBit2, R, I, N);
50 }
51 //---------------------------------------------------------------------------
52 #ifdef __USE__MPI
53 void
54 QC_ccnot::calcmpi(int t1, int c1, int c2, double R[], double I[], int N) {
55
56     // Sort Bits to reglar order of bit number
57     int Bit0 = t1;
58     int Bit1 = c1;
59     int Bit2 = c2;
60     if (Bit0 > Bit1) {
61         swap(Bit0, Bit1);
62     }
63     if (Bit1 > Bit2) {
64         swap(Bit1, Bit2);
65     }
66     if (Bit0 > Bit1) {
67         swap(Bit0, Bit1);
68     }
69
70     double r0 = 0.0;
71     double i0 = 0.0;
72     double r1 = 0.0;
73     double i1 = 0.0;
74     unsigned int ix0, ix1;
75
76     for (int i = 0; i < (1 << (N - 3)); i++) {
77         unsigned int ix = i;
78         ix = insert1(ix,Bit0);
79         ix = insert1(ix,Bit1);
80         ix = insert1(ix,Bit2);
81         unsigned int ix_1 = ix;
82         unsigned int ix_0 = ix & ~(1 << t1); //Set TargetBit 0
83         bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1);
84         if (bstore) {
85             swap(r0, r1);
86             swap(i0, i1);
87             // Store:
88             store(R, I, ix0, ix1, r0, i0, r1, i1);
89         }
90     }
91 }
92 #endif
93 //---------------------------------------------------------------------------