Commit 78901a96 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Fixing bug in GateFunction evaluateVariableParameters

parent 0262a1d4
......@@ -240,6 +240,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
std::string res = extract<std::string>(main_namespace["extractStr"]);
std::string measStr = extract<std::string>(main_namespace["measStr"]);
std::cout << "MEASUREMENT STR: " << measStr << "\n";
Py_Finalize();
int i = 0;
......@@ -328,6 +329,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
ss<<postResponse.content.rdbuf();
std::string resp_str = ss.str();
std::cout << "Rigetti Response Str:\n" << resp_str << "\n";
int i_qbit = -1;
boost::dynamic_bitset<> outcome(buffer->size());
......@@ -358,7 +360,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
for(std::string tmp; std::getline(ss,tmp,','); ){
oss<<tmp<<std::endl;
}
XACCInfo(oss.str());
// XACCInfo(oss.str());
} else if (type == "ping" || type == "version") {
......
......@@ -136,10 +136,9 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
kernelSource = src;
// Replace the __qpu__ attribute with module
if (boost::contains(kernelSource, "__qpu__")) {
kernelSource.erase(kernelSource.find("__qpu__"), 7);
kernelSource = std::string("module") + kernelSource;
std::cout << "\n" << kernelSource << "\n";
boost::replace_all(kernelSource, "__qpu__", "module");
}
// Create a temporary scaffold source file
......
......@@ -194,25 +194,25 @@ BOOST_AUTO_TEST_CASE(checkTeleportWithFunctions) {
BOOST_AUTO_TEST_CASE(checkVQEExample) {
const std::string src(""
"module initializeState(qbit qreg[2], float theta) {\n"
"__qpu__ initializeState(qbit qreg[2], float theta) {\n"
" Rx(qreg[0], 1.57079);\n"
" Ry(qreg[1], 1.57079);\n"
" Rx(qreg[0], -1.57079);\n"
" CNOT(qreg[0], qreg[1]);\n"
" Rx(qreg[0], 7.8539752);\n"
" CNOT(qreg[1], qreg[0]);\n"
" Rz(qreg[0], theta);\n"
" CNOT(qreg[0], qreg[1]);\n"
" Ry(qreg[1], -1.57079);\n"
" CNOT(qreg[1], qreg[0]);\n"
" Ry(qreg[1], 7.8539752);\n"
" Rx(qreg[0], 1.57079);\n"
"}\n"
""
"module g1Term (qbit qreg[2], float theta) {\n"
"__qpu__ g1Term (qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
"}\n"
""
"module g4Term(qbit qreg[2], float theta) {\n"
"__qpu__ g4Term(qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" Rx(qreg[0], -1.57079);\n"
......@@ -221,28 +221,41 @@ BOOST_AUTO_TEST_CASE(checkVQEExample) {
" creg[1] = MeasZ(qreg[1]);\n"
"}\n"
""
"module g5Term(qbit qreg[2], float theta) {\n"
"__qpu__ g5Term(qbit qreg[2], float theta) {\n"
" initializeState(qreg, theta);\n"
" cbit creg[2];\n"
" H(qreg[0]);\n"
" H(qreg[1]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
"}\n"
"");
"}\n");
ScaffoldCompiler compiler;
auto ir = compiler.compile(src);
// for (auto k : ir->getKernels()) {
// JsonVisitor v(k);
// std::cout << v.write() << "\n";
// }
JsonVisitor visitor(ir->getKernels());
std::cout << "HI:\n" << visitor.write() << "\n";
auto k = ir->getKernel("initializeState");
xacc::InstructionParameter p(22.2);
std::vector<xacc::InstructionParameter> pars{p};
k->evaluateVariableParameters(pars);
JsonVisitor v(k);
std::cout << "AfterParams:\n" << v.write() << "\n";
xacc::InstructionParameter p2(44.2);
std::vector<xacc::InstructionParameter> pars2{p2};
k->evaluateVariableParameters(pars2);
JsonVisitor v2(k);
std::cout << "AfterParams2:\n" << v2.write() << "\n";
}
/*
......
......@@ -56,6 +56,11 @@ protected:
std::vector<InstructionParameter> parameters;
/**
* Map of Instruction Index to ( Instruction's Runtime Parameter Index, Dependent Variable name)
*/
std::map<int, std::pair<int, std::string>> cachedVariableInstructions;
public:
/**
......@@ -146,8 +151,7 @@ public:
virtual InstructionParameter getParameter(const int idx) {
if (idx + 1 > parameters.size()) {
XACCError(
"Invalid Parameter requested.");
XACCError("Invalid Parameter requested.");
}
return parameters[idx];
......@@ -155,8 +159,7 @@ public:
virtual void setParameter(const int idx, InstructionParameter& p) {
if (idx + 1 > parameters.size()) {
XACCError(
"Invalid Parameter requested.");
XACCError("Invalid Parameter requested.");
}
parameters[idx] = p;
......@@ -170,45 +173,60 @@ public:
return parameters.size();
}
std::map<int,int> cachedVariableInstructions;
virtual void evaluateVariableParameters(std::vector<InstructionParameter> runtimeParameters) {
virtual void evaluateVariableParameters(
std::vector<InstructionParameter> runtimeParameters) {
std::map<std::string, InstructionParameter> varToValMap;
int i = 0;
for (auto funcParam : parameters) {
varToValMap.insert(
std::make_pair(boost::get<std::string>(funcParam),
runtimeParameters[i]));
i++;
}
for (const auto& gateIdVarName : cachedVariableInstructions) {
auto inst = getInstruction(gateIdVarName.first);
auto varInstDependsOn = gateIdVarName.second.second;
auto instParamIdx = gateIdVarName.second.first;
int indexOfRuntimeParam, counter = 0;
for (auto p : parameters) {
if (boost::get<std::string>(p) == varInstDependsOn) {
indexOfRuntimeParam = counter;
break;
}
counter++;
}
inst->setParameter(instParamIdx, runtimeParameters[indexOfRuntimeParam]);
}
i = 0;
for (auto inst : instructions) {
if (inst->isComposite()) {
std::dynamic_pointer_cast<Function>(inst)->evaluateVariableParameters(
runtimeParameters);
} else {
if (inst->isParameterized() && inst->getName() != "Measure") {
for (int i = 0; i < inst->nParameters(); i++) {
auto param = inst->getParameter(i);
// See if this is a string parameter
if (param.which() == 3) {
auto variable = boost::get<std::string>(param);
// Get index
auto it = std::find(parameters.begin(),
parameters.end(), param);
if (it == parameters.end()) {
XACCError("Variable " + variable + " not found in Function parameters.");
} else {
auto index = std::distance(parameters.begin(),
it);
inst->setParameter(index, runtimeParameters[index]);
cachedVariableInstructions.insert(std::make_pair(i, index));
}
} else {
// See if we have a cached instruction
if (cachedVariableInstructions.find(i) != cachedVariableInstructions.end()) {
auto idx = cachedVariableInstructions[i];
inst->setParameter(idx, runtimeParameters[idx]);
}
}
} else if (inst->isParameterized() && inst->getName() != "Measure") {
for (int j = 0; j < inst->nParameters(); ++j) {
auto instParam = inst->getParameter(j);
if (instParam.which() == 3) {
// This is a variable
auto variable = boost::get<std::string>(instParam);
auto runtimeParameter = varToValMap[variable];
inst->setParameter(j, runtimeParameter);
cachedVariableInstructions.insert(std::make_pair(i, std::make_pair(j, variable)));
}
}
}
i++;
}
}
......
......@@ -64,7 +64,7 @@ public:
JsonVisitor(std::shared_ptr<xacc::Function> f) :
buffer(std::make_shared<StringBuffer>()), writer(
std::make_shared<PrettyWriter<StringBuffer>>(
*buffer.get())), function(f) {
*buffer.get())), functions{f} {
}
JsonVisitor(std::vector<std::shared_ptr<xacc::Function>> fs) :
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment