Skip to content

Commit

Permalink
Fixing OpenMP config settings
Browse files Browse the repository at this point in the history
Increase shots to fix failing test
cleaning up some simulator variable names
  • Loading branch information
chriseclectic committed Apr 13, 2018
1 parent 4b27671 commit 93b3092
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 95 deletions.
14 changes: 7 additions & 7 deletions cmake/python-build.cmake
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ function(add_pypi_package_target TARGET_NAME PACKAGE_TYPE)
endif()

if(PIP_PACKAGE_PLATFORM_WHEELS)
set(COPY_QASM_SIM_TARGET ${TARGET_NAME}_copy_qasm_simulator)
set(COPY_QASM_SIM_CPP_TARGET ${TARGET_NAME}_copy_qasm_simulator_cpp)
# We create a target which will depend on TARGET_NAME_WHEELS for
# copying all the binaries to their final locations
add_custom_target(${COPY_QASM_SIM_TARGET})
add_custom_command(TARGET ${COPY_QASM_SIM_TARGET}
add_custom_target(${COPY_QASM_SIM_CPP_TARGET})
add_custom_command(TARGET ${COPY_QASM_SIM_CPP_TARGET}
COMMAND ${CMAKE_COMMAND} -E copy
${QASM_SIMULATOR_OUTPUT_DIR}/qasm_simulator_cpp${EXECUTABLE_FILE_EXTENSION}
${QASM_SIMULATOR_CPP_OUTPUT_DIR}/qasm_simulator_cpp${EXECUTABLE_FILE_EXTENSION}
${CMAKE_CURRENT_SOURCE_DIR}/qiskit/backends)
# For ' make clean' target
set_property(DIRECTORY APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES
${CMAKE_CURRENT_SOURCE_DIR}/qiskit/backends/qasm_simulator_cpp${EXECUTABLE_FILE_EXTENSION})
# For Windows, we need to copy external .dll dependencies too
if(MINGW)
foreach(dll_file ${QASM_SIMULATOR_THIRD_PARTY_DLLS})
add_custom_command(TARGET ${COPY_QASM_SIM_TARGET}
foreach(dll_file ${QASM_SIMULATOR_CPP_THIRD_PARTY_DLLS})
add_custom_command(TARGET ${COPY_QASM_SIM_CPP_TARGET}
COMMAND ${CMAKE_COMMAND} -E copy
${dll_file}
${CMAKE_CURRENT_SOURCE_DIR}/qiskit/backends)
Expand Down Expand Up @@ -97,7 +97,7 @@ function(add_pypi_package_target TARGET_NAME PACKAGE_TYPE)
if(PIP_PACKAGE_PLATFORM_WHEELS)
add_dependencies(${TARGET_NAME}
${TARGET_NAME_WHEELS}
${COPY_QASM_SIM_TARGET}
${COPY_QASM_SIM_CPP_TARGET}
qasm_simulator_cpp)
endif()

Expand Down
2 changes: 1 addition & 1 deletion doc/conf.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
sys.path.insert(0, os.path.abspath('.'))

# Imported manually, as otherwise it will not be fully imported.
import qiskit.extensions.qiskit_simulator
import qiskit.extensions.qasm_simulator_cpp

# -- General configuration ------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions setup.py.in
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages = ["qiskit",


# C++ components compilation
class QiskitSimulatorBuild(build):
class QasmSimulatorBuild(build):
def run(self):
super().run()
# Store the current working directory, as invoking cmake involves
Expand All @@ -82,7 +82,7 @@ class QiskitSimulatorBuild(build):
cmd_cmake.append("-GMinGW Makefiles")
cmd_cmake.append('..')

cmd_make = ['make', 'pypi_package_copy_qiskit_simulator']
cmd_make = ['make', 'pypi_package_copy_qasm_simulator_cpp']

try:
cmd_make.append('-j%d' % cpu_count())
Expand All @@ -95,7 +95,7 @@ class QiskitSimulatorBuild(build):
call(cmd_cmake)
call(cmd_make)

self.execute(compile_simulator, [], 'Compiling QISKit C++ Simulator')
self.execute(compile_simulator, [], 'Compiling C++ QASM Simulator')
except Exception as e:
print(str(e))
print("WARNING: Seems like the cpp simulator can't be built, Qiskit will "
Expand Down Expand Up @@ -140,7 +140,7 @@ setup(
include_package_data=True,
python_requires=">=3.5",
cmdclass={
'build': QiskitSimulatorBuild,
'build': QasmSimulatorBuild,
},
distclass=BinaryDistribution,
extras_require={
Expand Down
62 changes: 31 additions & 31 deletions src/qasm-simulator-cpp/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CMake config file to build the C++ Simulator
#
# For Windows, we always build static executables. QISKit provides with the
# necessary external binary libraries (.lib and .dll), these .lib comes from an
# For Windows, we always build static executables. QISKit provides the
# necessary external binary libraries (.lib and .dll), these .lib come from an
# external source and they are simple import files, so we still need the .dll
# files in order to run the final executable. We only support MinGW64 toolchain
# so the static linking makes sure that other required libraries from this
Expand All @@ -17,27 +17,27 @@
#
# For Mac, you probably need to install static versions of the toolchain in order
# to make a static executable. Additionaly, the OpenMP features are only supported
# on GNU g++ comipler, CLang doesn't include it so if you are building with CLang
# on GNU g++ compiler, CLang doesn't include it so if you are building with CLang
# you won't get all the performance.

project(qasm_simulator_cpp VERSION 1.0 LANGUAGES CXX)

set(QASM_SIMULATOR_SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(QASM_SIMULATOR_SRC
"${QASM_SIMULATOR_SRC_DIR}/main.cpp")
set(QASM_SIMULATOR_EXTERNAL_LIBS
"${QASM_SIMULATOR_SRC_DIR}/third-party/headers"
"${QASM_SIMULATOR_SRC_DIR}/third-party/win64/lib"
set(QASM_SIMULATOR_CPP_SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(QASM_SIMULATOR_CPP_SRC
"${QASM_SIMULATOR_CPP_SRC_DIR}/main.cpp")
set(QASM_SIMULATOR_CPP_EXTERNAL_LIBS
"${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/headers"
"${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/win64/lib"
"${USER_LIB_PATH}")

# Target definition
add_executable(qasm_simulator_cpp ${QASM_SIMULATOR_SRC})
add_executable(qasm_simulator_cpp ${QASM_SIMULATOR_CPP_SRC})

# Target properties: C++ program
set_target_properties(qasm_simulator_cpp PROPERTIES LINKER_LANGUAGE CXX)

# Toolchain options
set_property(TARGET qasm_simulator_cpp PROPERTY CXX_STANDARD 14)
set_property(TARGET qasm_simulator_cpp PROPERTY CXX_STANDARD 11)

# Compiler flags
enable_cxx_compiler_flag_if_supported("-O3")
Expand All @@ -48,13 +48,13 @@ enable_cxx_compiler_flag_if_supported("-fopenmp")
if(STATIC_LINKING OR MINGW)
# Hack: Seems like enable_cxx_compiler_flag_if_supported() is not properly
# working on MacOS, when a flag is not supported, it cascades errors
# to the rest of the flgas being tested... and -static compilation on Mac
# to the rest of the flags being tested... and -static compilation on Mac
# with gcc is failing...
if(NOT APPLE)
enable_cxx_compiler_flag_if_supported("-static")
enable_cxx_compiler_flag_if_supported("-static-libgcc")
endif()
enable_cxx_compiler_flag_if_supported("-static-libstdc++")
enable_cxx_compiler_flag_if_supported("-static-libgcc")
endif()

# Warnings and Errors
Expand All @@ -69,11 +69,11 @@ enable_cxx_compiler_flag_if_supported("-Wredundant-decls")
enable_cxx_compiler_flag_if_supported("-Wshadow")
enable_cxx_compiler_flag_if_supported("-Woverloaded-virtual")

target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_SRC_DIR})
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_SRC_DIR}/backends)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_SRC_DIR}/engines)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_SRC_DIR}/utilities)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_SRC_DIR}/third-party/headers)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_CPP_SRC_DIR})
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_CPP_SRC_DIR}/backends)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_CPP_SRC_DIR}/engines)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_CPP_SRC_DIR}/utilities)
target_include_directories(qasm_simulator_cpp PRIVATE ${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/headers)

# For header only libraries
SET(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} "")
Expand All @@ -89,28 +89,28 @@ endif()
# want CMake to find it.
find_library(LIB_JSON
NAMES json
PATHS ${QASM_SIMULATOR_EXTERNAL_LIBS})
PATHS ${QASM_SIMULATOR_CPP_EXTERNAL_LIBS})
if(NOT LIB_JSON)
message(FATAL_ERROR "JSON library not found!. Please provide with a USER_LIB_PATH to CMake so it can be searched there")
endif()

