Loading examples/CMakeLists.txt +1 −1 Original line number Diff line number Diff line Loading @@ -10,4 +10,4 @@ add_test(NAME qrt_qpe_example COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURR add_test(NAME qrt_kernel_include COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURRENT_SOURCE_DIR}/shared ${CMAKE_CURRENT_SOURCE_DIR}/simple/multiple_kernels.cpp) add_test(NAME qrt_period_finding COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURRENT_SOURCE_DIR}/shared ${CMAKE_CURRENT_SOURCE_DIR}/simple/period_finding.cpp) add_test(NAME qrt_grover COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/grover/grover.cpp) add_test(NAME qrt_adapt COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/hybrid/adapt_h2.cpp) No newline at end of file examples/hybrid/adapt_h2.cpp 0 → 100644 +42 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include <iomanip> // Define the state preparation kernel __qpu__ void initial_state(qreg q) { X(q[0]); X(q[2]); } int main() { // Define the Hamiltonian using the QCOR API auto H = 0.1202 * Z(0) * Z(1) + 0.168336 * Z(0) * Z(2) + 0.1202 * Z(2) * Z(3) + 0.17028 * Z(2) + 0.17028 * Z(0) + 0.165607 * Z(0) * Z(3) + 0.0454063 * Y(0) * Y(1) * X(2) * X(3) - 0.106477 - 0.220041 * Z(3) + 0.174073 * Z(1) * Z(3) + 0.0454063 * Y(0) * Y(1) * Y(2) * Y(3) - 0.220041 * Z(1) + 0.165607 * Z(1) * Z(2) + 0.0454063 * X(0) * X(1) * Y(2) * Y(3) + 0.0454063 * X(0) * X(1) * X(2) * X(3); // optimizer auto optimizer = qcor::createOptimizer( "nlopt", {std::make_pair("nlopt-optimizer", "l-bfgs")}); // Create ADAPT-VQE instance // Run H2 with the singlet-adapted-uccsd pool int nElectrons = 2; std::string pool = "singlet-adapted-uccsd"; std::string subAlgo = "vqe"; std::string gradStrategy = "central-difference-gradient"; qcor::ADAPT adapt(initial_state, H, optimizer, {{"sub-algorithm", subAlgo}, {"pool", pool}, {"n-electrons", nElectrons}, {"gradient_strategy", gradStrategy}}); // Execute! auto energy = adapt.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; } lib/hybrid/qcor_hybrid.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -35,4 +35,9 @@ void execute_qite(qreg q, const HeterogeneousMap &&m) { qite->execute(xacc::as_shared_ptr(q.results())); } void execute_adapt(qreg q, const HeterogeneousMap &&m) { auto adapt = xacc::getAlgorithm("adapt", m); adapt->execute(xacc::as_shared_ptr(q.results())); } } // namespace qcor No newline at end of file lib/hybrid/qcor_hybrid.hpp +43 −1 Original line number Diff line number Diff line Loading @@ -468,7 +468,49 @@ public: } }; // namespace qcor // Next, add Adapt void execute_adapt(qreg q, const HeterogeneousMap &&m); template <typename... KernelArgs> class ADAPT { protected: Observable &observable; void *state_prep_ptr; std::shared_ptr<Optimizer> optimizer; HeterogeneousMap options; // Register of qubits to operate on qreg q; public: ADAPT(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), Observable &obs, std::shared_ptr<Optimizer> opt, HeterogeneousMap _options) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), optimizer(opt), options(_options) { 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("adapt_composite"); state_prep_casted(parent_composite, q, initial_args...); // parent_composite now has the circuit in it auto accelerator = xacc::internal_compiler::get_qpu(); options.insert("initial-state", parent_composite); options.insert("observable", &observable); options.insert("optimizer", optimizer); options.insert("accelerator", accelerator); execute_adapt(q, std::move(options)); return q.results()->getInformation("opt-val").template as<double>(); } }; void execute_qite(qreg q, const HeterogeneousMap &&m); // Next, QITE Loading Loading
examples/CMakeLists.txt +1 −1 Original line number Diff line number Diff line Loading @@ -10,4 +10,4 @@ add_test(NAME qrt_qpe_example COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURR add_test(NAME qrt_kernel_include COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURRENT_SOURCE_DIR}/shared ${CMAKE_CURRENT_SOURCE_DIR}/simple/multiple_kernels.cpp) add_test(NAME qrt_period_finding COMMAND ${CMAKE_BINARY_DIR}/qcor -c -I${CMAKE_CURRENT_SOURCE_DIR}/shared ${CMAKE_CURRENT_SOURCE_DIR}/simple/period_finding.cpp) add_test(NAME qrt_grover COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/grover/grover.cpp) add_test(NAME qrt_adapt COMMAND ${CMAKE_BINARY_DIR}/qcor -c ${CMAKE_CURRENT_SOURCE_DIR}/hybrid/adapt_h2.cpp) No newline at end of file
examples/hybrid/adapt_h2.cpp 0 → 100644 +42 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include <iomanip> // Define the state preparation kernel __qpu__ void initial_state(qreg q) { X(q[0]); X(q[2]); } int main() { // Define the Hamiltonian using the QCOR API auto H = 0.1202 * Z(0) * Z(1) + 0.168336 * Z(0) * Z(2) + 0.1202 * Z(2) * Z(3) + 0.17028 * Z(2) + 0.17028 * Z(0) + 0.165607 * Z(0) * Z(3) + 0.0454063 * Y(0) * Y(1) * X(2) * X(3) - 0.106477 - 0.220041 * Z(3) + 0.174073 * Z(1) * Z(3) + 0.0454063 * Y(0) * Y(1) * Y(2) * Y(3) - 0.220041 * Z(1) + 0.165607 * Z(1) * Z(2) + 0.0454063 * X(0) * X(1) * Y(2) * Y(3) + 0.0454063 * X(0) * X(1) * X(2) * X(3); // optimizer auto optimizer = qcor::createOptimizer( "nlopt", {std::make_pair("nlopt-optimizer", "l-bfgs")}); // Create ADAPT-VQE instance // Run H2 with the singlet-adapted-uccsd pool int nElectrons = 2; std::string pool = "singlet-adapted-uccsd"; std::string subAlgo = "vqe"; std::string gradStrategy = "central-difference-gradient"; qcor::ADAPT adapt(initial_state, H, optimizer, {{"sub-algorithm", subAlgo}, {"pool", pool}, {"n-electrons", nElectrons}, {"gradient_strategy", gradStrategy}}); // Execute! auto energy = adapt.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; }
lib/hybrid/qcor_hybrid.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -35,4 +35,9 @@ void execute_qite(qreg q, const HeterogeneousMap &&m) { qite->execute(xacc::as_shared_ptr(q.results())); } void execute_adapt(qreg q, const HeterogeneousMap &&m) { auto adapt = xacc::getAlgorithm("adapt", m); adapt->execute(xacc::as_shared_ptr(q.results())); } } // namespace qcor No newline at end of file
lib/hybrid/qcor_hybrid.hpp +43 −1 Original line number Diff line number Diff line Loading @@ -468,7 +468,49 @@ public: } }; // namespace qcor // Next, add Adapt void execute_adapt(qreg q, const HeterogeneousMap &&m); template <typename... KernelArgs> class ADAPT { protected: Observable &observable; void *state_prep_ptr; std::shared_ptr<Optimizer> optimizer; HeterogeneousMap options; // Register of qubits to operate on qreg q; public: ADAPT(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), Observable &obs, std::shared_ptr<Optimizer> opt, HeterogeneousMap _options) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), optimizer(opt), options(_options) { 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("adapt_composite"); state_prep_casted(parent_composite, q, initial_args...); // parent_composite now has the circuit in it auto accelerator = xacc::internal_compiler::get_qpu(); options.insert("initial-state", parent_composite); options.insert("observable", &observable); options.insert("optimizer", optimizer); options.insert("accelerator", accelerator); execute_adapt(q, std::move(options)); return q.results()->getInformation("opt-val").template as<double>(); } }; void execute_qite(qreg q, const HeterogeneousMap &&m); // Next, QITE Loading