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 +27 −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 std::string opstr = "(0.1202,0) Z0 Z1 + (0.168336,0) Z0 Z2 + (0.1202,0) Z2 Z3 + (0.17028,0) Z2 + (0.17028,0) Z0 + (0.165607,0) Z0 Z3 + (0.0454063,0) Y0 Y1 X2 X3 + (-0.106477,0) + (-0.220041,0) Z3 + (0.174073,0) Z1 Z3 + (0.0454063,0) Y0 Y1 Y2 Y3 + (-0.220041,0) Z1 + (0.165607,0) Z1 Z2 + (0.0454063,0) X0 X1 Y2 Y3 + (0.0454063,0) X0 X1 X2 X3"; auto H = qcor::createObservable(opstr); // optimizer auto optimizer = qcor::createOptimizer("nlopt"); // Create ADAPT-VQE instance // Run H2 with the singlet-adapted-uccsd pool qcor::ADAPT adapt(initial_state, H, 2, "singlet-adapted-uccsd", "vqe", optimizer); // 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 +44 −1 Original line number Diff line number Diff line Loading @@ -468,7 +468,50 @@ public: } }; // namespace qcor // Next, add Adapt void execute_adapt(qreg q, const HeterogeneousMap &&m); template <typename... KernelArgs> class ADAPT { protected: std::shared_ptr<Observable> observable; void *state_prep_ptr; const std::string pool, subAlgo; const int nElectrons; std::shared_ptr<Optimizer> optimizer; // Register of qubits to operate on qreg q; public: ADAPT(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), std::shared_ptr<Observable> obs, const int _ne, const std::string _pool, const std::string _subAlgo, std::shared_ptr<Optimizer> opt) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), nElectrons(_ne), pool(_pool), subAlgo(_subAlgo), optimizer(opt) { 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(); execute_adapt(q, {{"n-electrons", nElectrons}, {"pool", pool}, {"initial-state", parent_composite}, {"optimizer", optimizer}, {"sub-algorithm", subAlgo}, {"accelerator", accelerator}, {"observable", observable}}); 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 +27 −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 std::string opstr = "(0.1202,0) Z0 Z1 + (0.168336,0) Z0 Z2 + (0.1202,0) Z2 Z3 + (0.17028,0) Z2 + (0.17028,0) Z0 + (0.165607,0) Z0 Z3 + (0.0454063,0) Y0 Y1 X2 X3 + (-0.106477,0) + (-0.220041,0) Z3 + (0.174073,0) Z1 Z3 + (0.0454063,0) Y0 Y1 Y2 Y3 + (-0.220041,0) Z1 + (0.165607,0) Z1 Z2 + (0.0454063,0) X0 X1 Y2 Y3 + (0.0454063,0) X0 X1 X2 X3"; auto H = qcor::createObservable(opstr); // optimizer auto optimizer = qcor::createOptimizer("nlopt"); // Create ADAPT-VQE instance // Run H2 with the singlet-adapted-uccsd pool qcor::ADAPT adapt(initial_state, H, 2, "singlet-adapted-uccsd", "vqe", optimizer); // 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 +44 −1 Original line number Diff line number Diff line Loading @@ -468,7 +468,50 @@ public: } }; // namespace qcor // Next, add Adapt void execute_adapt(qreg q, const HeterogeneousMap &&m); template <typename... KernelArgs> class ADAPT { protected: std::shared_ptr<Observable> observable; void *state_prep_ptr; const std::string pool, subAlgo; const int nElectrons; std::shared_ptr<Optimizer> optimizer; // Register of qubits to operate on qreg q; public: ADAPT(void (*state_prep_kernel)(std::shared_ptr<CompositeInstruction>, qreg, KernelArgs...), std::shared_ptr<Observable> obs, const int _ne, const std::string _pool, const std::string _subAlgo, std::shared_ptr<Optimizer> opt) : state_prep_ptr(reinterpret_cast<void *>(state_prep_kernel)), observable(obs), nElectrons(_ne), pool(_pool), subAlgo(_subAlgo), optimizer(opt) { 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(); execute_adapt(q, {{"n-electrons", nElectrons}, {"pool", pool}, {"initial-state", parent_composite}, {"optimizer", optimizer}, {"sub-algorithm", subAlgo}, {"accelerator", accelerator}, {"observable", observable}}); return q.results()->getInformation("opt-val").template as<double>(); } }; void execute_qite(qreg q, const HeterogeneousMap &&m); // Next, QITE Loading