Commit 76229d9e authored by Mccaskey, Alex's avatar Mccaskey, Alex

Removing Compiler template parameter. Removing common submodule, adding fire...

Removing Compiler template parameter. Removing common submodule, adding fire as submodule. general clean up
parent 0b4c5d53
[submodule "tpls/common"]
path = tpls/common
url = https://github.com/ORNL-QCI/common
[submodule "tpls/fire"]
path = tpls/fire
url = https://github.com/jayjaybillings/fire
......@@ -38,9 +38,9 @@ message(STATUS "C++ version ${CXX_STANDARD} configured.")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
if (EXISTS ${CMAKE_SOURCE_DIR}/tpls/common)
message(STATUS "Common found in tpls")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/cmake/Modules")
if (EXISTS ${CMAKE_SOURCE_DIR}/tpls/fire)
message(STATUS "Fire found in tpls")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/tpls/fire/cmake/Modules")
include(tests)
endif()
......@@ -67,9 +67,6 @@ include_directories("${CMAKE_SOURCE_DIR}/xacc/compiler")
include_directories("${CMAKE_SOURCE_DIR}/xacc/program")
include_directories("${CMAKE_SOURCE_DIR}/xacc/accelerator")
include_directories("${CMAKE_SOURCE_DIR}/xacc/utils")
include_directories("${CMAKE_SOURCE_DIR}/tpls/common/factory")
include_directories("${CMAKE_SOURCE_DIR}/tpls/common/graph")
include_directories("${CMAKE_SOURCE_DIR}/tpls/common/exception")
add_subdirectory(tpls)
add_subdirectory(xacc)
......
......@@ -30,9 +30,9 @@
#**********************************************************************************/
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/accelerators)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/accelerators/firetensoraccelerator)
include_directories(${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tpls/eigen)
include_directories(${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors)
include_directories(${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors/impl)
include_directories(${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen)
include_directories(${CMAKE_SOURCE_DIR}/tpls/fire/tensors)
include_directories(${CMAKE_SOURCE_DIR}/tpls/fire/tensors/impl)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/utils)
add_executable(teleport_scaffold teleport_scaffold.cpp)
......
......@@ -29,10 +29,10 @@
#
#**********************************************************************************/
include_directories(${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensor)
include_directories(${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tpls/eigen)
include_directories(${CMAKE_SOURCE_DIR}/tpls/fire/tensor)
include_directories(${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_SOURCE_DIR}/firetensoraccelerator;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tpls/eigen" "${Boost_LIBRARIES}")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_SOURCE_DIR}/firetensoraccelerator;${CMAKE_SOURCE_DIR}/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen" "${Boost_LIBRARIES}")
......@@ -86,7 +86,7 @@ public:
// Get the requested qubit buffer
auto qubits = this->allocatedBuffers[bufferId];
if (!qubits) {
QCIError("Invalid buffer id. Could not get qubit buffer.");
XACCError("Invalid buffer id. Could not get qubit buffer.");
}
// Set the size
......@@ -95,7 +95,7 @@ public:
// Cast to a GraphIR, if we can...
auto graphir = std::dynamic_pointer_cast<QuantumGraphIR>(ir);
if (!graphir) {
QCIError("Invalid IR - this Accelerator only accepts GraphIR<Graph<CircuitNode>>.");
XACCError("Invalid IR - this Accelerator only accepts GraphIR<Graph<CircuitNode>>.");
}
// Get the Graph and related info
......@@ -144,7 +144,7 @@ public:
auto qubitWeNeed = std::get<3>(gate.properties)[0];
auto qubitFound = qubitIdToMeasuredResult.find(qubitWeNeed);
if (qubitFound == qubitIdToMeasuredResult.end()) {
QCIError("Invalid conditional node - this qubit has not been measured.");
XACCError("Invalid conditional node - this qubit has not been measured.");
}
auto result = qubitIdToMeasuredResult[qubitWeNeed];
......@@ -247,7 +247,7 @@ public:
// Sum them up
localU = localU + temp;
} else {
QCIError("Can only simulate one and two qubit gates.");
XACCError("Can only simulate one and two qubit gates.");
}
// Make sure that localU is the correct size
......@@ -288,7 +288,7 @@ protected:
auto rotation = std::exp(i * param);
g.setValues( { { 1, 0 }, { 0, rotation } });
} else {
QCIError("We don't know what this gate is... yet.");
XACCError("We don't know what this gate is... yet.");
}
return g;
......
......@@ -34,7 +34,6 @@
#include <boost/test/included/unit_test.hpp>
#include "SimulatedQubits.hpp"
using namespace qci::common;
using namespace xacc::quantum;
BOOST_AUTO_TEST_CASE(checkConstruction) {
......
......@@ -32,12 +32,10 @@
#define QUANTUM_GATE_SCAFFOLD_SCAFFCCAPI_HPP_
#include <cstdlib>
#include "QCIError.hpp"
#include "XACCError.hpp"
#include <iostream>
#include <fstream>
using namespace qci::common;
namespace scaffold {
/**
......@@ -88,7 +86,7 @@ public:
// Return the QASM
return qasm;
} else {
QCIError(
XACCError(
"Cannot find scaffold compiler. Make sure scaffcc is in PATH and executable.")
}
}
......
......@@ -29,11 +29,8 @@
*
**********************************************************************************/
#include "ScaffoldCompiler.hpp"
#include "GraphIR.hpp"
#include <regex>
#include "ScaffCCAPI.hpp"
#include "QasmToGraph.hpp"
#include "QuantumCircuit.hpp"
namespace xacc {
......@@ -206,7 +203,51 @@ void ScaffoldCompiler::modifySource() {
// std::cout << "\n" << kernelSource << "\n";
}
std::shared_ptr<IR> ScaffoldCompiler::compile() {
std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src,
std::shared_ptr<IAccelerator> acc) {
kernelSource = src;
kernelArgsToMap();
// Set the accelerator
accelerator = acc;
// Get the bit variable type string
auto bitTypeStr = getBitType();
// Get the qubit variable name, if it exists
std::string varName;
for (auto it = typeToVarKernelArgs.begin(); it != typeToVarKernelArgs.end();
it++) {
if (boost::contains(it->first, bitTypeStr)) {
varName = it->second;
auto nBits = accelerator->getBufferSize(varName);
boost::replace_first(kernelSource,
std::string(bitTypeStr + " " + varName),
std::string(
bitTypeStr + " " + varName + "["
+ std::to_string(nBits) + "]"));
// Replace the varname in the map with varName[#]
typeToVarKernelArgs[bitTypeStr] = varName + "["
+ std::to_string(nBits) + "]";
}
}
return compile(kernelSource);
}
std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
kernelSource = src;
if (!accelerator) {
kernelArgsToMap();
}
modifySource();
// Create an instance of our ScaffCC API
// so that we can interact with a locally installed
......@@ -273,6 +314,6 @@ std::shared_ptr<IR> ScaffoldCompiler::compile() {
} // end namespace xacc
// Required in CPP file to be discovered by factory pattern
REGISTER_QCIOBJECT_WITH_QCITYPE(xacc::quantum::ScaffoldCompiler, "compiler",
REGISTER_XACCOBJECT_WITH_XACCTYPE(xacc::quantum::ScaffoldCompiler, "compiler",
"scaffold");
......@@ -31,13 +31,14 @@
#ifndef QUANTUM_SCAFFOLDCOMPILER_HPP_
#define QUANTUM_SCAFFOLDCOMPILER_HPP_
#include <iostream>
#include <memory>
#include "AbstractFactory.hpp"
#include "Compiler.hpp"
#include "QCIError.hpp"
using namespace qci::common;
#include "XACCError.hpp"
#include <boost/algorithm/string.hpp>
#include "Accelerator.hpp"
#include "GraphIR.hpp"
#include "ScaffCCAPI.hpp"
#include "QasmToGraph.hpp"
#include "QuantumCircuit.hpp"
namespace xacc {
......@@ -49,7 +50,7 @@ namespace quantum {
* to handle generation of quantum assembly language (or QASM)
* using an installed Scaffold compiler.
*/
class ScaffoldCompiler : public Compiler<ScaffoldCompiler> {
class ScaffoldCompiler : public xacc::Compiler {
public:
......@@ -58,7 +59,10 @@ public:
* XACC intermediate representation instance.
* @return ir XACC intermediate representation
*/
virtual std::shared_ptr<IR> compile();
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
std::shared_ptr<IAccelerator> acc);
virtual std::shared_ptr<xacc::IR> compile(const std::string& src);
/**
*
......@@ -94,6 +98,49 @@ protected:
std::map<int, std::vector<std::string>> gateIdToParameterMap;
/**
*
*/
std::map<std::string, std::string> typeToVarKernelArgs;
std::vector<std::string> orderOfArgs;
std::vector<std::string> orderedVarNames;
virtual std::vector<std::string> getKernelArgumentVariableNames() {
return orderedVarNames;
}
void kernelArgsToMap() {
auto firstParen = kernelSource.find_first_of('(');
auto secondParen = kernelSource.find_first_of(')', firstParen);
auto functionArguments = kernelSource.substr(firstParen+1, (secondParen-firstParen)-1);
int counter = 0;
if (!functionArguments.empty()) {
// First search the prototype to see if it has
// and argument that declares the accelerator bit buffer
// to use in the kernel
std::vector<std::string> splitArgs, splitTypeVar;
boost::split(splitArgs, functionArguments, boost::is_any_of(","));
std::string varName;
for (int i = 0; i < splitArgs.size(); i++) {
// split type from var name
auto s = splitArgs[i];
boost::trim(s);
boost::split(splitTypeVar, s, boost::is_any_of(" "));
auto type = splitTypeVar[0];
auto var = splitTypeVar[1];
boost::trim(type);
boost::trim(var);
typeToVarKernelArgs.insert(std::make_pair(type, var));
orderOfArgs.push_back(type);
orderedVarNames.push_back(var);
}
}
}
};
}
......
......@@ -32,30 +32,43 @@
#define BOOST_TEST_MODULE ScaffoldCompilerTester
#include <boost/test/included/unit_test.hpp>
#include "AbstractFactory.hpp"
#include "Compiler.hpp"
#include "QasmToGraph.hpp"
#include "GraphIR.hpp"
#include "ScaffoldCompiler.hpp"
#include "XACCFactory.hpp"
using namespace qci::common;
using namespace xacc::quantum;
using GraphType = QuantumCircuit;
BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
using GraphType = QuantumCircuit;
struct F {
F() :
compiler(
xacc::XACCFactory::createAndCast<xacc::Compiler>("compiler",
"scaffold")) {
BOOST_TEST_MESSAGE("setup fixture");
BOOST_VERIFY(compiler);
}
~F() {
BOOST_TEST_MESSAGE("teardown fixture");
}
std::shared_ptr<xacc::Compiler> compiler;
};
//____________________________________________________________________________//
auto compiler = qci::common::AbstractFactory::createAndCast<xacc::ICompiler>("compiler", "scaffold");
BOOST_VERIFY(compiler);
BOOST_FIXTURE_TEST_SUITE( s, F )
BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
const std::string src("__qpu__ eprCreation () {\n"
" qbit qreg[2];\n"
" H(qreg[0]);\n"
" CNOT(qreg[0],qreg[1]);\n"
"}\n");
" qbit qreg[2];\n"
" H(qreg[0]);\n"
" CNOT(qreg[0],qreg[1]);\n"
"}\n");
auto ir = compiler->compile(src);
BOOST_VERIFY(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<GraphType>>(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
BOOST_VERIFY(graphir);
// The above code should produce a graph
......@@ -70,29 +83,22 @@ BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
}
BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) {
using GraphType = QuantumCircuit;
auto compiler =
qci::common::AbstractFactory::createAndCast<xacc::ICompiler>(
"compiler", "scaffold");
BOOST_VERIFY(compiler);
const std::string src("__qpu__ teleport () {\n"
" qbit qreg[3];\n"
" cbit creg[2];\n"
" H(qreg[1]);\n"
" CNOT(qreg[1],qreg[2]);\n"
" CNOT(qreg[0],qreg[1]);\n"
" H(qreg[0]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
" if (creg[0] == 1) Z(qreg[2]);\n"
" if (creg[1] == 1) X(qreg[2]);\n"
"}\n");
" qbit qreg[3];\n"
" cbit creg[2];\n"
" H(qreg[1]);\n"
" CNOT(qreg[1],qreg[2]);\n"
" CNOT(qreg[0],qreg[1]);\n"
" H(qreg[0]);\n"
" creg[0] = MeasZ(qreg[0]);\n"
" creg[1] = MeasZ(qreg[1]);\n"
" if (creg[0] == 1) Z(qreg[2]);\n"
" if (creg[1] == 1) X(qreg[2]);\n"
"}\n");
auto ir = compiler->compile(src);
BOOST_VERIFY(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<GraphType>>(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
BOOST_VERIFY(graphir);
graphir->persist(std::cout);
......@@ -102,21 +108,17 @@ BOOST_AUTO_TEST_CASE(checkCodeWithMeasurementIf) {
}
BOOST_AUTO_TEST_CASE(checkCodeWithArgument) {
using GraphType = QuantumCircuit;
auto compiler =
qci::common::AbstractFactory::createAndCast<xacc::ICompiler>(
"compiler", "scaffold");
BOOST_VERIFY(compiler);
const std::string src("__qpu__ kernel (qbit qreg[1], double phi) {\n"
" Rz(qreg[0], phi);\n"
"}\n");
" Rz(qreg[0], phi);\n"
"}\n");
auto ir = compiler->compile(src);
BOOST_VERIFY(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<GraphType>>(ir);
auto graphir = std::dynamic_pointer_cast<xacc::GraphIR<QuantumCircuit>>(ir);
BOOST_VERIFY(graphir);
graphir->persist(std::cout);
}
BOOST_AUTO_TEST_SUITE_END()
......@@ -57,7 +57,6 @@ public:
const std::string& flatQasmStr) {
// Local Declarations
using namespace qci::common;
QuantumCircuit graph;
std::map<std::string, int> qubitVarNameToId;
std::vector<std::string> qasmLines;
......
......@@ -43,11 +43,11 @@ namespace quantum {
* Parameters: Gate, Layer (ie time sequence), Gate Vertex Id,
* Qubit Ids that the gate acts on, enabled state, vector of parameters names
*/
class CircuitNode: public qci::common::QCIVertex<std::string, int, int,
class CircuitNode: public XACCVertex<std::string, int, int,
std::vector<int>, bool, std::vector<std::string>> {
public:
CircuitNode() :
QCIVertex() {
XACCVertex() {
propertyNames[0] = "Gate";
propertyNames[1] = "Circuit Layer";
propertyNames[2] = "Gate Vertex Id";
......@@ -67,7 +67,7 @@ public:
* its Vertex template parameter as a CircuitNode. It adds the
* ability to read QuantumCircuits from a graphviz dot file.
*/
class QuantumCircuit : virtual public qci::common::Graph<CircuitNode> {
class QuantumCircuit : virtual public Graph<CircuitNode> {
public:
virtual void read(std::istream& stream) {
......
......@@ -59,7 +59,7 @@ int main(int argc, char** argv) {
std::ofstream outputStream(outputFileName);
std::stringstream ss;
if (!inputStream) {
QCIError("Invalid input qasm file");
XACCError("Invalid input qasm file");
}
ss << inputStream.rdbuf();
......
add_subdirectory(common)
#add_subdirectory(fire)
Subproject commit f406c0dd32474e0267a381085957e962ca5baa6b
Subproject commit e1e149dcf6e37f609fe9f806bfdcbc8e68439772
......@@ -41,7 +41,7 @@ file (GLOB SRC program/*.cpp compiler/*.cpp accelerator/*.cpp)
file(GLOB test_files tests/*Tester.cpp)
# Add the tests
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils;${CMAKE_CURRENT_SOURCE_DIR}/tests;${CMAKE_CURRENT_SOURCE_DIR}/compiler;${CMAKE_CURRENT_SOURCE_DIR}/program;${CMAKE_CURRENT_SOURCE_DIR}/accelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators/firetensoraccelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators;${CMAKE_SOURCE_DIR}/quantum/gate/utils;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/common/tpls/fire/tpls/eigen" "${Boost_LIBRARIES};xacc-scaffold")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}/utils;${CMAKE_CURRENT_SOURCE_DIR}/tests;${CMAKE_CURRENT_SOURCE_DIR}/compiler;${CMAKE_CURRENT_SOURCE_DIR}/program;${CMAKE_CURRENT_SOURCE_DIR}/accelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators/firetensoraccelerator;${CMAKE_SOURCE_DIR}/quantum/gate/accelerators;${CMAKE_SOURCE_DIR}/quantum/gate/utils;${CMAKE_SOURCE_DIR}/tpls/fire/tensors;${CMAKE_SOURCE_DIR}/tpls/fire/tensors/impl;${CMAKE_SOURCE_DIR}/tpls/fire/tpls/eigen" "${Boost_LIBRARIES};xacc-scaffold")
install(FILES ${HEADERS} DESTINATION include)
......
......@@ -35,7 +35,6 @@
#include <memory>
#include "Accelerator.hpp"
#include "Program.hpp"
#include "AbstractFactory.hpp"
namespace xacc {
......
......@@ -33,6 +33,7 @@
#include <string>
#include "IRTransformation.hpp"
#include "XACCFactory.hpp"
#include <array>
#include <bitset>
......@@ -96,7 +97,7 @@ protected:
std::string bufferId;
};
class IAccelerator : public qci::common::QCIObject {
class IAccelerator : public xacc::XACCObject {
public:
/**
* Return the type of this Accelerator.
......@@ -186,16 +187,16 @@ public:
allocatedBuffers.insert(std::make_pair(varId, buffer));
return buffer;
} else {
QCIError("Invalid buffer variable name.");
XACCError("Invalid buffer variable name.");
}
}
BitsTypePtr createBuffer(const std::string& varId, const int size) {
if (!isValidBufferVarId(varId)) {
QCIError("Invalid buffer variable name.");
XACCError("Invalid buffer variable name.");
}
if (!isValidBufferSize(size)) {
QCIError("Invalid buffer size.");
XACCError("Invalid buffer size.");
}
auto buffer = std::make_shared<BitsType>(varId, size);
allocatedBuffers.insert(std::make_pair(varId, buffer));
......@@ -206,13 +207,13 @@ public:
BitsTypePtr createBuffer(const std::string& varId, int firstIndex,
Indices ... indices) {
if (!isValidBufferVarId(varId)) {
QCIError("Invalid buffer variable name.");
XACCError("Invalid buffer variable name.");
}
if (!isValidBufferSize(sizeof...(indices) + 1)) {
QCIError("Invalid buffer size.");
XACCError("Invalid buffer size.");
}
if (!validIndices(indices...)) {
QCIError("Invalid buffer indices.");
XACCError("Invalid buffer indices.");
}
auto buffer = std::make_shared<BitsType>(varId, firstIndex, indices...);
allocatedBuffers.insert(std::make_pair(varId, buffer));
......
......@@ -31,62 +31,18 @@
#ifndef XACC_COMPILER_HPP_
#define XACC_COMPILER_HPP_
#include <iostream>
#include <memory>
#include "AbstractFactory.hpp"
#include "QCIError.hpp"
#include <iostream>
#include "XACCFactory.hpp"
#include "IR.hpp"
#include <ostream>
#include <boost/algorithm/string.hpp>
#include "Accelerator.hpp"
using namespace qci::common;
namespace xacc {
/**
* The ICompiler provides an interface for clients to realize to
* provide a mechanism for compiling a provided source string to
* the XACC Intermediate Representation (IR).
*/
class ICompiler : public qci::common::QCIObject {
public:
virtual ~ICompiler() {}
/**
*
* @param src
* @return
*/
virtual std::shared_ptr<IR> compile(const std::string& src,
std::shared_ptr<IAccelerator>& accelerator) = 0;
/**
*
* @param src
* @return
*/
virtual std::shared_ptr<IR> compile(const std::string& src) = 0;
virtual std::vector<std::string> getKernelArgumentVariableNames() = 0;
/**
*
* @return
*/
virtual std::string getBitType() = 0;
};
/**
* The Compiler provides implements the ICompiler interface by
* delegating to available concrete language compilers. This class employs
* the curiously recurring template pattern to delegate (static polymorphism)
* compilation requests to subclasses that have knowledge of the source
* string language. This interface is a subclass of the QCIObject to enable
* dynamic injection into the QCI Common AbstractFactory.
*
*/
template<typename Derived>
class Compiler: public ICompiler {
class Compiler: public xacc::XACCObject {
public:
......@@ -99,79 +55,9 @@ public:
* @return ir Intermediate representation for provided source kernel code.
*/
virtual std::shared_ptr<IR> compile(const std::string& src,
std::shared_ptr<IAccelerator>& acc) {
// Set the provided kernel source string
// so derived types can have reference to it
kernelSource = src;
kernelArgsToMap();
std::shared_ptr<IAccelerator> acc) = 0;
// Set the accelerator
accelerator = acc;
// Get the bit variable type string
auto bitTypeStr = getAsDerived().getBitType();