Loading examples/xasm/objective_from_composite.cpp 0 → 100644 +32 −0 Original line number Diff line number Diff line #include "qcor.hpp" const std::string src = R"(__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) { // When manually writing kernel source strings // you have to specify the backend you are targeting qcor::set_backend("tnqvm"); // 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"); // JIT compile the ansatz auto ansatz = qcor::compile(src); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Evaluate the ObjectiveFunction at a specified set of parameters auto energy = (*objective)(q, .59); printf("vqe-energy = %f\n", energy); } runtime/qcor.cpp +12 −8 Original line number Diff line number Diff line Loading @@ -3,8 +3,8 @@ #include "Optimizer.hpp" #include "xacc.hpp" #include "xacc_service.hpp" #include "xacc_quantum_gate_api.hpp" #include "xacc_service.hpp" #include "qalloc.hpp" Loading Loading @@ -42,7 +42,8 @@ double observe(xacc::CompositeInstruction* program, } } // namespace __internal__ std::shared_ptr<xacc::Optimizer> createOptimizer(const char * type, HeterogeneousMap&& options) { std::shared_ptr<xacc::Optimizer> createOptimizer(const char *type, HeterogeneousMap &&options) { if (!xacc::isInitialized()) xacc::internal_compiler::compiler_InitializeXACC(); return xacc::getOptimizer(type, options); Loading @@ -54,5 +55,8 @@ std::shared_ptr<xacc::Observable> createObservable(const char *repr) { return xacc::quantum::getObservable("pauli", std::string(repr)); } std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src) { return xacc::getCompiler("xasm")->compile(src)->getComposites()[0]; } } // namespace qcor No newline at end of file runtime/qcor.hpp +30 −6 Original line number Diff line number Diff line #ifndef RUNTIME_QCOR_HPP_ #define RUNTIME_QCOR_HPP_ #include <CompositeInstruction.hpp> #include <memory> #include <qalloc> Loading Loading @@ -43,8 +44,7 @@ double observe(xacc::CompositeInstruction *program, // Observe the kernel and return the measured kernels std::vector<std::shared_ptr<xacc::CompositeInstruction>> observe(std::shared_ptr<Observable> obs, xacc::CompositeInstruction *program); observe(std::shared_ptr<Observable> obs, xacc::CompositeInstruction *program); // Get the objective function from the service registry std::shared_ptr<ObjectiveFunction> get_objective(const char *type); Loading Loading @@ -127,13 +127,25 @@ public: auto functor = reinterpret_cast<void (*)(ArgumentTypes...)>(pointer_to_functor); kernel = __internal__::kernel_as_composite_instruction(functor, args...); } if (!qreg.results()) { // this hasn't been set, so set it qreg = std::get<0>(std::forward_as_tuple(args...)); } kernel->updateRuntimeArguments(args...); return operator()(); } }; void set_backend(const std::string &backend) { xacc::internal_compiler::compiler_InitializeXACC(); xacc::internal_compiler::setAccelerator(backend.c_str()); } std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src); // Public observe function, returns expected value of Observable template <typename QuantumKernel, typename... Args> auto observe(QuantumKernel &kernel, std::shared_ptr<Observable> obs, Loading Loading @@ -170,6 +182,18 @@ 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 = {}) { auto obj_func = qcor::__internal__::get_objective(obj_name); obj_func->initialize(observable, kernel.get()); obj_func->set_options(options); return obj_func; } // Create an Objective Function that makes calls to the // provided Quantum Kernel, with measurements dictated by // the provided Observable. Optionally can provide problem-specific Loading Loading
examples/xasm/objective_from_composite.cpp 0 → 100644 +32 −0 Original line number Diff line number Diff line #include "qcor.hpp" const std::string src = R"(__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) { // When manually writing kernel source strings // you have to specify the backend you are targeting qcor::set_backend("tnqvm"); // 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"); // JIT compile the ansatz auto ansatz = qcor::compile(src); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Evaluate the ObjectiveFunction at a specified set of parameters auto energy = (*objective)(q, .59); printf("vqe-energy = %f\n", energy); }
runtime/qcor.cpp +12 −8 Original line number Diff line number Diff line Loading @@ -3,8 +3,8 @@ #include "Optimizer.hpp" #include "xacc.hpp" #include "xacc_service.hpp" #include "xacc_quantum_gate_api.hpp" #include "xacc_service.hpp" #include "qalloc.hpp" Loading Loading @@ -42,7 +42,8 @@ double observe(xacc::CompositeInstruction* program, } } // namespace __internal__ std::shared_ptr<xacc::Optimizer> createOptimizer(const char * type, HeterogeneousMap&& options) { std::shared_ptr<xacc::Optimizer> createOptimizer(const char *type, HeterogeneousMap &&options) { if (!xacc::isInitialized()) xacc::internal_compiler::compiler_InitializeXACC(); return xacc::getOptimizer(type, options); Loading @@ -54,5 +55,8 @@ std::shared_ptr<xacc::Observable> createObservable(const char *repr) { return xacc::quantum::getObservable("pauli", std::string(repr)); } std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src) { return xacc::getCompiler("xasm")->compile(src)->getComposites()[0]; } } // namespace qcor No newline at end of file
runtime/qcor.hpp +30 −6 Original line number Diff line number Diff line #ifndef RUNTIME_QCOR_HPP_ #define RUNTIME_QCOR_HPP_ #include <CompositeInstruction.hpp> #include <memory> #include <qalloc> Loading Loading @@ -43,8 +44,7 @@ double observe(xacc::CompositeInstruction *program, // Observe the kernel and return the measured kernels std::vector<std::shared_ptr<xacc::CompositeInstruction>> observe(std::shared_ptr<Observable> obs, xacc::CompositeInstruction *program); observe(std::shared_ptr<Observable> obs, xacc::CompositeInstruction *program); // Get the objective function from the service registry std::shared_ptr<ObjectiveFunction> get_objective(const char *type); Loading Loading @@ -127,13 +127,25 @@ public: auto functor = reinterpret_cast<void (*)(ArgumentTypes...)>(pointer_to_functor); kernel = __internal__::kernel_as_composite_instruction(functor, args...); } if (!qreg.results()) { // this hasn't been set, so set it qreg = std::get<0>(std::forward_as_tuple(args...)); } kernel->updateRuntimeArguments(args...); return operator()(); } }; void set_backend(const std::string &backend) { xacc::internal_compiler::compiler_InitializeXACC(); xacc::internal_compiler::setAccelerator(backend.c_str()); } std::shared_ptr<xacc::CompositeInstruction> compile(const std::string &src); // Public observe function, returns expected value of Observable template <typename QuantumKernel, typename... Args> auto observe(QuantumKernel &kernel, std::shared_ptr<Observable> obs, Loading Loading @@ -170,6 +182,18 @@ 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 = {}) { auto obj_func = qcor::__internal__::get_objective(obj_name); obj_func->initialize(observable, kernel.get()); obj_func->set_options(options); return obj_func; } // Create an Objective Function that makes calls to the // provided Quantum Kernel, with measurements dictated by // the provided Observable. Optionally can provide problem-specific Loading