Commit 3b576034 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

upload work on compute action in qpu_lambda, still remaining seg fault at program end


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 19ee8d86
FROM codercom/code-server:latest
ENV DEBIAN_FRONTEND noninteractive
RUN echo 'deb http://deb.debian.org/debian testing main' | sudo tee /etc/apt/sources.list.d/testing.list
RUN sudo apt-get update \
&& sudo apt-get install -y \
curl build-essential wget gdb \
dumb-init \
zsh \
htop \
locales \
man \
nano \
git \
procps \
openssh-client \
sudo \
vim.tiny libssl-dev liblapack-dev libblas-dev ninja-build zlib1g-dev \
libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev \
lsb-release libcurl4-openssl-dev \
&& sudo rm -rf /var/lib/apt/lists/* \
&& sudo wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz \
&& sudo tar xzf Python-3.8.2.tgz && cd Python-3.8.2 \
&& sudo CCSHARED='-fPIC' ./configure --prefix=/usr --exec-prefix=/usr --enable-shared --enable-optimizations \
&& sudo make altinstall && cd ../ && sudo rm -rf Python-3.8.2* \
&& wget -qO- https://aide-qc.github.io/deploy/aide_qc/debian/PUBLIC-KEY.gpg | sudo apt-key add - \
&& wget -qO- "https://aide-qc.github.io/deploy/aide_qc/debian/focal/aide-qc.list" | sudo tee -a /etc/apt/sources.list.d/aide-qc.list \
&& /usr/bin/pip3.8 install ipopo cmake --user \
&& sudo unlink /usr/bin/python3 && sudo ln -s /usr/bin/python3.8 /usr/bin/python3 \
&& sudo sed -i 's/#!\/usr\/bin\/python3/#!\/usr\/bin\/python3.9/' /usr/bin/lsb_release
......@@ -510,7 +510,7 @@ void QJIT::jit_compile(const std::string &code,
// Add dependency before JIT compile:
new_code = dependencyCode + new_code;
// std::cout << "New code:\n" << new_code << "\n";
std::cout << "New code:\n" << new_code << "\n";
// Hash the new code
std::hash<std::string> hasher;
auto hash = hasher(new_code);
......@@ -524,6 +524,7 @@ void QJIT::jit_compile(const std::string &code,
auto module_bitcode_file_name = cached_kernel_codes[hash];
std::string full_path = qjit_cache_path + "/" + module_bitcode_file_name;
std::cout << "REUSING QJIT CACHED MODULE\n";
// Load the bitcode file as Module
SMDiagnostic error;
auto ctx = std::make_unique<LLVMContext>();
......@@ -578,10 +579,11 @@ void QJIT::jit_compile(const std::string &code,
for (Function &f : *module) {
auto name = f.getName().str();
auto demangled = demangle(name.c_str());
if (demangled.find(kernel_name) != std::string::npos &&
if (demangled.find(kernel_name+"(") != std::string::npos && // has to be KERNEL_NAME(
demangled.find(kernel_name+"::"+kernel_name) == std::string::npos && // don't pick the class constructor
demangled.find("qcor::QuantumKernel<"+kernel_name) == std::string::npos && // don't pick the QuantumKernel
demangled.find("std::shared_ptr<xacc::CompositeInstruction>") != std::string::npos) {
demangled.find("__internal__compute_context_") == std::string::npos && // don't pick any of our internally generated compute functions
demangled.find("std::shared_ptr<xacc::CompositeInstruction>") != std::string::npos) { // must have composite inst arg
parent_mangled_name = name;
}
}
......@@ -670,6 +672,7 @@ void QJIT::jit_compile(const std::string &code,
// Get and store the kernel_name(CompositeInstruction parent, Args...) function
auto parent_symbol = cantFail(jit->lookup(parent_mangled_name));
auto parent_rawFPtr = parent_symbol.getAddress();
std::cout << "INSERTING " << kernel_name << ", " << parent_mangled_name << "\n";
kernel_name_to_f_ptr_with_parent.insert({kernel_name, parent_rawFPtr});
for (const auto &[orig_name, mangled_name] : mangled_kernel_dep_map) {
......
......@@ -112,7 +112,7 @@ class QuantumKernel {
static void print_kernel(std::ostream &os, Args... args) {
Derived derived(args...);
KernelSignature<Args...> callable(derived);
return internal::print_kernel(callable, os, args...);
return internal::print_kernel<Args...>(callable, os, args...);
}
static void print_kernel(Args... args) { print_kernel(std::cout, args...); }
......@@ -121,7 +121,7 @@ class QuantumKernel {
static std::size_t n_instructions(Args... args) {
Derived derived(args...);
KernelSignature<Args...> callable(derived);
return internal::n_instructions(callable, args...);
return internal::n_instructions<Args...>(callable, args...);
}
// Create the Adjoint of this quantum kernel
......@@ -129,7 +129,7 @@ class QuantumKernel {
Args... args) {
Derived derived(args...);
KernelSignature<Args...> callable(derived);
return internal::apply_adjoint(parent_kernel, callable, args...);
return internal::apply_adjoint<Args...>(parent_kernel, callable, args...);
}
// Create the controlled version of this quantum kernel
......@@ -162,7 +162,8 @@ class QuantumKernel {
// instantiate and don't let it call the destructor
Derived derived(args...);
KernelSignature<Args...> callable(derived);
internal::apply_control(parent_kernel, ctrl_qbits, callable, args...);
internal::apply_control<Args...>(parent_kernel, ctrl_qbits, callable,
args...);
}
// Create the controlled version of this quantum kernel
......@@ -174,13 +175,13 @@ class QuantumKernel {
static Eigen::MatrixXcd as_unitary_matrix(Args... args) {
Derived derived(args...);
KernelSignature<Args...> callable(derived);
return internal::as_unitary_matrix(callable, args...);
return internal::as_unitary_matrix<Args...>(callable, args...);
}
static double observe(Observable &obs, Args... args) {
Derived derived(args...);
KernelSignature<Args...> callable(derived);
return internal::observe(obs, callable, args...);
return internal::observe<Args...>(obs, callable, args...);
}
static double observe(std::shared_ptr<Observable> obs, Args... args) {
......@@ -609,7 +610,6 @@ class _qpu_lambda {
FunctionArgs &&...args) {
// Map the function args to a tuple
auto kernel_args_tuple = std::forward_as_tuple(args...);
if (!optional_copy_capture_vars.has_value()) {
// By-ref:
// Merge the function args and the capture vars and execute
......@@ -619,6 +619,7 @@ class _qpu_lambda {
qjit.invoke_with_parent_forwarding("foo", parent, args...);
},
final_args_tuple);
} else if constexpr (std::conjunction_v<
std::is_copy_assignable<CaptureArgs>...>) {
// constexpr compile-time check to prevent compiler from looking at this
......@@ -717,7 +718,7 @@ class _qpu_lambda {
template <typename... FunctionArgs>
void print_kernel(std::ostream &os, FunctionArgs... args) {
KernelSignature<FunctionArgs...> callable(*this);
return internal::print_kernel(callable, os, args...);
return internal::print_kernel<FunctionArgs...>(callable, os, args...);
}
template <typename... FunctionArgs>
......@@ -804,9 +805,7 @@ class KernelSignature {
function_pointer(ir, args...);
}
void operator()(Args... args) {
operator()(parent_kernel, args...);
}
void operator()(Args... args) { operator()(parent_kernel, args...); }
void set_parent_kernel(std::shared_ptr<xacc::CompositeInstruction> ir) {
parent_kernel = ir;
......@@ -846,25 +845,25 @@ class KernelSignature {
}
void adjoint(std::shared_ptr<CompositeInstruction> ir, Args... args) {
internal::apply_adjoint(ir, *this, args...);
internal::apply_adjoint<Args...>(ir, *this, args...);
}
void print_kernel(std::ostream &os, Args... args) {
return internal::print_kernel(*this, os, args...);
return internal::print_kernel<Args...>(*this, os, args...);
}
void print_kernel(Args... args) { print_kernel(std::cout, args...); }
std::size_t n_instructions(Args... args) {
return internal::n_instructions(*this, args...);
return internal::n_instructions<Args...>(*this, args...);
}
Eigen::MatrixXcd as_unitary_matrix(Args... args) {
return internal::as_unitary_matrix(*this, args...);
return internal::as_unitary_matrix<Args...>(*this, args...);
}
std::string openqasm(Args... args) {
return internal::openqasm(*this, args...);
return internal::openqasm<Args...>(*this, args...);
}
double observe(std::shared_ptr<Observable> obs, Args... args) {
......@@ -872,7 +871,7 @@ class KernelSignature {
}
double observe(Observable &obs, Args... args) {
return internal::observe(obs, *this, args...);
return internal::observe<Args...>(obs, *this, args...);
}
};
......@@ -891,13 +890,13 @@ inline void init_kernel_signature_args_impl(
std::shared_ptr<xacc::CompositeInstruction> ir) {}
template <typename T, typename... ArgsType>
void init_kernel_signature_args_impl(
std::shared_ptr<xacc::CompositeInstruction> ir, T &t, ArgsType &... Args);
std::shared_ptr<xacc::CompositeInstruction> ir, T &t, ArgsType &...Args);
// Main function: to be added by the token collector at the beginning
// of each kernel operator().
template <typename... T>
void init_kernel_signature_args(std::shared_ptr<xacc::CompositeInstruction> ir,
T &... multi_inputs) {
T &...multi_inputs) {
init_kernel_signature_args_impl(ir, multi_inputs...);
}
......@@ -905,7 +904,7 @@ void init_kernel_signature_args(std::shared_ptr<xacc::CompositeInstruction> ir,
// just ignore, proceed to the next arg.
template <typename T, typename... ArgsType>
void init_kernel_signature_args_impl(
std::shared_ptr<xacc::CompositeInstruction> ir, T &t, ArgsType &... Args) {
std::shared_ptr<xacc::CompositeInstruction> ir, T &t, ArgsType &...Args) {
init_kernel_signature_args(ir, Args...);
}
......
......@@ -697,6 +697,45 @@ createObjectiveFunction(_qpu_lambda<CaptureArgs...> &lambda,
helper, nParams, options);
}
template <typename... CaptureArgs>
std::shared_ptr<ObjectiveFunction>
createObjectiveFunction(_qpu_lambda<CaptureArgs...> &lambda,
std::shared_ptr<Observable> observable, qreg &q, const int nParams,
HeterogeneousMap &&options = {}) {
if (lambda.var_type ==
_qpu_lambda<CaptureArgs...>::Variational_Arg_Type::None) {
error("qpu_lambda has an incompatible signature. Please provide an "
"ArgsTranslator.");
}
auto helper = qcor::__internal__::get_objective("vqe");
std::function<void(std::shared_ptr<CompositeInstruction>, qreg,
std::vector<double>)>
kernel_fn = [&lambda](std::shared_ptr<CompositeInstruction> comp, qreg q,
std::vector<double> params) -> void {
if (lambda.var_type ==
_qpu_lambda<CaptureArgs...>::Variational_Arg_Type::Vec_Double) {
return lambda.eval_with_parent(comp, q, params);
}
if (lambda.var_type ==
_qpu_lambda<CaptureArgs...>::Variational_Arg_Type::Double) {
if (params.size() != 1) {
error("Invalid number of parameters. Expected 1, got " +
std::to_string(params.size()));
}
return lambda.eval_with_parent(comp, q, params[0]);
}
error("Internal error: invalid qpu lambda type encountered.");
};
auto args_translator =
std::make_shared<ArgsTranslator<qreg, std::vector<double>>>(
[&](const std::vector<double> x) { return std::make_tuple(q, x); });
return std::make_shared<ObjectiveFunctionImpl<qreg, std::vector<double>>>(
kernel_fn, observable, q, args_translator,
helper, nParams, options);
}
// Objective function with gradient options:
// Generic method: user provides a gradient calculation method.
template <typename... Args>
......
......@@ -78,6 +78,20 @@ class NISQ : public ::quantum::QuantumRuntime,
}
public:
// AJM DEBUGGING
// virtual ~NISQ() override {
// std::cout << "DESTROYING NISQ\n";
// if (program) std::cout << program->name();
// std::cout << ", " << program.use_count() << "\n";
// }
// NISQ() = default;
// NISQ(const NISQ& n) : program(n.program), provider(n.provider), mark_as_compute(n.mark_as_compute) {
// }
std::shared_ptr<::quantum::QuantumRuntime> clone() override {
return std::make_shared<NISQ>();
}
......
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