Commit 453b1a44 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

minor updates, cleanup



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent f4ae93ba
Pipeline #69504 passed with stage
in 4 minutes and 14 seconds
......@@ -61,11 +61,10 @@ void Circuit::throwIfInvalidInstructionParameter(InstPtr instruction) {
std::shared_ptr<CompositeInstruction>
Circuit::operator()(const std::vector<double> &params) {
if (params.size() != variables.size()) {
xacc::XACCLogger::instance()->error(
"Invalid Circuit evaluation: number "
"of parameters don't match. " +
std::to_string(params.size()) + ", " +
std::to_string(variables.size()));
xacc::XACCLogger::instance()->error("Invalid Circuit evaluation: number "
"of parameters don't match. " +
std::to_string(params.size()) + ", " +
std::to_string(variables.size()));
exit(0);
}
......@@ -127,6 +126,13 @@ void Circuit::load(std::istream &inStream) {
auto &kernel = doc["circuits"].GetArray()[0];
circuitName = kernel["circuit"].GetString();
auto vars = kernel["variables"].GetArray();
for (int i = 0; i < vars.Size(); i++) {
addVariable(vars[i].GetString());
}
auto coeff = kernel["coefficient"].GetDouble();
setCoefficient(coeff);
auto instructionsArray = kernel["instructions"].GetArray();
......@@ -156,14 +162,6 @@ void Circuit::load(std::istream &inStream) {
std::shared_ptr<Instruction> instToAdd;
instToAdd = provider->createInstruction(gname, qbits, local_parameters);
auto vars = kernel["variables"].GetArray();
for (int i = 0; i < vars.Size(); i++ ){
addVariable(vars[i].GetString());
}
auto coeff = kernel["coefficient"].GetDouble();
setCoefficient(coeff);
if (!inst["enabled"].GetBool()) {
instToAdd->disable();
}
......
......@@ -19,6 +19,7 @@
#include "Utils.hpp"
#include "expression_parsing_util.hpp"
#include "xacc_service.hpp"
#include <limits>
#include <memory>
#include <stdexcept>
......@@ -221,8 +222,39 @@ public:
const std::string persistGraph() override;
std::shared_ptr<Graph> toGraph() override;
const std::size_t nLogicalBits() override { return 0; }
const std::size_t nPhysicalBits() override { return 0; }
const std::size_t nLogicalBits() override {
// n logical should just be the number of
// unique qbit integers
std::set<std::size_t> bits;
InstructionIterator iter(shared_from_this());
while(iter.hasNext()) {
auto inst = iter.next();
if (!inst->isComposite()) {
for (auto& b : inst->bits()) {
bits.insert(b);
}
}
}
return bits.size();
}
const std::size_t nPhysicalBits() override {
// n physical bits should be the largest bit
// plus 1
std::size_t maxBit = 0;
InstructionIterator iter(shared_from_this());
while(iter.hasNext()) {
auto inst = iter.next();
if (!inst->isComposite()) {
for (auto& b : inst->bits()) {
if (b > maxBit) {
maxBit = b;
}
}
}
}
return maxBit+1;
}
std::shared_ptr<CompositeInstruction> enabledView() override {
auto newF = std::make_shared<Circuit>(circuitName, variables);
......
include_directories(${CMAKE_BINARY_DIR})
add_xacc_test(VQE)
target_link_libraries(VQETester xacc CppMicroServices xacc-pauli)
\ No newline at end of file
target_link_libraries(VQETester xacc xacc-pauli)
\ No newline at end of file
......@@ -27,6 +27,7 @@ const std::string rucc = R"rucc(__qpu__ void f(qbit q, double t0) {
H(q[2]);
H(q[3]);
})rucc";
TEST(VQETester, checkSimple) {
if (xacc::hasAccelerator("tnqvm")) {
auto acc = xacc::getAccelerator("tnqvm", {std::make_pair("vqe-mode",true)});
......
......@@ -16,7 +16,6 @@
#include "Compiler.hpp"
#include "RemoteAccelerator.hpp"
#include "IRProvider.hpp"
// #include "IRGenerator.hpp"
#include "Algorithm.hpp"
#include "Optimizer.hpp"
......
......@@ -44,14 +44,14 @@ public:
void updateConfiguration(const HeterogeneousMap &config) override {
}
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<CompositeInstruction> circuit);
void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<CompositeInstruction> circuit) override;
virtual void
void
execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<CompositeInstruction>> circuits);
const std::vector<std::shared_ptr<CompositeInstruction>> circuits) override;
virtual bool isRemote() { return true; }
bool isRemote() override { return true; }
void setClient(std::shared_ptr<Client> client) {
restClient = client;
}
......
......@@ -19,8 +19,10 @@ namespace xacc {
class Observable : public Identifiable {
public:
virtual std::vector<std::shared_ptr<CompositeInstruction>>
observe(std::shared_ptr<CompositeInstruction> CompositeInstruction) = 0;
virtual const std::string toString() = 0;
virtual void fromString(const std::string str) = 0;
virtual const int nBits() = 0;
......
......@@ -12,8 +12,8 @@ OptResult NLOptimizer::optimize(OptFunction &function) {
double tol = 1e-8;
int maxeval = 1000;
if (options.keyExists<std::string>("nlopt-optimizer")) {
auto optimizerAlgo = options.get<std::string>("nlopt-optimizer");
if (options.stringExists("nlopt-optimizer")) {
auto optimizerAlgo = options.getString("nlopt-optimizer");
if (optimizerAlgo == "cobyla") {
algo = nlopt::algorithm::LN_COBYLA;
} else if (optimizerAlgo == "nelder-mead") {
......
......@@ -2,5 +2,6 @@ add_xacc_test(XACCAPI xacc)
target_include_directories(XACCAPITester PRIVATE ${CMAKE_BINARY_DIR})
add_xacc_test(CLIParser xacc)
add_xacc_test(Algorithm xacc)
add_xacc_test(Heterogeneous xacc)
target_compile_features(HeterogeneousTester PRIVATE cxx_std_14)
......@@ -2,6 +2,8 @@
#include <gtest/gtest.h>
#include <stdexcept>
#include "XACC.hpp"
#include "Algorithm.hpp"
#include "xacc_service.hpp"
class print_visitor : public xacc::visitor_base<int,double> {
public:
......@@ -16,7 +18,7 @@ TEST(HeterogeneousMapTester, checkSimple) {
c.insert("intkey", 1);
c.insert("intkey2", 2);
c.insert("doublekey", 2.2);
c.insert("variable", std::string{"t0"});
c.insert("variable", std::string("t0"));
print_visitor v;
c.visit(v);
......@@ -24,39 +26,39 @@ TEST(HeterogeneousMapTester, checkSimple) {
EXPECT_EQ(2, c.number_of<int>());
EXPECT_EQ(2.2, c.get<double>("doublekey"));
EXPECT_TRUE(c.keyExists<int>("intkey"));
EXPECT_TRUE(c.keyExists<std::string>("variable"));
EXPECT_TRUE(c.stringExists("variable"));
EXPECT_FALSE(c.keyExists<int>("random"));
EXPECT_EQ("t0", c.get<std::string>("variable"));
EXPECT_EQ("t0", c.getString("variable"));
EXPECT_ANY_THROW(c.get_with_throw<bool>("test"));
// auto provider = xacc::getIRProvider("gate");
// auto f = provider->createCompositeInstruction("f", {});
auto provider = xacc::getIRProvider("quantum");
auto f = provider->createComposite("f", {});
// c.insert("function", f);
c.insert("function", f);
// EXPECT_EQ(
// "f",
// c.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
EXPECT_EQ(
"f",
c.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
xacc::HeterogeneousMap m2{std::make_pair("doublekey", 2.0),
std::make_pair("intkey", 22)};
// std::make_pair("function", f)};
std::make_pair("intkey", 22),
std::make_pair("function", f)};
EXPECT_EQ(2.0, m2.get<double>("doublekey"));
EXPECT_EQ(22, m2.get<int>("intkey"));
// EXPECT_EQ(
// "f",
// m2.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
EXPECT_EQ(
"f",
m2.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
xacc::HeterogeneousMap m3(std::make_pair("doublekey", 2.0),
std::make_pair("intkey", 22));
// std::make_pair("function", f));
std::make_pair("intkey", 22),
std::make_pair("function", f));
EXPECT_EQ(2.0, m3.get<double>("doublekey"));
EXPECT_EQ(22, m3.get<int>("intkey"));
// EXPECT_EQ(
// "f",
// m3.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
EXPECT_EQ(
"f",
m3.get<std::shared_ptr<xacc::CompositeInstruction>>("function")->name());
xacc::HeterogeneousMap cc;
EXPECT_ANY_THROW(cc.get_with_throw<int>("intkey"));
......@@ -65,6 +67,34 @@ TEST(HeterogeneousMapTester, checkSimple) {
std::pair<std::string, int>{"intkey", 22}});
}
TEST(HeterogeneousMapTester, checkVQE) {
using namespace xacc;
class TestObservable : public xacc::Observable {
public:
std::vector<std::shared_ptr<CompositeInstruction>>
observe(std::shared_ptr<CompositeInstruction> CompositeInstruction) override {
return {};
}
const std::string toString() override { return ""; }
void fromString(const std::string str) override {}
const int nBits() override { return 0; }
const std::string name() const override { return "test"; }
const std::string description() const override { return ""; }
void fromOptions(const HeterogeneousMap& options) override {}
};
std::shared_ptr<Observable> observable = std::make_shared<TestObservable>();
xacc::HeterogeneousMap options;
auto provider = xacc::getIRProvider("quantum");
auto function = provider->createComposite("f", {});
options.insert("observable", observable);
options.insert("ansatz", function);
auto vqe = xacc::getService<Algorithm>("vqe");
vqe->initialize(options);
}
int main(int argc, char **argv) {
xacc::Initialize();
::testing::InitGoogleTest(&argc, argv);
......
......@@ -12,6 +12,7 @@
*******************************************************************************/
#ifndef XACC_HETEROGENEOUS_HPP_
#define XACC_HETEROGENEOUS_HPP_
#include <Utils.hpp>
#include <map>
#include <stdexcept>
#include <vector>
......@@ -107,7 +108,7 @@ public:
}
template <class T> const T &get(const std::string key) const {
if (!items<T>.count(this) && !items<T>[this].count(key)) {
if (!keyExists<T>(key)) {
XACCLogger::instance()->error("Invalid type (" +
std::string(typeid(T).name()) +
") or key (" + key + ").");
......
Supports Markdown
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