| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Compile intermediates into list of the calc units |
| 3 |
//---------------------------------------------------------------------------- |
| 4 |
#ifdef __BORLANDC__ |
| 5 |
#include <vcl.h> |
| 6 |
#pragma hdrstop |
| 7 |
#endif //__BORLANDC__ |
| 8 |
|
| 9 |
#include <iostream> |
| 10 |
#include <vector> |
| 11 |
#include "QCompiler.h" |
| 12 |
#include "QCompilerCntl.h" |
| 13 |
#include "QParseInfo.h" |
| 14 |
#include "QCalcUnit.h" |
| 15 |
|
| 16 |
#include "QC_not.h" |
| 17 |
#include "QC_hadamard.h" |
| 18 |
#include "QC_cnot.h" |
| 19 |
#include "QC_crot.h" |
| 20 |
#include "QC_rot.h" |
| 21 |
#include "QC_ccnot.h" |
| 22 |
#include "QC_pauliX.h" |
| 23 |
#include "QC_pauliY.h" |
| 24 |
#include "QC_pauliZ.h" |
| 25 |
#include "QC_swap.h" |
| 26 |
#include "QC_measure.h" |
| 27 |
|
| 28 |
// --------------------------------------------------------------------------- |
| 29 |
#ifdef __BORLANDC__ |
| 30 |
#pragma package(smart_init) |
| 31 |
#endif //__BORLANDC__ |
| 32 |
|
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
/** |
| 35 |
* |
| 36 |
*/ |
| 37 |
QCompilerCntl::QCompilerCntl(void) : QCompiler() { |
| 38 |
mNumOfQBits = 0; |
| 39 |
mQParseInfo.clear(); |
| 40 |
} |
| 41 |
//---------------------------------------------------------------------------- |
| 42 |
/** |
| 43 |
* |
| 44 |
*/ |
| 45 |
QCompilerCntl::QCompilerCntl(std::istream &is) : QCompiler(is) { |
| 46 |
mNumOfQBits = 0; |
| 47 |
mQParseInfo.clear(); |
| 48 |
} |
| 49 |
//---------------------------------------------------------------------------- |
| 50 |
/** |
| 51 |
* |
| 52 |
*/ |
| 53 |
QCompilerCntl::~QCompilerCntl() { |
| 54 |
mQParseInfo.clear(); |
| 55 |
} |
| 56 |
//---------------------------------------------------------------------------- |
| 57 |
/** |
| 58 |
* |
| 59 |
*/ |
| 60 |
QCalcUnit * |
| 61 |
QCompilerCntl::AllocateControl(const unsigned int index) { |
| 62 |
if (index >= mQParseInfo.size()) { |
| 63 |
return NULL; // Error |
| 64 |
} |
| 65 |
QCalcUnit *cu = NULL; |
| 66 |
const std::vector<int> &targets = mQParseInfo[index].QParseInfo::getTargetIndices(); |
| 67 |
switch (mQParseInfo[index].getOperator()) { |
| 68 |
case cn_cnot: |
| 69 |
cu = new QC_cnot(targets[0], targets[1]); |
| 70 |
break; |
| 71 |
case cn_crot: |
| 72 |
cu = new QC_crot(targets[0], targets[1], mQParseInfo[index].getRotation()); |
| 73 |
break; |
| 74 |
case cn_rot: |
| 75 |
cu = new QC_rot(targets[0], mQParseInfo[index].getRotation()); |
| 76 |
break; |
| 77 |
case cn_h: |
| 78 |
cu = new QC_hadamard(targets[0]); |
| 79 |
break; |
| 80 |
case cn_m: |
| 81 |
cu = new QC_measure(targets[0]); |
| 82 |
break; |
| 83 |
case cn_swap: |
| 84 |
cu = new QC_swap(targets[0], targets[1]); |
| 85 |
break; |
| 86 |
case cn_x: |
| 87 |
cu = new QC_pauliX(targets[0]); |
| 88 |
break; |
| 89 |
case cn_y: |
| 90 |
cu = new QC_pauliY(targets[0]); |
| 91 |
break; |
| 92 |
case cn_z: |
| 93 |
cu = new QC_pauliZ(targets[0]); |
| 94 |
break; |
| 95 |
case cn_not: |
| 96 |
cu = new QC_not(targets[0]); |
| 97 |
break; |
| 98 |
case cn_ccnot: |
| 99 |
cu = new QC_ccnot(targets[0], targets[1], targets[2]); |
| 100 |
break; |
| 101 |
case cn_init: |
| 102 |
mNumOfQBits = targets[0]; |
| 103 |
default: |
| 104 |
break; |
| 105 |
} |
| 106 |
return cu; |
| 107 |
} |
| 108 |
//---------------------------------------------------------------------------- |
| 109 |
/** |
| 110 |
* |
| 111 |
*/ |
| 112 |
bool |
| 113 |
QCompilerCntl::CompileOneLine(const QParseInfo &pinfo) { |
| 114 |
if (!pinfo.getParseResult()) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
mQParseInfo.push_back(pinfo); |
| 118 |
return true; |
| 119 |
} |
| 120 |
//---------------------------------------------------------------------------- |
| 121 |
/** |
| 122 |
* |
| 123 |
*/ |
| 124 |
void |
| 125 |
QCompilerCntl::CatchError(const QParseInfo &pinfo, const int at) { |
| 126 |
#ifdef __BORLANDC__ |
| 127 |
//ShowMessage(IntToStr(pinfo.getErrorNo())); |
| 128 |
#else |
| 129 |
std::cerr << "Parse error occurred at line " << at << "\n" |
| 130 |
<< "Error code = " << pinfo.getErrorNo() << "\n"; |
| 131 |
#endif //__BORLANDC__ |
| 132 |
//TODO: |
| 133 |
} |
| 134 |
//---------------------------------------------------------------------------- |