Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/calcunits/QC_swap.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: 2598 byte(s)
First commit
1 //---------------------------------------------------------------------------
2 // Swap gate
3 //---------------------------------------------------------------------------
4 #include "QC_swap.h"
5 //---------------------------------------------------------------------------
6
7 /**
8 * Constructor
9 */
10 QC_swap::QC_swap(int target1,int target2) : QCalcUnit() {
11 TargetBit1 = target1;
12 TargetBit2 = target2;
13 }
14 //---------------------------------------------------------------------------
15 /**
16 * Calculation
17 */
18 void QC_swap::calc(int target1, int target2,
19 double BitsR[], double BitsI[], int NumberOfBits) {
20 unsigned int states = (1 << (NumberOfBits - 2));
21 // Sort Bits to regular order of bit index
22 int Bit0 = target1;
23 int Bit1 = target2;
24 if (Bit0 > Bit1) {
25 QCalcUnit::swap(Bit0, Bit1);
26 }
27
28 for (unsigned int i = 0; i < states; i++) {
29 unsigned int ix1 = insert1(i, Bit0);
30 ix1 = insert0(ix1, Bit1);
31 unsigned int ix2 = insert0(i, Bit0);
32 ix2 = insert1(ix2, Bit1);
33
34 QCalcUnit::swap(BitsR[ix1], BitsR[ix2]);
35 QCalcUnit::swap(BitsI[ix1], BitsI[ix2]);
36 }
37 }
38 //---------------------------------------------------------------------------
39 void QC_swap::Calc(QBits *qBits)
40 {
41 int N = qBits->GetNumberOfQBits();
42 double *R = qBits->GetBitsR();
43 double *I = qBits->GetBitsI();
44
45 QC_swap::calc(TargetBit1, TargetBit2, R, I, N);
46 }
47 //---------------------------------------------------------------------------
48 #ifdef __USE__MPI
49 void
50 QC_swap::calcmpi(int t1, int t2, double R[], double I[], int N) {
51 // Sort Bits to regular order of bit index
52 int Bit0 = t1;
53 int Bit1 = t2;
54 if (Bit0 > Bit1) {
55 QCalcUnit::swap(Bit0, Bit1);
56 }
57
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 printf("Swap Gate has not been implemented yet.\n");
65 //TODO:
66 /*
67 for (int i = 0; i < (1 << (N - 2)); i++)
68 {
69 // Obtain indices of state:
70 unsigned int ix1 = insert1(i, Bit0);
71 ix1 = insert0(ix1, Bit1);
72 unsigned int ix2 = insert0(i, Bit0);
73 ix2 = insert1(ix2, Bit1);
74
75 bool bstore = setup(R, I, ix0, ix1, r0, i0, r1, i1);
76 if (bstore) {
77 // Calc:
78 QCalcUnit::swap(r0, r1);
79 QCalcUnit::swap(i0, i1);
80 // Store:
81 store(R, I, ix0, ix1, r0, i0, r1, i1);
82 }
83 }
84 */
85 }
86 #endif
87 //---------------------------------------------------------------------------

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