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/deuteron/deuteron_exp_inst.cpp +9 −23 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta, std::shared_ptr<xacc::Observable> H) { __qpu__ void ansatz(qreg q, double theta) { X(q[0]); exp_i_theta(q, theta, H); auto exponent_op = X(0) * Y(1) - Y(0) * X(1); exp_i_theta(q, theta, exponent_op); } int main(int argc, char **argv) { Loading @@ -10,31 +10,17 @@ int main(int argc, char **argv) { auto q = qalloc(2); // Create the Deuteron Hamiltonian auto H = qcor::createObservable( auto H = createObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Create the Ansatz exponent Operator auto ansatz_exponent = qcor::createObservable("X0 Y1 - Y0 X1"); // Create an objective function to optimize // Each call evaluates E(theta) = <ansatz(theta) | H | ansatz(theta)> qcor::OptFunction opt_func( [&](const std::vector<double> &x, std::vector<double> &grad) -> double { // Affect Observable observation and evaluate at given ansatz parameters auto e = qcor::observe(ansatz, H, q, x[0], ansatz_exponent); // Need to clean the current qubit register q.reset(); return e; }, 1); // Create the objective function auto objective = createObjectiveFunction(ansatz, H, q, 1); // Create a qcor Optimizer auto optimizer = qcor::createOptimizer("nlopt"); auto optimizer = createOptimizer("nlopt"); // Optimize the above function auto result = optimizer->optimize(opt_func); auto result = optimizer->optimize(*objective.get()); // Print the result printf("energy = %f\n", result.first); Loading examples/deuteron/deuteron_task_initiate.cpp +8 −16 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Loading @@ -10,31 +9,24 @@ int main(int argc, char **argv) { // Allocate 2 qubits auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = 5.907 - 2.1433 * qcor::X(0) * qcor::X(1) - 2.1433 * qcor::Y(0) * qcor::Y(1) + .21829 * qcor::Z(0) - 6.125 * qcor::Z(1); // Create the Deuteron Hamiltonian auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Create the ObjectiveFunction auto objective = qcor::createObjectiveFunction(ansatz, H, q, 1); // Create the Optimizer auto optimizer = qcor::createOptimizer("nlopt"); auto optimizer = createOptimizer("nlopt"); // Call taskInitiate, kick off optimization of the give // functor dependent on the ObjectiveFunction, async call auto handle = qcor::taskInitiate( objective, optimizer, [&](const std::vector<double> x, std::vector<double> &dx) { return (*objective)(q, x[0]); }, 1); auto handle = taskInitiate(objective, optimizer); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); auto results = sync(handle); // Print the optimal value. printf("<H> = %f\n", results.opt_val); Loading examples/hybrid/adapt_h2.cpp 0 → 100644 +39 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include "qcor_utils.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 ADAPT adapt(initial_state, H, optimizer, {{"sub-algorithm", "vqe"}, {"pool", "singlet-adapted-uccsd"}, {"n-electrons", 2}, {"gradient_strategy", "central"}}); // Execute! auto energy = adapt.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; } examples/hybrid/deuteron_h2_qaoa.cpp +0 −5 Original line number Diff line number Diff line Loading @@ -27,11 +27,6 @@ int main() { // Create the QAOA instance QAOA qaoa(H, ref_ham, steps); // we're using a gradient-based optimizer, // by default QAOA will use parameter-shift-rule, // here we set the scale factor. qaoa.set_parameter_shift_scale_factor(.1); // Execute synchronously and display const auto [energy, params] = qaoa.execute(lbfgs); 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/deuteron/deuteron_exp_inst.cpp +9 −23 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta, std::shared_ptr<xacc::Observable> H) { __qpu__ void ansatz(qreg q, double theta) { X(q[0]); exp_i_theta(q, theta, H); auto exponent_op = X(0) * Y(1) - Y(0) * X(1); exp_i_theta(q, theta, exponent_op); } int main(int argc, char **argv) { Loading @@ -10,31 +10,17 @@ int main(int argc, char **argv) { auto q = qalloc(2); // Create the Deuteron Hamiltonian auto H = qcor::createObservable( auto H = createObservable( "5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1"); // Create the Ansatz exponent Operator auto ansatz_exponent = qcor::createObservable("X0 Y1 - Y0 X1"); // Create an objective function to optimize // Each call evaluates E(theta) = <ansatz(theta) | H | ansatz(theta)> qcor::OptFunction opt_func( [&](const std::vector<double> &x, std::vector<double> &grad) -> double { // Affect Observable observation and evaluate at given ansatz parameters auto e = qcor::observe(ansatz, H, q, x[0], ansatz_exponent); // Need to clean the current qubit register q.reset(); return e; }, 1); // Create the objective function auto objective = createObjectiveFunction(ansatz, H, q, 1); // Create a qcor Optimizer auto optimizer = qcor::createOptimizer("nlopt"); auto optimizer = createOptimizer("nlopt"); // Optimize the above function auto result = optimizer->optimize(opt_func); auto result = optimizer->optimize(*objective.get()); // Print the result printf("energy = %f\n", result.first); Loading
examples/deuteron/deuteron_task_initiate.cpp +8 −16 Original line number Diff line number Diff line #include "qcor.hpp" __qpu__ void ansatz(qreg q, double theta) { X(q[0]); Loading @@ -10,31 +9,24 @@ int main(int argc, char **argv) { // Allocate 2 qubits auto q = qalloc(2); // Create the Deuteron Hamiltonian (Observable) auto H = 5.907 - 2.1433 * qcor::X(0) * qcor::X(1) - 2.1433 * qcor::Y(0) * qcor::Y(1) + .21829 * qcor::Z(0) - 6.125 * qcor::Z(1); // Create the Deuteron Hamiltonian auto H = 5.907 - 2.1433 * X(0) * X(1) - 2.1433 * Y(0) * Y(1) + .21829 * Z(0) - 6.125 * Z(1); // Create the ObjectiveFunction, here we want to run VQE // need to provide ansatz and the Observable auto objective = qcor::createObjectiveFunction("vqe", ansatz, H); // Create the ObjectiveFunction auto objective = qcor::createObjectiveFunction(ansatz, H, q, 1); // Create the Optimizer auto optimizer = qcor::createOptimizer("nlopt"); auto optimizer = createOptimizer("nlopt"); // Call taskInitiate, kick off optimization of the give // functor dependent on the ObjectiveFunction, async call auto handle = qcor::taskInitiate( objective, optimizer, [&](const std::vector<double> x, std::vector<double> &dx) { return (*objective)(q, x[0]); }, 1); auto handle = taskInitiate(objective, optimizer); // Go do other work... // Query results when ready. auto results = qcor::sync(handle); auto results = sync(handle); // Print the optimal value. printf("<H> = %f\n", results.opt_val); Loading
examples/hybrid/adapt_h2.cpp 0 → 100644 +39 −0 Original line number Diff line number Diff line #include "qcor_hybrid.hpp" #include "qcor_utils.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 ADAPT adapt(initial_state, H, optimizer, {{"sub-algorithm", "vqe"}, {"pool", "singlet-adapted-uccsd"}, {"n-electrons", 2}, {"gradient_strategy", "central"}}); // Execute! auto energy = adapt.execute(); // Print the energy std::cout << std::setprecision(12) << energy << "\n"; }
examples/hybrid/deuteron_h2_qaoa.cpp +0 −5 Original line number Diff line number Diff line Loading @@ -27,11 +27,6 @@ int main() { // Create the QAOA instance QAOA qaoa(H, ref_ham, steps); // we're using a gradient-based optimizer, // by default QAOA will use parameter-shift-rule, // here we set the scale factor. qaoa.set_parameter_shift_scale_factor(.1); // Execute synchronously and display const auto [energy, params] = qaoa.execute(lbfgs); Loading