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

fixing bugs related to seg faults in python bindings



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent c7723d24
......@@ -9,13 +9,8 @@
if (NOT XACC_ROOT)
get_filename_component(XACC_ROOT "${CMAKE_CURRENT_LIST_FILE}" PATH)
endif()
set (XACC_INCLUDE_DIRS "${XACC_ROOT}/include/cppmicroservices3;${XACC_ROOT}/include/xacc;${XACC_ROOT}/include/quantum/gate;${XACC_ROOT}/include/quantum/aqc;${XACC_ROOT}/include/spdlog;${XACC_ROOT}/include/eigen;${XACC_ROOT}/include/rapidjson/include;${XACC_ROOT}/include;@OPENSSL_INCLUDE_DIR@")
set (XACC_INCLUDE_DIRS "${XACC_ROOT}/include/cppmicroservices3;${XACC_ROOT}/include/xacc;${XACC_ROOT}/include/quantum/gate;${XACC_ROOT}/include/quantum/aqc;${XACC_ROOT}/include/spdlog;${XACC_ROOT}/include/exprtk;${XACC_ROOT}/include/eigen;${XACC_ROOT}/include/rapidjson/include;${XACC_ROOT}/include;@OPENSSL_INCLUDE_DIR@")
set (XACC_LIBRARY_DIR "${XACC_ROOT}/lib")
set(CppUsLib CppMicroServicesd)
link_directories("${XACC_ROOT}/lib")
find_library(cppus_has_d_suffix CppMicroServicesd PATHS ${XACC_ROOT}/lib)
if (NOT cppus_has_d_suffix)
set (XACC_LIBRARIES "xacc;xacc-quantum-gate;xacc-quantum-aqc;CppMicroServices;cpprest;@OPENSSL_LIBRARIES@")
else()
set (XACC_LIBRARIES "xacc;xacc-quantum-gate;xacc-quantum-aqc;CppMicroServicesd;cpprest;@OPENSSL_LIBRARIES@")
endif()
set (XACC_LIBRARIES "xacc;CppMicroServices;cpprest;@OPENSSL_LIBRARIES@")
......@@ -9,8 +9,8 @@ message(STATUS "C++ version ${CXX_STANDARD} configured.")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
#find_package(PythonLibs)
#if(PythonLibs_FOUND)
find_package(PythonLibs)
if(PythonLibs_FOUND)
set(CppMicroServices_DIR "${CPPUS_LIBRARY_DIR}/../share/cppmicroservices3/cmake")
......@@ -32,28 +32,33 @@ include_directories(${CPPUS_INCLUDE_DIR})
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${XACC_ROOT}/tpls/rapidjson/include)
include_directories(${XACC_ROOT}/tpls/eigen)
include_directories(${XACC_ROOT}/tpls/exprtk)
include_directories(${XACC_ROOT}/tpls/pybind11/include)
include_directories(${XACC_ROOT}/tpls/spdlog)
include_directories(${XACC_PYTHON_INCLUDE_DIR})
include_directories(${PYTHON_INCLUDE_DIR})
link_directories(${XACC_LIBRARY_DIR})
add_subdirectory(pybind11)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing -O2 -g -pipe -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wformat -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv")
if(APPLE)
set(CMAKE_SHARED_LIBRARY_SUFFIX ".so")
endif(APPLE)
pybind11_add_module(pyxacc xacc-py.cpp)
set(CppUsLib CppMicroServicesd)
add_library(pyxacc SHARED xacc-py.cpp)
set_target_properties(pyxacc PROPERTIES PREFIX "")
link_directories(${CPPUS_LIBRARY_DIR})
link_directories(${Boost_LIBRARY_DIR})
find_library(cppus_has_d_suffix CppMicroServicesd)
if (NOT cppus_has_d_suffix)
set(CppUsLib CppMicroServices)
endif()
target_link_libraries(pyxacc PRIVATE xacc xacc-quantum-gate xacc-quantum-aqc ${CppUsLib} ${Boost_LIBRARIES} cpprest ${OPENSSL_LIBRARIES})
#find_library(cppus_has_d_suffix CppMicroServicesd)
#if (NOT cppus_has_d_suffix)
# set(CppUsLib CppMicroServices)
#endif()
target_link_libraries(pyxacc ${PYTHON_LIBRARY} xacc xacc-quantum-gate xacc-quantum-aqc CppMicroServices ${Boost_LIBRARIES} cpprest ${OPENSSL_LIBRARIES})
if (FROM_SETUP_PY)
install(TARGETS pyxacc DESTINATION ${CMAKE_INSTALL_PREFIX})
if(APPLE)
set_target_properties(pyxacc PROPERTIES INSTALL_RPATH "@loader_path/lib")
else()
install(TARGETS pyxacc DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/python)
set_target_properties(pyxacc PROPERTIES INSTALL_RPATH "$ORIGIN/lib;$ORIGIN/plugins")
endif()
#endif()
install(TARGETS pyxacc DESTINATION ${CMAKE_INSTALL_PREFIX})
endif()
This diff is collapsed.
......@@ -34,6 +34,7 @@ find_package(Boost COMPONENTS system program_options filesystem chrono thread RE
include_directories(${XACC_INCLUDE_DIR})
include_directories(${XACC_ROOT}/tpls/rapidjson/include)
include_directories(${XACC_ROOT}/tpls/exprtk)
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${XACC_ROOT}/tpls/spdlog)
......
......@@ -30,7 +30,7 @@ protected:
std::list<InstPtr> instructions;
std::string name;
std::string _name;
public:
......@@ -41,7 +41,7 @@ public:
* @param id
* @param name
*/
DWKernel(std::string kernelName) : name(kernelName) {
DWKernel(std::string kernelName) : _name(kernelName) {
}
virtual const int nInstructions() {
......@@ -104,10 +104,13 @@ public:
* Return the name of this function
* @return
*/
virtual const std::string getName() {
return name;
virtual const std::string name() const {
return _name;
}
virtual const std::string description() const {
return "";
}
/**
* Return the qubits this function acts on.
* @return
......@@ -177,7 +180,7 @@ public:
return 0;
}
virtual void evaluateVariableParameters(std::vector<InstructionParameter> parameters) {
virtual std::shared_ptr<Function> operator()(const Eigen::VectorXd& params) {
xacc::error("DWKernel does not contain runtime parameters.");
}
......
......@@ -95,10 +95,14 @@ public:
*
* @return name The name of this Instruction
*/
virtual const std::string getName() {
virtual const std::string name() const {
return "dw-qmi";
}
virtual const std::string description() const {
return "";
}
virtual const std::string getTag() {
return "";
}
......
......@@ -30,7 +30,7 @@ BOOST_AUTO_TEST_CASE(checkDWKernelConstruction) {
kernel.addInstruction(qmi3);
BOOST_VERIFY(kernel.nInstructions() == 3);
BOOST_VERIFY(kernel.getName() == "foo");
BOOST_VERIFY(kernel.name() == "foo");
BOOST_VERIFY(kernel.getInstruction(0) == qmi);
BOOST_VERIFY(kernel.getInstruction(1) == qmi2);
BOOST_VERIFY(kernel.getInstruction(2) == qmi3);
......
......@@ -57,7 +57,6 @@ usFunctionEmbedResources(TARGET ${LIBRARY_NAME}
)
link_directories(${Boost_LIBRARY_DIR})
set(CppUsLib CppMicroServicesd)
link_directories(${CPPUS_LIBRARY_DIR})
......
......@@ -19,6 +19,22 @@
#include "cppmicroservices/BundleContext.h"
#include "cppmicroservices/ServiceProperties.h"
#include "GateIRProvider.hpp"
#include "Hadamard.hpp"
#include "CNOT.hpp"
#include "X.hpp"
#include "Y.hpp"
#include "Z.hpp"
#include "ConditionalFunction.hpp"
#include "Rz.hpp"
#include "Rx.hpp"
#include "Ry.hpp"
#include "CPhase.hpp"
#include "Swap.hpp"
#include "Measure.hpp"
#include "Identity.hpp"
#include "CZ.hpp"
#include <memory>
#include <set>
......@@ -46,6 +62,9 @@ public:
auto readout = std::make_shared<xacc::quantum::ReadoutErrorIRPreprocessor>();
auto qbitmap = std::make_shared<xacc::quantum::QubitMapIRPreprocessor>();
auto giservice = std::make_shared<xacc::quantum::GateIRProvider>();
context.RegisterService<xacc::IRProvider>(giservice);
context.RegisterService<xacc::Preprocessor>(kp);
context.RegisterService<xacc::IRGenerator>(iqft);
context.RegisterService<xacc::IRGenerator>(qft);
......@@ -53,6 +72,34 @@ public:
context.RegisterService<xacc::IRPreprocessor>(readout);
context.RegisterService<xacc::IRPreprocessor>(qbitmap);
auto h = std::make_shared<xacc::quantum::Hadamard>();
auto cn = std::make_shared<xacc::quantum::CNOT>();
auto cp = std::make_shared<xacc::quantum::CPhase>();
auto cz = std::make_shared<xacc::quantum::CZ>();
auto id = std::make_shared<xacc::quantum::Identity>();
auto m = std::make_shared<xacc::quantum::Measure>();
auto rx = std::make_shared<xacc::quantum::Rx>();
auto ry = std::make_shared<xacc::quantum::Ry>();
auto rz = std::make_shared<xacc::quantum::Rz>();
auto x = std::make_shared<xacc::quantum::X>();
auto y = std::make_shared<xacc::quantum::Y>();
auto z = std::make_shared<xacc::quantum::Z>();
auto sw = std::make_shared<xacc::quantum::Swap>();
context.RegisterService<xacc::quantum::GateInstruction>(h);
context.RegisterService<xacc::quantum::GateInstruction>(cn);
context.RegisterService<xacc::quantum::GateInstruction>(cp);
context.RegisterService<xacc::quantum::GateInstruction>(cz);
context.RegisterService<xacc::quantum::GateInstruction>(id);
context.RegisterService<xacc::quantum::GateInstruction>(m);
context.RegisterService<xacc::quantum::GateInstruction>(rx);
context.RegisterService<xacc::quantum::GateInstruction>(ry);
context.RegisterService<xacc::quantum::GateInstruction>(rz);
context.RegisterService<xacc::quantum::GateInstruction>(x);
context.RegisterService<xacc::quantum::GateInstruction>(y);
context.RegisterService<xacc::quantum::GateInstruction>(z);
context.RegisterService<xacc::quantum::GateInstruction>(sw);
}
/**
......
......@@ -77,7 +77,7 @@ std::vector<std::shared_ptr<AcceleratorBuffer>> ReadoutErrorAcceleratorBufferPos
first = true;
}
xacc::info(kernel->getName() + " - Computing measurement probability for bit string = " + localBitStr);
xacc::info(kernel->name() + " - Computing measurement probability for bit string = " + localBitStr);
probs.push_back(subList[i]->computeMeasurementProbability(localBitStr));
counter++;
......
......@@ -15,11 +15,12 @@
#include <boost/test/included/unit_test.hpp>
#include "ReadoutErrorAcceleratorBufferPostprocessor.hpp"
#include "GateQIR.hpp"
#include "GateIR.hpp"
#include <boost/math/constants/constants.hpp>
#include "XACC.hpp"
#include "GateFunction.hpp"
#include "GateInstruction.hpp"
#include "GateIRProvider.hpp"
using namespace xacc;
......@@ -29,10 +30,12 @@ using Term = std::map<int, std::string>;
std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
// Create a new GateQIR to hold the spin based terms
auto newIr = std::make_shared<xacc::quantum::GateQIR>();
auto newIr = std::make_shared<xacc::quantum::GateIR>();
int counter = 0;
auto pi = boost::math::constants::pi<double>();
GateIRProvider gateRegistry;
// Populate GateQIR now...
for (auto& inst : terms) {
......@@ -46,7 +49,7 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
// Loop over all terms in the Spin Instruction
// and create instructions to run on the Gate QPU.
std::vector<std::shared_ptr<xacc::quantum::GateInstruction>> measurements;
std::vector<std::shared_ptr<xacc::Instruction>> measurements;
std::vector<std::pair<int, std::string>> terms;
for (auto& kv : spinInst) {
......@@ -58,20 +61,18 @@ std::shared_ptr<IR> createXACCIR(std::unordered_map<std::string, Term> terms) {
for (int i = terms.size() - 1; i >= 0; i--) {
auto qbit = terms[i].first;
auto gateName = terms[i].second;
auto gateRegistry =
xacc::quantum::GateInstructionRegistry::instance();
auto meas = gateRegistry->create("Measure",
auto meas = gateRegistry.createInstruction("Measure",
std::vector<int> { qbit });
xacc::InstructionParameter classicalIdx(qbit);
meas->setParameter(0, classicalIdx);
measurements.push_back(meas);
if (gateName == "X") {
auto hadamard = gateRegistry->create("H", std::vector<int> {
auto hadamard = gateRegistry.createInstruction("H", std::vector<int> {
qbit });
gateFunction->addInstruction(hadamard);
} else if (gateName == "Y") {
auto rx = gateRegistry->create("Rx", std::vector<int> { qbit });
auto rx = gateRegistry.createInstruction("Rx", std::vector<int> { qbit });
InstructionParameter p(pi / 2.0);
rx->setParameter(0, p);
gateFunction->addInstruction(rx);
......@@ -115,24 +116,25 @@ public:
BOOST_AUTO_TEST_CASE(checkSimple) {
xacc::Initialize();
// Simple example H = X0
std::vector<std::string> orderedTerms{"Z0"};
auto gateRegistry = GateInstructionRegistry::instance();
auto gateRegistry = xacc::getService<IRProvider>("gate");
// Construct IR for this
auto ir = createXACCIR( { { "Z0", Term { { 0, "Z" } } } });
auto f01 = std::make_shared<GateFunction>("measure0_qubit_0",
"readout-error");
auto meas01 = gateRegistry->create("Measure", std::vector<int> { 0 });
auto meas01 = gateRegistry->createInstruction("Measure", std::vector<int> { 0 });
InstructionParameter p(0);
meas01->setParameter(0, p);
f01->addInstruction(meas01);
auto f10 = std::make_shared<GateFunction>("measure1_qubit_0",
"readout-error");
auto x = gateRegistry->create("X", std::vector<int> { 0 });
auto meas10 = gateRegistry->create("Measure", std::vector<int> { 0 });
auto x = gateRegistry->createInstruction("X", std::vector<int> { 0 });
auto meas10 = gateRegistry->createInstruction("Measure", std::vector<int> { 0 });
InstructionParameter p2(0);
meas10->setParameter(0, p2);
f10->addInstruction(x);
......@@ -158,6 +160,7 @@ BOOST_AUTO_TEST_CASE(checkSimple) {
std::cout << "HELLO: " << fixed[0]->getExpectationValueZ() << "\n";
xacc::Finalize();
// BOOST_VERIFY(std::fabs(fixed[0]->getExpectationValueZ() - .911111) < 1e-6);
}
......@@ -54,14 +54,6 @@ public:
std::shared_ptr<Compiler> compiler,
std::shared_ptr<Accelerator> accelerator);
/**
* Return the name of this Preprocessor
* @return name Preprocessor name
*/
virtual const std::string getName() {
return "kernel-replacement";
}
virtual const std::string name() const {
return "kernel-replacement";
}
......
......@@ -16,7 +16,7 @@
#include "KernelReplacementPreprocessor.hpp"
#include "Compiler.hpp"
#include "Accelerator.hpp"
#include "GateQIR.hpp"
#include "GateIR.hpp"
#include "XACC.hpp"
#include "ServiceRegistry.hpp"
......@@ -29,11 +29,11 @@ class DummyCompiler: public xacc::Compiler {
public:
virtual std::shared_ptr<IR> compile(const std::string& src,
std::shared_ptr<Accelerator> acc) {
return std::make_shared<GateQIR>();
return std::make_shared<GateIR>();
}
virtual std::shared_ptr<IR> compile(const std::string& src) {
return std::make_shared<GateQIR>();
return std::make_shared<GateIR>();
}
virtual const std::string translate(const std::string& bufferVariable,
......@@ -68,10 +68,6 @@ public:
return translated;
}
virtual const std::string getName() {
return "Dummy";
}
virtual const std::string name() const {
return "";
}
......
......@@ -13,9 +13,11 @@
#ifndef QUANTUM_GATEQIR_QFUNCTION_HPP_
#define QUANTUM_GATEQIR_QFUNCTION_HPP_
#include "Registry.hpp"
#include "Function.hpp"
#include "XACC.hpp"
#include <boost/math/constants/constants.hpp>
#include "exprtk.hpp"
#include "IRProvider.hpp"
namespace xacc {
namespace quantum {
......@@ -138,10 +140,18 @@ public:
* Return the name of this function
* @return
*/
virtual const std::string getName() {
virtual const std::string name() const {
return functionName;
}
/**
* Return the description of this instance
* @return description The description of this object.
*/
virtual const std::string description() const {
return "";
}
/**
* Return the qubits this function acts on.
* @return
......@@ -195,60 +205,134 @@ public:
return parameters.size();
}
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++;
// 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->name() != "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++;
// }
// }
static constexpr double pi = boost::math::constants::pi<double>();
using symbol_table_t = exprtk::symbol_table<double>;
using expression_t = exprtk::expression<double>;
using parser_t = exprtk::parser<double>;
virtual std::shared_ptr<Function> operator()(const Eigen::VectorXd& params) {
if (params.size() != nParameters()) {
xacc::error("Invalid GateFunction evaluation: number "
"of parameters don't match. " + std::to_string(params.size()) +
", " + std::to_string(nParameters()));
}
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]);
std::vector<std::string> variableNames;
for (int i = 0; i < params.size(); i++) {
variableNames.push_back(
boost::get<std::string>(getParameter(i)));
}
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 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];
auto gateRegistry = xacc::getService<IRProvider>("gate");
auto evaluatedFunction = std::make_shared<GateFunction>("evaled_"+name());
inst->setParameter(j, runtimeParameter);
cachedVariableInstructions.insert(std::make_pair(i, std::make_pair(j, variable)));
// Walk the IR Tree, handle functions and instructions differently
for (auto inst : getInstructions()) {
if (inst->isComposite()) {
// If a Function, call this method recursively
auto evaled =
std::dynamic_pointer_cast<Function>(inst)->operator()(
params);
evaluatedFunction->addInstruction(evaled);
} else {
// If a concrete GateInstruction, then check that it
// is parameterized and that it has a string parameter
if (inst->isParameterized()
&& inst->getParameter(0).which() == 3) {
int idx = -1;
auto expression = boost::get<std::string>(
inst->getParameter(0));
for (int i = 0; i < params.size(); i++) {
if (boost::contains(expression, variableNames[i])) {
idx = i;
}
}
std::string varName = variableNames[idx];
double val;
symbol_table_t symbol_table;
symbol_table.add_variable(varName, val);
symbol_table.add_constants();
expression_t expr;
expr.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression, expr);
val = params(idx);
auto res = expr.value();
// if (res < 0.0) {
// res = 4 * pi + res;
// }
InstructionParameter p(res);
auto updatedInst = gateRegistry->createInstruction(inst->name(),
inst->bits());
updatedInst->setParameter(0, p);
evaluatedFunction->addInstruction(updatedInst);
} else {
evaluatedFunction->addInstruction(inst);
}
}
i++;
}
return evaluatedFunction;
}