This GitLab instance is undergoing maintenance and is operating in read-only mode.

You are on a read-only GitLab instance.
Commit 1dfaafdf authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

fixing cmake-config for apple


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parents 17ee51c4 51187a2b
Pipeline #66124 passed with stage
in 6 minutes and 49 seconds
......@@ -63,6 +63,23 @@ macro(xacc_configure_plugin_rpath LIBNAME)
endif()
endmacro()
set(XACCLIB "libxacc.so")
set(XACCGATELIB "libxacc-quantum-gate.so")
set(XACCAQCLIB "libxacc-quantum-aqc.so")
set(XACCPAULILIB "libxacc-pauli.so")
set(XACCFERMIONLIB "libxacc-fermion.so")
set(CPPUSLIB "libCppMicroServices.so.4.0.0")
if (APPLE)
set(XACCLIB "libxacc.dylib")
set(XACCGATELIB "libxacc-quantum-gate.dylib")
set(XACCAQCLIB "libxacc-quantum-aqc.dylib")
set(XACCPAULILIB "libxacc-pauli.dylib")
set(XACCFERMIONLIB "libxacc-fermion.dylib")
set(CPPUSLIB "libCppMicroServices.4.0.0.dylib")
endif()
# Create imported target CppMicroServices::CppMicroServices
add_library(CppMicroServices::CppMicroServices SHARED IMPORTED)
......@@ -75,8 +92,8 @@ set_target_properties(CppMicroServices::CppMicroServices PROPERTIES
# Import target "CppMicroServices::CppMicroServices" for configuration "Release"
set_property(TARGET CppMicroServices::CppMicroServices APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(CppMicroServices::CppMicroServices PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libCppMicroServices.so.4.0.0"
IMPORTED_SONAME_RELEASE "libCppMicroServices.so.4.0.0"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${CPPUSLIB}"
IMPORTED_SONAME_RELEASE "${CPPUSLIB}"
)
# Create imported target xacc::xacc
......@@ -90,8 +107,8 @@ set_target_properties(xacc::xacc PROPERTIES
# Import target "xacc::xacc" for configuration "Release"
set_property(TARGET xacc::xacc APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(xacc::xacc PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libxacc.so"
IMPORTED_SONAME_RELEASE "libxacc.so"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${XACCLIB}"
IMPORTED_SONAME_RELEASE "${XACCLIB}"
)
......@@ -105,8 +122,8 @@ set_target_properties(xacc::quantum_gate PROPERTIES
# Import target "xacc::quantum_gate" for configuration "Release"
set_property(TARGET xacc::quantum_gate APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(xacc::quantum_gate PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libxacc-quantum-gate.so"
IMPORTED_SONAME_RELEASE "libxacc-quantum-gate.so"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${XACCGATELIB}"
IMPORTED_SONAME_RELEASE "${XACCGATELIB}"
)
# Create imported target xacc::quantum_aqc
......@@ -120,8 +137,8 @@ set_target_properties(xacc::quantum_aqc PROPERTIES
# Import target "xacc::quantum_aqc" for configuration "Release"
set_property(TARGET xacc::quantum_aqc APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(xacc::quantum_aqc PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libxacc-quantum-aqc.so"
IMPORTED_SONAME_RELEASE "libxacc-quantum-aqc.so"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${XACCAQCLIB}"
IMPORTED_SONAME_RELEASE "${XACCAQCLIB}"
)
......@@ -136,8 +153,8 @@ set_target_properties(xacc::pauli PROPERTIES
# Import target "xacc::pauli" for configuration "Release"
set_property(TARGET xacc::pauli APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(xacc::pauli PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libxacc-pauli.so"
IMPORTED_SONAME_RELEASE "libxacc-pauli.so"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${XACCPAULILIB}"
IMPORTED_SONAME_RELEASE "${XACCPAULILIB}"
)
......@@ -152,8 +169,8 @@ set_target_properties(xacc::fermion PROPERTIES
# Import target "xacc::fermion" for configuration "Release"
set_property(TARGET xacc::fermion APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(xacc::fermion PROPERTIES
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/libxacc-fermion.so"
IMPORTED_SONAME_RELEASE "libxacc-fermion.so"
IMPORTED_LOCATION_RELEASE "${XACC_ROOT}/lib/${XACCFERMIONLIB}"
IMPORTED_SONAME_RELEASE "${XACCFERMIONLIB}"
)
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
......
......@@ -3,4 +3,7 @@ add_subdirectory(rigetti)
add_subdirectory(cmr)
add_subdirectory(dwave)
add_subdirectory(algorithms)
add_subdirectory(decorators)
\ No newline at end of file
add_subdirectory(decorators)
#add_subdirectory(scaffold)
#add_subdirectory(xasm)
option(SCAFFOLD_PATH "Path of scaffold install" "scaffcc")
if (SCAFFOLD_PATH)
add_subdirectory(compiler)
#add_subdirectory(pragmas)
endif()
\ No newline at end of file
# XACC-Scaffold
XACC-Scaffold is a XACC and QCOR plugin meant to compile and run scaffold
quantum code on a quantum processor while working in a classical environment.
## Dependencies
Compiler (C++11): GNU 5+, Clang 8+
CMake 3.9+
XACC: see https://xacc.readthedocs.io/en/latest/install.html#building-xacc
QCOR: see https://github.com/ORNL-QCI/qcor/
SCAFFOLD: see https://github.com/epiqc/ScaffCC
##Build instructions
For CMake 3.9+, do not use the apt-get installer, instead use `pip`,
and ensure that `/usr/local/bin` is in your PATH:
```bash
$ python -m pip install --upgrade cmake
$ export PATH=$PATH:/usr/local/bin
```
On Ubuntu 16+, install latest clang and llvm libraries and headers (you may need sudo)
```bash
$ wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -
$ echo "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main" > /etc/apt/sources.list.d/llvm.list
$ apt-get update
$ apt-get install -y libclang-9-dev llvm-9-dev
$ ln -s /usr/bin/llvm-config-9 /usr/bin/llvm-config
```
Note that, for now, developers must clone XACC-Scaffold manually:
``` bash
$ git clone https://code.ornl.gov/qci/xacc-scaffold
$ cd xacc-scaffold
$ mkdir build && cd build
$ cmake .. -DXACC_DIR=~/.xacc (or wherever you installed XACC) -DSCAFFOLD_PATH = ~/home/project/scaffcc (Or wherever you installed scaffold)
$ make install
```
Update your PATH to ensure that the ```qcor``` compiler is available.
```bash
$ export PATH=$PATH:$HOME/.xacc/bin
```
## Example Usage
Here we demonstrate how to program, compile and run basic quantum scaffold code to make a bell state.
Create the following file called bell_state.cpp
```cpp
#include qcor
int main(int argc, char** argv) {
qcor::Initialize(argc, argv);
auto buffer = std::make_shared<AcceleratorBuffer>(2);
#pragma qcor scaffold(buffer, 2)
{
cbit out[2];
H(buffer[0]);
CNOT(buffer[0], buffer[1]);
out[0] = MeasZ(buffer[0]);
out[1] = MeasZ(buffer[1]);
}
buffer->print();
}
```
Top compile this with QCOR, run the following
```bash
$ qcor bell_state.cpp
```
Here we demonstrate how to program, compile and run quantum scaffold code with functions and modules. Create the following file
called scaff_function_example.cpp
```cpp
#include "qcor.hpp"
void biina (std::shared_ptr<xacc::AcceleratorBuffer> b) {
#pragma qcor scaffold(b, 2)
{
module hi (qbit* b1) {
H(b1[0]);
}
module hello(qbit* b1) {
int i = 0;
for (i = 0; i < 2; i++) {
hi(b1);
}
}
cbit out[2];
hello(b);
CNOT(b[0], b[1]);
out[0] = MeasZ(b[0]);
out[1] = MeasZ(b[1]);
}
b -> print();
}
int main(int argc, char** argv) {
qcor::Initialize(argc, argv);
auto buffer = std::make_shared<AcceleratorBuffer>(2);
biina(buffer);
buffer->print();
}
```
and run
```bash
$ qcor scaff_function_example.cpp
```
Here we demonstrate how to program, compile, and run a VQE problem with scaffold. Create the following file called vqe_example.cpp
```cpp
#include "qcor.hpp"
int main(int argc, char** argv) {
qcor::Initialize(argc, argv);
// Create a buffer of qubits
auto buffer = xacc::qalloc(2);
// allocate the required vqe parameters
// initialized to 0.0
std::vector<double> params(1);
// Create an Optimizer
auto optimizer = qcor::getOptimizer(
"nlopt", {{"nlopt-optimizer", "cobyla"},
{"nlopt-maxeval", 20}});
// Create the Deuteron N=2 Hamiltonian
// This will dictate measurements on our ansatz
auto op = qcor::getObservable("pauli", "5.907 - 2.1433 X0X1 "
"- 2.1433 Y0Y1"
"+ .21829 Z0 - 6.125 Z1");
// Create the ansatz with Scaffold code
// Annotate to indicate we want to use this ansatz
// for VQE, providing the qreg and its size, and
// VQE-pertinent information
#pragma qcor scaffold(buffer, 2) vqe(params, op, optimizer)
{
X(buffer[0]);
Ry(buffer[1], params[0]);
CNOT(buffer[1], buffer[0]);
}
// Print the results
std::cout << "Optimal Energy = " << mpark::get<double>(buffer->getInformation("opt-val")) << "\n";
}
```
To compile this with QCOR, run the following
```bash
$ qcor vqe_example.cpp
```
# PLUGIN STUFF HERE
set(LIBRARY_NAME xacc-scaffold)
configure_file(ScaffCompiler.in.cpp ${CMAKE_BINARY_DIR}/compiler/ScaffCompiler.cpp)
set(SRC SCAFFCompilerActivator.cpp ${CMAKE_BINARY_DIR}/compiler/ScaffCompiler.cpp)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME} PUBLIC .)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc)
set(_bundle_name xacc_scaffold)
set_target_properties(${LIBRARY_NAME}
PROPERTIES COMPILE_DEFINITIONS
US_BUNDLE_NAME=${_bundle_name}
US_BUNDLE_NAME
${_bundle_name})
usfunctionembedresources(TARGET
${LIBRARY_NAME}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
FILES
manifest.json)
if(APPLE)
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "@loader_path/../lib")
set_target_properties(${LIBRARY_NAME}
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
if (XACC_BUILD_TESTS)
add_subdirectory(tests)
endif()
#include "ScaffCompiler.hpp"
#include "cppmicroservices/BundleActivator.h"
#include "cppmicroservices/BundleContext.h"
#include <memory>
#include <set>
using namespace cppmicroservices;
namespace {
/**
*/
class US_ABI_LOCAL SCAFFActivator : public BundleActivator {
public:
SCAFFActivator() {}
/**
*/
void Start(BundleContext context) {
auto s = std::make_shared<scaffold::ScaffCompiler>();
context.RegisterService<xacc::Compiler>(s);
}
/**
*/
void Stop(BundleContext /*context*/) {}
};
} // namespace
CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(SCAFFActivator)
/***********************************************************************************
*
* Contributors:
* Initial API and implementation - Alex McCaskey, Amrut Nadgir
*
**********************************************************************************/
#ifndef COMPILER_SCAFFCOMPILER_HPP_
#define COMPILER_SCAFFCOMPILER_HPP_
#include "XACC.hpp"
using namespace xacc;
namespace scaffold {
/**
* The PyXACCCompiler is an XACC Compiler that compiles
* python-like gate instruction source code to produce a
* XACC IR.
*/
class ScaffCompiler : public xacc::Compiler {
public:
/**
* The Compiler.
*/
ScaffCompiler() {}
/*
* Erase First Occurrence of given substring from main string.
*/
std::string eraseSubStr(std::string mainStr, const std::string toErase) {
// Search for the substring in string
size_t pos = mainStr.find(toErase);
if (pos != std::string::npos) {
// If found then erase it from string
mainStr.erase(pos, toErase.length());
}
return mainStr;
}
/**
* Helper function for the compile method.
* Takes in source string and writes to a scaffold file.
* @param src The source code
* @return
*/
virtual std::string const writeScaff(const std::string &src);
/**
* Helper function for the compile method.
* Reads in a qasmf file to string
* @param
* @return
*/
virtual std::string readQasm(std::string filename);
/**
* Helper function for the compile method.
* Reads in a qasmf string created by the scaffold compiler
* and converts it to an IR representation
* @param qasmf_string The qasmf assembly code
* @return shared pointer to an IR
*/
virtual std::shared_ptr<xacc::IR> qasm_to_IR(std::string qasm_string);
/**
* Compile the given kernel code for the
* given Accelerator.
*
* @param src The source code
* @param acc Reference to an Accelerator
* @return
*/
virtual std::shared_ptr<xacc::IR>
compile(const std::string &src, std::shared_ptr<Accelerator> acc) override;
/**
* Compile the given kernel code.
*
* @param src The source code
* @return
*/
virtual std::shared_ptr<xacc::IR> compile(const std::string &src) override;
/**
* No translations yet
* @param bufferVariable
* @param function
* @return
*/
virtual const std::string
translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) override;
virtual const std::string name() const { return "scaffold"; }
virtual const std::string description() const {
return "Compiler class for scaffold code";
}
/**
* The destructor
*/
virtual ~ScaffCompiler() {}
};
} // namespace scaffold
#endif
/***********************************************************************************
*
* Contributors:
* Initial API and implementation - Amrut Nadgir, Alex McCaskey
*
**********************************************************************************/
#include "ScaffCompiler.hpp"
#include "IRProvider.hpp"
#include "xacc_service.hpp"
#include <fstream>
#include <iostream>
#include <vector>
namespace scaffold {
//Writes scaffold code string into a .scaffold fild to be compiled by
//scaffold.sh
std::string const ScaffCompiler::writeScaff(const std::string &src) {
std::ofstream scaff_src;
scaff_src.open("src.scaffold", std::ios::trunc);
scaff_src << src;
scaff_src.close();
return "src";
}
//Reads the qasm file created by scaffold.sh and returns it as a string
std::string ScaffCompiler::readQasm(std::string filename) {
std::string line;
std::string qasm_string;
std::ifstream scaff_qasm(filename);
if (scaff_qasm.is_open()) {
while (getline(scaff_qasm, line)) {
qasm_string += line + "\n";
}
} else {
xacc::error("Unable to open qasm file.");
}
scaff_qasm.close();
return qasm_string;
}
//Converts the qasm string to IR by using the build in xacc openqasm compile method.
//If vqe is found, the qasm string has to be edited before it can be compiled
std::shared_ptr<xacc::IR> ScaffCompiler::qasm_to_IR(std::string qasm_string) {
auto compiler = xacc::getCompiler("openqasm");
qasm_string = "__qpu__ f(AcceleratorBuffer b) {\n" + qasm_string + "\n}";
//If the vqe algorithm is being run, use the global map variable from
//ScaffoldPragmaHandler.hpp to convert all the stand in numbers
//back into their correct variable names.
if (xacc::optionExists("foundVQE")) {
auto keyValue = xacc::getOption("__scaffold_handle_vars__");
std::vector<std::string> split;
split = xacc::split(keyValue, ',');
for (auto pairString : split) {
auto pair = xacc::split(pairString, ':');
std::string string_to_replace = pair[1];
std::string replacement = pair[0];
std::size_t loc = 0;
while(loc < qasm_string.size()) {
loc = qasm_string.find(string_to_replace, loc);
std::size_t origLoc = loc;
if (loc == std::string::npos) {
break;
}
qasm_string.replace(loc, string_to_replace.size(), replacement);
}
}
}
auto IR = compiler->compile(qasm_string);
std::cout << "IR:\n" << IR->getKernels()[0]->toString() << "\n";
return IR;
}
//Uses the helper functions above to take the source string,
//write it to a .scaffold file, convert it to openqasm
//using scaffold.sh, and then converting the openqasm to IR.
std::shared_ptr<IR> ScaffCompiler::compile(const std::string &src,
std::shared_ptr<Accelerator> acc) {
std::string filename;
filename = writeScaff(src);
std::string command;
command = "@SCAFFOLD_PATH@/scaffold.sh -b " + filename + ".scaffold";
std::system(command.c_str());
std::string qasm;
qasm = readQasm(filename + ".qasm");
auto ir = qasm_to_IR(qasm);
return ir;
}
//Compile methof without a buffer
std::shared_ptr<IR> ScaffCompiler::compile(const std::string &src) {
return compile(src, nullptr);
}
const std::string ScaffCompiler::translate(const std::string &bufferVariable,
std::shared_ptr<Function> function) {
xacc::error("ScaffCompiler::translate() not implemented.");
return "";
}
} // namespace scaffold
{
"bundle.symbolic_name" : "xacc_scaffold_compiler",
"bundle.activator" : true,
"bundle.name" : "XACC-SCAFFOLD Compiler",
"bundle.description" : "Converts scaffold code into an xacc IR"
}
include_directories(${CMAKE_SOURCE_DIR}/compiler)
add_xacc_test(ScaffoldCompiler)
target_link_libraries(ScaffoldCompilerTester xacc-scaffold)
\ No newline at end of file
#include "ScaffCompiler.hpp"
#include "gtest/gtest.h"
using namespace scaffold;
TEST(ScaffoldCompilerTester, checkSimple) {
auto src = R"src(int main() {
qbit reg[2];
cbit out[2];
H(reg[0]);
CNOT(reg[0], reg[1]);
out[0] = MeasZ(reg[0]);
out[1] = MeasZ(reg[1]);
return 0;
})src";
ScaffCompiler compiler;
auto IR = compiler.compile(src);
EXPECT_EQ(1, IR->getKernels().size());
std::cout << "KERNEL\n" << IR->getKernels()[0]->toString() << "\n";
}
int main(int argc, char **argv) {