OSDN Git Service

Initial Commit
[qcad/qcad.git] / calcunits / QC_cnot.cpp
1 //---------------------------------------------------------------------------
2 //  Controled-not gate
3 //---------------------------------------------------------------------------
4 #include "QC_cnot.h"
5 //---------------------------------------------------------------------------
6 /**
7  *  Constructor
8  */
9 QC_cnot::QC_cnot(int _TargetBit,int _ControlBit) : QCalcUnit() {
10     TargetBit  = _TargetBit;
11     ControlBit = _ControlBit;
12 }
13 //---------------------------------------------------------------------------
14 /**
15  *  Calculation
16  */
17 void QC_cnot::calc(int Target, int Control,
18                    double BitsR[], double BitsI[], int NumberOfBits) {
19     unsigned int states = (1 << (NumberOfBits - 2));
20
21     // Sort BitsNumber to regular order
22     int Bit0 = Target;
23     int Bit1 = Control;
24     if (Bit0 > Bit1) {
25         swap(Bit0, Bit1);
26     }
27
28     for (unsigned int i = 0; i < states; i++) {
29         unsigned int ix1 = QCalcUnit::insert1(i,   Bit0);
30         ix1 = QCalcUnit::insert1(ix1, Bit1);
31         unsigned int ix0 = ix1 & ~(1 << Target);
32
33         swap(BitsR[ix0], BitsR[ix1]);
34         swap(BitsI[ix0], BitsI[ix1]);
35     }
36 }
37 //---------------------------------------------------------------------------
38 void
39 QC_cnot::Calc(QBits *qBits) {
40     int N = qBits->GetNumberOfQBits();
41     double *R = qBits->GetBitsR();//Real Part
42     double *I = qBits->GetBitsI();//Imaginary Part
43
44     QC_cnot::calc(TargetBit, ControlBit, R, I, N);
45 }
46 //---------------------------------------------------------------------------
47 #ifdef __USE__MPI
48 void
49 QC_cnot::calcmpi(int t1, int c1, double R[], double I[], int N) {
50     double r0 = 0.0;
51     double i0 = 0.0;
52     double r1 = 0.0;
53     double i1 = 0.0;
54     unsigned int ix0, ix1;
55
56     // Sort BitsNumber to regular order
57     int Bit0 = t1;
58     int Bit1 = c1;
59     if (Bit0 > Bit1) {
60         swap(Bit0, Bit1);
61     }
62
63     for (int i = 0; i < (1 << (N - 2)); i++) {
64         // Obtain indices of state:
65         unsigned int ix1 = QCalcUnit::insert1(i,   Bit0);
66         ix1 = QCalcUnit::insert1(ix1, Bit1);
67         unsigned int ix0 = ix1 & ~(1 << t1);
68
69         bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1);
70         if (bstore) {
71             swap(r0, r1);
72             swap(i0, i1);
73             // Store:
74             store(R, I, ix0, ix1, r0, i0, r1, i1);
75         }
76     }
77 }
78 #endif
79 //---------------------------------------------------------------------------