Skip to content

Commit 4e4a84e

Browse files
mergify[bot]ystadeburgholzer
authored
🎨 Reorganize QDMI Codebase (backport #1444) (#1449)
## Description This PR reorganizes the QDMI codebase to improve its structure and maintainability. It extracts and incorporates the best practices from #1347 (Refactor QDMI Devices and Fix Singleton Handling). ## Checklist: - [x] The pull request only contains commits that are focused and relevant to this change. - [x] I have added appropriate tests that cover the new/changed functionality. - [x] I have updated the documentation to reflect these changes. - [x] I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals. - [ ] I have added migration instructions to the upgrade guide (if needed). - [x] The changes follow the project's style guidelines and introduce no new warnings. - [x] The changes are fully tested and pass the CI checks. - [x] I have reviewed my own code changes.<hr>This is an automatic backport of pull request #1444 done by [Mergify](https://mergify.com). --------- Signed-off-by: burgholzer <burgholzer@me.com> Co-authored-by: Yannick Stade <100073938+ystade@users.noreply.github.com> Co-authored-by: burgholzer <burgholzer@me.com>
1 parent a870c74 commit 4e4a84e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+214
-191
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ This project adheres to [Semantic Versioning], with the exception that minor rel
99

1010
## [Unreleased]
1111

12+
### Changed
13+
14+
- ♻️ Extract singleton pattern into reusable template base class for QDMI devices and driver ([#1444]) ([**@ystade**], [**@burgholzer**])
15+
- 🚚 Reorganize QDMI code structure by moving devices into dedicated subdirectories and separating driver and common utilities ([#1444]) ([**@ystade**])
16+
1217
## [3.4.0] - 2026-01-08
1318

1419
### Added
@@ -295,6 +300,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool
295300

296301
<!-- PR links -->
297302

303+
[#1444]: https://github.com/munich-quantum-toolkit/core/pull/1444
298304
[#1415]: https://github.com/munich-quantum-toolkit/core/pull/1415
299305
[#1414]: https://github.com/munich-quantum-toolkit/core/pull/1414
300306
[#1413]: https://github.com/munich-quantum-toolkit/core/pull/1413

bindings/fomac/fomac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "fomac/FoMaC.hpp"
1212

13-
#include "qdmi/Driver.hpp"
13+
#include "qdmi/driver/Driver.hpp"
1414

1515
#include <nanobind/nanobind.h>
1616
#include <nanobind/operators.h>

bindings/na/register_fomac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include "fomac/FoMaC.hpp"
1212
#include "na/fomac/Device.hpp"
13-
#include "qdmi/na/Generator.hpp"
13+
#include "qdmi/devices/na/Generator.hpp"
1414

1515
#include <nanobind/nanobind.h>
1616
#include <nanobind/operators.h>

include/mqt-core/fomac/FoMaC.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#pragma once
1212

13-
#include "qdmi/Common.hpp"
13+
#include "qdmi/common/Common.hpp"
1414

1515
#include <algorithm>
1616
#include <complex>

include/mqt-core/na/fomac/Device.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#pragma once
1212

1313
#include "fomac/FoMaC.hpp"
14-
#include "qdmi/na/Generator.hpp"
14+
#include "qdmi/devices/na/Generator.hpp"
1515

1616
// NOLINTNEXTLINE(misc-include-cleaner)
1717
#include <nlohmann/json.hpp>
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,40 @@
1919
#include <string>
2020

2121
namespace qdmi {
22+
template <class Concrete> class Singleton {
23+
protected:
24+
/// @brief Protected constructor to enforce the singleton pattern.
25+
Singleton() = default;
26+
27+
public:
28+
// Delete move constructor and move assignment operator because of the
29+
// following reason:
30+
//
31+
// - Users access the singleton via get() which returns a reference
32+
// - Moving would invalidate the singleton's state
33+
// - Users never own the singleton instance
34+
Singleton(Singleton&&) = delete;
35+
Singleton& operator=(Singleton&&) = delete;
36+
// Delete copy constructor and assignment operator to enforce singleton.
37+
Singleton(const Singleton&) = delete;
38+
Singleton& operator=(const Singleton&) = delete;
39+
40+
/// @brief Virtual destructor for the Singleton base class.
41+
virtual ~Singleton() = default;
42+
43+
/// @returns the singleton instance of the derived class.
44+
[[nodiscard]] static auto get() -> Concrete& {
45+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
46+
static auto* instance = new Concrete();
47+
// The instance is intentionally leaked to avoid static deinitialization
48+
// issues (cf. static (de)initialization order fiasco)
49+
return *instance;
50+
}
51+
};
52+
2253
/**
2354
* @brief Function used to mark unreachable code
24-
* @details Uses compiler specific extensions if possible. Even if no extension
55+
* @details Uses compiler-specific extensions if possible. Even if no extension
2556
* is used, undefined behavior is still raised by an empty function body and the
2657
* noreturn attribute.
2758
*/

include/mqt-core/qdmi/dd/Device.hpp renamed to include/mqt-core/qdmi/devices/dd/Device.hpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "dd/DDDefinitions.hpp"
1818
#include "dd/Package.hpp"
1919
#include "mqt_ddsim_qdmi/device.h"
20+
#include "qdmi/common/Common.hpp"
2021

2122
#include <atomic>
2223
#include <cstddef>
@@ -31,7 +32,9 @@
3132
#include <unordered_map>
3233

3334
namespace qdmi::dd {
34-
class Device final {
35+
class Device final : public Singleton<Device> {
36+
friend class Singleton;
37+
3538
/// Provides access to the device name.
3639
std::string name_;
3740

@@ -64,19 +67,6 @@ class Device final {
6467
Device();
6568

6669
public:
67-
// Default move constructor and move assignment operator.
68-
Device(Device&&) = delete;
69-
Device& operator=(Device&&) = delete;
70-
// Delete copy constructor and assignment operator to enforce singleton.
71-
Device(const Device&) = delete;
72-
Device& operator=(const Device&) = delete;
73-
74-
/// @brief Destructor for the Device class.
75-
~Device() = default;
76-
77-
/// @returns the singleton instance of the Device class.
78-
[[nodiscard]] static auto get() -> Device&;
79-
8070
/**
8171
* @brief Allocates a new device session.
8272
* @see MQT_DDSIM_QDMI_device_session_alloc

include/mqt-core/qdmi/na/Device.hpp renamed to include/mqt-core/qdmi/devices/na/Device.hpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "mqt_na_qdmi/device.h"
18+
#include "qdmi/common/Common.hpp"
1819

1920
#include <cstddef>
2021
#include <cstdint>
@@ -27,7 +28,9 @@
2728
#include <vector>
2829

2930
namespace qdmi::na {
30-
class Device final {
31+
class Device final : public Singleton<Device> {
32+
friend class Singleton;
33+
3134
/// @brief Provides access to the device name.
3235
std::string name_;
3336

@@ -69,19 +72,6 @@ class Device final {
6972
Device();
7073

7174
public:
72-
// Default move constructor and move assignment operator.
73-
Device(Device&&) = default;
74-
Device& operator=(Device&&) = default;
75-
// Delete copy constructor and assignment operator to enforce singleton.
76-
Device(const Device&) = delete;
77-
Device& operator=(const Device&) = delete;
78-
79-
/// @brief Destructor for the Device class.
80-
~Device() = default;
81-
82-
/// @returns the singleton instance of the Device class.
83-
[[nodiscard]] static auto get() -> Device&;
84-
8575
/**
8676
* @brief Allocates a new device session.
8777
* @see MQT_NA_QDMI_device_session_alloc
File renamed without changes.

include/mqt-core/qdmi/sc/Device.hpp renamed to include/mqt-core/qdmi/devices/sc/Device.hpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "mqt_sc_qdmi/device.h"
18+
#include "qdmi/common/Common.hpp"
1819

1920
#include <cstddef>
2021
#include <cstdint>
@@ -26,7 +27,9 @@
2627
#include <vector>
2728

2829
namespace qdmi::sc {
29-
class Device final {
30+
class Device final : public Singleton<Device> {
31+
friend class Singleton;
32+
3033
/// @brief Provides access to the device name.
3134
std::string name_;
3235

@@ -51,19 +54,6 @@ class Device final {
5154
Device();
5255

5356
public:
54-
// Delete move constructor and move assignment operator.
55-
Device(Device&&) = delete;
56-
Device& operator=(Device&&) = delete;
57-
// Delete copy constructor and assignment operator to enforce singleton.
58-
Device(const Device&) = delete;
59-
Device& operator=(const Device&) = delete;
60-
61-
/// @brief Destructor for the Device class.
62-
~Device();
63-
64-
/// @returns the singleton instance of the Device class.
65-
[[nodiscard]] static auto get() -> Device&;
66-
6757
/**
6858
* @brief Allocates a new device session.
6959
* @see MQT_SC_QDMI_device_session_alloc

0 commit comments

Comments
 (0)