Loading examples/hybrid/deuteron_h2_qite.cpp 0 → 100644 +23 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include <iomanip> // Define the state preparation kernel __qpu__ void state_prep(qreg q) { X(q[0]); } int main() { // Define the Hamiltonian using the QCOR API auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1); // Create a QITE instance, give it // the parameterized state_prep functor, // Observable, and n_steps and step_size QITE qite(state_prep, H, 5, .1); // Execute! auto energy = qite.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; } examples/simple/simple-objective-function.cpp +32 −5 Original line number Diff line number Diff line // Note no includes here, we are just // using the language extension // // run this with // qcor -qpu tnqvm simple-objective-function.cpp // ./a.out __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Loading @@ -10,14 +16,35 @@ int main(int argc, char **argv) { auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = qcor::createObservable( auto H = 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); auto objective = createObjectiveFunction("vqe", ansatz, H); // Create the Optimizer. This will give us COBYLA from nlopt auto optimizer = createOptimizer("nlopt"); // Launch the Optimization Task with taskInitiate auto handle = taskInitiate( objective, optimizer, // Provide the ObjectiveFunction and Optimizer // Need to provide a way to map vector<double> x to ansatz(qreg, double) // QCOR provides the TranslationFunctor for this TranslationFunctor<qreg, double>([&](const std::vector<double> x) { return std::make_tuple(q, x[0]); }), // Need to specify number of parameters in x // because qcor has to instantiate that vector 1); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); printf("vqe-energy from taskInitiate = %f\n", results.opt_val); // Evaluate the ObjectiveFunction at a specified set of parameters auto energy = (*objective)(q, .59); printf("vqe-energy = %f\n", energy); auto energy = (*objective)(q, results.opt_params[0]); printf("vqe-energy just evaluating objective function = %f\n", energy); } handlers/token_collector/xasm/xasm_token_collector.cpp +3 −0 Original line number Diff line number Diff line Loading @@ -143,6 +143,9 @@ void XasmTokenCollector::collect(clang::Preprocessor &PP, CommonTokenStream tokens(&lexer); xasm_singleParser parser(&tokens); lexer.removeErrorListeners(); parser.removeErrorListeners(); tree::ParseTree *tree = parser.line(); visitor.visitChildren(tree); Loading lib/hybrid/qcor_hybrid.cpp +7 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ #include "Compiler.hpp" #include "xacc.hpp" #include "xacc_service.hpp" #include <Utils.hpp> namespace qcor { namespace __internal__ { Loading @@ -28,4 +29,10 @@ void QAOA::initial_compile_qaoa_code() { } void QAOA::error(const std::string &message) { xacc::error(message); } void execute_qite(qreg q, const HeterogeneousMap &&m) { auto qite = xacc::getAlgorithm("qite", m); qite->execute(xacc::as_shared_ptr(q.results())); } } // namespace qcor No newline at end of file lib/hybrid/qcor_hybrid.hpp +41 −1 Original line number Diff line number Diff line Loading @@ -2,8 +2,10 @@ #include "AcceleratorBuffer.hpp" #include "qcor.hpp" #include "qcor_utils.hpp" #include "qrt.hpp" #include <memory> #include <xacc_internal_compiler.hpp> namespace qcor { static constexpr double pi = 3.141592653589793238; Loading Loading @@ -468,4 +470,42 @@ public: // Next, add Adapt void execute_qite(qreg q, const HeterogeneousMap &&m); // Next, QITE template <typename... KernelArgs> class QITE { protected: Observable &observable; void *state_prep_ptr; const int n_steps; const double step_size; // Register of qubits to operate on qreg q; public: QITE(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), Observable &obs, const int _n_steps, const double _step_size) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), n_steps(_n_steps), step_size(_step_size) { q = qalloc(obs.nBits()); } double execute(KernelArgs... initial_args) { auto state_prep_casted = reinterpret_cast<void (*)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...)>(state_prep_ptr); auto parent_composite = qcor::__internal__::create_composite("qite_composite"); state_prep_casted(parent_composite, q, initial_args...); // parent_composite now has the circuit in it auto accelerator = xacc::internal_compiler::get_qpu(); execute_qite(q, {{"steps", n_steps}, {"step-size", step_size}, {"ansatz", parent_composite}, {"accelerator", accelerator}, {"observable", &observable}}); return q.results()->getInformation("opt-val").template as<double>(); } }; } // namespace qcor Loading
examples/hybrid/deuteron_h2_qite.cpp 0 → 100644 +23 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include <iomanip> // Define the state preparation kernel __qpu__ void state_prep(qreg q) { X(q[0]); } int main() { // Define the Hamiltonian using the QCOR API auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1); // Create a QITE instance, give it // the parameterized state_prep functor, // Observable, and n_steps and step_size QITE qite(state_prep, H, 5, .1); // Execute! auto energy = qite.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; }
examples/simple/simple-objective-function.cpp +32 −5 Original line number Diff line number Diff line // Note no includes here, we are just // using the language extension // // run this with // qcor -qpu tnqvm simple-objective-function.cpp // ./a.out __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Loading @@ -10,14 +16,35 @@ int main(int argc, char **argv) { auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = qcor::createObservable( auto H = 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); auto objective = createObjectiveFunction("vqe", ansatz, H); // Create the Optimizer. This will give us COBYLA from nlopt auto optimizer = createOptimizer("nlopt"); // Launch the Optimization Task with taskInitiate auto handle = taskInitiate( objective, optimizer, // Provide the ObjectiveFunction and Optimizer // Need to provide a way to map vector<double> x to ansatz(qreg, double) // QCOR provides the TranslationFunctor for this TranslationFunctor<qreg, double>([&](const std::vector<double> x) { return std::make_tuple(q, x[0]); }), // Need to specify number of parameters in x // because qcor has to instantiate that vector 1); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); printf("vqe-energy from taskInitiate = %f\n", results.opt_val); // Evaluate the ObjectiveFunction at a specified set of parameters auto energy = (*objective)(q, .59); printf("vqe-energy = %f\n", energy); auto energy = (*objective)(q, results.opt_params[0]); printf("vqe-energy just evaluating objective function = %f\n", energy); }
handlers/token_collector/xasm/xasm_token_collector.cpp +3 −0 Original line number Diff line number Diff line Loading @@ -143,6 +143,9 @@ void XasmTokenCollector::collect(clang::Preprocessor &PP, CommonTokenStream tokens(&lexer); xasm_singleParser parser(&tokens); lexer.removeErrorListeners(); parser.removeErrorListeners(); tree::ParseTree *tree = parser.line(); visitor.visitChildren(tree); Loading
lib/hybrid/qcor_hybrid.cpp +7 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ #include "Compiler.hpp" #include "xacc.hpp" #include "xacc_service.hpp" #include <Utils.hpp> namespace qcor { namespace __internal__ { Loading @@ -28,4 +29,10 @@ void QAOA::initial_compile_qaoa_code() { } void QAOA::error(const std::string &message) { xacc::error(message); } void execute_qite(qreg q, const HeterogeneousMap &&m) { auto qite = xacc::getAlgorithm("qite", m); qite->execute(xacc::as_shared_ptr(q.results())); } } // namespace qcor No newline at end of file
lib/hybrid/qcor_hybrid.hpp +41 −1 Original line number Diff line number Diff line Loading @@ -2,8 +2,10 @@ #include "AcceleratorBuffer.hpp" #include "qcor.hpp" #include "qcor_utils.hpp" #include "qrt.hpp" #include <memory> #include <xacc_internal_compiler.hpp> namespace qcor { static constexpr double pi = 3.141592653589793238; Loading Loading @@ -468,4 +470,42 @@ public: // Next, add Adapt void execute_qite(qreg q, const HeterogeneousMap &&m); // Next, QITE template <typename... KernelArgs> class QITE { protected: Observable &observable; void *state_prep_ptr; const int n_steps; const double step_size; // Register of qubits to operate on qreg q; public: QITE(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), Observable &obs, const int _n_steps, const double _step_size) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), n_steps(_n_steps), step_size(_step_size) { q = qalloc(obs.nBits()); } double execute(KernelArgs... initial_args) { auto state_prep_casted = reinterpret_cast<void (*)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...)>(state_prep_ptr); auto parent_composite = qcor::__internal__::create_composite("qite_composite"); state_prep_casted(parent_composite, q, initial_args...); // parent_composite now has the circuit in it auto accelerator = xacc::internal_compiler::get_qpu(); execute_qite(q, {{"steps", n_steps}, {"step-size", step_size}, {"ansatz", parent_composite}, {"accelerator", accelerator}, {"observable", &observable}}); return q.results()->getInformation("opt-val").template as<double>(); } }; } // namespace qcor