Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qchannel.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-qchannel.h
11 * @brief Declares q2ns::QChannel, a duplex quantum channel supporting
12 * per-direction delay, jitter, and transit quantum maps.
13 */
14
15#pragma once
16
17#include "ns3/channel.h"
18#include "ns3/nstime.h"
19#include "ns3/ptr.h"
20#include "ns3/random-variable-stream.h"
21#include "ns3/type-id.h"
22
23#include <cstdint>
24#include <memory>
25
26namespace q2ns {
27
28class Qubit;
29class QNetDevice;
30class QMap;
31
32/**
33 * @ingroup q2ns_network
34 * @class QChannel
35 * @brief Duplex quantum channel with configurable delay, jitter, and transit maps.
36 *
37 * QChannel connects exactly two QNetDevice endpoints, referred to as A and B.
38 * Delay, jitter, and QMap configuration may be set symmetrically for both
39 * directions or independently for A->B and B->A.
40 *
41 * On each transmission, the channel selects the direction-specific delay,
42 * applies an optional random jitter to that delay, samples the direction-
43 * specific QMap to obtain a per-transmission QMapInstance, and schedules
44 * delivery to the opposite endpoint.
45 *
46 * The sampled QMapInstance is not applied immediately in the channel. Instead,
47 * it is delivered alongside the qubit and later executed at the receiving node
48 * after the qubit becomes local there.
49 *
50 * @see QNetDevice
51 * @see QMap
52 */
53class QChannel : public ns3::Channel {
54public:
55 /**
56 * @brief Get the ns-3 TypeId.
57 * @return TypeId for q2ns::QChannel.
58 */
59 static ns3::TypeId GetTypeId(void);
60
61 /**
62 * @brief Default constructor.
63 */
64 QChannel();
65
66 /**
67 * @brief Set the same propagation delay in both directions.
68 * @param d Propagation delay.
69 *
70 * @see SetDelayAB
71 * @see SetDelayBA
72 */
73 void SetDelay(ns3::Time d);
74
75 /**
76 * @brief Get the A->B propagation delay.
77 *
78 * If the channel is configured asymmetrically, this returns the A->B delay.
79 * Use GetDelayBA() to query the opposite direction explicitly.
80 *
81 * @return A->B propagation delay.
82 *
83 * @see GetDelayAB
84 * @see GetDelayBA
85 */
86 ns3::Time GetDelay() const;
87
88 /**
89 * @brief Set the same jitter in both directions.
90 *
91 * Jitter is sampled uniformly in [-jitter, +jitter] and added to the base
92 * delay on each transmission, with a minimum resulting delay of zero.
93 *
94 * @param j Symmetric jitter magnitude.
95 *
96 * @see SetJitterAB
97 * @see SetJitterBA
98 */
99 void SetJitter(ns3::Time j);
100
101 /**
102 * @brief Get the A->B jitter.
103 *
104 * If the channel is configured asymmetrically, this returns the A->B jitter.
105 * Use GetJitterBA() to query the opposite direction explicitly.
106 *
107 * @return A->B jitter magnitude.
108 *
109 * @see GetJitterAB
110 * @see GetJitterBA
111 */
112 ns3::Time GetJitter() const;
113
114 /**
115 * @brief Set the same transit QMap in both directions.
116 *
117 * A null map is treated as the identity map.
118 *
119 * @param m Symmetric transit QMap.
120 *
121 * @see SetQMapAB
122 * @see SetQMapBA
123 */
124 void SetQMap(ns3::Ptr<QMap> m);
125
126 /**
127 * @brief Get the A->B transit QMap.
128 *
129 * If the channel is configured asymmetrically, this returns the A->B map.
130 * Use GetQMapBA() to query the opposite direction explicitly.
131 *
132 * @return A->B transit QMap, or nullptr for identity behavior.
133 *
134 * @see GetQMapAB
135 * @see GetQMapBA
136 */
137 ns3::Ptr<QMap> GetQMap() const;
138
139 /**
140 * @brief Set the one-way delay from A to B.
141 * @param d Propagation delay from A to B.
142 *
143 * @see SetDelay
144 * @see GetDelayAB
145 */
146 void SetDelayAB(ns3::Time d);
147
148 /**
149 * @brief Get the one-way delay from A to B.
150 * @return Propagation delay from A to B.
151 */
152 ns3::Time GetDelayAB() const;
153
154 /**
155 * @brief Set the one-way delay from B to A.
156 * @param d Propagation delay from B to A.
157 *
158 * @see SetDelay
159 * @see GetDelayBA
160 */
161 void SetDelayBA(ns3::Time d);
162
163 /**
164 * @brief Get the one-way delay from B to A.
165 * @return Propagation delay from B to A.
166 */
167 ns3::Time GetDelayBA() const;
168
169 /**
170 * @brief Set the one-way jitter from A to B.
171 * @param j Jitter magnitude from A to B.
172 *
173 * @see SetJitter
174 * @see GetJitterAB
175 */
176 void SetJitterAB(ns3::Time j);
177
178 /**
179 * @brief Get the one-way jitter from A to B.
180 * @return Jitter magnitude from A to B.
181 */
182 ns3::Time GetJitterAB() const;
183
184 /**
185 * @brief Set the one-way jitter from B to A.
186 * @param j Jitter magnitude from B to A.
187 *
188 * @see SetJitter
189 * @see GetJitterBA
190 */
191 void SetJitterBA(ns3::Time j);
192
193 /**
194 * @brief Get the one-way jitter from B to A.
195 * @return Jitter magnitude from B to A.
196 */
197 ns3::Time GetJitterBA() const;
198
199 /**
200 * @brief Set the one-way transit QMap from A to B.
201 * @param m Transit QMap from A to B.
202 *
203 * @see SetQMap
204 * @see GetQMapAB
205 */
206 void SetQMapAB(ns3::Ptr<QMap> m);
207
208 /**
209 * @brief Get the one-way transit QMap from A to B.
210 * @return Transit QMap from A to B, or nullptr for identity behavior.
211 */
212 ns3::Ptr<QMap> GetQMapAB() const;
213
214 /**
215 * @brief Set the one-way transit QMap from B to A.
216 * @param m Transit QMap from B to A.
217 *
218 * @see SetQMap
219 * @see GetQMapBA
220 */
221 void SetQMapBA(ns3::Ptr<QMap> m);
222
223 /**
224 * @brief Get the one-way transit QMap from B to A.
225 * @return Transit QMap from B to A, or nullptr for identity behavior.
226 */
227 ns3::Ptr<QMap> GetQMapBA() const;
228
229 /**
230 * @brief Connect two QNetDevice endpoints to this channel.
231 *
232 * The provided devices become endpoints A and B. Each device is also updated
233 * to reference this channel.
234 *
235 * @param a Endpoint A device.
236 * @param b Endpoint B device.
237 *
238 * @see QNetDevice::AttachChannel
239 */
240 void Connect(ns3::Ptr<QNetDevice> a, ns3::Ptr<QNetDevice> b);
241
242 /**
243 * @brief Return the number of attached devices.
244 * @return Number of attached devices in the range [0, 2].
245 */
246 std::size_t GetNDevices() const override;
247
248 /**
249 * @brief Return an attached device by endpoint index.
250 * @param i Endpoint index, where 0 selects A and 1 selects B.
251 * @return Attached device pointer, or nullptr if the index is invalid or the
252 * endpoint is not attached.
253 */
254 ns3::Ptr<ns3::NetDevice> GetDevice(std::size_t i) const override;
255
256 /**
257 * @brief Assign RNG streams used by this channel.
258 *
259 * Streams are assigned to the jitter and QMap sampling random variables.
260 *
261 * @param stream Starting stream index.
262 * @return Number of streams consumed.
263 */
264 int64_t AssignStreams(int64_t stream);
265
266protected:
267 /**
268 * @brief Protected destructor to prevent stack allocation.
269 */
270 ~QChannel() override = default;
271
272private:
273 /**
274 * @brief Send a qubit from one endpoint to the opposite endpoint.
275 *
276 * The source device must be one of the two attached endpoints. The direction-
277 * specific delay, jitter, and QMap are selected from that source endpoint.
278 * Delivery is scheduled after the resulting propagation delay.
279 *
280 * @param src Source device. Must be either endpoint A or endpoint B.
281 * @param q Qubit to transmit.
282 * @return True if the transmission request was accepted, false otherwise.
283 */
284 bool SendFrom(QNetDevice* src, std::shared_ptr<Qubit> q);
285
286 ns3::Ptr<QNetDevice> aDev_; //!< Device attached to endpoint A.
287 ns3::Ptr<QNetDevice> bDev_; //!< Device attached to endpoint B.
288
289 ns3::Time delayAB_{}; //!< Propagation delay from A to B.
290 ns3::Time delayBA_{}; //!< Propagation delay from B to A.
291
292 ns3::Time jitterAB_{ns3::Time(0.0)}; //!< Jitter magnitude from A to B.
293 ns3::Time jitterBA_{ns3::Time(0.0)}; //!< Jitter magnitude from B to A.
294 ns3::Ptr<ns3::UniformRandomVariable> jitterRv_; //!< Jitter sampler.
295
296 ns3::Ptr<QMap> qmapAB_; //!< Transit QMap from A to B. Null means identity.
297 ns3::Ptr<QMap> qmapBA_; //!< Transit QMap from B to A. Null means identity.
298 ns3::Ptr<ns3::UniformRandomVariable> mapRv_; //!< QMap sampler.
299
301};
302
303} // namespace q2ns
Duplex quantum channel with configurable delay, jitter, and transit maps.
ns3::Ptr< ns3::NetDevice > GetDevice(std::size_t i) const override
Return an attached device by endpoint index.
void SetJitter(ns3::Time j)
Set the same jitter in both directions.
void SetJitterBA(ns3::Time j)
Set the one-way jitter from B to A.
std::size_t GetNDevices() const override
Return the number of attached devices.
ns3::Time delayAB_
Propagation delay from A to B.
ns3::Time delayBA_
Propagation delay from B to A.
ns3::Ptr< QMap > GetQMap() const
Get the A->B transit QMap.
bool SendFrom(QNetDevice *src, std::shared_ptr< Qubit > q)
Send a qubit from one endpoint to the opposite endpoint.
void SetDelay(ns3::Time d)
Set the same propagation delay in both directions.
~QChannel() override=default
Protected destructor to prevent stack allocation.
void SetQMap(ns3::Ptr< QMap > m)
Set the same transit QMap in both directions.
ns3::Ptr< ns3::UniformRandomVariable > jitterRv_
Jitter sampler.
void SetDelayAB(ns3::Time d)
Set the one-way delay from A to B.
void SetJitterAB(ns3::Time j)
Set the one-way jitter from A to B.
ns3::Ptr< QMap > qmapAB_
Transit QMap from A to B. Null means identity.
ns3::Ptr< QNetDevice > aDev_
Device attached to endpoint A.
ns3::Ptr< QNetDevice > bDev_
Device attached to endpoint B.
void SetQMapAB(ns3::Ptr< QMap > m)
Set the one-way transit QMap from A to B.
int64_t AssignStreams(int64_t stream)
Assign RNG streams used by this channel.
void SetQMapBA(ns3::Ptr< QMap > m)
Set the one-way transit QMap from B to A.
ns3::Time GetJitterAB() const
Get the one-way jitter from A to B.
void SetDelayBA(ns3::Time d)
Set the one-way delay from B to A.
ns3::Ptr< QMap > GetQMapBA() const
Get the one-way transit QMap from B to A.
static ns3::TypeId GetTypeId(void)
Get the ns-3 TypeId.
ns3::Time GetDelay() const
Get the A->B propagation delay.
void Connect(ns3::Ptr< QNetDevice > a, ns3::Ptr< QNetDevice > b)
Connect two QNetDevice endpoints to this channel.
ns3::Time GetDelayAB() const
Get the one-way delay from A to B.
ns3::Time jitterBA_
Jitter magnitude from B to A.
ns3::Ptr< QMap > qmapBA_
Transit QMap from B to A. Null means identity.
ns3::Time GetJitter() const
Get the A->B jitter.
ns3::Time jitterAB_
Jitter magnitude from A to B.
ns3::Time GetJitterBA() const
Get the one-way jitter from B to A.
ns3::Time GetDelayBA() const
Get the one-way delay from B to A.
ns3::Ptr< ns3::UniformRandomVariable > mapRv_
QMap sampler.
QChannel()
Default constructor.
ns3::Ptr< QMap > GetQMapAB() const
Get the one-way transit QMap from A to B.
Minimal quantum net device that bridges a QChannel and a QNetworker.