Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qnode.cc
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.cc
11 * @brief Defines q2ns::QNode.
12 */
13
14#include "ns3/q2ns-qnode.h"
15
16#include "ns3/q2ns-qnet-device.h"
17#include "ns3/q2ns-qstate-registry.h"
18#include "ns3/q2ns-qstate.h"
19#include "ns3/q2ns-qubit.h"
20#include "q2ns-qnetworker.h"
21#include "q2ns-qprocessor.h"
22
23namespace q2ns {
24
25NS_OBJECT_ENSURE_REGISTERED(QNode);
26
27ns3::TypeId QNode::GetTypeId(void) {
28 static ns3::TypeId tid = ns3::TypeId("q2ns::QNode").SetParent<ns3::Node>().SetGroupName("q2ns");
29 return tid;
30}
31
32
33
35 : registry_(registry), processor_(std::make_unique<QProcessor>(registry_, *this)),
36 networker_(std::make_unique<QNetworker>(*this)) {}
37
38
39
40QNode::~QNode() = default;
41
42
43
44void QNode::AddRoute(uint32_t dstNodeId, uint32_t oif) {
45 networker_->AddRoute(dstNodeId, oif);
46}
47
48
49
50void QNode::AddDevice(ns3::Ptr<ns3::NetDevice> device) {
51 // Register with the underlying ns-3 Node first so the device participates in
52 // the normal node device inventory regardless of whether it is quantum-aware.
53 ns3::Node::AddDevice(device);
54
55 // If the device is quantum-aware, also bind it to the internal QNetworker so
56 // arrivals from the channel are forwarded into the node's quantum stack.
57 if (auto qdev = ns3::DynamicCast<q2ns::QNetDevice>(device)) {
58 qdev->SetNode(this);
59 qdev->BindNetworker(*networker_);
60 }
61}
62
63
64
65std::shared_ptr<Qubit> QNode::CreateQubit(const std::string& label) {
66 return processor_->CreateQubit(label);
67}
68
69
70
71std::shared_ptr<Qubit> QNode::CreateQubit(const std::shared_ptr<QState>& state,
72 const std::string& label) {
73 return processor_->CreateQubit(state, label);
74}
75
76
77
78std::shared_ptr<Qubit> QNode::GetQubit(const std::string& label) const {
79 return processor_->GetQubit(label);
80}
81
82
83
84std::shared_ptr<Qubit> QNode::GetQubit(QubitId id) const {
85 return processor_->GetQubit(id);
86}
87
88
89
90std::vector<std::shared_ptr<Qubit>> QNode::GetLocalQubits() const {
91 return processor_->GetLocalQubits();
92}
93
94
95
96std::pair<std::shared_ptr<Qubit>, std::shared_ptr<Qubit>> QNode::CreateBellPair() {
97 return processor_->CreateBellPair();
98}
99
100
101
102std::shared_ptr<QState> QNode::GetState(const std::shared_ptr<Qubit>& q) {
103 return processor_->GetState(q);
104}
105
106
107
108bool QNode::Apply(const QGate& gate, const std::vector<std::shared_ptr<Qubit>>& qs) {
109 return processor_->Apply(gate, qs);
110}
111
112
113
114bool QNode::Apply(const q2ns::Matrix& gate, const std::vector<std::shared_ptr<Qubit>>& qs) {
115 return Apply(q2ns::gates::Custom(gate), qs);
116}
117
118
119
120int QNode::Measure(const std::shared_ptr<Qubit>& q, q2ns::Basis basis) {
121 return processor_->Measure(q, basis);
122}
123
124
125
126std::pair<int, int> QNode::MeasureBell(const std::shared_ptr<Qubit>& a,
127 const std::shared_ptr<Qubit>& b) {
128 return processor_->MeasureBell(a, b);
129}
130
131
132
133bool QNode::Send(std::shared_ptr<Qubit> q, uint32_t dstNodeId) {
134 return networker_->Send(std::move(q), dstNodeId);
135}
136
137
138
140 networker_->SetRecvCallback(std::move(cb));
141}
142
143
144
145void QNode::AdoptQubit(const std::shared_ptr<Qubit>& q) {
146 processor_->AdoptQubit(q);
147}
148
149
150
151} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Internal helper owned by QNode for node-local quantum networking.
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(QStateRegistry &registry)
Construct a node bound to a shared state registry.
Definition q2ns-qnode.cc:34
~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 helper owned by a QNode to handle local quantum operations.
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
QGate Custom(const Matrix &U, ns3::Time d=ns3::Seconds(0))
Return a custom gate descriptor by copying a matrix.
Definition q2ns-qgate.h:438
Declares q2ns::QNetworker, an internal per-node networking component for quantum transmission and rec...
Declares q2ns::QProcessor, a per-node processor that handles local quantum state operations.