Commit 74900cf2 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

fixed bug where -qpu wasn't passed, added createObjectiveFunction overloads for no qreg arg



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 132dce5b
Pipeline #123445 passed with stage
in 15 minutes and 44 seconds
......@@ -61,6 +61,55 @@ public:
}
};
namespace __internal__ {
// Get the objective function from the service registry
std::shared_ptr<ObjectiveFunction> get_objective(const std::string &type);
template <typename T> std::shared_ptr<T> qcor_as_shared(T *t) {
return std::shared_ptr<T>(t, [](T *const) {});
}
template <std::size_t... Is>
auto create_tuple_impl(std::index_sequence<Is...>,
const std::vector<double> &arguments) {
return std::make_tuple(arguments[Is]...);
}
template <std::size_t N>
auto create_tuple(const std::vector<double> &arguments) {
return create_tuple_impl(std::make_index_sequence<N>{}, arguments);
}
struct ArgsTranslatorAutoGenerator {
std::shared_ptr<ArgsTranslator<qreg, std::vector<double>>>
operator()(qreg &q, std::tuple<qreg, std::vector<double>> &&) {
return std::make_shared<ArgsTranslator<qreg, std::vector<double>>>(
[&](const std::vector<double> &x) { return std::make_tuple(q, x); });
}
template <typename... DoubleTypes>
std::shared_ptr<ArgsTranslator<qreg, DoubleTypes...>>
operator()(qreg &q, std::tuple<qreg, DoubleTypes...> &&t) {
if constexpr ((std::is_same<DoubleTypes, double>::value && ...)) {
return std::make_shared<ArgsTranslator<qreg, DoubleTypes...>>(
[&](const std::vector<double> &x) {
auto qreg_tuple = std::make_tuple(q);
auto double_tuple = create_tuple<sizeof...(DoubleTypes)>(x);
return std::tuple_cat(qreg_tuple, double_tuple);
});
} else {
error("QCOR cannot auto-generate a ArgsTranslator for this "
"ObjectiveFunction. Please provide a custom ArgsTranslator to "
"createObjectiveFunction.");
return std::make_shared<ArgsTranslator<qreg, DoubleTypes...>>(
[&](const std::vector<double> &x) { return t; });
}
}
};
} // namespace __internal__
template <typename... KernelArgs>
class ObjectiveFunctionImpl : public ObjectiveFunction {
private:
......@@ -77,7 +126,7 @@ private:
protected:
std::shared_ptr<LocalArgsTranslator> args_translator;
std::shared_ptr<ObjectiveFunction> helper;
xacc::internal_compiler::qreg &qreg;
xacc::internal_compiler::qreg qreg;
public:
ObjectiveFunctionImpl(void *k_ptr, std::shared_ptr<Observable> obs,
......@@ -97,6 +146,25 @@ public:
helper->update_observable(observable);
helper->set_options(options);
}
ObjectiveFunctionImpl(void *k_ptr, std::shared_ptr<Observable> obs,
std::shared_ptr<ObjectiveFunction> obj_helper,
const int dim, HeterogeneousMap opts) {
qreg = ::qalloc(obs->nBits());
kernel_ptr = k_ptr;
observable = obs;
__internal__::ArgsTranslatorAutoGenerator auto_gen;
args_translator = auto_gen(qreg, std::tuple<KernelArgs...>());
// args_translator = translator;
helper = obj_helper;
_dim = dim;
_function = *this;
options = opts;
options.insert("observable", observable);
helper->update_observable(observable);
helper->set_options(options);
}
void set_options(HeterogeneousMap &opts) override {
options = opts;
helper->set_options(opts);
......@@ -164,55 +232,6 @@ public:
const std::string description() const override { return ""; }
};
namespace __internal__ {
// Get the objective function from the service registry
std::shared_ptr<ObjectiveFunction> get_objective(const std::string &type);
template <typename T> std::shared_ptr<T> qcor_as_shared(T *t) {
return std::shared_ptr<T>(t, [](T *const) {});
}
template <std::size_t... Is>
auto create_tuple_impl(std::index_sequence<Is...>,
const std::vector<double> &arguments) {
return std::make_tuple(arguments[Is]...);
}
template <std::size_t N>
auto create_tuple(const std::vector<double> &arguments) {
return create_tuple_impl(std::make_index_sequence<N>{}, arguments);
}
struct ArgsTranslatorAutoGenerator {
std::shared_ptr<ArgsTranslator<qreg, std::vector<double>>>
operator()(qreg &q, std::tuple<qreg, std::vector<double>> &&) {
return std::make_shared<ArgsTranslator<qreg, std::vector<double>>>(
[&](const std::vector<double> &x) { return std::make_tuple(q, x); });
}
template <typename... DoubleTypes>
std::shared_ptr<ArgsTranslator<qreg, DoubleTypes...>>
operator()(qreg &q, std::tuple<qreg, DoubleTypes...> &&t) {
if constexpr ((std::is_same<DoubleTypes, double>::value && ...)) {
return std::make_shared<ArgsTranslator<qreg, DoubleTypes...>>(
[&](const std::vector<double> &x) {
auto qreg_tuple = std::make_tuple(q);
auto double_tuple = create_tuple<sizeof...(DoubleTypes)>(x);
return std::tuple_cat(qreg_tuple, double_tuple);
});
} else {
error("QCOR cannot auto-generate a ArgsTranslator for this "
"ObjectiveFunction. Please provide a custom ArgsTranslator to "
"createObjectiveFunction.");
return std::make_shared<ArgsTranslator<qreg, DoubleTypes...>>(
[&](const std::vector<double> &x) { return t; });
}
}
};
} // namespace __internal__
template <typename... Args>
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
......@@ -435,4 +454,75 @@ std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
kernel_ptr, observable, q, args_translator, helper, nParams, options);
}
/// no qreg args
template <typename... Args>
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
Args...),
std::shared_ptr<Observable> observable, const int nParams,
HeterogeneousMap &&options = {}) {
auto helper = qcor::__internal__::get_objective("vqe");
void *kernel_ptr = reinterpret_cast<void *>(quantum_kernel_functor);
return std::make_shared<ObjectiveFunctionImpl<Args...>>(
kernel_ptr, observable, helper, nParams, options);
}
template <typename... Args>
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
const std::string obj_name,
void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
Args...),
std::shared_ptr<Observable> observable, const int nParams,
HeterogeneousMap &&options = {}) {
auto helper = qcor::__internal__::get_objective(obj_name);
void *kernel_ptr = reinterpret_cast<void *>(quantum_kernel_functor);
return std::make_shared<ObjectiveFunctionImpl<Args...>>(
kernel_ptr, observable, helper, nParams, options);
}
template <typename... Args>
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
Args...),
Observable &observable, const int nParams,
HeterogeneousMap &&options = {}) {
auto helper = qcor::__internal__::get_objective("vqe");
void *kernel_ptr = reinterpret_cast<void *>(quantum_kernel_functor);
return std::make_shared<ObjectiveFunctionImpl<Args...>>(
kernel_ptr, __internal__::qcor_as_shared(&observable),
helper, nParams, options);
}
template <typename... Args>
std::shared_ptr<ObjectiveFunction> createObjectiveFunction(
const std::string obj_name,
void (*quantum_kernel_functor)(std::shared_ptr<CompositeInstruction>,
Args...),
Observable &observable, const int nParams,
HeterogeneousMap &&options = {}) {
auto helper = qcor::__internal__::get_objective(obj_name);
void *kernel_ptr = reinterpret_cast<void *>(quantum_kernel_functor);
return std::make_shared<ObjectiveFunctionImpl<Args...>>(
kernel_ptr, __internal__::qcor_as_shared(&observable),
helper, nParams, options);
}
} // namespace qcor
\ No newline at end of file
......@@ -62,6 +62,10 @@ def main(argv=None):
sys.argv.remove('-qpu')
sys.argv += ['-D__internal__qcor__compile__backend=\"'+accName+'\"']
sHandlerArgs = ['-Xclang', '-plugin-arg-qcor-args', '-Xclang', '-qpu', '-Xclang', '-plugin-arg-qcor-args', '-Xclang', accName]
else:
accName = 'qpp'
sys.argv += ['-D__internal__qcor__compile__backend=\"'+accName+'\"']
sHandlerArgs = ['-Xclang', '-plugin-arg-qcor-args', '-Xclang', '-qpu', '-Xclang', '-plugin-arg-qcor-args', '-Xclang', accName]
# Get the shots if necessary
shots = 0
......
......@@ -59,7 +59,11 @@ def main(argv=None):
sys.argv.remove('-qpu')
sys.argv += ['-D__internal__qcor__compile__backend=\"'+accName+'\"']
sHandlerArgs = ['-Xclang', '-plugin-arg-qcor-args', '-Xclang', '-qpu', '-Xclang', '-plugin-arg-qcor-args', '-Xclang', accName]
else:
accName = 'qpp'
sys.argv += ['-D__internal__qcor__compile__backend=\"'+accName+'\"']
sHandlerArgs = ['-Xclang', '-plugin-arg-qcor-args', '-Xclang', '-qpu', '-Xclang', '-plugin-arg-qcor-args', '-Xclang', accName]
# Get the shots if necessary
shots = 0
if '-shots' in sys.argv[1:]:
......
Supports Markdown
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