Skip to content
Snippets Groups Projects
Commit 9453da29 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

Adding Fake classes, implementing kernel arg compiler test

parent 5d3f1f65
No related branches found
No related tags found
No related merge requests found
......@@ -68,6 +68,13 @@ public:
return "qbit";
}
/**
* The destructor
*/
virtual ~ScaffoldCompiler() {}
protected:
/**
* This method is intended to modify the incoming
* source code to be compiled to be amenable to the
......@@ -76,16 +83,13 @@ public:
virtual void modifySource();
/**
* The destructor
* Reference to potential conditional code
*/
virtual ~ScaffoldCompiler() {}
protected:
std::vector<std::string> conditionalCodeSegments;
/**
* Reference to potential conditional code
*
*/
std::vector<std::string> conditionalCodeSegments;
std::vector<int> conditionalCodeSegmentActingQubits;
};
......
......@@ -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}/compiler;${CMAKE_CURRENT_SOURCE_DIR}/program;${CMAKE_CURRENT_SOURCE_DIR}/accelerator" "${Boost_LIBRARIES}")
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" "${Boost_LIBRARIES}")
install(FILES ${HEADERS} DESTINATION include)
......
......@@ -121,6 +121,7 @@ public:
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];
......@@ -156,6 +157,11 @@ public:
return getAsDerived().compile();
}
/**
*
* @param src
* @return
*/
virtual std::shared_ptr<IR> compile(const std::string& src) {
kernelSource = src;
// Xacc requires that clients provide
......@@ -185,20 +191,15 @@ protected:
*/
std::string kernelSource;
/**
*
*/
std::map<std::string, std::string> typeToVarKernelArgs;
std::shared_ptr<IAccelerator> accelerator;
/**
*
* Derived types implementing compile should perform language
* specific compilation and return a shared pointer to an
* intermediate representation IR instance.
*
* @return
*/
virtual std::shared_ptr<IR> compile() {
QCIError("Compile must be overridden by derived types.\n");
}
std::shared_ptr<IAccelerator> accelerator;
/**
*
......
......@@ -29,60 +29,35 @@
*
**********************************************************************************/
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE ProgramTester
#define BOOST_TEST_MODULE CompilerTester
#include <boost/test/included/unit_test.hpp>
#include "Compiler.hpp"
#include "FakeCompiler.hpp"
#include "FakeAccelerator.hpp"
using namespace xacc;
class FakeIR: public IR {
public:
FakeIR() {
}
virtual std::string toString() { return std::string();}
virtual void persist(std::ostream& stream) {}
virtual void read(std::istream& inStream) {}
};
class FakeCompiler: public Compiler<FakeCompiler> {
friend Compiler<FakeCompiler>;
BOOST_AUTO_TEST_CASE(checkKernelArgs) {
protected:
virtual std::shared_ptr<IR> compile() {
return std::make_shared<FakeIR>();
}
auto acc = std::make_shared<FakeAccelerator>();
auto qreg = acc->createBuffer("qreg", 3);
virtual std::string getBitType() {
return "qbit";
}
virtual void modifySource() {
const std::string src("__qpu__ void function(qbit qreg, double phi) {\n"
"}\n");
}
auto compiler = std::shared_ptr<ICompiler>(new FakeCompiler());
public:
auto a = std::dynamic_pointer_cast<IAccelerator>(acc);
compiler->compile(src, a);
virtual ~FakeCompiler() {
}
};
auto asFake = std::dynamic_pointer_cast<FakeCompiler>(compiler);
BOOST_AUTO_TEST_CASE(checkCompile) {
auto argMap = asFake->getTypeToVarArgs();
const std::string src("__qpu__ void teleport() {"
" qbit qs[3];"
" H(q[1]);"
" CNot(q[1],q[2]);"
" CNot(q[0], q[1]);"
" H(q[1]);"
" if(q[1].measure()) {"
" X(q[2]);"
" }"
" if(q[1].measure()) {"
" Z(q[2]);"
" }"
"}");
BOOST_VERIFY(argMap.find("double") != argMap.end());
BOOST_VERIFY(argMap.find("qbit") != argMap.end());
BOOST_VERIFY(argMap["qbit"] == "qreg[3]");
BOOST_VERIFY(argMap["double"] == "phi");
auto compiler = std::shared_ptr<ICompiler>(new FakeCompiler());
// auto ir = compiler->compile(src);
// BOOST_VERIFY(ir);
}
/***********************************************************************************
* 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 <organization> 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 XACC_TESTS_FAKEACCELERATOR_HPP_
#define XACC_TESTS_FAKEACCELERATOR_HPP_
#include "Accelerator.hpp"
using namespace xacc;
class FakeAccelerator: virtual public Accelerator<AcceleratorBuffer> {
public:
virtual AcceleratorType getType() {
return qpu_gate;
}
virtual std::vector<IRTransformation> getIRTransformations() {
std::vector<IRTransformation> v;
return v;
}
virtual void execute(const std::string& bufferId,
const std::shared_ptr<IR> ir) {
}
virtual ~FakeAccelerator() {
}
};
#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 <organization> 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 XACC_TESTS_FAKECOMPILER_HPP_
#define XACC_TESTS_FAKECOMPILER_HPP_
#include "Compiler.hpp"
#include "FakeIR.hpp"
using namespace xacc;
class FakeCompiler: public Compiler<FakeCompiler> {
public:
virtual std::shared_ptr<IR> compile() {
return std::make_shared<FakeIR>();
}
virtual void modifySource() {
}
std::map<std::string, std::string> getTypeToVarArgs() {
return typeToVarKernelArgs;
}
virtual std::string getBitType() {
return "qbit";
}
virtual ~FakeCompiler() {
}
};
#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 <organization> 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 XACC_TESTS_FAKEIR_HPP_
#define XACC_TESTS_FAKEIR_HPP_
#include "IR.hpp"
using namespace xacc;
class FakeIR: public IR {
public:
FakeIR() {
}
virtual std::string toString() { return std::string();}
virtual void persist(std::ostream& stream) {}
virtual void read(std::istream& inStream) {}
};
#endif
......@@ -33,30 +33,11 @@
#include <boost/test/included/unit_test.hpp>
#include "Program.hpp"
#include "FakeIR.hpp"
#include "FakeAccelerator.hpp"
using namespace xacc;
class FakeAccelerator : public Accelerator<AcceleratorBuffer> {
public:
virtual AcceleratorType getType() { return qpu_gate; }
virtual std::vector<IRTransformation> getIRTransformations() {std::vector<IRTransformation> v; return v;}
virtual void execute(const std::string& bufferId, const std::shared_ptr<IR> ir) {}
virtual ~FakeAccelerator() {}
};
class FakeIR: public IR {
public:
FakeIR() {
}
virtual std::string toString() { return std::string();}
virtual void persist(std::ostream& stream) {}
virtual void read(std::istream& inStream) {}
};
class DummyCompiler : public Compiler<DummyCompiler> {
public:
virtual std::shared_ptr<IR> compile() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment