Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-netcontroller.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------
2 * Q2NS - Quantum Network Simulator
3 * Copyright (c) 2026 quantuminternet.it
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *---------------------------------------------------------------------------*/
9/**
10 * @file q2ns-netcontroller.h
11 * @brief Declares q2ns::NetController, the main user-facing facade for
12 * creating and configuring a quantum network in ns-3.
13 */
14
15#pragma once
16
17#include "ns3/ptr.h"
18#include "ns3/simulator.h"
19#include "ns3/traced-callback.h"
20
21#include "ns3/q2ns-qchannel.h"
22#include "ns3/q2ns-qmap.h"
23#include "ns3/q2ns-qstate-registry.h"
24
25#include <cstdint>
26#include <map>
27#include <memory>
28#include <optional>
29#include <string>
30#include <string_view>
31#include <unordered_map>
32#include <utility>
33#include <vector>
34
35namespace q2ns {
36
37class QNode;
38class Qubit;
39class QState;
40
41/**
42 * @ingroup q2ns_api
43 * @class NetController
44 * @brief Main user-facing facade for creating and configuring a quantum network.
45 *
46 * NetController owns the shared QStateRegistry used by the q2ns network and
47 * provides convenience helpers to create QNodes, install QChannels, and query
48 * network objects by node or link.
49 *
50 * Qubit creation and manipulation are normally performed through QNode. In
51 * contrast, NetController is primarily responsible for topology construction,
52 * shared configuration, and deterministic RNG stream assignment.
53 *
54 * Responsibilities:
55 * - Own the shared QStateRegistry for the network.
56 * - Create and track QNode instances.
57 * - Install quantum links, chains, and all-to-all topologies.
58 * - Configure the default QState backend used for newly created states.
59 * - Assign RNG streams to q2ns-owned random sources.
60 *
61 * @see QNode
62 * @see QChannel
63 * @see QStateRegistry
64 */
66public:
67 /**
68 * @brief Default constructor.
69 */
71
72 /**
73 * @brief Access the shared state registry.
74 * @return Reference to the internal QStateRegistry.
75 */
77
78 /**
79 * @brief Set the default backend used for newly created quantum states.
80 * @param b Backend enum value.
81 *
82 * @see SetQStateBackend(std::string_view)
83 * @see GetQStateBackend
84 */
86
87 /**
88 * @brief Set the default backend by name.
89 *
90 * This is a convenience overload intended for CLI or configuration use.
91 *
92 * @param name Backend name such as "ket", "dm", or "stab".
93 *
94 * @see SetQStateBackend(QStateBackend)
95 * @see GetQStateBackend
96 */
97 void SetQStateBackend(std::string_view name);
98
99 /**
100 * @brief Get the current default backend.
101 * @return Current backend enum value.
102 *
103 * @see SetQStateBackend(QStateBackend)
104 * @see SetQStateBackend(std::string_view)
105 */
107
108 /**
109 * @brief Assign RNG streams to q2ns-owned random sources.
110 *
111 * This method should normally be called after the topology has been built and
112 * before the simulation is run. Streams are assigned deterministically from
113 * the provided starting index.
114 *
115 * @param stream Starting stream index.
116 * @return Number of streams consumed.
117 */
118 int64_t AssignStreams(int64_t stream);
119
120 /**
121 * @brief Create a QNode with an optional human-readable label.
122 *
123 * The node is registered internally and bound to this controller's shared
124 * QStateRegistry.
125 *
126 * @param label Optional human-readable label.
127 * @return Pointer to the created QNode.
128 *
129 * @see GetNode
130 * @see GetRegistry
131 */
132 ns3::Ptr<QNode> CreateNode(const std::string& label = "");
133
134 /**
135 * @brief Return the QNode associated with a node id.
136 * @param nodeId Desired node identifier.
137 * @return Pointer to the matching QNode, or nullptr if none exists.
138 *
139 * @see CreateNode
140 */
141 ns3::Ptr<QNode> GetNode(uint32_t nodeId);
142
143 /**
144 * @brief Install a duplex quantum link between two nodes.
145 *
146 * This method creates two QNetDevice instances and one QChannel, attaches one
147 * device to each node, and installs per-destination host routes so each node
148 * can send to the other over the created link.
149 *
150 * @param a First endpoint node.
151 * @param b Second endpoint node.
152 * @return Pointer to the created QChannel, or the existing channel if the
153 * link already exists.
154 *
155 * @see GetChannel
156 * @see InstallQuantumChain
157 * @see InstallQuantumAllToAll
158 */
159 ns3::Ptr<QChannel> InstallQuantumLink(ns3::Ptr<QNode> a, ns3::Ptr<QNode> b);
160
161 /**
162 * @brief Connect a sequence of nodes as a linear chain.
163 *
164 * For a sequence n0, n1, ..., n{k-1}, this installs links
165 * (n0,n1), (n1,n2), ..., (n{k-2},n{k-1}).
166 *
167 * Existing links are left unchanged and their existing channel pointers are
168 * returned in the corresponding positions.
169 *
170 * @param nodes Ordered node sequence.
171 *
172 * @see InstallQuantumLink
173 * @see InstallQuantumAllToAll
174 */
175 std::vector<ns3::Ptr<QChannel>> InstallQuantumChain(const std::vector<ns3::Ptr<QNode>>& nodes);
176
177 /**
178 * @brief Connect a set of nodes with all-to-all quantum links.
179 *
180 * For N nodes, this installs one link for every unordered node pair.
181 * Existing links are left unchanged and their existing channel pointers are
182 * returned in the corresponding positions.
183 *
184 * @param nodes Node set.
185 *
186 * @see InstallQuantumLink
187 * @see InstallQuantumChain
188 */
189 std::vector<ns3::Ptr<QChannel>> InstallQuantumAllToAll(const std::vector<ns3::Ptr<QNode>>& nodes);
190
191 /**
192 * @brief Return the QChannel connecting two nodes.
193 * @param a One endpoint node.
194 * @param b The other endpoint node.
195 * @return Pointer to the connecting QChannel, or nullptr if no such link exists.
196 *
197 * @see InstallQuantumLink
198 */
199 ns3::Ptr<QChannel> GetChannel(ns3::Ptr<QNode> a, ns3::Ptr<QNode> b);
200
201 /**
202 * @brief Convenience helper to get a qubit's current backend state.
203 * @param q Target qubit.
204 * @return Shared pointer to the current QState, or nullptr if unknown.
205 */
206 std::shared_ptr<QState> GetState(const std::shared_ptr<Qubit>& q) const;
207
208private:
209 /**
210 * @brief Ensure RNG streams have been assigned before simulation activity begins.
211 *
212 * This is used as an internal safety net so q2ns behaves deterministically
213 * even if the user calls ns3::Simulator::Run() directly instead of going
214 * through a controller-owned wrapper.
215 */
217
218 bool streamsAssigned_ = false; //!< True once AssignStreams has been called.
219 int64_t nextStream_ = 0; //!< Next stream index to assign.
220
221 QStateRegistry registry_; //!< Shared state registry.
222 std::unordered_map<uint32_t, ns3::Ptr<QNode>> nodes_; //!< Node id to QNode map.
223 std::map<std::pair<uint32_t, uint32_t>, ns3::Ptr<QChannel>>
224 channels_; //!< Undirected link map keyed by sorted endpoint ids.
225};
226
227} // namespace q2ns
Main user-facing facade for creating and configuring a quantum network.
std::map< std::pair< uint32_t, uint32_t >, ns3::Ptr< QChannel > > channels_
Undirected link map keyed by sorted endpoint ids.
QStateRegistry registry_
Shared state registry.
QStateRegistry & GetRegistry()
Access the shared state registry.
void EnsureStreamsAssigned_()
Ensure RNG streams have been assigned before simulation activity begins.
int64_t nextStream_
Next stream index to assign.
ns3::Ptr< QChannel > GetChannel(ns3::Ptr< QNode > a, ns3::Ptr< QNode > b)
Return the QChannel connecting two nodes.
int64_t AssignStreams(int64_t stream)
Assign RNG streams to q2ns-owned random sources.
std::vector< ns3::Ptr< QChannel > > InstallQuantumAllToAll(const std::vector< ns3::Ptr< QNode > > &nodes)
Connect a set of nodes with all-to-all quantum links.
QStateBackend GetQStateBackend()
Get the current default backend.
std::shared_ptr< QState > GetState(const std::shared_ptr< Qubit > &q) const
Convenience helper to get a qubit's current backend state.
ns3::Ptr< QNode > GetNode(uint32_t nodeId)
Return the QNode associated with a node id.
ns3::Ptr< QNode > CreateNode(const std::string &label="")
Create a QNode with an optional human-readable label.
ns3::Ptr< QChannel > InstallQuantumLink(ns3::Ptr< QNode > a, ns3::Ptr< QNode > b)
Install a duplex quantum link between two nodes.
bool streamsAssigned_
True once AssignStreams has been called.
NetController()
Default constructor.
std::unordered_map< uint32_t, ns3::Ptr< QNode > > nodes_
Node id to QNode map.
std::vector< ns3::Ptr< QChannel > > InstallQuantumChain(const std::vector< ns3::Ptr< QNode > > &nodes)
Connect a sequence of nodes as a linear chain.
void SetQStateBackend(QStateBackend b)
Set the default backend used for newly created quantum states.
Internal registry that owns backend states and tracks qubit membership and location.
QStateBackend
Backend family used when creating new quantum states.
Definition q2ns-types.h:94