diff --git a/CMakeLists.txt b/CMakeLists.txt index e5935ea0f5695186d1fce6d94e397751d364af58..e8752db3f69e323d9cb07186157b4a56e6d04986 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,33 @@ +/*********************************************************************************** + * 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 + * + **********************************************************************************/ project(XACC CXX) cmake_minimum_required(VERSION 2.8) set(CMAKE_DISABLE_IN_SOURCE_BUILDS ON) @@ -37,6 +67,7 @@ include_directories("${CMAKE_SOURCE_DIR}/xacc") 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") @@ -44,3 +75,4 @@ include_directories("${CMAKE_SOURCE_DIR}/tpls/common/exception") add_subdirectory(tpls) add_subdirectory(xacc) add_subdirectory(quantum) +add_subdirectory(examples) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..daa9a6f799b5d361b6814eae058d6ef8c3a6a040 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(quantum) diff --git a/examples/quantum/CMakeLists.txt b/examples/quantum/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..00128b095b6a273f23a659c64fe537a91253071d --- /dev/null +++ b/examples/quantum/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(gate) diff --git a/examples/quantum/gate/CMakeLists.txt b/examples/quantum/gate/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..1ec8a7aea0b5d3bc188d1abad9e04fd78f018bc6 --- /dev/null +++ b/examples/quantum/gate/CMakeLists.txt @@ -0,0 +1,33 @@ +/*********************************************************************************** + * 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 + * + **********************************************************************************/ + +add_executable(teleport_scaffold teleport_scaffold.cpp) +target_link_libraries(teleport_scaffold xacc-scaffold ${Boost_LIBRARIES}) \ No newline at end of file diff --git a/examples/quantum/gate/teleport_scaffold.cpp b/examples/quantum/gate/teleport_scaffold.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac496ed6327bdd1278e8636ff3e6b9a49d676ef1 --- /dev/null +++ b/examples/quantum/gate/teleport_scaffold.cpp @@ -0,0 +1,81 @@ +/*********************************************************************************** + * 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 "Program.hpp" + +/** + * FIXME For now, create a fake accelerator + * This will later come from QVM + */ +class IBM5Qubit: public xacc::Accelerator { +public: + virtual AcceleratorType getType() { + return AcceleratorType::qpu_gate; + } + virtual std::vector<xacc::IRTransformation> getIRTransformations() { + std::vector<xacc::IRTransformation> v; + return v; + } + virtual ~IBM5Qubit() { + } +}; + +// Quantum Kernel executing teleportation of +// qubit state to another. +const std::string src("__qpu__ teleport () {\n" + " qbit q[3];\n" + " cbit c[2];\n" + " H(q[1]);\n" + " CNOT(q[1],q[2]);\n" + " CNOT(q[0],q[1]);\n" + " H(q[0]);\n" + " MeasZ(q[0]);\n" + " MeasZ(q[1]);\n" + " // cZ\n" + " H(q[2]);\n" + " CNOT(q[2], q[1]);\n" + " H(q[2]);\n" + " // cX = CNOT\n" + " CNOT(q[2], q[0]);\n" + "}\n"); + +int main (int argc, char** argv) { + + auto ibm_qpu = std::make_shared<IBM5Qubit>(); + xacc::Program quantumProgram(ibm_qpu, src); + quantumProgram.build("--compiler scaffold --writeIR teleport.xaccir"); + + // FIXME Get Kernel, execute, get result + + return 0; +} + + + diff --git a/quantum/gate/scaffold/CMakeLists.txt b/quantum/gate/scaffold/CMakeLists.txt index 87834dd69a7e7fc97020bd1ce6c080a2e3132869..180838dbcbada39046c193499ee99ba792ddf818 100644 --- a/quantum/gate/scaffold/CMakeLists.txt +++ b/quantum/gate/scaffold/CMakeLists.txt @@ -1,3 +1,34 @@ +/*********************************************************************************** + * 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 + * + **********************************************************************************/ + set (PACKAGE_NAME "Scaffold XACC Runtime") set (PACKAGE_DESCIPTION "Scaffold XACC Programming Framework") set (LIBRARY_NAME xacc-scaffold) diff --git a/quantum/gate/scaffold/tests/ScaffoldCompilerTester.cpp b/quantum/gate/scaffold/tests/ScaffoldCompilerTester.cpp index 4d26c2d1e86a3ef44881fd7be00a7417b122a2be..dfca52959dc9e4928e6b579369c5f12800fb69db 100644 --- a/quantum/gate/scaffold/tests/ScaffoldCompilerTester.cpp +++ b/quantum/gate/scaffold/tests/ScaffoldCompilerTester.cpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * diff --git a/quantum/gate/utils/QasmToGraph.hpp b/quantum/gate/utils/QasmToGraph.hpp index b93129caf99918d0a41fd16272153afaff1557ef..d6bbb05e21cf3454e6984a8bbb85941403e4f9aa 100644 --- a/quantum/gate/utils/QasmToGraph.hpp +++ b/quantum/gate/utils/QasmToGraph.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * @@ -46,7 +46,9 @@ using boost::assign::map_list_of; * Enumeration of gates we support */ enum SupportedGates { - H, CNot, C_Z, C_X, Measure, X, Y, Z, T, S, ZZ, SS, Swap, Toffoli, InitialState, FinalState + H, CNot, C_Z, C_X, Measure, X, Y, Z, + T, S, ZZ, SS, Swap, Toffoli, InitialState, + FinalState }; /** diff --git a/xacc/accelerator/Accelerator.hpp b/xacc/accelerator/Accelerator.hpp index cbde2bc350faeb3db689689e0282b569a736d1ec..85eebc3d84c9a5cce6b8a2a3f6f51843382712b6 100644 --- a/xacc/accelerator/Accelerator.hpp +++ b/xacc/accelerator/Accelerator.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * @@ -50,6 +50,7 @@ public: } + virtual ~Accelerator() {} }; } #endif diff --git a/xacc/compiler/Compiler.hpp b/xacc/compiler/Compiler.hpp index 39eecae39e4a93e9f403349421b01c5784cd1592..fb11f29efbeb5d5188a4dd871568749e60f27a47 100644 --- a/xacc/compiler/Compiler.hpp +++ b/xacc/compiler/Compiler.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * @@ -41,14 +41,6 @@ using namespace qci::common; namespace xacc { -template<typename T> -class Type { - -public: - void * allocate() { return new T; } - T * cast(void * obj) { return static_cast<T*>(obj); } -}; - /** * The ICompiler provides an interface for clients to realize to * provide a mechanism for compiling a provided source string to @@ -101,7 +93,6 @@ public: // amenable to their specific compilation. modifySource(); - // Unpack args into Types and values return getAsDerived().compile(); } diff --git a/xacc/compiler/GraphIR.hpp b/xacc/compiler/GraphIR.hpp index 6ce9c1a0dfbac554edaa0ee33e088413d4692c5b..1c9bea42bacb45c81cbe1e90a05e56960ff7db16 100644 --- a/xacc/compiler/GraphIR.hpp +++ b/xacc/compiler/GraphIR.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * diff --git a/xacc/compiler/IR.hpp b/xacc/compiler/IR.hpp index 013343dc516ffbed072f963a7ba83d6d03ff368d..d4b102b4af13053c6d790faea6f78ec66a2e2f9d 100644 --- a/xacc/compiler/IR.hpp +++ b/xacc/compiler/IR.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * @@ -32,7 +32,7 @@ #define XACC_COMPILER_IR_HPP_ #include "AbstractFactory.hpp" -#include <ostream> +#include <iostream> namespace xacc { @@ -43,6 +43,7 @@ class IR { public: virtual std::string toString() = 0; virtual void persist(std::ostream& outStream) = 0; + virtual void read(std::istream& inStream) = 0; virtual ~IR() {} }; diff --git a/xacc/program/Program.hpp b/xacc/program/Program.hpp index 2a48948d38ab95380081f4ac906390e6dda2cc92..0110b26ec11af1a3b5dc9c14d275bfb2ebbc7850 100644 --- a/xacc/program/Program.hpp +++ b/xacc/program/Program.hpp @@ -9,7 +9,7 @@ * * 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 + * * 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. * @@ -43,6 +43,7 @@ #include "QCIError.hpp" #include "XaccUtils.hpp" #include "XACC.hpp" +#include <fstream> using namespace boost::program_options; using namespace qci::common; @@ -50,20 +51,44 @@ using namespace qci::common; namespace xacc { /** - * + * The Program is the main entrypoint for the XACC + * API. Users with accelerator kernels must construct a + * valid Program to be compiled and executed on the + * attached accelerator. Programs must be given the + * Accelerator reference to be used and kernel source + * code at construction time. */ class Program { protected: + /** + * Reference to the source accelerator + * kernel code to be compiled and executed + */ std::string src; + /** + * Reference to the attached Accelerator to + * use in this compilation and execution + */ std::shared_ptr<Accelerator> accelerator; + /** + * Reference to a set of compiler command + * line options. + */ std::shared_ptr<options_description> compilerOptions; public: + /** + * The Constructor, takes the Accelerator + * to execute on, and the source to compile and execute + * + * @param acc Attached Accelerator to execute + * @param sourceFile The kernel source code + */ Program(std::shared_ptr<Accelerator> acc, const std::string& sourceFile) : src(sourceFile) { accelerator = std::move(acc); @@ -71,9 +96,18 @@ public: "XACC Compiler Options"); compilerOptions->add_options()("help", "Help Message")("compiler", value<std::string>()->default_value("scaffold"), - "Indicate the compiler to be used."); + "Indicate the compiler to be used.") + ("writeIR", value<std::string>(), "Persist generated IR to provided file name."); } + /** + * Execute the compilation mechanism on the provided program + * source kernel code to produce XACC IR that can be executed + * on the attached Accelerator. + * + * @param compilerArgStr Arguments for the compiler's execution + * @param runtimeArgs Runtime values for kernel arguments + */ template<typename ... RuntimeArgs> void build(const std::string& compilerArgStr, RuntimeArgs ... runtimeArgs) { @@ -104,21 +138,26 @@ public: QCIError("Bad source string or something.\n"); } + // Write the IR to file if the user requests it + if (compileParameters.count("writeIR")) { + auto fileStr = compileParameters["writeIR"].as<std::string>(); + std::ofstream ostr(fileStr); + ir->persist(ostr); + } + // Execute IR Translations and Optimizations // FIXME GET LIST OF TRANSFORMATION FROM auto acceleratorType = accelerator->getType(); auto defaultTransforms = getAcceleratorIndependentTransformations(acceleratorType); auto accDepTransforms = accelerator->getIRTransformations(); - for (IRTransformation& t : defaultTransforms) { t.transform(*ir.get()); } - for (IRTransformation& t : accDepTransforms) { t.transform(*ir.get()); } - // Create Kernel from IR + // FIXME Create Kernel from IR return; } diff --git a/xacc/tests/ProgramTester.cpp b/xacc/tests/ProgramTester.cpp index 3abf8d923568f014897c308a7d59023fc77ef23d..55c28ee6218ca52ca905e747aa48d957962046ca 100644 --- a/xacc/tests/ProgramTester.cpp +++ b/xacc/tests/ProgramTester.cpp @@ -9,7 +9,7 @@ * * 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 + * * 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. *