From 4925b9945f9138c6fcb30fdc2ac7e3057c9cc404 Mon Sep 17 00:00:00 2001 From: Alex McCaskey <mccaskeyaj@ornl.gov> Date: Wed, 25 Jan 2017 14:02:07 +0000 Subject: [PATCH] Adding first example code for XACC API usage - teleportation --- CMakeLists.txt | 32 ++++++++ examples/CMakeLists.txt | 1 + examples/quantum/CMakeLists.txt | 1 + examples/quantum/gate/CMakeLists.txt | 33 ++++++++ examples/quantum/gate/teleport_scaffold.cpp | 81 +++++++++++++++++++ quantum/gate/scaffold/CMakeLists.txt | 31 +++++++ .../scaffold/tests/ScaffoldCompilerTester.cpp | 2 +- quantum/gate/utils/QasmToGraph.hpp | 6 +- xacc/accelerator/Accelerator.hpp | 3 +- xacc/compiler/Compiler.hpp | 11 +-- xacc/compiler/GraphIR.hpp | 2 +- xacc/compiler/IR.hpp | 5 +- xacc/program/Program.hpp | 51 ++++++++++-- xacc/tests/ProgramTester.cpp | 2 +- 14 files changed, 237 insertions(+), 24 deletions(-) create mode 100644 examples/CMakeLists.txt create mode 100644 examples/quantum/CMakeLists.txt create mode 100644 examples/quantum/gate/CMakeLists.txt create mode 100644 examples/quantum/gate/teleport_scaffold.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e5935ea0f..e8752db3f 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 000000000..daa9a6f79 --- /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 000000000..00128b095 --- /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 000000000..1ec8a7aea --- /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 000000000..ac496ed63 --- /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 87834dd69..180838dbc 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 4d26c2d1e..dfca52959 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 b93129caf..d6bbb05e2 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 cbde2bc35..85eebc3d8 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 39eecae39..fb11f29ef 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 6ce9c1a0d..1c9bea42b 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 013343dc5..d4b102b4a 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 2a48948d3..0110b26ec 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 3abf8d923..55c28ee62 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. * -- GitLab