Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qnode.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-qnode.h
11 * @brief Declares q2ns::QNode, the main user-facing per-node API for quantum
12 * operations and transmission.
13 */
14
15#pragma once
16
17#include "ns3/node.h"
18#include "ns3/type-id.h"
19
20#include "ns3/q2ns-qgate.h"
21#include "ns3/q2ns-qstate.h"
22#include "ns3/q2ns-types.h"
23
24#include <cstdint>
25#include <memory>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace ns3 {
31class NetDevice;
32} // namespace ns3
33
34namespace q2ns {
35
36class Qubit;
37class QStateRegistry;
38class QProcessor;
39class QNetworker;
40
41/**
42 * @ingroup q2ns_api
43 * @class QNode
44 * @brief Main user-facing per-node API for quantum operations and transmission.
45 *
46 * QNode is the primary interface through which users create qubits, apply
47 * local operations, perform measurements, and send qubits to other nodes.
48 *
49 * Internally, QNode delegates local quantum state manipulation to QProcessor
50 * and network-facing transmission and reception to QNetworker. These internal
51 * components operate against a shared QStateRegistry.
52 *
53 * Responsibilities:
54 * - Provide user-facing helpers for qubit creation, lookup, measurement,
55 * entanglement, and gate application.
56 * - Own an internal QProcessor for local quantum operations.
57 * - Own an internal QNetworker for transmission and reception.
58 * - Integrate quantum devices with the underlying ns-3 Node device model.
59 *
60 * @see QProcessor
61 * @see QNetworker
62 */
63class QNode : public ns3::Node {
64public:
65 /**
66 * @brief Construct a node bound to a shared state registry.
67 * @param registry Registry used for state and location tracking.
68 */
69 explicit QNode(QStateRegistry& registry);
70
71 /**
72 * @brief Destructor.
73 */
74 ~QNode() override;
75
76 /**
77 * @brief Get the ns-3 TypeId.
78 * @return TypeId for q2ns::QNode.
79 */
80 static ns3::TypeId GetTypeId(void);
81
82 /**
83 * @brief Lookup a local qubit by application label.
84 *
85 * Labels are optional and need not be unique. Prefer GetQubit(QubitId) when a
86 * stable unique identifier is available.
87 *
88 * @param label Application-level qubit label.
89 * @return Matching local qubit, or nullptr if no local match is found.
90 *
91 * @see GetQubit(QubitId)
92 */
93 std::shared_ptr<Qubit> GetQubit(const std::string& label) const;
94
95 /**
96 * @brief Lookup a local qubit by unique identifier.
97 * @param id Qubit identifier.
98 * @return Matching local qubit, or nullptr if the qubit is not local or does
99 * not exist.
100 *
101 * @see GetQubit(const std::string&)
102 */
103 std::shared_ptr<Qubit> GetQubit(QubitId id) const;
104
105 /**
106 * @brief Return the qubits currently located at this node.
107 *
108 * The returned vector is a snapshot taken at call time. No stable internal
109 * container is exposed, and element order is not guaranteed.
110 *
111 * @return Snapshot of qubit handles currently local to this node.
112 */
113 std::vector<std::shared_ptr<Qubit>> GetLocalQubits() const;
114
115 /**
116 * @brief Register a device with this node.
117 *
118 * The device is added to the underlying ns-3 Node. If the device is a
119 * QNetDevice, it is also bound to this node's internal QNetworker so that
120 * quantum arrivals are forwarded upward correctly.
121 *
122 * @param device Device to add.
123 *
124 * @see QNetDevice
125 * @see AddRoute
126 */
127 void AddDevice(ns3::Ptr<ns3::NetDevice> device);
128
129 /**
130 * @brief Add or replace a simple host route for quantum transmission.
131 *
132 * The route maps a destination node id to an outgoing interface index in the
133 * internal QNetworker.
134 *
135 * @param dstNodeId Destination node identifier.
136 * @param oif Outgoing interface index.
137 *
138 * @see Send
139 */
140 void AddRoute(uint32_t dstNodeId, uint32_t oif);
141
142 /**
143 * @brief Send a qubit toward a destination node.
144 *
145 * The qubit must be local to this node and a valid route must exist to the
146 * destination node.
147 *
148 * @param q Qubit to send.
149 * @param dstNodeId Destination node identifier.
150 * @return True if the send request was accepted, false otherwise.
151 *
152 * @see AddRoute
153 * @see SetRecvCallback
154 */
155 bool Send(std::shared_ptr<Qubit> q, uint32_t dstNodeId);
156
157 /**
158 * @brief Set the application-level receive callback.
159 *
160 * The callback is invoked for qubits successfully delivered to this node
161 * after destination-side adoption and channel-map processing.
162 *
163 * @param cb Callback invoked with the arriving qubit.
164 *
165 * @see Send
166 */
168
169 /**
170 * @brief Create a new local qubit initialized in the |0> state.
171 * @param label Optional application-level qubit label.
172 * @return Shared pointer to the new qubit.
173 */
174 std::shared_ptr<Qubit> CreateQubit(const std::string& label = "");
175
176 /**
177 * @brief Create a new local qubit from an existing single-qubit backend state.
178 * @param state Backend state to use for the created qubit.
179 * @param label Optional application-level qubit label.
180 * @return Shared pointer to the new qubit, or nullptr if state is null.
181 *
182 * @see CreateQubit(const std::string&)
183 */
184 std::shared_ptr<Qubit> CreateQubit(const std::shared_ptr<QState>& state,
185 const std::string& label = "");
186
187 /**
188 * @brief Create a local Bell pair in the |Phi+> state.
189 * @return Pair of local qubits {q0, q1}.
190 */
191 std::pair<std::shared_ptr<Qubit>, std::shared_ptr<Qubit>> CreateBellPair();
192
193 /**
194 * @brief Get the current backend state of a qubit.
195 * @param q Target qubit.
196 * @return Shared pointer to the current backend state, or nullptr if the
197 * qubit is null or unknown to the registry.
198 */
199 std::shared_ptr<QState> GetState(const std::shared_ptr<Qubit>& q);
200
201 /**
202 * @brief Apply a gate to one or more local qubits.
203 * @param gate Gate to apply.
204 * @param qs Target qubits.
205 * @return True if the gate was applied successfully, false otherwise.
206 *
207 * @see Apply(const q2ns::Matrix&, const std::vector<std::shared_ptr<Qubit>>&)
208 */
209 bool Apply(const QGate& gate, const std::vector<std::shared_ptr<Qubit>>& qs);
210
211 /**
212 * @brief Apply a custom matrix gate to one or more local qubits.
213 * @param gate Gate matrix to wrap as a custom gate.
214 * @param qs Target qubits.
215 * @return True if the gate was applied successfully, false otherwise.
216 *
217 * @see Apply(const QGate&, const std::vector<std::shared_ptr<Qubit>>&)
218 */
219 bool Apply(const q2ns::Matrix& gate, const std::vector<std::shared_ptr<Qubit>>& qs);
220
221 /**
222 * @brief Measure a local qubit in the given basis.
223 * @param q Target qubit.
224 * @param basis Measurement basis. Defaults to q2ns::Basis::Z.
225 * @return Classical outcome bit (0 or 1), or -1 if the measurement is
226 * rejected.
227 *
228 * @see MeasureBell
229 */
230 int Measure(const std::shared_ptr<Qubit>& q, q2ns::Basis basis = q2ns::Basis::Z);
231
232 /**
233 * @brief Perform a Bell-state measurement on two local qubits.
234 * @param a First qubit.
235 * @param b Second qubit.
236 * @return Pair of classical outcomes {mZZ, mXX}, or {-1, -1} if the
237 * operation is rejected.
238 *
239 * @see Measure
240 * @see QProcessor::MeasureBell
241 */
242 std::pair<int, int> MeasureBell(const std::shared_ptr<Qubit>& a, const std::shared_ptr<Qubit>& b);
243
244private:
245 /**
246 * @brief Mark a qubit as local to this node.
247 *
248 * This is an internal helper used by the receive path. Locality is tracked
249 * centrally by QStateRegistry through QProcessor.
250 *
251 * @param q Qubit handle.
252 */
253 void AdoptQubit(const std::shared_ptr<Qubit>& q);
254
255 QStateRegistry& registry_; //!< Shared state registry.
256 std::unique_ptr<QProcessor> processor_; //!< Internal local quantum processor.
257 std::unique_ptr<QNetworker> networker_; //!< Internal networking component.
258
259 friend class NetController;
260 friend class QNetworker;
261};
262
263} // namespace q2ns
Main user-facing facade for creating and configuring a quantum network.
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Internal helper owned by QNode for node-local quantum networking.
Main user-facing per-node API for quantum operations and transmission.
Definition q2ns-qnode.h:63
QStateRegistry & registry_
Shared state registry.
Definition q2ns-qnode.h:255
std::pair< std::shared_ptr< Qubit >, std::shared_ptr< Qubit > > CreateBellPair()
Create a local Bell pair in the |Phi+> state.
Definition q2ns-qnode.cc:96
int Measure(const std::shared_ptr< Qubit > &q, q2ns::Basis basis=q2ns::Basis::Z)
Measure a local qubit in the given basis.
std::shared_ptr< QState > GetState(const std::shared_ptr< Qubit > &q)
Get the current backend state of a qubit.
~QNode() override
Destructor.
std::shared_ptr< Qubit > GetQubit(const std::string &label) const
Lookup a local qubit by application label.
Definition q2ns-qnode.cc:78
static ns3::TypeId GetTypeId(void)
Get the ns-3 TypeId.
Definition q2ns-qnode.cc:27
void AddDevice(ns3::Ptr< ns3::NetDevice > device)
Register a device with this node.
Definition q2ns-qnode.cc:50
void SetRecvCallback(RecvCallback cb)
Set the application-level receive callback.
bool Send(std::shared_ptr< Qubit > q, uint32_t dstNodeId)
Send a qubit toward a destination node.
std::unique_ptr< QProcessor > processor_
Internal local quantum processor.
Definition q2ns-qnode.h:256
void AddRoute(uint32_t dstNodeId, uint32_t oif)
Add or replace a simple host route for quantum transmission.
Definition q2ns-qnode.cc:44
std::unique_ptr< QNetworker > networker_
Internal networking component.
Definition q2ns-qnode.h:257
std::shared_ptr< Qubit > CreateQubit(const std::string &label="")
Create a new local qubit initialized in the |0> state.
Definition q2ns-qnode.cc:65
std::pair< int, int > MeasureBell(const std::shared_ptr< Qubit > &a, const std::shared_ptr< Qubit > &b)
Perform a Bell-state measurement on two local qubits.
std::vector< std::shared_ptr< Qubit > > GetLocalQubits() const
Return the qubits currently located at this node.
Definition q2ns-qnode.cc:90
void AdoptQubit(const std::shared_ptr< Qubit > &q)
Mark a qubit as local to this node.
bool Apply(const QGate &gate, const std::vector< std::shared_ptr< Qubit > > &qs)
Apply a gate to one or more local qubits.
Internal registry that owns backend states and tracks qubit membership and location.
ns3::Callback< void, std::shared_ptr< Qubit > > RecvCallback
Callback invoked when a qubit is successfully received at a node.
Definition q2ns-types.h:82
std::uint64_t QubitId
Stable identifier for a registered qubit handle.
Definition q2ns-types.h:61
Eigen::MatrixXcd Matrix
Dynamic complex matrix type used for custom gates and matrix-based states.
Definition q2ns-types.h:43
Basis
Measurement basis for single-qubit projective measurement.
Definition q2ns-types.h:88