This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
Commit 2c27773a authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

updates to range generator to include start and end keys


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 6398d23e
Pipeline #66424 passed with stage
in 7 minutes and 29 seconds
......@@ -6,11 +6,19 @@ using namespace xacc;
namespace xacc {
namespace generators {
bool Range::validateOptions() {
if (options.count("nqubits") && options.count("gate")) {
return true;
if (!options.count("gate")) {
return false;
}
return false;
if (!options.count("nqubits")) {
if (!options.count("start") && !options.count("end")) {
return false;
}
}
return true;
}
std::shared_ptr<xacc::Function>
Range::generate(std::map<std::string, xacc::InstructionParameter> &parameters) {
......@@ -19,16 +27,32 @@ Range::generate(std::map<std::string, xacc::InstructionParameter> &parameters) {
}
auto gate = options["gate"].as<std::string>();
int nqubits;
try {
nqubits = options["nqubits"].as_no_error<int>();
} catch (std::exception& e) {
nqubits = (int) options["nqubits"].as<double>();
int start = 0, end, nqubits;
if (options.count("nqubits")) {
try {
nqubits = options["nqubits"].as_no_error<int>();
} catch (std::exception &e) {
nqubits = (int)options["nqubits"].as<double>();
}
end = nqubits;
} else if (options.count("start")) {
try {
start = options["start"].as_no_error<int>();
} catch (std::exception &e) {
start = (int)options["start"].as<double>();
}
try {
end = options["end"].as_no_error<int>();
} catch (std::exception &e) {
end = (int)options["end"].as<double>();
}
}
auto provider = xacc::getIRProvider("quantum");
auto f = provider->createFunction("range" + gate, {});
for (int i = 0; i < nqubits; i++) {
for (int i = start; i < end; i++) {
auto g = provider->createInstruction(gate, {i});
f->addInstruction(g);
}
......
......@@ -10,8 +10,12 @@ TEST(ExpTester, checkSimple) {
auto f =
exp->generate({{"gate","H"}, {"nqubits",10}});
std::cout << "F:\n" << f->toString() << "\n";
EXPECT_EQ(10, f->nInstructions());
std::cout << "F:\n" << f->toString() << "\n";
EXPECT_EQ(10, f->nInstructions());
auto g = exp->generate({{"gate","H"}, {"start",3},{"end",10}});
std::cout << "G:\n" << g->toString() << "\n";
EXPECT_EQ(7, g->nInstructions());
}
int main(int argc, char **argv) {
......
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