Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qprocessor.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-qprocessor.h
11 * @brief Declares q2ns::QProcessor, a per-node processor that handles local quantum state
12 * operations.
13 */
14
15#pragma once
16
17#include "ns3/q2ns-qgate.h"
18#include "ns3/q2ns-types.h"
19
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24
25namespace q2ns {
26
27class QStateRegistry;
28class QNode;
29class QState;
30class Qubit;
31
32
33/**
34 * @ingroup q2ns_qstate
35 * @class QProcessor
36 * @brief Internal helper owned by a QNode to handle local quantum operations.
37 *
38 * QProcessor is not intended to be the main user-facing API. Instead, users should use QNode, which
39 * will internally delegate to QProcessor where necessary.
40 *
41 * Responsibilities:
42 * - Create and adopt qubit handles associated with the owning node (via registry location).
43 * - Enforce locality checks for operations intended to be local to the owning node.
44 * - Delegate gates, measurement, and state merging/splitting to QStateRegistry and QState.
45 *
46 * QProcessor does not own a persistent local-qubit container; locality is tracked centrally and
47 * only by QStateRegistry.
48 */
50public:
51 /**
52 * @brief Construct a processor bound to a registry and owning node.
53 * @param registry Reference to the global quantum state registry.
54 * @param owner Reference to the owning node.
55 */
56 QProcessor(QStateRegistry& registry, QNode& owner);
57
58 /**
59 * @brief Get owning node.
60 * @return Reference to the owning node.
61 */
62 const QNode& GetOwnerNode() const;
63
64 /**
65 * @brief Create a new local qubit in |0>.
66 * @param label Optional human-readable qubit label.
67 * @return Shared pointer to the new qubit.
68 */
69 std::shared_ptr<Qubit> CreateQubit(const std::string& label = "");
70
71 /**
72 * @brief Create a new local qubit handle in the given state.
73 * @param state State to prepare the qubit in.
74 * @param label Optional human-readable qubit label.
75 * @return Shared pointer to the new qubit, or nullptr if @p state is null.
76 */
77 std::shared_ptr<Qubit> CreateQubit(const std::shared_ptr<QState>& state,
78 const std::string& label = "");
79
80 /**
81 * @brief Create a local Bell pair in |Phi+>
82 * @return Pair of local qubits {q0, q1}.
83 */
84 std::pair<std::shared_ptr<Qubit>, std::shared_ptr<Qubit>> CreateBellPair();
85
86 /**
87 * @brief Lookup a local qubit by application label.
88 *
89 * Labels are optional and need not be unique. Prefer GetQubit(QubitId) when a
90 * stable unique identifier is available.
91 *
92 * @param label Application-level qubit label.
93 * @return Matching local qubit, or nullptr if no local match is found.
94 *
95 * @see GetQubit(QubitId)
96 */
97 std::shared_ptr<Qubit> GetQubit(const std::string& label) const;
98
99 /**
100 * @brief Lookup a local qubit by unique identifier.
101 * @param id Qubit identifier.
102 * @return Matching local qubit, or nullptr if the qubit is not local or does
103 * not exist.
104 *
105 * @see GetQubit(const std::string&)
106 */
107 std::shared_ptr<Qubit> GetQubit(QubitId id) const;
108
109 /**
110 * @brief Return the qubits currently located at the owning node.
111 *
112 * The returned vector is a snapshot taken at call time. No stable internal
113 * container is exposed, and element order is not guaranteed.
114 *
115 * @return Snapshot of qubit handles currently local to the owning node.
116 */
117 std::vector<std::shared_ptr<Qubit>> GetLocalQubits() const;
118
119 /**
120 * @brief Adopt a qubit into this processor's owning node.
121 *
122 * Adoption updates the qubit's registry-authoritative location to the owning
123 * node.
124 *
125 * @param q Qubit handle.
126 */
127 void AdoptQubit(const std::shared_ptr<Qubit>& q);
128
129 /**
130 * @brief Get the current state of the qubit.
131 * @param q Target qubit.
132 * @return Shared pointer to the QState, or nullptr if the
133 * qubit is null or unknown to the registry.
134 */
135 std::shared_ptr<QState> GetState(const std::shared_ptr<Qubit>& q) const;
136
137 /**
138 * @brief Apply a gate to one or more local qubits.
139 * @param gate Gate to apply.
140 * @param qs Target qubits. All must be local and not lost.
141 * @return True if the application was successful, false otherwise.
142 */
143 bool Apply(const QGate& gate, const std::vector<std::shared_ptr<Qubit>>& qs);
144
145 /**
146 * @brief Measure a single, local qubit in the given basis (default Z).
147 *
148 * The measured qubit is rebound to a new one-qubit state. Any surviving
149 * qubits from the original backend state are rebound to a new survivor state.
150 *
151 * @param [in] qubit Target qubit.
152 * @param [in] basis Measurement basis.
153 * @return Classical outcome bit (0 or 1), or -1 if the measurement is
154 * rejected.
155 */
156 int Measure(const std::shared_ptr<Qubit>& qubit, q2ns::Basis basis = q2ns::Basis::Z);
157
158 /**
159 * @brief Perform a Bell-state measurement on two local qubits.
160 *
161 * This implementation performs a Bell-basis rotation using CNOT(a,b)
162 * followed by H(a), then measures both qubits in the computational basis.
163 *
164 * @param a First qubit.
165 * @param b Second qubit.
166 * @return Pair of classical outcomes {mZZ, mXX}, or {-1, -1} if the
167 * operation is rejected.
168 *
169 * @see Measure
170 */
171 std::pair<int, int> MeasureBell(const std::shared_ptr<Qubit>& a, const std::shared_ptr<Qubit>& b);
172
173private:
174 QStateRegistry& registry_; //!< Quantum state registry.
175 QNode& owner_; //!< Owning QNode.
176};
177
178} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
Main user-facing per-node API for quantum operations and transmission.
Definition q2ns-qnode.h:63
Internal helper owned by a QNode to handle local quantum operations.
int Measure(const std::shared_ptr< Qubit > &qubit, q2ns::Basis basis=q2ns::Basis::Z)
Measure a single, local qubit in the given basis (default Z).
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.
void AdoptQubit(const std::shared_ptr< Qubit > &q)
Adopt a qubit into this processor's owning node.
std::shared_ptr< QState > GetState(const std::shared_ptr< Qubit > &q) const
Get the current state of the qubit.
std::pair< std::shared_ptr< Qubit >, std::shared_ptr< Qubit > > CreateBellPair()
Create a local Bell pair in |Phi+>
QNode & owner_
Owning QNode.
std::shared_ptr< Qubit > GetQubit(const std::string &label) const
Lookup a local qubit by application label.
const QNode & GetOwnerNode() const
Get owning node.
std::shared_ptr< Qubit > CreateQubit(const std::string &label="")
Create a new local qubit in |0>.
bool Apply(const QGate &gate, const std::vector< std::shared_ptr< Qubit > > &qs)
Apply a gate to one or more local qubits.
QStateRegistry & registry_
Quantum state registry.
std::vector< std::shared_ptr< Qubit > > GetLocalQubits() const
Return the qubits currently located at the owning node.
Internal registry that owns backend states and tracks qubit membership and location.
std::uint64_t QubitId
Stable identifier for a registered qubit handle.
Definition q2ns-types.h:61
Basis
Measurement basis for single-qubit projective measurement.
Definition q2ns-types.h:88