Loading handlers/xasm/xasm_syntax_handler.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -139,7 +139,7 @@ public: OS << "}\n"; std::cout << "HELLO:\n" << OS.str() << "\n"; // std::cout << "HELLO:\n" << OS.str() << "\n"; } void AddToPredefines(llvm::raw_string_ostream &OS) override { Loading runtime/qcor_version2.cpp +37 −25 Original line number Diff line number Diff line Loading @@ -2,50 +2,62 @@ #include "xacc.hpp" #include "PauliOperator.hpp" #include "Optimizer.hpp" #include <future> namespace qcor { xacc::Optimizer *getOptimizer() { if (!xacc::isInitialized()) xacc::Initialize(); return xacc::getOptimizer("nlopt").get(); } xacc::Observable *getObservable(const char *repr) { if (!xacc::isInitialized()) xacc::Initialize(); // auto sptr = xacc::quantum::getObservable("pauli", std::string(repr)); auto obs = new xacc::quantum::PauliOperator(repr); return obs; void initialize() { xacc::Initialize(); } void finalize() { xacc::Finalize(); } xacc::AcceleratorBuffer* sync(Handle& h) {return h.get();} std::future<xacc::internal_compiler::qreg> execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<double>& parameters) { namespace __internal__ { Handle execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, double * parameters) { return std::async(std::launch::async, [parameters, objective, opt, obs, program]() { auto qpu = xacc::internal_compiler::get_qpu(); opt->appendOption("initial-parameters", parameters); std::vector<double> pvec(parameters, parameters + program->nVariables()); opt->appendOption("initial-parameters", pvec); auto algo = xacc::getAlgorithm(objective, {std::make_pair("optimizer", opt), std::make_pair("observable", obs), std::make_pair("ansatz", program), std::make_pair("accelerator", qpu)}); auto q = qalloc(program->nLogicalBits()); auto q = qalloc(program->nLogicalBits()); auto buffer = q.results(); auto buffer_as_shared = xacc::as_shared_ptr(buffer); xacc::set_verbose(true); algo->execute(buffer_as_shared); delete obs; return q; return buffer; }); delete[] parameters; } } template <> double extract_results<double>(xacc::internal_compiler::qreg& q, const char * key) { return q.results()->operator[](key).as<double>(); xacc::Optimizer *getOptimizer() { if (!xacc::isInitialized()) xacc::Initialize(); return xacc::getOptimizer("nlopt").get(); } // FIXME CLEAN UP OBS xacc::Observable *getObservable(const char *repr) { if (!xacc::isInitialized()) xacc::Initialize(); // auto sptr = xacc::quantum::getObservable("pauli", std::string(repr)); auto obs = new xacc::quantum::PauliOperator(repr); return obs; } template <> double extract_results<double>(xacc::AcceleratorBuffer* q, const char * key) { return q->operator[](key).as<double>(); } template <> double* extract_results<double*>(xacc::internal_compiler::qreg& q, const char * key) { template <> double* extract_results<double*>(xacc::AcceleratorBuffer* q, const char * key) { // we expect that if they ask for double * it is really stored as a vector return q.results()->operator[](key).as<std::vector<double>>().data(); return q->operator[](key).as<std::vector<double>>().data(); } template <> std::vector<double> extract_results<std::vector<double>>(xacc::internal_compiler::qreg& q, const char * key) { template <> std::vector<double> extract_results<std::vector<double>>(xacc::AcceleratorBuffer* q, const char * key) { // we expect that if they ask for double * it is really stored as a vector return q.results()->operator[](key).as<std::vector<double>>(); return q->operator[](key).as<std::vector<double>>(); } } No newline at end of file runtime/qcor_version2.hpp +48 −17 Original line number Diff line number Diff line Loading @@ -4,8 +4,10 @@ #include "qalloc.hpp" #include "xacc_internal_compiler.hpp" #include <future> #include <vector> #define __qpu__ [[clang::syntax(xasm)]] // Forward declare xacc types to speed up compile times namespace xacc { class Optimizer; class Observable; Loading @@ -14,25 +16,34 @@ class CompositeInstruction; } // namespace xacc namespace qcor { xacc::Optimizer *getOptimizer(); xacc::Observable *getObservable(const char *repr); using Handle = std::future<xacc::AcceleratorBuffer*>; template<typename T> T extract_results(xacc::internal_compiler::qreg& q, const char * key); void initialize(); void finalize(); void constructInitialParameters(std::vector<double>& p) {} namespace __internal__ { std::size_t param_counter = 0; // Helper function for creating a vector of doubles // from a variadic pack of doubles void constructInitialParameters(double * p) {param_counter = 0;} template <typename First, typename... Rest> void constructInitialParameters(std::vector<First> &p, First firstArg, void constructInitialParameters(First *p, First firstArg, Rest... rest) { p.push_back(firstArg); p[param_counter] = firstArg; param_counter++; constructInitialParameters(p, rest...); } std::future<xacc::internal_compiler::qreg> // Execute the hybrid variational Algorithm with given name, providing // the required Optimizer and Observable. Handle execute_algorithm(const char *algorithm, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<double>& parameters); xacc::Optimizer *opt, xacc::Observable *obs, double * parameters); // Given a quantum kernel functor, create the xacc // CompositeInstruction representation of it template <typename QuantumKernel, typename... Args> xacc::CompositeInstruction *kernel_as_composite_instruction(QuantumKernel &k, Args... args) { Loading @@ -47,16 +58,36 @@ xacc::CompositeInstruction *kernel_as_composite_instruction(QuantumKernel &k, xacc::internal_compiler::__execute = true; return xacc::internal_compiler::getLastCompiled(); } } // Get the default Optimizer xacc::Optimizer *getOptimizer(); // Get a pauli observable from a string representation xacc::Observable *getObservable(const char *repr); // Helper function for extracting Variant keys from the // underlying resultant AcceleratorBuffer. We specialize // this for certain types (double, std::vector<double>) template<typename T> T extract_results(xacc::AcceleratorBuffer* q, const char * key); // Given some objective function (like VQE) that takes // a parameterized circuit and quantum observable dictating // measurements on that circuit, asynchronously compute // circuit parameters that are optimal with respect to the // return value of the objective function, using the provided // classical optimizer. Clients can provide the initial // circuit parameters (the Args... variadic parameter pack) template <typename QuantumKernel, typename... Args> std::future<xacc::internal_compiler::qreg> taskInitiateWithSyntax(QuantumKernel &kernel, const char *objective, Handle taskInitiate(QuantumKernel &kernel, const char *objective, xacc::Optimizer *opt, xacc::Observable *obs, Args... args) { auto program = kernel_as_composite_instruction(kernel, args...); std::vector<double> parameters; constructInitialParameters(parameters, args...); return execute_algorithm(objective, program, opt, obs, parameters); auto program = __internal__::kernel_as_composite_instruction(kernel, args...); double * parameters = new double[sizeof...(args)]; __internal__::constructInitialParameters(parameters, args...); auto handle = __internal__::execute_algorithm(objective, program, opt, obs, parameters); return handle; } } #endif No newline at end of file Loading
handlers/xasm/xasm_syntax_handler.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -139,7 +139,7 @@ public: OS << "}\n"; std::cout << "HELLO:\n" << OS.str() << "\n"; // std::cout << "HELLO:\n" << OS.str() << "\n"; } void AddToPredefines(llvm::raw_string_ostream &OS) override { Loading
runtime/qcor_version2.cpp +37 −25 Original line number Diff line number Diff line Loading @@ -2,50 +2,62 @@ #include "xacc.hpp" #include "PauliOperator.hpp" #include "Optimizer.hpp" #include <future> namespace qcor { xacc::Optimizer *getOptimizer() { if (!xacc::isInitialized()) xacc::Initialize(); return xacc::getOptimizer("nlopt").get(); } xacc::Observable *getObservable(const char *repr) { if (!xacc::isInitialized()) xacc::Initialize(); // auto sptr = xacc::quantum::getObservable("pauli", std::string(repr)); auto obs = new xacc::quantum::PauliOperator(repr); return obs; void initialize() { xacc::Initialize(); } void finalize() { xacc::Finalize(); } xacc::AcceleratorBuffer* sync(Handle& h) {return h.get();} std::future<xacc::internal_compiler::qreg> execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<double>& parameters) { namespace __internal__ { Handle execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, double * parameters) { return std::async(std::launch::async, [parameters, objective, opt, obs, program]() { auto qpu = xacc::internal_compiler::get_qpu(); opt->appendOption("initial-parameters", parameters); std::vector<double> pvec(parameters, parameters + program->nVariables()); opt->appendOption("initial-parameters", pvec); auto algo = xacc::getAlgorithm(objective, {std::make_pair("optimizer", opt), std::make_pair("observable", obs), std::make_pair("ansatz", program), std::make_pair("accelerator", qpu)}); auto q = qalloc(program->nLogicalBits()); auto q = qalloc(program->nLogicalBits()); auto buffer = q.results(); auto buffer_as_shared = xacc::as_shared_ptr(buffer); xacc::set_verbose(true); algo->execute(buffer_as_shared); delete obs; return q; return buffer; }); delete[] parameters; } } template <> double extract_results<double>(xacc::internal_compiler::qreg& q, const char * key) { return q.results()->operator[](key).as<double>(); xacc::Optimizer *getOptimizer() { if (!xacc::isInitialized()) xacc::Initialize(); return xacc::getOptimizer("nlopt").get(); } // FIXME CLEAN UP OBS xacc::Observable *getObservable(const char *repr) { if (!xacc::isInitialized()) xacc::Initialize(); // auto sptr = xacc::quantum::getObservable("pauli", std::string(repr)); auto obs = new xacc::quantum::PauliOperator(repr); return obs; } template <> double extract_results<double>(xacc::AcceleratorBuffer* q, const char * key) { return q->operator[](key).as<double>(); } template <> double* extract_results<double*>(xacc::internal_compiler::qreg& q, const char * key) { template <> double* extract_results<double*>(xacc::AcceleratorBuffer* q, const char * key) { // we expect that if they ask for double * it is really stored as a vector return q.results()->operator[](key).as<std::vector<double>>().data(); return q->operator[](key).as<std::vector<double>>().data(); } template <> std::vector<double> extract_results<std::vector<double>>(xacc::internal_compiler::qreg& q, const char * key) { template <> std::vector<double> extract_results<std::vector<double>>(xacc::AcceleratorBuffer* q, const char * key) { // we expect that if they ask for double * it is really stored as a vector return q.results()->operator[](key).as<std::vector<double>>(); return q->operator[](key).as<std::vector<double>>(); } } No newline at end of file
runtime/qcor_version2.hpp +48 −17 Original line number Diff line number Diff line Loading @@ -4,8 +4,10 @@ #include "qalloc.hpp" #include "xacc_internal_compiler.hpp" #include <future> #include <vector> #define __qpu__ [[clang::syntax(xasm)]] // Forward declare xacc types to speed up compile times namespace xacc { class Optimizer; class Observable; Loading @@ -14,25 +16,34 @@ class CompositeInstruction; } // namespace xacc namespace qcor { xacc::Optimizer *getOptimizer(); xacc::Observable *getObservable(const char *repr); using Handle = std::future<xacc::AcceleratorBuffer*>; template<typename T> T extract_results(xacc::internal_compiler::qreg& q, const char * key); void initialize(); void finalize(); void constructInitialParameters(std::vector<double>& p) {} namespace __internal__ { std::size_t param_counter = 0; // Helper function for creating a vector of doubles // from a variadic pack of doubles void constructInitialParameters(double * p) {param_counter = 0;} template <typename First, typename... Rest> void constructInitialParameters(std::vector<First> &p, First firstArg, void constructInitialParameters(First *p, First firstArg, Rest... rest) { p.push_back(firstArg); p[param_counter] = firstArg; param_counter++; constructInitialParameters(p, rest...); } std::future<xacc::internal_compiler::qreg> // Execute the hybrid variational Algorithm with given name, providing // the required Optimizer and Observable. Handle execute_algorithm(const char *algorithm, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<double>& parameters); xacc::Optimizer *opt, xacc::Observable *obs, double * parameters); // Given a quantum kernel functor, create the xacc // CompositeInstruction representation of it template <typename QuantumKernel, typename... Args> xacc::CompositeInstruction *kernel_as_composite_instruction(QuantumKernel &k, Args... args) { Loading @@ -47,16 +58,36 @@ xacc::CompositeInstruction *kernel_as_composite_instruction(QuantumKernel &k, xacc::internal_compiler::__execute = true; return xacc::internal_compiler::getLastCompiled(); } } // Get the default Optimizer xacc::Optimizer *getOptimizer(); // Get a pauli observable from a string representation xacc::Observable *getObservable(const char *repr); // Helper function for extracting Variant keys from the // underlying resultant AcceleratorBuffer. We specialize // this for certain types (double, std::vector<double>) template<typename T> T extract_results(xacc::AcceleratorBuffer* q, const char * key); // Given some objective function (like VQE) that takes // a parameterized circuit and quantum observable dictating // measurements on that circuit, asynchronously compute // circuit parameters that are optimal with respect to the // return value of the objective function, using the provided // classical optimizer. Clients can provide the initial // circuit parameters (the Args... variadic parameter pack) template <typename QuantumKernel, typename... Args> std::future<xacc::internal_compiler::qreg> taskInitiateWithSyntax(QuantumKernel &kernel, const char *objective, Handle taskInitiate(QuantumKernel &kernel, const char *objective, xacc::Optimizer *opt, xacc::Observable *obs, Args... args) { auto program = kernel_as_composite_instruction(kernel, args...); std::vector<double> parameters; constructInitialParameters(parameters, args...); return execute_algorithm(objective, program, opt, obs, parameters); auto program = __internal__::kernel_as_composite_instruction(kernel, args...); double * parameters = new double[sizeof...(args)]; __internal__::constructInitialParameters(parameters, args...); auto handle = __internal__::execute_algorithm(objective, program, opt, obs, parameters); return handle; } } #endif No newline at end of file