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

Starting on D-Wave IR infrastructure. Started simple D-Wave QMI Compiler realization

parent 75292254
......@@ -31,4 +31,4 @@
add_subdirectory(scaffold)
add_subdirectory(rigetti)
add_subdirectory(simple-simulator)
add_subdirectory(dwave)
#***********************************************************************************
# Copyright (c) 2016, UT-Battelle
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the xacc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Contributors:
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
include_directories(${CMAKE_SOURCE_DIR}/quantum/aqc/ir)
include_directories(${CMAKE_SOURCE_DIR}/quantum/aqc/compiler)
include_directories(${CMAKE_SOURCE_DIR}/tpls/rapidjson/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set (PACKAGE_NAME "XACC D-Wave Compiler and Accelerator Support")
set (PACKAGE_DESCIPTION "Extensions to XACC to work with the D-Wave QPU")
set (LIBRARY_NAME xacc-dwave)
file (GLOB_RECURSE HEADERS *.hpp)
file (GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES})
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "xacc-dwave;dl;pthread")
#add_subdirectory(examples)
/***********************************************************************************
* Copyright (c) 2016, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#include <regex>
#include <boost/algorithm/string.hpp>
#include "DWQMICompiler.hpp"
#include "DWKernel.hpp"
namespace xacc {
namespace quantum {
DWQMICompiler::DWQMICompiler() {
}
std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src,
std::shared_ptr<Accelerator> acc) {
// Set the Kernel Source code
kernelSource = src;
return std::make_shared<DWIR>();
}
std::shared_ptr<IR> DWQMICompiler::compile(const std::string& src) {
kernelSource = src;
// Here we expect we have a kernel, only one kernel,
// and that it is just machine level instructions
// So just pick out where the opening and closing
// brackets are, and then get the text between them.
// First off, split the string into lines
std::vector<std::string> lines;
boost::split(lines, src, boost::is_any_of("\n"));
auto firstCodeLine = lines.begin() + 1;
auto lastCodeLine = lines.end() - 1;
std::vector<std::string> qmiStrVec(firstCodeLine, lastCodeLine);
auto dwKernel = std::make_shared<DWKernel>();
for (auto qmi : qmiStrVec) {
std::cout << "LINE: " << qmi << "\n";
boost::trim(qmi);
if (!qmi.empty()) {
std::vector<std::string> splitOnSpaces;
boost::split(splitOnSpaces, qmi, boost::is_any_of(" "));
auto qbit1 = std::stoi(splitOnSpaces[0]);
auto qbit2 = std::stoi(splitOnSpaces[1]);
auto weight = std::stod(splitOnSpaces[2]);
auto dwqmi = std::make_shared<DWQMI>(qbit1, qbit2, weight);
dwKernel->addInstruction(dwqmi);
}
}
auto ir = std::make_shared<DWIR>();
// ir->addKernel(ir);
return ir;
}
}
}
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_AQC_COMPILERS_DWQMICOMPILER_HPP_
#define QUANTUM_AQC_COMPILERS_DWQMICOMPILER_HPP_
#include "Compiler.hpp"
#include "Utils.hpp"
#include "DWIR.hpp"
namespace xacc {
namespace quantum {
/**
*/
class DWQMICompiler: public xacc::Compiler {
public:
DWQMICompiler();
/**
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
std::shared_ptr<Accelerator> acc);
/**
*
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src);
/**
* Return the name of this Compiler
* @return name Compiler name
*/
virtual const std::string getName() {
return "dwave-qmi";
}
/**
* Register this Compiler with the framework.
*/
static void registerCompiler() {
xacc::RegisterCompiler<xacc::quantum::DWQMICompiler> Scaffold(
"dwave-qmi");
}
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
XACCError("DWQMICompiler::translate - Method not implemented");
};
/**
* The destructor
*/
virtual ~DWQMICompiler() {}
};
RegisterCompiler(xacc::quantum::DWQMICompiler)
}
}
#endif
......@@ -33,14 +33,14 @@
#define BOOST_TEST_MODULE DWaveCompilerTester
#include <boost/test/included/unit_test.hpp>
#include "DWaveCompiler.hpp"
#include "DWQMICompiler.hpp"
#include "XACC.hpp"
using namespace xacc::quantum;
struct F {
F() :
compiler(std::make_shared<DWaveCompiler>()) {
compiler(std::make_shared<DWQMICompiler>()) {
BOOST_TEST_MESSAGE("setup fixture");
BOOST_VERIFY(compiler);
}
......@@ -57,13 +57,18 @@ BOOST_FIXTURE_TEST_SUITE( s, F )
BOOST_AUTO_TEST_CASE(checkSimpleCompile) {
auto argc = boost::unit_test::framework::master_test_suite().argc;
auto argv = boost::unit_test::framework::master_test_suite().argv;
xacc::Initialize(argc, argv);
compiler->compile("");
const std::string simpleQMI =
"__qpu__ dwaveKernel() {\n"
" 0 0 0.98\n"
" 1 1 .33\n"
" 0 1 .22\n"
"}";
auto ir = compiler->compile(simpleQMI);
xacc::Finalize();
}
BOOST_AUTO_TEST_SUITE_END()
......@@ -28,7 +28,23 @@
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
set (PACKAGE_NAME "XACC Adiabatic Quantum Computing and Quantum Annealing Runtime")
set (PACKAGE_DESCIPTION "Runtime library for aqc and qa")
set (LIBRARY_NAME xacc-quantum-aqc)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ir)
add_subdirectory(compilers)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/compiler)
file (GLOB_RECURSE HEADERS *.hpp)
file (GLOB SRC ir/*.cpp compiler/*.cpp)
#add_library(${LIBRARY_NAME} SHARED ${SRC})
#target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES})
add_subdirectory(ir)
add_subdirectory(compiler)
install(FILES ${HEADERS} DESTINATION include/quantum/aqc)
#install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
......@@ -35,10 +35,10 @@ set (LIBRARY_NAME xacc-dwavecompiler)
file (GLOB HEADERS *.hpp)
file (GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
#add_library(${LIBRARY_NAME} SHARED ${SRC})
install(FILES ${HEADERS} DESTINATION include/quantum/aqc)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
#install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
# Gather tests
file (GLOB test_files tests/*.cpp)
......
......@@ -28,8 +28,8 @@
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_AQC_COMPILERS_EMBEDDINGALGORITHM_HPP_
#define QUANTUM_AQC_COMPILERS_EMBEDDINGALGORITHM_HPP_
#ifndef QUANTUM_AQC_COMPILER_EMBEDDINGALGORITHM_HPP_
#define QUANTUM_AQC_COMPILER_EMBEDDINGALGORITHM_HPP_
#include <boost/dll.hpp>
#include <map>
......
/*
* DWaveCompiler.hpp
*
* Created on: May 30, 2017
* Author: aqw
*/
#ifndef QUANTUM_AQC_COMPILERS_DWAVECOMPILER_HPP_
#define QUANTUM_AQC_COMPILERS_DWAVECOMPILER_HPP_
#include "Compiler.hpp"
#include "Utils.hpp"
#include "EmbeddingAlgorithm.hpp"
#include "DWaveIR.hpp"
namespace xacc {
namespace quantum {
/**
*/
class DWaveCompiler: public xacc::Compiler {
public:
DWaveCompiler();
/**
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src,
std::shared_ptr<Accelerator> acc);
/**
*
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string& src);
/**
* Return the name of this Compiler
* @return name Compiler name
*/
virtual const std::string getName() {
return "DWave";
}
/**
* Register this Compiler with the framework.
*/
static void registerCompiler() {
xacc::RegisterCompiler<xacc::quantum::DWaveCompiler> Scaffold(
"DWave");
}
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
};
/**
* The destructor
*/
virtual ~DWaveCompiler() {}
};
RegisterCompiler(xacc::quantum::DWaveCompiler)
}
}
#endif
#***********************************************************************************
# Copyright (c) 2016, UT-Battelle
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the xacc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Contributors:
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES}")
#ifndef QUANTUM_AQC_IR_DWAVEIR_HPP_
#define QUANTUM_AQC_IR_DWAVEIR_HPP_
#ifndef QUANTUM_AQC_IR_DWIR_HPP_
#define QUANTUM_AQC_IR_DWIR_HPP_
#include "IR.hpp"
#include "Graph.hpp"
namespace xacc {
namespace quantum {
class DWaveIR : public virtual IR {
using DWVertex = XACCVertex<double>;
class DWGraph :public Graph<DWVertex> {
public:
virtual void read(std::istream& stream) {
}
virtual ~DWGraph() {}
};
class DWIR : public virtual IR {
public:
/**
......@@ -56,4 +70,4 @@ public:
}
}
#endif /* QUANTUM_AQC_IR_DWAVEIR_HPP_ */
#endif /* QUANTUM_AQC_IR_DWIR_HPP_ */
/***********************************************************************************
* Copyright (c) 2017, UT-Battelle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the xacc nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Contributors:
* Initial API and implementation - Alex McCaskey
*
**********************************************************************************/
#ifndef QUANTUM_AQC_DWKERNEL_HPP_
#define QUANTUM_AQC_DWKERNEL_HPP_
#include "Registry.hpp"
#include "Function.hpp"
#include "DWQMI.hpp"
#include "Utils.hpp"
namespace xacc {
namespace quantum {
/**
*/
class DWKernel: public virtual Function {
protected:
std::list<InstPtr> instructions;
public:
/**
* The constructor, takes the function unique id and its name.
*
* @param id
* @param name
*/
DWKernel() {
}
virtual const int nInstructions() {
return instructions.size();
}
virtual InstPtr getInstruction(const int idx) {
if (instructions.size() > idx) {
return *std::next(instructions.begin(), idx);
} else {
XACCError("Invalid instruction index.");
}
}
virtual std::list<InstPtr> getInstructions() {
return instructions;
}
virtual void removeInstruction(const int idx) {
instructions.remove(getInstruction(idx));
}
/**
* Add an instruction to this quantum
* intermediate representation.
*
* @param instruction
*/
virtual void addInstruction(InstPtr instruction) {
instructions.push_back(instruction);
}
/**
* Replace the given current quantum instruction
* with the new replacingInst quantum Instruction.
*
* @param currentInst
* @param replacingInst
*/
virtual void replaceInstruction(const int idx, InstPtr replacingInst) {
std::replace(instructions.begin(), instructions.end(),
getInstruction(idx), replacingInst);
}
virtual void insertInstruction(const int idx, InstPtr newInst) {
auto iter = std::next(instructions.begin(), idx);
instructions.insert(iter, newInst);
}
/**
* Return the name of this function
* @return
*/
virtual const std::string getName() {
return "dw-kernel";
}
/**
* Return the qubits this function acts on.
* @return
*/
virtual const std::vector<int> bits() {
return std::vector<int> { };
}
/**
* Return an assembly-like string representation for this function .
* @param bufferVarName
* @return
*/
virtual const std::string toString(const std::string& bufferVarName) {
std::stringstream ss;