Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qubit.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-qubit.h
11 * @brief Declares q2ns::Qubit, a lightweight handle for one qubit within a
12 * registry-managed quantum state.
13 */
14
15#pragma once
16
17#include "ns3/q2ns-types.h"
18
19#include <cstdint>
20#include <memory>
21#include <string>
22
23namespace q2ns {
24
25class QStateRegistry;
26class QState;
27
28/**
29 * @ingroup q2ns_api
30 * @class Qubit
31 * @brief Lightweight handle for one qubit inside a registry-managed state.
32 *
33 * Qubit does not own the underlying QState. Instead, it stores:
34 * - a stable qubit id assigned by QStateRegistry
35 * - a state id identifying the current backend state
36 * - a zero-based index within that state
37 * - an optional human-readable label
38 *
39 * Resolution of the underlying backend state and tracked location is performed
40 * through QStateRegistry.
41 *
42 * Qubit is intentionally a lightweight handle object. Most user-facing
43 * operations are performed through QNode rather than directly through Qubit.
44 *
45 * @see QNode
46 * @see QStateRegistry
47 */
48class Qubit : public std::enable_shared_from_this<Qubit> {
49public:
50 /**
51 * @brief Construct a qubit handle bound to a registry, state id, and index.
52 *
53 * The stable qubit id is assigned later by QStateRegistry::Register().
54 *
55 * @param registry Backing state registry.
56 * @param stateId Current backend state id.
57 * @param index Index within that backend state.
58 * @param label Optional human-readable qubit label.
59 *
60 * @see QStateRegistry::Register
61 */
62 Qubit(QStateRegistry& registry, StateId stateId, unsigned int index, std::string label = "");
63
64 /**
65 * @brief Get the stable qubit id.
66 * @return Stable qubit id, or 0 if not yet registered.
67 *
68 * @see QStateRegistry::Register
69 */
70 QubitId GetQubitId() const;
71
72 /**
73 * @brief Get the current backend state id.
74 * @return Current state id.
75 */
76 StateId GetStateId() const;
77
78 /**
79 * @brief Get the current index within the backend state.
80 * @return Zero-based in-state index.
81 */
82 unsigned int GetIndexInState() const;
83
84 /**
85 * @brief Get the application-level label.
86 * @return Label string, possibly empty.
87 */
88 const std::string& GetLabel() const;
89
90 /**
91 * @brief Set the application-level label.
92 * @param label New label string.
93 */
94 void SetLabel(std::string label);
95
96 /**
97 * @brief Return the registry-tracked current location of this qubit.
98 * @return Current tracked location, or Unset if unknown.
99 *
100 * @see QStateRegistry::GetLocation
101 */
102 Location GetLocation() const;
103
104private:
105 /**
106 * @brief Assign the stable qubit id.
107 * @param id Stable qubit id.
108 *
109 * @see QStateRegistry::Register
110 */
111 void SetQubitId(QubitId id);
112
113 /**
114 * @brief Update the current backend state id.
115 * @param stateId New state id.
116 */
117 void SetStateId(StateId stateId);
118
119 /**
120 * @brief Update the current index within the backend state.
121 * @param index New zero-based in-state index.
122 */
123 void SetIndexInState(unsigned int index);
124
125 /**
126 * @brief Rebind this handle to a different state id and index.
127 *
128 * This is an internal helper used during state rewrites such as merging or
129 * splitting.
130 *
131 * @param newStateId New backend state id.
132 * @param newIndex New zero-based in-state index.
133 */
134 void Rebind(StateId newStateId, std::size_t newIndex);
135
136 /**
137 * @brief Mark this qubit as local to a node.
138 * @param nodeId Owning node id.
139 */
140 void SetLocationNode(uint32_t nodeId);
141
142 /**
143 * @brief Mark this qubit as in transit on a channel.
144 * @param channelId Channel id.
145 */
146 void SetLocationChannel(uint32_t channelId);
147
148 /**
149 * @brief Mark this qubit as lost.
150 */
151 void SetLocationLost();
152
153 QStateRegistry& registry_; //!< Backing state registry.
154
155 QubitId qubitId_{0}; //!< Stable qubit id assigned by the registry.
156 StateId stateId_{0}; //!< Current backend state id.
157 unsigned int indexInState_{}; //!< Current zero-based index within the state.
158 std::string label_; //!< Optional human-readable label.
159
160 friend class QStateRegistry;
161 friend class QProcessor;
162 friend class QNetworker;
163 friend class QMap;
164};
165
166} // namespace q2ns
Abstract base class for channel map models.
Definition q2ns-qmap.h:65
Internal helper owned by QNode for node-local quantum networking.
Internal helper owned by a QNode to handle local quantum operations.
Internal registry that owns backend states and tracks qubit membership and location.
Lightweight handle for one qubit inside a registry-managed state.
Definition q2ns-qubit.h:48
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
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
Current tracked location of a qubit.
Definition q2ns-types.h:136