Skip to content

Commit 655610b

Browse files
📝 Add docstrings to ystade/qdmi-sc-device
Docstrings generation was requested by @ystade. * #1328 (comment) The following files were modified: * `include/mqt-core/qdmi/sc/Device.hpp` * `src/na/device/Device.cpp` * `src/qdmi/Driver.cpp` * `src/qdmi/sc/App.cpp` * `src/qdmi/sc/Device.cpp` * `src/qdmi/sc/DynDevice.cpp` * `src/qdmi/sc/Generator.cpp` * `test/na/device/test_app.cpp` * `test/na/device/test_device.cpp` * `test/na/device/test_generator.cpp` * `test/qdmi/sc/test_app.cpp` * `test/qdmi/sc/test_device.cpp` * `test/qdmi/sc/test_generator.cpp` * `test/qdmi/test_driver.cpp`
1 parent 8553ccb commit 655610b

File tree

14 files changed

+1052
-87
lines changed

14 files changed

+1052
-87
lines changed

include/mqt-core/qdmi/sc/Device.hpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,38 @@ class Device final {
4747
Device();
4848

4949
public:
50-
// Default move constructor and move assignment operator.
50+
/**
51+
* @brief Move-constructs a Device, transferring ownership of internal resources.
52+
*
53+
* Leaves the moved-from instance in a valid but unspecified state.
54+
*/
5155
Device(Device&&) = default;
52-
Device& operator=(Device&&) = default;
53-
// Delete copy constructor and assignment operator to enforce singleton.
56+
/**
57+
* @brief Move-assigns the Device instance from another Device.
58+
*
59+
* @param other rvalue reference to the source Device to move from.
60+
* @return Device& Reference to this Device after move assignment.
61+
*/
62+
Device& operator=(Device&&) = default;
63+
/**
64+
* @brief Deleted copy constructor to prevent copying of the singleton Device.
65+
*
66+
* Deleting this constructor enforces the singleton pattern by disallowing copy construction.
67+
*/
5468
Device(const Device&) = delete;
55-
Device& operator=(const Device&) = delete;
69+
/**
70+
* @brief Deleted copy assignment operator to prevent copying of the Device singleton.
71+
*
72+
* The Device class is non-copyable; assigning from another Device is disallowed to
73+
* preserve the singleton semantics and unique device state.
74+
*/
75+
Device& operator=(const Device&) = delete;
5676

57-
/// @returns the singleton instance of the Device class.
77+
/**
78+
* @brief Accesses the singleton Device instance.
79+
*
80+
* @return Device& Reference to the singleton Device instance.
81+
*/
5882
[[nodiscard]] static Device& get() {
5983
static Device instance;
6084
return instance;
@@ -162,7 +186,11 @@ struct MQT_SC_QDMI_Device_Job_impl_d {
162186
MQT_SC_QDMI_Device_Session_impl_d* session_;
163187

164188
public:
165-
/// @brief Constructor for the MQT_SC_QDMI_Device_Job_impl_d.
189+
/**
190+
* @brief Initializes a device job implementation bound to the given session.
191+
*
192+
* @param session Pointer to the owning MQT_SC_QDMI_Device_Session_impl_d. The session must remain valid for the job's lifetime.
193+
*/
166194
explicit MQT_SC_QDMI_Device_Job_impl_d(
167195
MQT_SC_QDMI_Device_Session_impl_d* session)
168196
: session_(session) {}
@@ -231,7 +259,11 @@ struct MQT_SC_QDMI_Site_impl_d {
231259
private:
232260
uint64_t id_ = 0; ///< Unique identifier of the site
233261

234-
/// @brief Constructor for regular sites.
262+
/**
263+
* @brief Initializes a site implementation with the given unique identifier.
264+
*
265+
* @param id Unique identifier for the site.
266+
*/
235267
MQT_SC_QDMI_Site_impl_d(uint64_t id) : id_(id) {}
236268

237269
public:
@@ -267,4 +299,4 @@ struct MQT_SC_QDMI_Operation_impl_d {
267299
size_t numParams, const double* params,
268300
QDMI_Operation_Property prop, size_t size, void* value,
269301
size_t* sizeRet) const -> int;
270-
};
302+
};

src/na/device/Device.cpp

Lines changed: 119 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
// NOLINTEND(bugprone-macro-parentheses)
9191

9292
namespace qdmi::na {
93+
/**
94+
* @brief Constructs the device and initializes its metadata and supported entities.
95+
*
96+
* Initializes the device name, number of qubits, minimum atom distance, length and
97+
* duration units/scale factors, and populates the site and operation lists.
98+
*/
9399
Device::Device() {
94100
// NOLINTBEGIN(cppcoreguidelines-prefer-member-initializer)
95101
INITIALIZE_NAME(name_);
@@ -120,6 +126,26 @@ auto Device::sessionFree(MQT_NA_QDMI_Device_Session session) -> void {
120126
}
121127
}
122128
}
129+
/**
130+
* @brief Retrieve a device-level property value.
131+
*
132+
* Query a named device property and copy its value into the provided buffer
133+
* (or report the required size). Supported properties include metadata
134+
* (name, version), numeric characteristics (qubit count, minimum atom
135+
* distance, units/scale factors), lists (sites, operations), and device
136+
* capabilities.
137+
*
138+
* @param prop The device property being queried.
139+
* @param size Size of the buffer pointed to by `value` in bytes; may be 0
140+
* when `value` is nullptr to request the required size.
141+
* @param value Pointer to a buffer to receive the property value, or nullptr
142+
* to only retrieve the required size.
143+
* @param sizeRet If non-null, receives the number of bytes written or the
144+
* number of bytes required.
145+
* @return int `QDMI_SUCCESS` on success; `QDMI_ERROR_INVALIDARGUMENT` if
146+
* arguments are invalid; `QDMI_ERROR_NOTSUPPORTED` if the property
147+
* is not recognized or not supported.
148+
*/
123149
auto Device::queryProperty(const QDMI_Device_Property prop, const size_t size,
124150
void* value, size_t* sizeRet) -> int {
125151
if ((value != nullptr && size == 0) || prop >= QDMI_DEVICE_PROPERTY_MAX) {
@@ -161,7 +187,13 @@ auto Device::queryProperty(const QDMI_Device_Property prop, const size_t size,
161187
operations_, prop, size, value, sizeRet)
162188
return QDMI_ERROR_NOTSUPPORTED;
163189
}
164-
} // namespace qdmi::na
190+
} /**
191+
* @brief Initialize the device session by transitioning it from ALLOCATED to INITIALIZED.
192+
*
193+
* Sets the session's internal state to INITIALIZED when it is currently ALLOCATED.
194+
*
195+
* @return int `QDMI_SUCCESS` on successful initialization, `QDMI_ERROR_BADSTATE` if the session was not in the ALLOCATED state.
196+
*/
165197

166198
auto MQT_NA_QDMI_Device_Session_impl_d::init() -> int {
167199
if (status_ != Status::ALLOCATED) {
@@ -201,6 +233,15 @@ auto MQT_NA_QDMI_Device_Session_impl_d::freeDeviceJob(
201233
jobs_.erase(job);
202234
}
203235
}
236+
/**
237+
* @brief Query a device-level property for this session.
238+
*
239+
* @param prop The device property to query.
240+
* @param size Size of the caller-provided buffer pointed to by `value`, in bytes.
241+
* @param value Pointer to a buffer to receive the property's value; may be null to query required size.
242+
* @param sizeRet Pointer to receive the number of bytes written or required; may be null if not needed.
243+
* @return int QDMI status code: `QDMI_SUCCESS` on success, `QDMI_ERROR_BADSTATE` if the session is not initialized, `QDMI_ERROR_NOTSUPPORTED` if the property is not supported, or other QDMI error codes on failure.
244+
*/
204245
auto MQT_NA_QDMI_Device_Session_impl_d::queryDeviceProperty(
205246
const QDMI_Device_Property prop, const size_t size, void* value,
206247
size_t* sizeRet) const -> int {
@@ -209,6 +250,20 @@ auto MQT_NA_QDMI_Device_Session_impl_d::queryDeviceProperty(
209250
}
210251
return qdmi::na::Device::get().queryProperty(prop, size, value, sizeRet);
211252
}
253+
/**
254+
* @brief Query a property of the specified site within this session.
255+
*
256+
* Validates the session state and the site handle before performing the query.
257+
*
258+
* @param site Site handle to query; must not be `nullptr`.
259+
* @param prop Property to query.
260+
* @param size Size of the buffer pointed to by `value`, in bytes.
261+
* @param value Output buffer for the property value; may be `nullptr` to obtain required size.
262+
* @param sizeRet Receives the number of bytes written or required; may be `nullptr` if not needed.
263+
* @return int `QDMI_ERROR_INVALIDARGUMENT` if `site` is `nullptr`.
264+
* `QDMI_ERROR_BADSTATE` if the session is not initialized.
265+
* Otherwise, returns the status code produced while retrieving the site's property.
266+
*/
212267
auto MQT_NA_QDMI_Device_Session_impl_d::querySiteProperty(
213268
MQT_NA_QDMI_Site site, const QDMI_Site_Property prop, const size_t size,
214269
void* value, size_t* sizeRet) const -> int {
@@ -542,7 +597,27 @@ auto MQT_NA_QDMI_Operation_impl_d::queryProperty(
542597
*duration_, prop, size, value, sizeRet)
543598
}
544599
if (fidelity_) {
545-
ADD_SINGLE_VALUE_PROPERTY(QDMI_OPERATION_PROPERTY_FIDELITY, double,
600+
/**
601+
* @brief Query an operation property for the specified sites and parameters.
602+
*
603+
* Validates the provided site pointers and parameter pointers, checks that the
604+
* supplied sites are compatible with this operation (including ordering for
605+
* two-qubit operations), and writes the requested property into the provided
606+
* buffer if supported.
607+
*
608+
* @param numSites Number of sites in `sites`.
609+
* @param sites Pointer to an array of site handles to query against.
610+
* @param numParams Number of numeric parameters in `params`.
611+
* @param params Pointer to an array of parameter values (may be nullptr if none).
612+
* @param prop The operation property to query.
613+
* @param size Size of the `value` buffer in bytes.
614+
* @param value Buffer to receive the property value (may be nullptr to query required size).
615+
* @param sizeRet If non-null, receives the number of bytes written or required.
616+
* @return int `QDMI_ERROR_SUCCESS` if the property was written to `value`;
617+
* `QDMI_ERROR_NOTSUPPORTED` if the property or the supplied site/parameter
618+
* combination is not supported; `QDMI_ERROR_BADARG` for invalid input arguments.
619+
*/
620+
ADD_SINGLE_VALUE_PROPERTY(QDMI_OPERATION_PROPERTY_FIDELITY, double,
546621
*fidelity_, prop, size, value, sizeRet)
547622
}
548623
if (numQubits_) {
@@ -558,28 +633,69 @@ auto MQT_NA_QDMI_Operation_impl_d::queryProperty(
558633
return QDMI_ERROR_NOTSUPPORTED;
559634
}
560635

636+
/**
637+
* @brief Initializes the NA QDMI device subsystem by ensuring the Device singleton exists.
638+
*
639+
* @return QDMI_SUCCESS on success.
640+
*/
561641
int MQT_NA_QDMI_device_initialize() {
562642
std::ignore = qdmi::na::Device::get(); // Ensure the singleton is created
563643
return QDMI_SUCCESS;
564644
}
565645

646+
/**
647+
* @brief Finalizes the NA QDMI device subsystem and releases any global resources.
648+
*
649+
* Performs any final cleanup required by the device subsystem prior to library unload.
650+
*
651+
* @return int `QDMI_SUCCESS` on success.
652+
*/
566653
int MQT_NA_QDMI_device_finalize() { return QDMI_SUCCESS; }
567654

655+
/**
656+
* @brief Allocates a new device session and returns its handle.
657+
*
658+
* @param session Pointer to storage that receives the new MQT_NA_QDMI_Device_Session handle; must not be null.
659+
* @return int Status code indicating success or failure. On success the out-parameter `session` is set to the allocated session handle.
660+
*/
568661
int MQT_NA_QDMI_device_session_alloc(MQT_NA_QDMI_Device_Session* session) {
569662
return qdmi::na::Device::get().sessionAlloc(session);
570663
}
571664

665+
/**
666+
* @brief Initializes a device session.
667+
*
668+
* @param session Pointer to an allocated device session handle.
669+
* @return int QDMI_SUCCESS on success; QDMI_ERROR_INVALIDARGUMENT if `session` is null; otherwise the status code returned by the session initialization.
670+
*/
572671
int MQT_NA_QDMI_device_session_init(MQT_NA_QDMI_Device_Session session) {
573672
if (session == nullptr) {
574673
return QDMI_ERROR_INVALIDARGUMENT;
575674
}
576675
return session->init();
577676
}
578677

678+
/**
679+
* @brief Release a device session handle and free its internal resources.
680+
*
681+
* If `session` is non-null and corresponds to an allocated session, the session
682+
* is removed and its resources are freed; passing a null handle has no effect.
683+
*
684+
* @param session Session handle to free.
685+
*/
579686
void MQT_NA_QDMI_device_session_free(MQT_NA_QDMI_Device_Session session) {
580687
qdmi::na::Device::get().sessionFree(session);
581688
}
582689

690+
/**
691+
* @brief Set a parameter for a device session.
692+
*
693+
* @param session Handle to an allocated device session.
694+
* @param param Identifier of the session parameter to set.
695+
* @param size Size in bytes of the data pointed to by `value`.
696+
* @param value Pointer to the parameter data.
697+
* @return int QDMI status code: `QDMI_ERROR_INVALIDARGUMENT` if `session` is null; otherwise the status returned by the session implementation.
698+
*/
583699
int MQT_NA_QDMI_device_session_set_parameter(
584700
MQT_NA_QDMI_Device_Session session, QDMI_Device_Session_Parameter param,
585701
const size_t size, const void* value) {
@@ -723,4 +839,4 @@ int MQT_NA_QDMI_device_session_query_operation_property(
723839
}
724840
return session->queryOperationProperty(operation, numSites, sites, numParams,
725841
params, prop, size, value, sizeRet);
726-
}
842+
}

src/qdmi/Driver.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ DEFINE_STATIC_LIBRARY(MQT_DDSIM)
8080
DEFINE_STATIC_LIBRARY(MQT_SC)
8181

8282
#ifndef _WIN32
83+
/**
84+
* @brief Load a device shared library, bind required QDMI symbols using the given prefix, and initialize the device.
85+
*
86+
* Opens the shared library specified by libName, resolves the set of required QDMI symbols named using
87+
* the pattern "<prefix>_QDMI_<symbol>", and calls the library's initialize function.
88+
*
89+
* @param libName Path or filename of the shared library to load.
90+
* @param prefix Symbol name prefix used to construct exported QDMI symbol names (e.g., prefix_QDMI_device_initialize).
91+
*
92+
* @throws std::runtime_error If the library cannot be opened, if the library is already loaded,
93+
* or if any required symbol cannot be resolved.
94+
*/
8395
DynamicDeviceLibrary::DynamicDeviceLibrary(const std::string& libName,
8496
const std::string& prefix)
8597
: libHandle_(dlopen(libName.c_str(), RTLD_NOW | RTLD_LOCAL)) {
@@ -333,6 +345,12 @@ auto QDMI_Session_impl_d::querySessionProperty(QDMI_Session_Property prop,
333345
}
334346

335347
namespace qdmi {
348+
/**
349+
* @brief Constructs the driver and registers built-in static device backends.
350+
*
351+
* Initializes the driver by creating and storing device entries for the bundled
352+
* static libraries: MQT_NA, MQT_DDSIM, and MQT_SC.
353+
*/
336354
Driver::Driver() {
337355
devices_.emplace_back(std::make_unique<QDMI_Device_impl_d>(
338356
std::make_unique<MQT_NADeviceLibrary>()));
@@ -342,6 +360,11 @@ Driver::Driver() {
342360
std::make_unique<MQT_SCDeviceLibrary>()));
343361
}
344362

363+
/**
364+
* @brief Releases all resources owned by the Driver.
365+
*
366+
* Destroys and removes all managed session and device objects so the Driver releases its held resources.
367+
*/
345368
Driver::~Driver() {
346369
sessions_.clear();
347370
devices_.clear();
@@ -508,4 +531,4 @@ int QDMI_device_query_operation_property(
508531
}
509532
return device->queryOperationProperty(operation, numSites, sites, numParams,
510533
params, prop, size, value, sizeRet);
511-
}
534+
}

0 commit comments

Comments
 (0)