Commit 2cbbd456 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding AlgorithmGenerator and KernelReplacementPreprocessor. Adding QFT...

Adding AlgorithmGenerator and KernelReplacementPreprocessor. Adding QFT example to simple and rigetti
parent a70cb2a0
......@@ -58,14 +58,14 @@ if (OPENSSL_FOUND)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} xacc-gate-ir ${PYTHON_LIBRARY})
target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} xacc-quantum-gate ${PYTHON_LIBRARY})
#install(FILES ${HEADERS} DESTINATION include)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc-gate-ir;xacc-rigetti")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc-rigetti")
add_subdirectory(examples)
endif()
......@@ -139,7 +139,8 @@ std::shared_ptr<IR> QuilCompiler::compile(const std::string& src) {
return ir;
}
const std::string QuilCompiler::translate(std::shared_ptr<Function> function) {
const std::string QuilCompiler::translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
auto visitor = std::make_shared<QuilVisitor>();
InstructionIterator it(function);
while (it.hasNext()) {
......
......@@ -87,7 +87,8 @@ public:
* @param function The XACC IR Function to translate
* @return src The source code as a string
*/
virtual const std::string translate(std::shared_ptr<Function> function);
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function);
/**
* The destructor
......
......@@ -229,6 +229,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
" re = struct.unpack('>d', re_be)[0]\n"
" im = struct.unpack('>d', im_be)[0]\n"
" wf[i] = complex(re, im)\n"
" print re, im\n"
" extractStr += str(re) + ', ' + str(im) + '\\n'";
try {
......@@ -254,6 +255,7 @@ void RigettiAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
auto im = splitOnComma[1];
boost::trim(re);
boost::trim(im);
std::cout << "OURS: " << re << ", " << im << "\n";
std::complex<double> d(std::stod(re), std::stod(im));
qubits->getState()(i) = d;
i++;
......
......@@ -32,6 +32,9 @@ if (TARGET xacc-scaffold)
add_executable(teleport_scaffold_rigetti teleport_scaffold_rigetti.cpp)
target_link_libraries(teleport_scaffold_rigetti ${Boost_LIBRARIES} dl)
add_executable(qft_scaffold_rigetti qft_scaffold_rigetti.cpp)
target_link_libraries(qft_scaffold_rigetti ${Boost_LIBRARIES} dl)
add_executable(h2_exp_vals_scaffold_rigetti h2_exp_vals_scaffold_rigetti.cpp)
target_link_libraries(h2_exp_vals_scaffold_rigetti ${Boost_LIBRARIES} dl)
......
/***********************************************************************************
* 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 "XACC.hpp"
const std::string src("__qpu__ qft (qbit qreg) {\n"
" // Testing out new Kernel\n"
" // Replacement Preprocessor!\n"
" xacc::QFT(qreg);\n"
"}\n");
int main (int argc, char** argv) {
// Initialize the XACC Framework
xacc::Initialize(argc, argv);
// Create a reference to the Rigetti
// QPU at api.rigetti.com/qvm
auto qpu = xacc::getAccelerator("rigetti");
// Allocate a register of 3 qubits
auto qubitReg = qpu->createBuffer("qreg", 3);
// Create a Program
xacc::Program program(qpu, src);
// Request the quantum kernel representing
// the above source code
auto qft = program.getKernel("qft");
// Execute!
qft(qubitReg);
qubitReg->print(std::cout);
// Finalize the XACC Framework
xacc::Finalize();
return 0;
}
......@@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(checkTranslateIR) {
f->addInstruction(cond1);
f->addInstruction(cond2);
std::string src = compiler->translate(f);
std::string src = compiler->translate("", f);
const std::string expected(
......
......@@ -54,6 +54,7 @@ if (CLANG_FOUND AND LLVM_FOUND)
include_directories(${CLANG_INCLUDE_DIRS}/extra-tools)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/ir)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/compiler)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/utils)
include_directories(${CMAKE_SOURCE_DIR}/tpls/rapidjson/include)
include_directories(${CMAKE_SOURCE_DIR}/quantum/gate/ir/instructions)
......@@ -65,9 +66,9 @@ if (CLANG_FOUND AND LLVM_FOUND)
link_directories(${CLANG_LIBRARY_DIRS} ${LLVM_LIBRARY_DIRS})
target_link_libraries(${LIBRARY_NAME} ${CLANG_LIBS} ${LLVM_LIBS} ${Boost_LIBRARIES} xacc-gate-ir)
target_link_libraries(${LIBRARY_NAME} xacc-quantum-gate ${CLANG_LIBS} ${LLVM_LIBS} ${Boost_LIBRARIES})
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};xacc-gate-ir;${CLANG_LIBS};${LLVM_LIBS};dl;pthread")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${LIBRARY_NAME};${CLANG_LIBS};${LLVM_LIBS};dl;pthread")
endif()
\ No newline at end of file
......@@ -159,9 +159,10 @@ std::shared_ptr<IR> ScaffoldCompiler::compile(const std::string& src) {
return consumer->getIR();
}
const std::string ScaffoldCompiler::translate(std::shared_ptr<Function> function) {
const std::string ScaffoldCompiler::translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
auto visitor = std::make_shared<ScaffoldIRToSrcVisitor>("");
auto visitor = std::make_shared<ScaffoldIRToSrcVisitor>(bufferVariable);
InstructionIterator it(function);
while (it.hasNext()) {
// Get the next node in the tree
......
......@@ -75,7 +75,8 @@ public:
* @param function The XACC IR Function to translate
* @return src The source code as a string
*/
virtual const std::string translate(std::shared_ptr<Function> function);
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function);
/**
* Return the name of this Compiler
......
......@@ -131,3 +131,60 @@ BOOST_AUTO_TEST_CASE(checkIRToScaffold) {
BOOST_VERIFY(expectedStr == visitor->getScaffoldString());
}
BOOST_AUTO_TEST_CASE(checkQFT) {
auto h1 = std::make_shared<Hadamard>(2);
auto cphase1 = std::make_shared<CPhase>(1, 2, 1.5707963);
auto h2 = std::make_shared<Hadamard>(2);
auto cphase2 = std::make_shared<CPhase>(0, 2, 0.78539815);
auto cphase3 = std::make_shared<CPhase>(0, 1, 1.5707963);
auto h3 = std::make_shared<Hadamard>(0);
auto swap = std::make_shared<Swap>(0, 2);
auto expectedF = std::make_shared<GateFunction>("qft");
expectedF->addInstruction(h1);
expectedF->addInstruction(cphase1);
expectedF->addInstruction(h2);
expectedF->addInstruction(cphase2);
expectedF->addInstruction(cphase3);
expectedF->addInstruction(h3);
expectedF->addInstruction(swap);
auto visitor = std::make_shared<ScaffoldIRToSrcVisitor>("qreg");
InstructionIterator it(expectedF);
while (it.hasNext()) {
// Get the next node in the tree
auto nextInst = it.next();
if (nextInst->isEnabled())
nextInst->accept(visitor);
}
std::string expected = "H(qreg[2]);\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[2],0.785398);\n"
"CNOT(qreg[2],qreg[1]);\n"
"Rz(qreg[2],-0.785398);\n"
"CNOT(qreg[2],qreg[1]);\n"
"// END CPHASE GATE\n"
"H(qreg[2]);\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[2],0.392699);\n"
"CNOT(qreg[2],qreg[0]);\n"
"Rz(qreg[2],-0.392699);\n"
"CNOT(qreg[2],qreg[0]);\n"
"// END CPHASE GATE\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[1],0.785398);\n"
"CNOT(qreg[1],qreg[0]);\n"
"Rz(qreg[1],-0.785398);\n"
"CNOT(qreg[1],qreg[0]);\n"
"// END CPHASE GATE\n"
"H(qreg[0]);\n"
"// BEGIN SWAP 0 2\n"
"CNOT(qreg[2],qreg[0]);\n"
"CNOT(qreg[0],qreg[2]);\n"
"CNOT(qreg[2],qreg[0]);\n"
"// END SWAP 0 2\n";
BOOST_VERIFY(expected == visitor->getScaffoldString());
}
......@@ -48,7 +48,7 @@ file (GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES} xacc-gate-ir)
target_link_libraries(${LIBRARY_NAME} ${Boost_LIBRARIES} xacc-quantum-gate)
#install(FILES ${HEADERS} DESTINATION include)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
......
......@@ -29,9 +29,13 @@
#
#**********************************************************************************/
if (TARGET xacc-scaffold)
add_executable(teleport_scaffold_simpleaccelerator teleport_scaffold_simpleaccelerator.cpp)
target_link_libraries(teleport_scaffold_simpleaccelerator ${Boost_LIBRARIES} dl)
add_executable(qft_scaffold_simpleaccelerator qft_scaffold_simpleaccelerator.cpp)
target_link_libraries(qft_scaffold_simpleaccelerator ${Boost_LIBRARIES} dl)
add_executable(single_qubit_rotation single_qubit_rotation.cpp)
target_link_libraries(single_qubit_rotation ${Boost_LIBRARIES} dl)
......
/***********************************************************************************
* 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 "XACC.hpp"
const std::string src("__qpu__ qft (qbit qreg) {\n"
" // Testing out new Kernel\n"
" // Replacement Preprocessor!\n"
" xacc::QFT(qreg);\n"
"}\n");
int main (int argc, char** argv) {
// Initialize the XACC Framework
xacc::Initialize(argc, argv);
// Create a reference to the Rigetti
// QPU at api.rigetti.com/qvm
auto qpu = xacc::getAccelerator("simple");
// Allocate a register of 3 qubits
auto qubitReg = qpu->createBuffer("qreg", 3);
// Create a Program
xacc::Program program(qpu, src);
// Request the quantum kernel representing
// the above source code
auto qft = program.getKernel("qft");
// Execute!
qft(qubitReg);
qubitReg->print(std::cout);
// Finalize the XACC Framework
xacc::Finalize();
return 0;
}
......@@ -52,7 +52,8 @@ public:
"DWave");
}
virtual const std::string translate(std::shared_ptr<Function> function) {
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
};
/**
......
......@@ -28,12 +28,25 @@
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
set (PACKAGE_NAME "XACC Gate Model Quantum Computing Runtime")
set (PACKAGE_DESCIPTION "Runtime library for gate model quantum computing")
set (LIBRARY_NAME xacc-quantum-gate)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/utils)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ir)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/compiler)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ir/instructions)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ir/algorithms)
file (GLOB_RECURSE HEADERS *.hpp)
file (GLOB SRC compiler/*.cpp ir/*.cpp ir/instructions/*.cpp utils/*.cpp ir/algorithms/*.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
add_subdirectory(compiler)
add_subdirectory(ir)
add_subdirectory(utils)
install(FILES ${HEADERS} DESTINATION include/quantum/gate)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
......@@ -28,20 +28,20 @@
# Initial API and implementation - Alex McCaskey
#
#**********************************************************************************/
set (PACKAGE_NAME "Quantum XACC Gate Compiler Runtime")
set (PACKAGE_DESCIPTION "Quantum XACC Gate Compiler Utilities")
set (LIBRARY_NAME xacc-gate-compiler)
#set (PACKAGE_NAME "Quantum XACC Gate Compiler Runtime")
#set (PACKAGE_DESCIPTION "Quantum XACC Gate Compiler Utilities")
#set (LIBRARY_NAME xacc-gate-compiler)
file (GLOB HEADERS *.hpp)
file (GLOB SRC *.cpp)
#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/gate)
install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
#install(FILES ${HEADERS} DESTINATION include/quantum/gate)
#install(TARGETS ${LIBRARY_NAME} DESTINATION lib)
# Gather tests
file (GLOB test_files tests/*.cpp)
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc-gate-ir;xacc-gate-compiler")
add_tests("${test_files}" "${CMAKE_CURRENT_SOURCE_DIR}" "${Boost_LIBRARIES};xacc-quantum-gate")
#include "KernelReplacementPreprocessor.hpp"
#include <boost/algorithm/string.hpp>
#include "AlgorithmGenerator.hpp"
#include <numeric>
namespace xacc {
namespace quantum {
......@@ -9,9 +12,61 @@ const std::string KernelReplacementPreprocessor::process(const std::string& sour
std::shared_ptr<Compiler> compiler,
std::shared_ptr<Accelerator> accelerator) {
std::cout << "Running Kernel Replacement Preprocessor.\n";
return source;
XACCInfo("Running Kernel Replacement Preprocessor.\n");
// Find any occurrences of xacc::FUNCTION
// Get the first occurrence
std::string search("xacc::"), newSource = source;
auto pos = source.find(search);
std::vector<std::size_t> allPositions;
// Repeat till end is reached
while (pos != std::string::npos) {
// Add position to the vector
allPositions.push_back(pos);
// Get the next occurrence from the current position
pos = source.find(search, pos + search.size());
}
for (auto position : allPositions) {
auto funcString = source.substr(position, source.find("\n", position)-position);
boost::trim(funcString);
std::vector<std::string> split;
boost::split(split, funcString, boost::is_any_of("::"));
auto funcName = split[2].substr(0, split[2].find("("));
std::string bufName = "";
auto parenLocation = split[2].find("(")+1;
if (boost::contains(split[2], ",")) {
bufName = split[2].substr(parenLocation, split[2].find(",")-parenLocation);
} else {
bufName = split[2].substr(parenLocation, split[2].find(")")-parenLocation);
}
auto buffer = accelerator->getBuffer(bufName);
auto bufferSize = buffer->size();
std::vector<int> qubits(bufferSize);
std::iota(qubits.begin(), qubits.end(), 0);
auto algoGen = AlgorithmGeneratorRegistry::instance()->create(funcName);
auto algoKernel = algoGen->generateAlgorithm(qubits);
auto translation = compiler->translate(bufName, algoKernel);
boost::replace_all(translation, "\n", "\n ");
boost::replace_all(newSource, funcString, translation);
}
return newSource;
}
xacc::RegisterPreprocessor<xacc::quantum::KernelReplacementPreprocessor> KernelReplacement(
"kernel-replacement");
}
}
......@@ -37,22 +37,10 @@ public:
return "kernel-replacement";
}
/**
* Register this Preprocessor with the framework.
*/
static void registerPreprocessor() {
std::cout << "REGISTERING PreProcessor\n";
xacc::RegisterPreprocessor<xacc::quantum::KernelReplacementPreprocessor> KernelReplacement(
"kernel-replacement");
}
virtual ~KernelReplacementPreprocessor() {}
};
// Create an alias to search for.
RegisterPreprocessor(xacc::quantum::KernelReplacementPreprocessor)
}
}
......
......@@ -32,9 +32,161 @@
#define BOOST_TEST_MODULE KernelReplacementPPTester
#include <boost/test/included/unit_test.hpp>
#include "KernelReplacementPreprocessor.hpp"
#include "Compiler.hpp"
#include "Accelerator.hpp"
#include "GateQIR.hpp"
using namespace xacc;
using namespace xacc::quantum;
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>();
}
virtual std::shared_ptr<IR> compile(const std::string& src) {
return std::make_shared<GateQIR>();
}
virtual const std::string translate(const std::string& bufferVariable,
std::shared_ptr<Function> function) {
std::string translated = "H(qreg[2]);\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[2],0.785398);\n"
"CNOT(qreg[2],qreg[1]);\n"
"Rz(qreg[2],-0.785398);\n"
"CNOT(qreg[2],qreg[1]);\n"
"// END CPHASE GATE\n"
"H(qreg[2]);\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[2],0.392699);\n"
"CNOT(qreg[2],qreg[0]);\n"
"Rz(qreg[2],-0.392699);\n"
"CNOT(qreg[2],qreg[0]);\n"
"// END CPHASE GATE\n"
"// BEGIN CPHASE GATE\n"
"Rz(qreg[1],0.785398);\n"
"CNOT(qreg[1],qreg[0]);\n"
"Rz(qreg[1],-0.785398);\n"
"CNOT(qreg[1],qreg[0]);\n"
"// END CPHASE GATE\n"
"H(qreg[0]);\n"
"// BEGIN SWAP 0 2\n"
"CNOT(qreg[2],qreg[0]);\n"
"CNOT(qreg[0],qreg[2]);\n"
"CNOT(qreg[2],qreg[0]);\n"
"// END SWAP 0 2\n";
return translated;
}
virtual const std::string getName() {
return "Dummy";
}
};
class DummyAccelerator : public xacc::Accelerator {
public:
virtual AcceleratorType getType() {
return AcceleratorType::qpu_gate;
}
virtual std::vector<IRTransformation> getIRTransformations() {
std::vector<IRTransformation> ts;
return ts;
}
virtual void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> function) {
}
virtual std::shared_ptr<AcceleratorBuffer> createBuffer(
const std::string& varId, const int size) {
auto b = std::make_shared<AcceleratorBuffer>(varId, 3);