Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qubit.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-qubit.cc
11 * @brief Defines q2ns::Qubit.
12 */
13
14#include "ns3/q2ns-qubit.h"
15
16#include "ns3/q2ns-qstate-registry.h"
17#include "ns3/q2ns-qstate.h"
18
19#include "ns3/log.h"
20
21#include <utility>
22
23namespace q2ns {
24
25NS_LOG_COMPONENT_DEFINE("Qubit");
26
27
28
29Qubit::Qubit(QStateRegistry& registry, StateId stateId, unsigned int indexInState,
30 std::string label)
31 : registry_(registry), stateId_(stateId), indexInState_(indexInState),
32 label_(std::move(label)) {}
33
34
35
37 return qubitId_;
38}
39
40
41
43 qubitId_ = id;
44}
45
46
47
49 return stateId_;
50}
51
52
53
55 stateId_ = stateId;
56}
57
58
59
60unsigned int Qubit::GetIndexInState() const {
61 return indexInState_;
62}
63
64
65
66void Qubit::SetIndexInState(unsigned int index) {
67 indexInState_ = index;
68}
69
70
71
72const std::string& Qubit::GetLabel() const {
73 return label_;
74}
75
76
77
78void Qubit::SetLabel(std::string label) {
79 label_ = std::move(label);
80}
81
82
83
87
88
89
90void Qubit::SetLocationNode(uint32_t nodeId) {
91 const auto loc = registry_.GetLocation(qubitId_);
92 if (loc.type == LocationType::Lost) {
93 NS_LOG_WARN("SetLocationNode rejected: lost qubits cannot be moved back to a node.");
94 return;
95 }
96
97 registry_.SetLocation(shared_from_this(), Location{LocationType::Node, nodeId});
98}
99
100
101
102void Qubit::SetLocationChannel(uint32_t channelId) {
103 const auto loc = registry_.GetLocation(qubitId_);
104 if (loc.type == LocationType::Lost) {
105 NS_LOG_WARN("SetLocationChannel rejected: lost qubits cannot be moved onto a channel.");
106 return;
107 }
108
109 registry_.SetLocation(shared_from_this(), Location{LocationType::Channel, channelId});
110}
111
112
113
115 registry_.SetLocation(shared_from_this(), Location{LocationType::Lost, 0});
116}
117
118
119
120void Qubit::Rebind(StateId newStateId, std::size_t newIndex) {
121 stateId_ = newStateId;
122 indexInState_ = static_cast<unsigned int>(newIndex);
123}
124
125} // namespace q2ns
Internal registry that owns backend states and tracks qubit membership and location.
void SetLocation(const std::shared_ptr< Qubit > &q, Location loc)
Set the tracked location for a qubit handle.
Location GetLocation(const std::shared_ptr< Qubit > &q) const
Get the tracked location for a qubit handle.
QubitId GetQubitId() const
Get the stable qubit id.
Definition q2ns-qubit.cc:36
std::string label_
Optional human-readable label.
Definition q2ns-qubit.h:158
void SetLabel(std::string label)
Set the application-level label.
Definition q2ns-qubit.cc:78
void SetLocationLost()
Mark this qubit as lost.
Location GetLocation() const
Return the registry-tracked current location of this qubit.
Definition q2ns-qubit.cc:84
void SetIndexInState(unsigned int index)
Update the current index within the backend state.
Definition q2ns-qubit.cc:66
StateId GetStateId() const
Get the current backend state id.
Definition q2ns-qubit.cc:48
Qubit(QStateRegistry &registry, StateId stateId, unsigned int index, std::string label="")
Construct a qubit handle bound to a registry, state id, and index.
Definition q2ns-qubit.cc:29
unsigned int indexInState_
Current zero-based index within the state.
Definition q2ns-qubit.h:157
void Rebind(StateId newStateId, std::size_t newIndex)
Rebind this handle to a different state id and index.
unsigned int GetIndexInState() const
Get the current index within the backend state.
Definition q2ns-qubit.cc:60
QubitId qubitId_
Stable qubit id assigned by the registry.
Definition q2ns-qubit.h:155
const std::string & GetLabel() const
Get the application-level label.
Definition q2ns-qubit.cc:72
void SetLocationNode(uint32_t nodeId)
Mark this qubit as local to a node.
Definition q2ns-qubit.cc:90
void SetQubitId(QubitId id)
Assign the stable qubit id.
Definition q2ns-qubit.cc:42
void SetStateId(StateId stateId)
Update the current backend state id.
Definition q2ns-qubit.cc:54
void SetLocationChannel(uint32_t channelId)
Mark this qubit as in transit on a channel.
QStateRegistry & registry_
Backing state registry.
Definition q2ns-qubit.h:153
StateId stateId_
Current backend state id.
Definition q2ns-qubit.h:156
std::uint64_t QubitId
Stable identifier for a registered qubit handle.
Definition q2ns-types.h:61
std::uint64_t StateId
Stable identifier for a registered backend state.
Definition q2ns-types.h:55
@ Node
Qubit is local to a node identified by node id.
@ Channel
Qubit is in transit on a channel identified by channel id.
@ Lost
Qubit was lost and is no longer accessible.
Current tracked location of a qubit.
Definition q2ns-types.h:136