Unverified Commit d05e0a30 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #160 from amirebrahimi/argparse

Add argument parsing [unitaryHACK]
parents 6b2833b1 79e169e5
......@@ -56,6 +56,7 @@ configure_file("${CMAKE_SOURCE_DIR}/cmake/qcor_config.hpp.in"
"${CMAKE_BINARY_DIR}/qcor_config.hpp")
install (FILES ${CMAKE_BINARY_DIR}/qcor_config.hpp DESTINATION include/qcor)
add_subdirectory(tpls)
add_subdirectory(handlers)
add_subdirectory(runtime)
add_subdirectory(tools)
......
......@@ -14,6 +14,8 @@ add_test(NAME qrt_bell_multi COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT
add_test(NAME qrt_add_3_5 COMMAND ${CMAKE_BINARY_DIR}/qcor -v -c ${CMAKE_CURRENT_SOURCE_DIR}/adder/add_3_5.cpp WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/adder)
add_test(NAME qrt_mixed_language COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/mixed_language.cpp)
add_test(NAME qrt_simple-demo COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/simple/simple-demo.cpp)
add_test(NAME qrt_simple-explicit-parser COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/argparse/simple-explicit-parser.cpp)
add_test(NAME qrt_simple-implicit-parser COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/argparse/simple-implicit-parser.cpp)
add_test(NAME qrt_deuteron_exp_inst COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/deuteron/deuteron_exp_inst.cpp)
add_test(NAME qrt_deuteron_task_initiate COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/deuteron/deuteron_task_initiate.cpp)
add_test(NAME qrt_qaoa_example COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/qaoa/qaoa_example.cpp)
......
__qpu__ void f(qreg q) {
H(q);
Measure(q);
}
int main(int argc, char** argv) {
using namespace qcor::arg;
ArgumentParser program(argv[0]);
program.add_argument("n-qubits")
.default_value(1)
.action([](const std::string& value) { return std::stoi(value); });
program.parse_args(argc, argv);
auto N = program.get<int>("n-qubits");
auto q = qalloc(N);
f(q);
q.print();
}
\ No newline at end of file
__qpu__ void f(qreg q) {
H(q);
Measure(q);
}
int main(int argc, char** argv) {
using namespace qcor::arg;
add_argument("n-qubits")
.default_value(1)
.action([](const std::string& value) { return std::stoi(value); });
parse_args(argc, argv);
auto N = get_argument<int>("n-qubits");
auto q = qalloc(N);
f(q);
q.print();
}
\ No newline at end of file
......@@ -4,7 +4,12 @@ file(GLOB SRC *.cpp)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(${LIBRARY_NAME} PUBLIC . ${CMAKE_BINARY_DIR} ${XACC_ROOT}/include/eigen)
target_include_directories(${LIBRARY_NAME}
PUBLIC
.
${CMAKE_BINARY_DIR}
${XACC_ROOT}/include/eigen
${CMAKE_SOURCE_DIR}/tpls/argparse)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc xacc::quantum_gate xacc::pauli)
......
......@@ -5,6 +5,19 @@
#include "xacc_service.hpp"
namespace qcor {
namespace arg {
static ArgumentParser _parser;
ArgumentParser &get_parser() {
return _parser;
}
void parse_args(int argc, const char *const argv[]) {
get_parser().parse_args(argc, argv);
}
}
void set_verbose(bool verbose) { xacc::set_verbose(verbose); }
bool get_verbose() { return xacc::verbose; }
void set_shots(const int shots) { ::quantum::set_shots(shots); }
......
#pragma once
#include <argparse.hpp>
#include <Eigen/Dense>
#include <functional>
#include <future>
......@@ -21,7 +22,38 @@ namespace constants {
static constexpr double pi = 3.141592653589793238;
}
// Typedefs mapping xacc types to qcor types
namespace arg {
// Expose ArgumentParser for custom instantiation
using ArgumentParser = argparse::ArgumentParser;
// Default argument parser
ArgumentParser &get_parser();
// Parameter packing
// Call add_argument with variadic number of string arguments
template <typename... Targs>
argparse::Argument &add_argument(Targs... Fargs) {
return get_parser().add_argument(Fargs...);
}
// Getter for options with default values.
// @throws std::logic_error if there is no such option
// @throws std::logic_error if the option has no value
// @throws std::bad_any_cast if the option is not of type T
//
template <typename T = std::string>
T get_argument(std::string_view argument_name) {
return get_parser().get<T>(argument_name);
}
// Main entry point for parsing command-line arguments using this
// ArgumentParser
// @throws std::runtime_error in case of any invalid argument
//
void parse_args(int argc, const char *const argv[]);
}
// Typedefs mapping xacc/Eigen types to qcor types
using CompositeInstruction = xacc::CompositeInstruction;
using HeterogeneousMap = xacc::HeterogeneousMap;
using IRTransformation = xacc::IRTransformation;
......
install(FILES "${CMAKE_SOURCE_DIR}/tpls/argparse/argparse.hpp" DESTINATION include/qcor)
\ No newline at end of file
Copyright (c) 2018 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment