Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of GQE in Python #4

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cudaqlib/gse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

#include "gse/adapt/adapt.h"
#include "gse/vqe/vqe.h"
#include "gse/utils/operator_pool.h"
#include "gse/utils/pools/uccsd_pool.h"
#include "gse/utils/pools/spin_complement_gsd.h"

#include "gse/adapt/pools/spin_complement_gsd.h"
4 changes: 3 additions & 1 deletion cudaqlib/gse/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

set(LIBRARY_NAME cudaq-gse)

add_subdirectory(utils)

add_library(${LIBRARY_NAME} INTERFACE)
target_include_directories(${LIBRARY_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CUDAQ_INCLUDE_DIR}>
$<INSTALL_INTERFACE:include>)

target_link_libraries(${LIBRARY_NAME} INTERFACE cudaq::cudaq)
target_link_libraries(${LIBRARY_NAME} INTERFACE cudaq::cudaq cudaq-operator-pools cudaq-optim)

install(
TARGETS ${LIBRARY_NAME}
Expand Down
12 changes: 5 additions & 7 deletions cudaqlib/gse/adapt/adapt.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

#pragma once

#include "cudaqlib/gse/vqe/vqe.h"
#include "cudaq/qis/qubit_qis.h"

#include "operator_pool.h"
#include "cudaqlib/gse/utils/operator_pool.h"
#include "cudaqlib/gse/vqe/vqe.h"

namespace cudaq::gse {

Expand All @@ -36,15 +35,14 @@ struct AdaptKernel {

template <typename InitialState>
auto adapt_vqe(const InitialState &initialState, const spin_op &H,
const operator_pool &operatorPool, optim::optimizer &optimizer,
const std::vector<spin_op> &poolList, optim::optimizer &optimizer,
observe_gradient *gradient,
const adapt_options options = adapt_options()) {

AdaptKernel kernel;
std::vector<spin_op> trotterList;
std::vector<double> thetas;
auto numQubits = H.num_qubits();
auto poolList = operatorPool.generate();
double energy = 0.0, lastNorm = std::numeric_limits<double>::max();

// Compute the [H,Oi]
Expand Down Expand Up @@ -114,14 +112,14 @@ auto adapt_vqe(const InitialState &initialState, const spin_op &H,

template <typename InitialState>
auto adapt_vqe(const InitialState &initialState, const spin_op &H,
const operator_pool &operatorPool, optim::optimizer &optimizer,
const std::vector<spin_op> &operatorPool, optim::optimizer &optimizer,
const adapt_options options = adapt_options()) {
return adapt_vqe(initialState, H, operatorPool, optimizer, nullptr, options);
}

template <typename InitialState>
auto adapt_vqe(const InitialState &initialState, const spin_op &H,
const operator_pool &operatorPool,
const std::vector<spin_op> &operatorPool,
const adapt_options options = adapt_options()) {
cudaq::optim::cobyla optimizer;
return adapt_vqe(initialState, H, operatorPool, optimizer, options);
Expand Down
40 changes: 0 additions & 40 deletions cudaqlib/gse/adapt/operator_pool.h

This file was deleted.

152 changes: 0 additions & 152 deletions cudaqlib/gse/adapt/pools/spin_complement_gsd.h

This file was deleted.

9 changes: 9 additions & 0 deletions cudaqlib/gse/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ============================================================================ #
# Copyright (c) 2022 - 2023 NVIDIA Corporation & Affiliates. #
# All rights reserved. #
# #
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

add_subdirectory(pools)
70 changes: 70 additions & 0 deletions cudaqlib/gse/utils/operator_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/****************************************************************-*- C++ -*-****
* Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. *
* All rights reserved. *
* *
* This source code and the accompanying materials are made available under *
* the terms of the Apache License 2.0 which accompanies this distribution. *
******************************************************************************/

#pragma once

#include <any>
#include <optional>
#include <unordered_map>

#include "cudaq/spin_op.h"
#include "cudaqlib/utils/extension_point.h"

namespace cudaq {
inline std::optional<std::unordered_map<std::string, std::any>::const_iterator>
findIter(const std::vector<std::string> &possibleNames,
const std::unordered_map<std::string, std::any> &m) {
for (auto &name : possibleNames) {
auto iter = m.find(name);
if (iter != m.end())
return iter;
}
return std::nullopt;
}

inline std::size_t getIntLike(const std::any &any) {
try {
return std::any_cast<std::size_t>(any);
} catch (...) {
// If this throws then we'll just have an error
return std::any_cast<int>(any);
}
}

class operator_pool : public extension_point<operator_pool> {
public:
operator_pool() = default;
virtual std::vector<spin_op>
generate(const std::unordered_map<std::string, std::any> &config) const = 0;
};

CUDAQ_DEFINE_EXTENSION_IMPL(operator_pool)

#define CUDAQ_REGISTER_OPERATOR_POOL(TYPE) \
static inline const std::string class_identifier = #TYPE; \
static std::unique_ptr<operator_pool> create() { \
return std::make_unique<TYPE>(); \
}

namespace fermion {
inline spin_op adag(std::size_t numQubits, std::size_t j) {
spin_op zprod(numQubits);
for (std::size_t k = 0; k < j; k++)
zprod *= spin::z(k);
return 0.5 * zprod * (spin::x(j) - std::complex<double>{0, 1} * spin::y(j));
}

inline spin_op a(std::size_t numQubits, std::size_t j) {
spin_op zprod(numQubits);
for (std::size_t k = 0; k < j; k++)
zprod *= spin::z(k);
return 0.5 * zprod * (spin::x(j) + std::complex<double>{0, 1} * spin::y(j));
}
} // namespace fermion

} // namespace cudaq
26 changes: 26 additions & 0 deletions cudaqlib/gse/utils/pools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# ============================================================================ #
# Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. #
# All rights reserved. #
# #
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #

add_compile_options(-Wno-attributes)
set(LIBRARY_NAME cudaq-operator-pools)
add_library(${LIBRARY_NAME} SHARED uccsd_pool.cpp spin_complement_gsd.cpp)
target_include_directories(
${LIBRARY_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>)
target_link_libraries(${LIBRARY_NAME} PRIVATE cudaq::cudaq-spin)

install(
TARGETS ${LIBRARY_NAME}
EXPORT ${LIBRARY_NAME}Targets
DESTINATION lib)

install(
EXPORT ${LIBRARY_NAME}Targets
FILE ${LIBRARY_NAME}Targets.cmake
NAMESPACE cudaq::
DESTINATION lib/cmake/gse)
Loading