Skip to content

Commit

Permalink
Add a get_operator_pool function to C++ to mirror the Python
Browse files Browse the repository at this point in the history
  • Loading branch information
amccaskey committed Dec 3, 2024
1 parent f19903d commit 40eac46
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
2 changes: 2 additions & 0 deletions docs/sphinx/api/solvers/cpp_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CUDA-Q Solvers C++ API
.. doxygenclass:: cudaq::solvers::uccsd
.. doxygenclass:: cudaq::solvers::qaoa_pool

.. doxygenfunction:: cudaq::solvers::get_operator_pool

.. doxygenstruct:: cudaq::solvers::atom
:members:

Expand Down
5 changes: 2 additions & 3 deletions docs/sphinx/components/solvers/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,8 @@ Basic Usage
auto h = molecule.hamiltonian;
// Generate operator pool
auto pool = cudaq::solvers::operator_pool::get(
"spin_complement_gsd");
auto operators = pool->generate({
auto operators = cudaq::solvers::get_operator_pool(
"spin_complement_gsd", {
{"num-orbitals", h.num_qubits() / 2}
});
Expand Down
6 changes: 3 additions & 3 deletions docs/sphinx/examples/solvers/cpp/adapt_h2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ int main() {
auto h = molecule.hamiltonian;

// Create the operator pool
auto pool = cudaq::solvers::operator_pool::get("spin_complement_gsd");
auto poolList = pool->generate({{"num-orbitals", h.num_qubits() / 2}});
std::vector<cudaq::spin_op> opPool = cudaq::solvers::get_operator_pool(
"spin_complement_gsd", {{"num-orbitals", h.num_qubits() / 2}});

// Run ADAPT
auto [energy, thetas, ops] = cudaq::solvers::adapt_vqe(
[](cudaq::qvector<> &q) __qpu__ {
x(q[0]);
x(q[1]);
},
h, poolList, {{"grad_norm_tolerance", 1e-3}});
h, opPool, {{"grad_norm_tolerance", 1e-3}});

printf("Final <H> = %.12lf\n", energy);
}
17 changes: 17 additions & 0 deletions libs/solvers/include/cudaq/solvers/operators/operator_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ class operator_pool : public extension_point<operator_pool> {
generate(const heterogeneous_map &config) const = 0;
};

/// @brief Retrieves a quantum operator pool based on the specified name and
/// configuration options.
///
/// This function creates and returns a vector of quantum spin operators by
/// instantiating the appropriate operator_pool implementation specified by the
/// name parameter. The generated operators are configured according to the
/// provided options.
///
/// @param name The identifier string for the desired operator pool
/// implementation
/// @param options Configuration parameters for operator pool generation stored
/// in a heterogeneous map
/// @return std::vector<cudaq::spin_op> A vector containing the generated
/// quantum spin operators
std::vector<cudaq::spin_op> get_operator_pool(const std::string &name,
const heterogeneous_map &options);

} // namespace cudaq::solvers
7 changes: 7 additions & 0 deletions libs/solvers/lib/operators/operator_pools/operator_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
#include "cudaq/solvers/operators/operator_pool.h"

INSTANTIATE_REGISTRY_NO_ARGS(cudaq::solvers::operator_pool)

namespace cudaq::solvers {
std::vector<cudaq::spin_op>
get_operator_pool(const std::string &name, const heterogeneous_map &options) {
return cudaq::solvers::operator_pool::get(name)->generate(options);
}
} // namespace cudaq::solvers
10 changes: 1 addition & 9 deletions libs/solvers/python/bindings/solvers/py_solvers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,7 @@ RuntimeError
mod.def(
"get_operator_pool",
[](const std::string &name, py::kwargs config) {
heterogeneous_map asCpp;
for (auto &[k, v] : config) {
std::string asStr = k.cast<std::string>();
if (py::isinstance<py::int_>(v))
asCpp.insert(asStr, v.cast<std::size_t>());
if (py::isinstance<py::list>(v))
asCpp.insert(asStr, v.cast<std::vector<double>>());
}
return operator_pool::get(name)->generate(asCpp);
return operator_pool::get(name)->generate(hetMapFromKwargs(config));
},
R"#(Get and generate an operator pool based on the specified name and configuration.
Expand Down
11 changes: 11 additions & 0 deletions libs/solvers/unittests/test_operator_pools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ TEST(UCCSDTest, GenerateWithDefaultConfig) {
}
}

TEST(UCCSDTest, GenerateFromAPIFunction) {
auto operators = cudaq::solvers::get_operator_pool(
"uccsd", {{"num-qubits", 4}, {"num-electrons", 2}});
ASSERT_FALSE(operators.empty());
EXPECT_EQ(operators.size(), 2 * 2 + 1 * 8);

for (const auto &op : operators) {
EXPECT_EQ(op.num_qubits(), 4);
}
}

TEST(UCCSDTest, GenerateWithCustomCoefficients) {
auto pool = cudaq::solvers::operator_pool::get("uccsd");
heterogeneous_map config;
Expand Down

0 comments on commit 40eac46

Please sign in to comment.