find_library(LIB_LAPACK
NAMES lapack
PATHS ${QASM_SIMULATOR_EXTERNAL_LIBS})
PATHS ${QASM_SIMULATOR_CPP_EXTERNAL_LIBS})
if(NOT LIB_LAPACK)
message(FATAL_ERROR "LAPACK library not found!. Please provide with a USER_LIB_PATH to CMake so it can be searched there")
endif()

find_library(LIB_BLAS
NAMES blas
PATHS ${QASM_SIMULATOR_EXTERNAL_LIBS})
PATHS ${QASM_SIMULATOR_CPP_EXTERNAL_LIBS})
if(NOT LIB_BLAS)
message(FATAL_ERROR "BLAS library not found!. Please provide with a USER_LIB_PATH to CMake so it can be searched there")
endif()

find_library(LIB_THREADS
NAMES pthread
PATHS ${QASM_SIMULATOR_EXTERNAL_LIBS})
PATHS ${QASM_SIMULATOR_CPP_EXTERNAL_LIBS})
if(NOT LIB_THREADS)
message(FATAL_ERROR "Pthreads library not found!. Please provide with a USER_LIB_PATH to CMake so it can be searched there")
endif()
Expand All @@ -122,31 +122,31 @@ set(LIBRARIES PRIVATE ${LIB_BLAS}
# Linking
target_link_libraries(qasm_simulator_cpp ${LIBRARIES})

set(QASM_SIMULATOR_OUTPUT_DIR $<TARGET_FILE_DIR:qasm_simulator_cpp>
set(QASM_SIMULATOR_CPP_OUTPUT_DIR $<TARGET_FILE_DIR:qasm_simulator_cpp>
CACHE INTERNAL "Output directory for building QISKit C++ Simulator")

if(MINGW)
set(QASM_SIMULATOR_THIRD_PARTY_DLLS
"${QASM_SIMULATOR_SRC_DIR}/third-party/win64/dll/libblas.dll"
"${QASM_SIMULATOR_SRC_DIR}/third-party/win64/dll/libgfortran_64-3.dll"
"${QASM_SIMULATOR_SRC_DIR}/third-party/win64/dll/libquadmath_64-0.dll"
set(QASM_SIMULATOR_CPP_THIRD_PARTY_DLLS
"${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/win64/dll/libblas.dll"
"${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/win64/dll/libgfortran_64-3.dll"
"${QASM_SIMULATOR_CPP_SRC_DIR}/third-party/win64/dll/libquadmath_64-0.dll"
CACHE INTERNAL "Third-party C++ Simulator DLLs")

foreach(dll_file ${QASM_SIMULATOR_THIRD_PARTY_DLLS})
foreach(dll_file ${QASM_SIMULATOR_CPP_THIRD_PARTY_DLLS})
add_custom_command(
TARGET qasm_simulator_cpp
POST_BUILD
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${dll_file} ${QASM_SIMULATOR_OUTPUT_DIR}/
ARGS -E copy ${dll_file} ${QASM_SIMULATOR_CPP_OUTPUT_DIR}/
)
# For 'make clean' target
get_filename_component(FINAL_FILE ${dll_file} NAME)
set_property(DIRECTORY APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES
${QASM_SIMULATOR_OUTPUT_DIR}/${FINAL_FILE})
${QASM_SIMULATOR_CPP_OUTPUT_DIR}/${FINAL_FILE})
endforeach()
endif()

# Tests
# TODO: Enable them when ready
#add_subdirectory(${QASM_SIMULATOR_DIR}/test)
#add_subdirectory(${QASM_SIMULATOR_CPP_DIR}/test)
4 changes: 1 addition & 3 deletions src/qasm-simulator-cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ results = QuantumProgram.execute(circs,
config=config)
```

You can check the backend was successfully added using the `available_backends` method of the `QuantumProgram` class. If successful the returned list will include `local_qasm_simulator` and `local_clifford_simulator`.

Note: `local_qasm_simulator` is provided as a shortcut and will automatically point to `local_qasm_simulator_cpp` if it is installed. However, if this simulator is not installed, then it will point to the (slower) `local_qasm_simulator_py`.
You can check the backend was successfully added using the `available_backends` method of the `QuantumProgram` class. If successful the returned list will include `local_qiskit_simulator` and `local_clifford_simulator`.

### Simulator output

Expand Down
15 changes: 6 additions & 9 deletions src/qasm-simulator-cpp/src/backends/base_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,12 @@ template <class StateType> class BaseBackend {
*/

virtual inline void set_config(json_t &config) = 0; // raises unused param warning
inline void set_omp_threads(int n) {
inline void set_num_threads(int n) {
if (n > 0)
omp_threads = n;
num_threads = n;
};
inline void set_omp_threshold(int n) {
if (n > 0)
omp_threshold = n;
inline int get_num_threads() {
return num_threads;
};

/**
Expand All @@ -201,10 +200,8 @@ template <class StateType> class BaseBackend {
/**
* Number of threads to use for inner parallelization.
*/
uint_t omp_threads = 1;
uint_t omp_threshold;
bool omp_flag = false;

int num_threads = 1;

/**
* Stores the state of measured classical registers in a QISKIT program. All
* bits are initialized in the 0 state, and any unmeasured bits will hence
Expand Down
17 changes: 8 additions & 9 deletions src/qasm-simulator-cpp/src/backends/ideal_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class IdealBackend : public BaseBackend<QubitVector> {
/************************
* BaseBackend Methods
************************/
void set_config(json_t &config);
void set_config(json_t &config) override;
virtual void initialize(const Circuit &prog);
virtual void qc_operation(const operation &op);

Expand All @@ -64,6 +64,10 @@ class IdealBackend : public BaseBackend<QubitVector> {


protected:
/************************
* OpenMP setting
************************/
int omp_threshold = 16;

/************************
* Apply matrices
Expand Down Expand Up @@ -122,10 +126,7 @@ class IdealBackend : public BaseBackend<QubitVector> {

void IdealBackend::set_config(json_t &config) {
// Set OMP threshold for state update functions
uint_t num_qubits_omp_threshold = 20; // default threshold
JSON::get_value(num_qubits_omp_threshold, "theshold_omp_gate", config);
qreg.set_omp_threshold(num_qubits_omp_threshold);

JSON::get_value(omp_threshold, "threshold_omp_gate", config);
// parse initial state from JSON
if (JSON::check_key("initial_state", config)) {
QubitVector initial_state = config["initial_state"].get<cvector_t>();
Expand Down Expand Up @@ -153,13 +154,11 @@ void IdealBackend::initialize(const Circuit &prog) {
} else {
// reset state std::vector to default state
qreg = QubitVector(prog.nqubits);
qreg.set_omp_threshold(omp_threshold);
qreg.set_omp_threads(num_threads);
qreg.initialize();
}

// OpenMP settings
qreg.set_omp_threshold(omp_threshold);
qreg.set_omp_threads(omp_threads);

// TODO: make a ClassicalRegister class using BinaryVector
creg.assign(prog.nclbits, 0);
qreg_saved.erase(qreg_saved.begin(), qreg_saved.end());
Expand Down
19 changes: 5 additions & 14 deletions src/qasm-simulator-cpp/src/engines/base_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ template <typename StateType = QubitVector> class BaseEngine {
// Initial State
StateType initial_state;

// OMP Threshold for backend
int_t omp_threshold = -1; // < 0 for automatic

//============================================================================
// Constructors
//============================================================================
Expand All @@ -115,8 +112,8 @@ template <typename StateType = QubitVector> class BaseEngine {
* @param nshots the number of simulation shots to run
*/
virtual void run_program(Circuit &circ, BaseBackend<StateType> *be,
uint_t nshots = 1, uint_t nthreads = 1);
virtual void initialize(BaseBackend<StateType> *be, uint_t nthreads);
uint_t nshots = 1);
virtual void initialize(BaseBackend<StateType> *be);
virtual void execute(Circuit &circ, BaseBackend<StateType> *be,
uint_t nshots);

Expand Down Expand Up @@ -155,18 +152,14 @@ template <typename StateType = QubitVector> class BaseEngine {
template <typename StateType>
void BaseEngine<StateType>::run_program(Circuit &prog,
BaseBackend<StateType> *be,
uint_t nshots, uint_t nthreads) {
initialize(be, nthreads);
uint_t nshots) {
initialize(be);
execute(prog, be, nshots);
total_shots += nshots;
}

template <typename StateType>
void BaseEngine<StateType>::initialize(BaseBackend<StateType> *be,
uint_t nthreads) {
// Set OpenMP settings
be->set_omp_threads(nthreads);
be->set_omp_threshold(omp_threshold);
void BaseEngine<StateType>::initialize(BaseBackend<StateType> *be) {
// Set custom initial state
if (initial_state_flag)
be->set_initial_state(initial_state);
Expand Down Expand Up @@ -308,8 +301,6 @@ inline void from_json(const json_t &js, BaseEngine<StateType> &engine) {
engine.initial_state_flag = true;
}

// Get omp threshold
JSON::get_value(engine.omp_threshold, "omp_threshold", js);
}

//------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 93b3092

Please sign in to comment.