Loading examples/xasm/deuteron_task_initiate.cpp 0 → 100644 +41 −0 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Ry(q[1], theta); CX(q[1], q[0]); } int main(int argc, char **argv) { // Allocate 2 qubits auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = qcor::createObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Create the Optimizer auto optimizer = qcor::createOptimizer("nlopt"); // Call taskInitiate, kick off optimization of the give // functor dependent on the ObjectiveFunction, async call auto handle = qcor::taskInitiate( objective, optimizer, [&](const std::vector<double> x, std::vector<double> &dx) { return (*objective)(q, x[0]); }, 1); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); // Print the optimal value. printf("<H> = %f\n", results.opt_val); } examples/xasm/hwe_ansatz_deuteron.cpp +0 −2 Original line number Diff line number Diff line Loading @@ -23,8 +23,6 @@ int main(int argc, char **argv) { // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable // Must also provide initial params for ansatz (under the hood, uses // variadic template) auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); Loading examples/xasm/objective_function.cpp +0 −2 Original line number Diff line number Diff line Loading @@ -35,8 +35,6 @@ int main(int argc, char **argv) { // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable // Must also provide initial params for ansatz (under the hood, uses // variadic template) auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Evaluate the ObjectiveFunction at a specified set of parameters Loading runtime/objectives/vqe/tests/VQETester.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ TEST(VQETester, checkSimple) { auto vqe = xacc::getService<qcor::ObjectiveFunction>("vqe"); vqe->initialize(observable, ruccsd.get()); vqe->set_results_buffer(buffer); vqe->set_qreg(buffer); qcor::OptFunction f( [&](const std::vector<double> x, std::vector<double> &grad) { Loading runtime/qcor.hpp +36 −8 Original line number Diff line number Diff line #ifndef RUNTIME_QCOR_HPP_ #define RUNTIME_QCOR_HPP_ #include <CompositeInstruction.hpp> #include <future> #include <memory> #include <qalloc> #include "CompositeInstruction.hpp" #include "Observable.hpp" #include "Optimizer.hpp" #include "qalloc" #include "xacc_internal_compiler.hpp" namespace qcor { using OptFunction = xacc::OptFunction; using HeterogeneousMap = xacc::HeterogeneousMap; using ResultsBuffer = xacc::internal_compiler::qreg; using Observable = xacc::Observable; using Optimizer = xacc::Optimizer; class ResultsBuffer { public: xacc::internal_compiler::qreg q_buffer; double opt_val; std::vector<double> opt_params; }; using Handle = std::future<ResultsBuffer>; ResultsBuffer sync(Handle& handle) { return handle.get(); } void set_verbose(bool verbose); Loading Loading @@ -79,7 +92,7 @@ protected: xacc::CompositeInstruction *kernel; // The buffer containing all execution results ResultsBuffer qreg; xacc::internal_compiler::qreg qreg; HeterogeneousMap options; Loading Loading @@ -116,7 +129,8 @@ public: void set_options(HeterogeneousMap &opts) { options = opts; } // Set the results buffer void set_results_buffer(ResultsBuffer q) { qreg = q; } void set_qreg(xacc::internal_compiler::qreg q) { qreg = q; } xacc::internal_compiler::qreg get_qreg() { return qreg; } // Evaluate this Objective function at the give parameters. // These variadic parameters must mirror the provided Loading Loading @@ -182,9 +196,6 @@ createOptimizer(const char *type, HeterogeneousMap &&options = {}); // Create an observable from a string representation std::shared_ptr<Observable> createObservable(const char *repr); // template <typename T> struct is_shared_ptr : std::false_type {}; // template <typename T> // struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; std::shared_ptr<ObjectiveFunction> createObjectiveFunction( const char *obj_name, std::shared_ptr<xacc::CompositeInstruction> kernel, std::shared_ptr<Observable> observable, HeterogeneousMap &&options = {}) { Loading Loading @@ -212,6 +223,23 @@ createObjectiveFunction(const char *obj_name, QuantumKernel &kernel, return obj_func; } Handle taskInitiate( std::shared_ptr<ObjectiveFunction> objective, std::shared_ptr<Optimizer> optimizer, std::function<double(const std::vector<double>, std::vector<double> &)> &&opt_function, const int nParameters) { return std::async(std::launch::async, [=]() -> ResultsBuffer { qcor::OptFunction f(opt_function, nParameters); auto results = optimizer->optimize(f); ResultsBuffer rb; rb.q_buffer = objective->get_qreg(); rb.opt_params = results.second; rb.opt_val = results.first; return rb; }); } } // namespace qcor #endif Loading
examples/xasm/deuteron_task_initiate.cpp 0 → 100644 +41 −0 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Ry(q[1], theta); CX(q[1], q[0]); } int main(int argc, char **argv) { // Allocate 2 qubits auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = qcor::createObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Create the Optimizer auto optimizer = qcor::createOptimizer("nlopt"); // Call taskInitiate, kick off optimization of the give // functor dependent on the ObjectiveFunction, async call auto handle = qcor::taskInitiate( objective, optimizer, [&](const std::vector<double> x, std::vector<double> &dx) { return (*objective)(q, x[0]); }, 1); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); // Print the optimal value. printf("<H> = %f\n", results.opt_val); }
examples/xasm/hwe_ansatz_deuteron.cpp +0 −2 Original line number Diff line number Diff line Loading @@ -23,8 +23,6 @@ int main(int argc, char **argv) { // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable // Must also provide initial params for ansatz (under the hood, uses // variadic template) auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); Loading
examples/xasm/objective_function.cpp +0 −2 Original line number Diff line number Diff line Loading @@ -35,8 +35,6 @@ int main(int argc, char **argv) { // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable // Must also provide initial params for ansatz (under the hood, uses // variadic template) auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Evaluate the ObjectiveFunction at a specified set of parameters Loading
runtime/objectives/vqe/tests/VQETester.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ TEST(VQETester, checkSimple) { auto vqe = xacc::getService<qcor::ObjectiveFunction>("vqe"); vqe->initialize(observable, ruccsd.get()); vqe->set_results_buffer(buffer); vqe->set_qreg(buffer); qcor::OptFunction f( [&](const std::vector<double> x, std::vector<double> &grad) { Loading
runtime/qcor.hpp +36 −8 Original line number Diff line number Diff line #ifndef RUNTIME_QCOR_HPP_ #define RUNTIME_QCOR_HPP_ #include <CompositeInstruction.hpp> #include <future> #include <memory> #include <qalloc> #include "CompositeInstruction.hpp" #include "Observable.hpp" #include "Optimizer.hpp" #include "qalloc" #include "xacc_internal_compiler.hpp" namespace qcor { using OptFunction = xacc::OptFunction; using HeterogeneousMap = xacc::HeterogeneousMap; using ResultsBuffer = xacc::internal_compiler::qreg; using Observable = xacc::Observable; using Optimizer = xacc::Optimizer; class ResultsBuffer { public: xacc::internal_compiler::qreg q_buffer; double opt_val; std::vector<double> opt_params; }; using Handle = std::future<ResultsBuffer>; ResultsBuffer sync(Handle& handle) { return handle.get(); } void set_verbose(bool verbose); Loading Loading @@ -79,7 +92,7 @@ protected: xacc::CompositeInstruction *kernel; // The buffer containing all execution results ResultsBuffer qreg; xacc::internal_compiler::qreg qreg; HeterogeneousMap options; Loading Loading @@ -116,7 +129,8 @@ public: void set_options(HeterogeneousMap &opts) { options = opts; } // Set the results buffer void set_results_buffer(ResultsBuffer q) { qreg = q; } void set_qreg(xacc::internal_compiler::qreg q) { qreg = q; } xacc::internal_compiler::qreg get_qreg() { return qreg; } // Evaluate this Objective function at the give parameters. // These variadic parameters must mirror the provided Loading Loading @@ -182,9 +196,6 @@ createOptimizer(const char *type, HeterogeneousMap &&options = {}); // Create an observable from a string representation std::shared_ptr<Observable> createObservable(const char *repr); // template <typename T> struct is_shared_ptr : std::false_type {}; // template <typename T> // struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; std::shared_ptr<ObjectiveFunction> createObjectiveFunction( const char *obj_name, std::shared_ptr<xacc::CompositeInstruction> kernel, std::shared_ptr<Observable> observable, HeterogeneousMap &&options = {}) { Loading Loading @@ -212,6 +223,23 @@ createObjectiveFunction(const char *obj_name, QuantumKernel &kernel, return obj_func; } Handle taskInitiate( std::shared_ptr<ObjectiveFunction> objective, std::shared_ptr<Optimizer> optimizer, std::function<double(const std::vector<double>, std::vector<double> &)> &&opt_function, const int nParameters) { return std::async(std::launch::async, [=]() -> ResultsBuffer { qcor::OptFunction f(opt_function, nParameters); auto results = optimizer->optimize(f); ResultsBuffer rb; rb.q_buffer = objective->get_qreg(); rb.opt_params = results.second; rb.opt_val = results.first; return rb; }); } } // namespace qcor #endif