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

Load available simulators and platforms lazily in Python #239

Merged
merged 2 commits into from
Jun 14, 2023
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
53 changes: 30 additions & 23 deletions python/utils/LinkedLibraryHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,15 @@ LinkedLibraryHolder::LinkedLibraryHolder() {
auto idx = simName.find_last_of(".");
simName = simName.substr(0, idx);

// FIXME until we have a better handle on MPI init / finalize
// we can't load these
if (simName == "tensornet" || simName == "cuquantum_mgpu") {
simulators.emplace(simName, nullptr);
continue;
}

// Store the dlopen handles
auto iter = libHandles.find(path.string());
if (iter == libHandles.end())
libHandles.emplace(path.string(), dlopen(path.string().c_str(),
RTLD_GLOBAL | RTLD_NOW));

// Load the plugin and get the CircuitSimulator.
std::string symbolName = fmt::format("getCircuitSimulator_{}", simName);
auto *simulator =
getUniquePluginInstance<nvqir::CircuitSimulator>(symbolName);

cudaq::info("Found simulator plugin {}.", simName);
simulators.emplace(simName, simulator);
availableSimulators.push_back(simName);

} else if (fileName.find("cudaq-platform-") != std::string::npos) {
// store all available platforms.
Expand All @@ -191,18 +180,14 @@ LinkedLibraryHolder::LinkedLibraryHolder() {
RTLD_GLOBAL | RTLD_NOW));

// Load the plugin and get the CircuitSimulator.
std::string symbolName =
fmt::format("getQuantumPlatform_{}", platformName);
auto *platform =
getUniquePluginInstance<cudaq::quantum_platform>(symbolName);
platforms.emplace(platformName, platform);
availablePlatforms.push_back(platformName);
cudaq::info("Found platform plugin {}.", platformName);
}
}

// We'll always start off with the default platform and the QPP simulator
__nvqir__setCircuitSimulator(simulators["qpp"]);
setQuantumPlatformInternal(platforms["default"]);
__nvqir__setCircuitSimulator(getSimulator("qpp"));
setQuantumPlatformInternal(getPlatform("default"));
targets.emplace("default",
RuntimeTarget{"default", "qpp", "default",
"Default OpenMP CPU-only simulated QPU."});
Expand All @@ -213,9 +198,31 @@ LinkedLibraryHolder::~LinkedLibraryHolder() {
dlclose(handle);
}

nvqir::CircuitSimulator *
LinkedLibraryHolder::getSimulator(const std::string &simName) {
auto end = availableSimulators.end();
auto iter = std::find(availableSimulators.begin(), end, simName);
if (iter == end)
throw std::runtime_error("Invalid simulator requested: " + simName);

return getUniquePluginInstance<nvqir::CircuitSimulator>(
std::string("getCircuitSimulator_") + simName);
}

quantum_platform *
LinkedLibraryHolder::getPlatform(const std::string &platformName) {
auto end = availablePlatforms.end();
auto iter = std::find(availablePlatforms.begin(), end, platformName);
if (iter == end)
throw std::runtime_error("Invalid platform requested: " + platformName);

return getUniquePluginInstance<quantum_platform>(
std::string("getQuantumPlatform_") + platformName);
}

void LinkedLibraryHolder::resetTarget() {
__nvqir__setCircuitSimulator(simulators["qpp"]);
setQuantumPlatformInternal(platforms["default"]);
__nvqir__setCircuitSimulator(getSimulator("qpp"));
setQuantumPlatformInternal(getPlatform("default"));
currentTarget = "default";
}

Expand Down Expand Up @@ -256,8 +263,8 @@ void LinkedLibraryHolder::setTarget(
cudaq::info("Setting target={} (sim={}, platform={})", targetName,
target.simulatorName, target.platformName);

__nvqir__setCircuitSimulator(simulators[target.simulatorName]);
auto *platform = platforms[target.platformName];
__nvqir__setCircuitSimulator(getSimulator(target.simulatorName));
auto *platform = getPlatform(target.platformName);

// Pack the config into the backend string name
std::string backendConfigStr = targetName;
Expand Down
14 changes: 10 additions & 4 deletions python/utils/LinkedLibraryHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,24 @@ class LinkedLibraryHolder {
/// @brief Map of path strings to loaded library handles.
std::unordered_map<std::string, void *> libHandles;

/// @brief Map of available simulators
std::unordered_map<std::string, nvqir::CircuitSimulator *> simulators;
/// @brief Vector of available simulators
std::vector<std::string> availableSimulators;

/// @brief Map of available platforms
std::unordered_map<std::string, quantum_platform *> platforms;
/// @brief Vector of available platforms
std::vector<std::string> availablePlatforms;

/// @brief Map of available targets.
std::unordered_map<std::string, RuntimeTarget> targets;

/// @brief Store the name of the current target
std::string currentTarget = "default";

/// @brief Return the registered simulator with the given name.
nvqir::CircuitSimulator *getSimulator(const std::string &name);

/// @brief Return the registered quantum_platform with the given name.
quantum_platform *getPlatform(const std::string &name);

public:
LinkedLibraryHolder();
~LinkedLibraryHolder();
Expand Down