Commit d6c489c1 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

update to enable for loop use with composite arguments and variadic update


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent b004e466
Pipeline #97671 passed with stage
in 9 minutes and 43 seconds
@startuml
class Observable {
+ observe (kernel : Kernel) : vector<Kernel>
+ fromString(str_rep : string)
+ fromOptions( opts : HeterogeneousMap)
+ operator+()
+ operator-()
+ operator*()
+ operator=()
+ operator==()
}
class ObservableTransform {
+ transform(obs : Observable) : Observable
}
class PauliOperator {
}
class RDMOperator{}
class FermionOperator {
}
class Chemistry
Observable <|-- PauliOperator
Observable <|-- FermionOperator
Observable <|-- RDMOperator
Observable <|-- Chemistry
note right of Observable
Users sees API like this
qcor_Observable * H = qcor_createObservable("pauli", "X0 Y1 - X1 Z2");
qcor_Pauli sig_x_0('X', 0, 2.2);
qcor_Observable * sig_x_2 = qcor_X(2);
qcor_Observable * tmp = qcor_multiplyObservables(sig_x_0, sig_x_2);
qcor_Observable * sum = qcor_addObservables(H, tmp);
qcor_Observable * f = qcor_createObservable("fermion", "0^ 1 + 1^ 0");
qcor_Observable * transformed = qcor_transformObservable(TransformType_JW, f);
MatrixData * data = qcor_getObservableMatrix(sum);
end note
@enduml
\ No newline at end of file
......@@ -169,7 +169,8 @@ void Exp::applyRuntimeArguments() {
x_val = arguments[0]->runtimeValue.get<std::vector<double>>(
INTERNAL_ARGUMENT_VALUE_KEY)[vector_mapping[variable_name]];
variable_name =
arguments[0]->name + std::to_string(vector_mapping[variable_name]);
arguments[0]->name +std::to_string(vector_mapping[variable_name]);
addVariable(variable_name);
} else {
x_val = arguments[0]->runtimeValue.get<double>(INTERNAL_ARGUMENT_VALUE_KEY);
}
......@@ -275,6 +276,7 @@ void Exp::applyRuntimeArguments() {
xasm_src = "__qpu__ void " + name + "(qbit q, double " +
variable_name + ") {\n" + xasm_src + "}";
// std::cout << xasm_src << "\n";
auto xasm = xacc::getCompiler("xasm");
auto tmp = xasm->compile(xasm_src)->getComposites()[0];
......
......@@ -143,70 +143,79 @@ TEST(XASMCompilerTester, checkVectorArg) {
<< IR->getComposites()[0]->operator()({2.})->toString() << "\n";
}
// TEST(XASMCompilerTester, checkSimpleFor) {
// auto compiler = xacc::getCompiler("xasm");
// auto IR =
// compiler->compile(R"(__qpu__ void testFor(qbit q, std::vector<double> x) {
// for (int i = 0; i < 5; i++) {
// H(q[i]);
// }
// for (int i = 0; i < 2; i++) {
// Rz(q[i], x[i]);
// }
// })");
// std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
// xacc::internal_compiler::qreg q(5);
// auto tt = IR->getComposites()[0];
// tt->updateRuntimeArguments(q, std::vector<double>{1.2, 3.4});
// std::cout << "EVALED NEW WAY:\n" << tt->toString() << "\n";
// IR = compiler->compile(
// R"(__qpu__ void testFor2(qbit q, std::vector<double> x) {
// for (int i = 0; i < 5; i++) {
// H(q[i]);
// Rx(q[i], x[i]);
// CX(q[0], q[i]);
// }
// for (int i = 0; i < 3; i++) {
// CX(q[i], q[i+1]);
// }
// Rz(q[3], 0.22);
// for (int i = 3; i > 0; i--) {
// CX(q[i-1],q[i]);
// }
// })");
// EXPECT_EQ(1, IR->getComposites().size());
// std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
// for (auto ii : IR->getComposites()[0]->getVariables())
// std::cout << ii << "\n";
// EXPECT_EQ(22, IR->getComposites()[0]->nInstructions());
// }
// TEST(XASMCompilerTester, checkHWEFor) {
// auto compiler = xacc::getCompiler("xasm");
// auto IR = compiler->compile(R"([&](qbit q, std::vector<double> x) {
// for (int i = 0; i < 2; i++) {
// Rx(q[i],x[i]);
// Rz(q[i],x[2+i]);
// }
// CX(q[1],q[0]);
// for (int i = 0; i < 2; i++) {
// Rx(q[i], x[i+4]);
// Rz(q[i], x[i+4+2]);
// Rx(q[i], x[i+4+4]);
// }
// })");
// EXPECT_EQ(1, IR->getComposites().size());
// std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
// for (auto ii : IR->getComposites()[0]->getVariables())
// std::cout << ii << "\n";
// EXPECT_EQ(11, IR->getComposites()[0]->nInstructions());
// }
TEST(XASMCompilerTester, checkSimpleFor) {
auto compiler = xacc::getCompiler("xasm");
auto IR =
compiler->compile(R"(__qpu__ void testFor(qbit q, std::vector<double> x) {
for (int i = 0; i < 5; i++) {
H(q[i]);
}
for (int i = 0; i < 2; i++) {
Rz(q[i], x[i]);
}
})");
std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
xacc::internal_compiler::qreg q(5);
auto tt = IR->getComposites()[0];
tt->updateRuntimeArguments(q, std::vector<double>{1.2, 3.4});
std::cout << "EVALED NEW WAY:\n" << tt->toString() << "\n";
IR = compiler->compile(
R"(__qpu__ void testFor2(qbit q, std::vector<double> x) {
for (int i = 0; i < 5; i++) {
H(q[i]);
Rx(q[i], x[i]);
CX(q[0], q[i]);
}
for (int i = 0; i < 3; i++) {
CX(q[i], q[i+1]);
}
Rz(q[3], 0.22);
for (int i = 3; i > 0; i--) {
CX(q[i-1],q[i]);
}
})");
EXPECT_EQ(1, IR->getComposites().size());
std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
for (auto ii : IR->getComposites()[0]->getVariables())
std::cout << ii << "\n";
EXPECT_EQ(22, IR->getComposites()[0]->nInstructions());
IR->getComposites()[0]->updateRuntimeArguments(
q, std::vector<double>{1, 2, 3, 4, 5});
std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
}
TEST(XASMCompilerTester, checkHWEFor) {
auto compiler = xacc::getCompiler("xasm");
auto IR = compiler->compile(R"([&](qbit q, std::vector<double> x) {
for (int i = 0; i < 2; i++) {
Rx(q[i],x[i]);
Rz(q[i],x[2+i]);
}
CX(q[1],q[0]);
for (int i = 0; i < 2; i++) {
Rx(q[i], x[i+4]);
Rz(q[i], x[i+4+2]);
Rx(q[i], x[i+4+4]);
}
})");
EXPECT_EQ(1, IR->getComposites().size());
std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
for (auto ii : IR->getComposites()[0]->getVariables())
std::cout << ii << "\n";
EXPECT_EQ(11, IR->getComposites()[0]->nInstructions());
xacc::internal_compiler::qreg q(2);
IR->getComposites()[0]->updateRuntimeArguments(
q, std::vector<double>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
std::cout << "KERNEL\n" << IR->getComposites()[0]->toString() << "\n";
}
TEST(XASMCompilerTester, checkIfStmt) {
......@@ -351,11 +360,11 @@ TEST(XASMCompilerTester, checkCallingPreviousKernel) {
}
TEST(XASMCompilerTester, checkIRV3) {
// auto v = xacc::qalloc(1);
// v->setName("v");
// xacc::storeBuffer(v);
// auto v = xacc::qalloc(1);
// v->setName("v");
// xacc::storeBuffer(v);
// auto v = xacc::internal_compiler::qalloc(1);
// auto v = xacc::internal_compiler::qalloc(1);
xacc::internal_compiler::qreg v(1);
auto H = xacc::quantum::getObservable("pauli", std::string("X0 Y1 + Y0 X1"));
......@@ -381,13 +390,12 @@ TEST(XASMCompilerTester, checkIRV3) {
}
}
TEST(XASMCompilerTester, checkIRV3Vector) {
// auto v = xacc::qalloc(1);
// v->setName("v");
// xacc::storeBuffer(v);
// auto v = xacc::qalloc(1);
// v->setName("v");
// xacc::storeBuffer(v);
// auto v = xacc::internal_compiler::qalloc(1);
// auto v = xacc::internal_compiler::qalloc(1);
xacc::internal_compiler::qreg v(1);
auto H = xacc::quantum::getObservable("pauli", std::string("X0 Y1 + Y0 X1"));
......@@ -412,7 +420,7 @@ TEST(XASMCompilerTester, checkIRV3Vector) {
std::cout << foo_test->toString() << "\n\n";
}
IR = compiler->compile(
IR = compiler->compile(
R"(
__qpu__ void ansatz2(qreg q, std::vector<double> theta) {
X(q[0]);
......@@ -420,12 +428,11 @@ IR = compiler->compile(
CX(q[1],q[0]);
}
)");
auto test = IR->getComposites()[0];
std::cout <<" HELLO: " << test->toString() << "\n";
test->updateRuntimeArguments(v, std::vector<double>{.48});
std::cout <<" HELLO: " << test->toString() << "\n";
auto test = IR->getComposites()[0];
std::cout << " HELLO: " << test->toString() << "\n";
test->updateRuntimeArguments(v, std::vector<double>{.48});
std::cout << " HELLO: " << test->toString() << "\n";
}
int main(int argc, char **argv) {
......
......@@ -27,23 +27,44 @@ namespace xacc {
void XASMListener::for_stmt_update_inst_args(Instruction *inst) {
auto parameters = inst->getParameters();
for (int i = 0; i < parameters.size(); i++) {
std::cout << "HELLO: " << inst->name() << ", " << parameters[i].toString()
<< "\n";
if (parameters[i].isVariable()) {
auto arg = function->getArgument(parameters[i].toString());
if (!arg) {
auto param_str = parameters[i].toString();
param_str.erase(
std::remove_if(param_str.begin(), param_str.end(),
[](char c) { return c == ']' || c == '['; }),
param_str.end());
param_str.erase(std::remove_if(param_str.begin(), param_str.end(),
[](char c) { return !std::isalpha(c); }),
param_str.end());
std::cout << "IS THSI HRE: " << param_str << "\n";
arg = function->getArgument(param_str);
if (arg && arg->type.find("std::vector<double>") != std::string::npos) {
// this was a container-like type
// give the instruction a mapping from i to vector idx
inst->addIndexMapping(
i, new_var_to_vector_idx[parameters[i].toString()]);
std::cout << "HELLO " << arg->name << "\n";
auto tmp_str = parameters[i].toString();
tmp_str.erase(
std::remove_if(tmp_str.begin(), tmp_str.end(),
[](char c) { return c == '[' || c == ']'; }),
tmp_str.end());
tmp_str.erase(tmp_str.find(arg->name), arg->name.length());
int vector_mapping = 0;
try {
auto vector_idx = std::stoi(tmp_str);
vector_mapping = vector_idx;
} catch (std::exception &e) {
std::cout << e.what() << "\n";
xacc::error("[xasm] could not compute vector index.");
}
inst->addIndexMapping(i, vector_mapping);
}
}
......@@ -95,7 +116,7 @@ void XASMListener::createForInstructions<XasmLessThan>(
irProvider->createInstruction(next->name(), new_bits, new_params);
copy->setBufferNames(next->getBufferNames());
for_stmt_update_inst_args(copy.get());
instructions.push_back(copy);
......
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