Loading handlers/examples/deuteronH2.cpp 0 → 100644 +29 −0 Original line number Diff line number Diff line #include "qcor_version2.hpp" [[clang::syntax(xasm)]] void ansatz(qreg q, double t) { X(q[0]); Ry(q[1], t); CX(q[1], q[0]); } int main(int argc, char **argv) { auto opt = qcor::getOptimizer(); auto obs = qcor::getObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Schedule an asynchronous VQE execution // with the given quantum kernel ansatz auto handle = qcor::taskInitiateWithSyntax(ansatz, "vqe", opt, obs, 0.45); auto results_buffer = handle.get(); auto energy = qcor::extract_results<double>(results_buffer, "opt-val"); auto angles = qcor::extract_results<std::vector<double>>(results_buffer, "opt-params"); printf("energy = %f\n", energy); printf("angles = ["); for (int i = 0; i < 1; i++) printf("%f ", angles[i]); printf("]\n"); } handlers/staq/examples/add_3_5.cpp +34 −1 Original line number Diff line number Diff line #include <qalloc> [[clang::syntax(staq)]] void add_3_5(qreg a, qreg b, qreg c) { oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "add_3_5.v" } oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { module top (\a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3],\c[0],\c[1],\c[2],\c[3]); input \a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3]; output \c[0],\c[1],\c[2],\c[3]; wire n386,n387,n388,n389,n390,n391,n392,n393,n394,n395,n396,n397,n398,n399,n400,n401,n402,n403,n404,n405,n406,n407,n408,n409,n410 ; assign n386 = \a[0] & ~\b[0] ; assign n387 = ~\a[0] & \b[0] ; assign \c[0] = n386 | n387; assign n389 = \a[0] & \b[0] ; assign n390 = ~\a[1] & ~\b[1] ; assign n391 = \a[1] & \b[1] ; assign n392 = ~n390 & ~n391; assign n393 = n389 & ~n392; assign n394 = ~n389 & n392; assign \c[1] = n393 | n394; assign n396 = n389 & ~n390; assign n397 = ~n391 & ~n396; assign n398 = ~\a[2] & ~\b[2] ; assign n399 = \a[2] & \b[2] ; assign n400 = ~n398 & ~n399; assign n401 = n397 & ~n400; assign n402 = ~n397 & n400; assign \c[2] = ~n401 & ~n402; assign n404 = ~n397 & ~n398; assign n405 = ~n399 & ~n404; assign n406 = ~\a[3] & ~\b[3] ; assign n407 = \a[3] & \b[3] ; assign n408 = ~n406 & ~n407; assign n409 = n405 & ~n408; assign n410 = ~n405 & n408; assign \c[3] = ~n409 & ~n410; endmodule } creg result[4]; // a = 3 x a[0]; Loading handlers/xasm/xasm_syntax_handler.cpp +5 −2 Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ public: OS << "}\n"; // OS << "optimize(program);\n"; OS << "if (__execute) {\n"; if (!program_parameters.empty()) { OS << "double * p = new double[" << program_parameters.size() << "];\n"; for (unsigned int i = 0; i < program_parameters.size(); i++) { Loading @@ -136,7 +137,9 @@ public: OS << "delete[] p;\n"; } // std::cout << "HELLO:\n" << OS.str() << "\n"; OS << "}\n"; std::cout << "HELLO:\n" << OS.str() << "\n"; } void AddToPredefines(llvm::raw_string_ostream &OS) override { Loading runtime/CMakeLists.txt +1 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc PRIVATE xacc::quantum_ga xacc_configure_library_rpath(${LIBRARY_NAME}) file(GLOB HEADERS qcor.hpp) file(GLOB HEADERS qcor.hpp qcor_version2.hpp) install(FILES ${HEADERS} DESTINATION include/qcor) install(TARGETS ${LIBRARY_NAME} DESTINATION lib) Loading runtime/qcor_version2.cpp 0 → 100644 +51 −0 Original line number Diff line number Diff line #include "qcor_version2.hpp" #include "xacc.hpp" #include "PauliOperator.hpp" #include "Optimizer.hpp" 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; } std::future<xacc::internal_compiler::qreg> execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<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); 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 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; }); } template <> double extract_results<double>(xacc::internal_compiler::qreg& q, const char * key) { return q.results()->operator[](key).as<double>(); } template <> double* extract_results<double*>(xacc::internal_compiler::qreg& 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(); } template <> std::vector<double> extract_results<std::vector<double>>(xacc::internal_compiler::qreg& 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>>(); } } No newline at end of file Loading
handlers/examples/deuteronH2.cpp 0 → 100644 +29 −0 Original line number Diff line number Diff line #include "qcor_version2.hpp" [[clang::syntax(xasm)]] void ansatz(qreg q, double t) { X(q[0]); Ry(q[1], t); CX(q[1], q[0]); } int main(int argc, char **argv) { auto opt = qcor::getOptimizer(); auto obs = qcor::getObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Schedule an asynchronous VQE execution // with the given quantum kernel ansatz auto handle = qcor::taskInitiateWithSyntax(ansatz, "vqe", opt, obs, 0.45); auto results_buffer = handle.get(); auto energy = qcor::extract_results<double>(results_buffer, "opt-val"); auto angles = qcor::extract_results<std::vector<double>>(results_buffer, "opt-params"); printf("energy = %f\n", energy); printf("angles = ["); for (int i = 0; i < 1; i++) printf("%f ", angles[i]); printf("]\n"); }
handlers/staq/examples/add_3_5.cpp +34 −1 Original line number Diff line number Diff line #include <qalloc> [[clang::syntax(staq)]] void add_3_5(qreg a, qreg b, qreg c) { oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "add_3_5.v" } oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { module top (\a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3],\c[0],\c[1],\c[2],\c[3]); input \a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3]; output \c[0],\c[1],\c[2],\c[3]; wire n386,n387,n388,n389,n390,n391,n392,n393,n394,n395,n396,n397,n398,n399,n400,n401,n402,n403,n404,n405,n406,n407,n408,n409,n410 ; assign n386 = \a[0] & ~\b[0] ; assign n387 = ~\a[0] & \b[0] ; assign \c[0] = n386 | n387; assign n389 = \a[0] & \b[0] ; assign n390 = ~\a[1] & ~\b[1] ; assign n391 = \a[1] & \b[1] ; assign n392 = ~n390 & ~n391; assign n393 = n389 & ~n392; assign n394 = ~n389 & n392; assign \c[1] = n393 | n394; assign n396 = n389 & ~n390; assign n397 = ~n391 & ~n396; assign n398 = ~\a[2] & ~\b[2] ; assign n399 = \a[2] & \b[2] ; assign n400 = ~n398 & ~n399; assign n401 = n397 & ~n400; assign n402 = ~n397 & n400; assign \c[2] = ~n401 & ~n402; assign n404 = ~n397 & ~n398; assign n405 = ~n399 & ~n404; assign n406 = ~\a[3] & ~\b[3] ; assign n407 = \a[3] & \b[3] ; assign n408 = ~n406 & ~n407; assign n409 = n405 & ~n408; assign n410 = ~n405 & n408; assign \c[3] = ~n409 & ~n410; endmodule } creg result[4]; // a = 3 x a[0]; Loading
handlers/xasm/xasm_syntax_handler.cpp +5 −2 Original line number Diff line number Diff line Loading @@ -111,6 +111,7 @@ public: OS << "}\n"; // OS << "optimize(program);\n"; OS << "if (__execute) {\n"; if (!program_parameters.empty()) { OS << "double * p = new double[" << program_parameters.size() << "];\n"; for (unsigned int i = 0; i < program_parameters.size(); i++) { Loading @@ -136,7 +137,9 @@ public: OS << "delete[] p;\n"; } // std::cout << "HELLO:\n" << OS.str() << "\n"; OS << "}\n"; std::cout << "HELLO:\n" << OS.str() << "\n"; } void AddToPredefines(llvm::raw_string_ostream &OS) override { Loading
runtime/CMakeLists.txt +1 −1 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ target_link_libraries(${LIBRARY_NAME} PUBLIC xacc::xacc PRIVATE xacc::quantum_ga xacc_configure_library_rpath(${LIBRARY_NAME}) file(GLOB HEADERS qcor.hpp) file(GLOB HEADERS qcor.hpp qcor_version2.hpp) install(FILES ${HEADERS} DESTINATION include/qcor) install(TARGETS ${LIBRARY_NAME} DESTINATION lib) Loading
runtime/qcor_version2.cpp 0 → 100644 +51 −0 Original line number Diff line number Diff line #include "qcor_version2.hpp" #include "xacc.hpp" #include "PauliOperator.hpp" #include "Optimizer.hpp" 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; } std::future<xacc::internal_compiler::qreg> execute_algorithm(const char *objective, xacc::CompositeInstruction *program, xacc::Optimizer *opt, xacc::Observable *obs, std::vector<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); 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 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; }); } template <> double extract_results<double>(xacc::internal_compiler::qreg& q, const char * key) { return q.results()->operator[](key).as<double>(); } template <> double* extract_results<double*>(xacc::internal_compiler::qreg& 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(); } template <> std::vector<double> extract_results<std::vector<double>>(xacc::internal_compiler::qreg& 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>>(); } } No newline at end of file