Quantum Resource Manager and Compiler Infrastructure (QRM&CI) 0.1.0
BackendRegistry.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 MQSS Maintainers
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 */
7
13
14#pragma once
15
17
18#include <chrono>
19#include <cstddef>
20#include <cstdint>
21#include <functional>
22#include <map>
23#include <string>
24#include <string_view>
25
26namespace mqss::qrmci {
27
40
53public:
55 using Clock = std::chrono::steady_clock;
56
58 static constexpr Clock::duration DefaultTimeToLive = std::chrono::seconds(30);
59
61 BackendRegistry() = default;
62
68 explicit BackendRegistry(Clock::duration timeToLive);
69
84 Clock::time_point now = Clock::now());
85
90 std::size_t expire(Clock::time_point now = Clock::now());
91
99 [[nodiscard]] const BackendWrapper *find(std::string_view backendName) const;
100
104 [[nodiscard]] bool contains(std::string_view backendName) const;
105
108 [[nodiscard]] std::size_t size() const noexcept { return entries.size(); }
109
112 [[nodiscard]] bool empty() const noexcept { return entries.empty(); }
113
116 [[nodiscard]] Clock::duration getTimeToLive() const noexcept {
117 return entryTimeToLive;
118 }
119
125 template <typename Fn> void forEachBackend(Fn &&fn) const {
126 for (const auto &[backendName, entry] : entries) {
127 std::invoke(fn, backendName, entry.backend);
128 }
129 }
130
131private:
132 struct Entry {
133 BackendWrapper backend;
134 Clock::time_point lastRefreshed;
135 };
136
137 // Ordered, so iteration -- and therefore selection -- is deterministic.
138 // std::less<> gives heterogeneous lookup from std::string_view without
139 // materialising a std::string per query.
140 std::map<std::string, Entry, std::less<>> entries;
141 Clock::duration entryTimeToLive{DefaultTimeToLive};
142};
143
144} // namespace mqss::qrmci
BackendSelectionPolicy
Named rule deciding which backend wins when several of them could run the same task.
@ LowestName
The compatible backend whose name sorts first.
@ SmallestSufficient
The compatible backend with the fewest qubits (best fit, leaving the larger devices free for larger c...
One backend's capabilities as the QRM&CI pipeline sees them, read either from a live device or from a...
BackendRegistry(Clock::duration timeToLive)
Construct a registry with an explicit time-to-live.
std::chrono::steady_clock Clock
Monotonic clock used for entry freshness.
std::size_t size() const noexcept
Get the number of registered backends.
BackendRegistry()=default
Construct a registry with the default time-to-live.
std::size_t expire(Clock::time_point now=Clock::now())
Drop every entry last refreshed longer ago than the registry's time-to-live.
bool insertOrRefresh(BackendWrapper backend, Clock::time_point now=Clock::now())
Insert a backend, or refresh the entry already registered under the same name, and stamp it as last h...
bool contains(std::string_view backendName) const
Check whether a backend is registered under a given name.
bool empty() const noexcept
Check whether the registry holds no backends.
const BackendWrapper * find(std::string_view backendName) const
Look up a backend by name.
Clock::duration getTimeToLive() const noexcept
Get the registry's entry time-to-live.
void forEachBackend(Fn &&fn) const
Apply a function to every registered backend, in ascending backend-name order.
static constexpr Clock::duration DefaultTimeToLive
How long an entry stays usable after it was last refreshed.
A snapshot of one backend's capabilities: name, qubit count, status, supported instructions,...