Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-qgate.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-qgate.h
11 * @brief Gate helpers, gate descriptors, and predefined matrices for the
12 * standard q2ns gate set.
13 */
14
15#pragma once
16
17#include "ns3/q2ns-types.h"
18
19#include "ns3/assert.h"
20#include "ns3/nstime.h"
21
22#include <cmath>
23#include <cstddef>
24#include <initializer_list>
25#include <memory>
26#include <utility>
27
28namespace q2ns {
29
30/**
31 * @ingroup q2ns_core
32 * @brief Enumerates built-in gate kinds.
33 *
34 * Custom represents a caller-supplied unitary matrix. All other values refer
35 * to predefined gates with built-in matrix definitions.
36 */
37enum class QGateKind {
38 Custom, //!< Caller-supplied matrix.
39
40 I,
41 X,
42 Y,
43 Z,
44 H,
45 S,
46 SDG,
47
48 CNOT,
49 CZ,
50 SWAP
51};
52
53/**
54 * @ingroup q2ns_core
55 * @class QGate
56 * @brief Lightweight gate descriptor used by QState backends.
57 *
58 * A QGate stores either:
59 * - a built-in gate kind, or
60 * - a custom unitary matrix
61 *
62 * It may also carry an optional duration value for higher-level scheduling or
63 * interpretation. QGate itself does not enforce timing behavior.
64 *
65 * @see QGateKind
66 * @see gates
67 */
68class QGate {
69public:
70 /**
71 * @brief Default constructor.
72 */
73 QGate() = default;
74
75 /**
76 * @brief Construct a built-in gate descriptor.
77 * @param k Built-in gate kind.
78 * @param d Optional duration metadata.
79 */
80 explicit QGate(QGateKind k, ns3::Time d = ns3::Seconds(0)) : kind_(k), duration_(d) {}
81
82 /**
83 * @brief Return the gate kind.
84 * @return Stored gate kind.
85 */
86 QGateKind Kind() const {
87 return kind_;
88 }
89
90 /**
91 * @brief Return the optional duration metadata.
92 * @return Stored duration.
93 */
94 ns3::Time Duration() const {
95 return duration_;
96 }
97
98 /**
99 * @brief Return the custom unitary matrix.
100 *
101 * This is only valid when Kind() returns QGateKind::Custom.
102 *
103 * @return Stored custom unitary matrix.
104 *
105 * @see Kind
106 */
107 const Matrix& Unitary() const {
108 NS_ASSERT_MSG(U_, "QGate::Unitary() called for a non-custom gate");
109 return *U_;
110 }
111
112 /**
113 * @brief Construct a custom gate by copying a matrix.
114 * @param U Unitary matrix to copy.
115 * @param d Optional duration metadata.
116 * @return Custom gate descriptor.
117 *
118 * @see Custom(Matrix&&, ns3::Time)
119 */
120 static QGate Custom(const Matrix& U, ns3::Time d = ns3::Seconds(0)) {
121 QGate g;
123 g.U_ = std::make_shared<const Matrix>(U);
124 g.duration_ = d;
125 return g;
126 }
127
128 /**
129 * @brief Construct a custom gate by moving a matrix.
130 * @param U Unitary matrix to move.
131 * @param d Optional duration metadata.
132 * @return Custom gate descriptor.
133 *
134 * @see Custom(const Matrix&, ns3::Time)
135 */
136 static QGate Custom(Matrix&& U, ns3::Time d = ns3::Seconds(0)) {
137 QGate g;
139 g.U_ = std::make_shared<const Matrix>(std::move(U));
140 g.duration_ = d;
141 return g;
142 }
143
144private:
145 QGateKind kind_{QGateKind::I}; //!< Gate kind.
146 std::shared_ptr<const Matrix> U_{}; //!< Custom unitary for Custom gates.
147 ns3::Time duration_{ns3::Seconds(0)}; //!< Optional duration metadata.
148};
149
150/**
151 * @brief Build a Matrix from nested initializer lists.
152 * @param rows Matrix rows.
153 * @return Constructed matrix.
154 */
155inline Matrix MakeMatrix(std::initializer_list<std::initializer_list<Complex>> rows) {
156 const std::size_t r = rows.size();
157 const std::size_t c = (r > 0) ? rows.begin()->size() : 0;
158
159 Matrix m(static_cast<int>(r), static_cast<int>(c));
160 std::size_t i = 0;
161 for (const auto& row : rows) {
162 NS_ASSERT_MSG(row.size() == c, "MakeMatrix: ragged initializer_list");
163 std::size_t j = 0;
164 for (const auto& v : row) {
165 m(static_cast<int>(i), static_cast<int>(j)) = v;
166 ++j;
167 }
168 ++i;
169 }
170 return m;
171}
172
173/**
174 * @brief Return the 1-qubit identity matrix.
175 * @return Reference to the cached matrix.
176 */
177inline const Matrix& MatrixI() {
178 static const Matrix I = [] {
179 return MakeMatrix({{Complex{1, 0}, Complex{0, 0}}, {Complex{0, 0}, Complex{1, 0}}});
180 }();
181 return I;
182}
183
184/**
185 * @brief Return the Pauli-X matrix.
186 * @return Reference to the cached matrix.
187 */
188inline const Matrix& MatrixX() {
189 static const Matrix X = [] {
190 return MakeMatrix({{Complex{0, 0}, Complex{1, 0}}, {Complex{1, 0}, Complex{0, 0}}});
191 }();
192 return X;
193}
194
195/**
196 * @brief Return the Pauli-Y matrix.
197 * @return Reference to the cached matrix.
198 */
199inline const Matrix& MatrixY() {
200 static const Matrix Y = [] {
201 return MakeMatrix({{Complex{0, 0}, Complex{0, -1}}, {Complex{0, 1}, Complex{0, 0}}});
202 }();
203 return Y;
204}
205
206/**
207 * @brief Return the Pauli-Z matrix.
208 * @return Reference to the cached matrix.
209 */
210inline const Matrix& MatrixZ() {
211 static const Matrix Z = [] {
212 return MakeMatrix({{Complex{1, 0}, Complex{0, 0}}, {Complex{0, 0}, Complex{-1, 0}}});
213 }();
214 return Z;
215}
216
217/**
218 * @brief Return the Hadamard matrix.
219 * @return Reference to the cached matrix.
220 */
221inline const Matrix& MatrixH() {
222 static const Matrix H = [] {
223 const double s = 1.0 / std::sqrt(2.0);
224 return MakeMatrix({{Complex{s, 0}, Complex{s, 0}}, {Complex{s, 0}, Complex{-s, 0}}});
225 }();
226 return H;
227}
228
229/**
230 * @brief Return the phase gate matrix S = diag(1, i).
231 * @return Reference to the cached matrix.
232 */
233inline const Matrix& MatrixS() {
234 static const Matrix S = [] {
235 return MakeMatrix({{Complex{1, 0}, Complex{0, 0}}, {Complex{0, 0}, Complex{0, 1}}});
236 }();
237 return S;
238}
239
240/**
241 * @brief Return the inverse phase gate matrix SDG = diag(1, -i).
242 * @return Reference to the cached matrix.
243 */
244inline const Matrix& MatrixSDG() {
245 static const Matrix SDG = [] {
246 return MakeMatrix({
247 {Complex{1, 0}, Complex{0, 0}},
248 {Complex{0, 0}, Complex{0, -1}},
249 });
250 }();
251 return SDG;
252}
253
254/**
255 * @brief Return the CNOT matrix with control qubit 0 and target qubit 1.
256 *
257 * Basis ordering is |00>, |01>, |10>, |11>.
258 *
259 * @return Reference to the cached matrix.
260 */
261inline const Matrix& MatrixCNOT() {
262 static const Matrix CNOT = [] {
263 return MakeMatrix({
264 {Complex{1, 0}, Complex{0, 0}, Complex{0, 0}, Complex{0, 0}},
265 {Complex{0, 0}, Complex{1, 0}, Complex{0, 0}, Complex{0, 0}},
266 {Complex{0, 0}, Complex{0, 0}, Complex{0, 0}, Complex{1, 0}},
267 {Complex{0, 0}, Complex{0, 0}, Complex{1, 0}, Complex{0, 0}},
268 });
269 }();
270 return CNOT;
271}
272
273/**
274 * @brief Return the CZ matrix.
275 * @return Reference to the cached matrix.
276 */
277inline const Matrix& MatrixCZ() {
278 static const Matrix CZ = [] {
279 return MakeMatrix({
280 {Complex{1, 0}, Complex{0, 0}, Complex{0, 0}, Complex{0, 0}},
281 {Complex{0, 0}, Complex{1, 0}, Complex{0, 0}, Complex{0, 0}},
282 {Complex{0, 0}, Complex{0, 0}, Complex{1, 0}, Complex{0, 0}},
283 {Complex{0, 0}, Complex{0, 0}, Complex{0, 0}, Complex{-1, 0}},
284 });
285 }();
286 return CZ;
287}
288
289/**
290 * @brief Return the SWAP matrix.
291 *
292 * Basis ordering is |00>, |01>, |10>, |11>.
293 *
294 * @return Reference to the cached matrix.
295 */
296inline const Matrix& MatrixSWAP() {
297 static const Matrix SWAP = [] {
298 return MakeMatrix({
299 {Complex{1, 0}, Complex{0, 0}, Complex{0, 0}, Complex{0, 0}},
300 {Complex{0, 0}, Complex{0, 0}, Complex{1, 0}, Complex{0, 0}},
301 {Complex{0, 0}, Complex{1, 0}, Complex{0, 0}, Complex{0, 0}},
302 {Complex{0, 0}, Complex{0, 0}, Complex{0, 0}, Complex{1, 0}},
303 });
304 }();
305 return SWAP;
306}
307
308/**
309 * @brief Return the built-in matrix for a non-custom gate kind.
310 * @param k Built-in gate kind.
311 * @return Reference to the corresponding cached matrix.
312 */
313inline const Matrix& MatrixOf(QGateKind k) {
314 switch (k) {
315 case QGateKind::I:
316 return MatrixI();
317 case QGateKind::X:
318 return MatrixX();
319 case QGateKind::Y:
320 return MatrixY();
321 case QGateKind::Z:
322 return MatrixZ();
323 case QGateKind::H:
324 return MatrixH();
325 case QGateKind::S:
326 return MatrixS();
327 case QGateKind::SDG:
328 return MatrixSDG();
329 case QGateKind::CNOT:
330 return MatrixCNOT();
331 case QGateKind::CZ:
332 return MatrixCZ();
333 case QGateKind::SWAP:
334 return MatrixSWAP();
335 default:
336 NS_ABORT_MSG("MatrixOf: custom gates do not have a built-in matrix");
337 }
338}
339
340namespace gates {
341
342/**
343 * @brief Return the identity gate descriptor.
344 * @param d Optional duration metadata.
345 * @return Gate descriptor.
346 */
347inline QGate I(ns3::Time d = ns3::Seconds(0)) {
348 return QGate(QGateKind::I, d);
349}
350
351/**
352 * @brief Return the Pauli-X gate descriptor.
353 * @param d Optional duration metadata.
354 * @return Gate descriptor.
355 */
356inline QGate X(ns3::Time d = ns3::Seconds(0)) {
357 return QGate(QGateKind::X, d);
358}
359
360/**
361 * @brief Return the Pauli-Y gate descriptor.
362 * @param d Optional duration metadata.
363 * @return Gate descriptor.
364 */
365inline QGate Y(ns3::Time d = ns3::Seconds(0)) {
366 return QGate(QGateKind::Y, d);
367}
368
369/**
370 * @brief Return the Pauli-Z gate descriptor.
371 * @param d Optional duration metadata.
372 * @return Gate descriptor.
373 */
374inline QGate Z(ns3::Time d = ns3::Seconds(0)) {
375 return QGate(QGateKind::Z, d);
376}
377
378/**
379 * @brief Return the Hadamard gate descriptor.
380 * @param d Optional duration metadata.
381 * @return Gate descriptor.
382 */
383inline QGate H(ns3::Time d = ns3::Seconds(0)) {
384 return QGate(QGateKind::H, d);
385}
386
387/**
388 * @brief Return the phase gate descriptor.
389 * @param d Optional duration metadata.
390 * @return Gate descriptor.
391 */
392inline QGate S(ns3::Time d = ns3::Seconds(0)) {
393 return QGate(QGateKind::S, d);
394}
395
396/**
397 * @brief Return the inverse phase gate descriptor.
398 * @param d Optional duration metadata.
399 * @return Gate descriptor.
400 */
401inline QGate SDG(ns3::Time d = ns3::Seconds(0)) {
402 return QGate(QGateKind::SDG, d);
403}
404
405/**
406 * @brief Return the CNOT gate descriptor.
407 * @param d Optional duration metadata.
408 * @return Gate descriptor.
409 */
410inline QGate CNOT(ns3::Time d = ns3::Seconds(0)) {
411 return QGate(QGateKind::CNOT, d);
412}
413
414/**
415 * @brief Return the CZ gate descriptor.
416 * @param d Optional duration metadata.
417 * @return Gate descriptor.
418 */
419inline QGate CZ(ns3::Time d = ns3::Seconds(0)) {
420 return QGate(QGateKind::CZ, d);
421}
422
423/**
424 * @brief Return the SWAP gate descriptor.
425 * @param d Optional duration metadata.
426 * @return Gate descriptor.
427 */
428inline QGate SWAP(ns3::Time d = ns3::Seconds(0)) {
429 return QGate(QGateKind::SWAP, d);
430}
431
432/**
433 * @brief Return a custom gate descriptor by copying a matrix.
434 * @param U Unitary matrix to copy.
435 * @param d Optional duration metadata.
436 * @return Gate descriptor.
437 */
438inline QGate Custom(const Matrix& U, ns3::Time d = ns3::Seconds(0)) {
439 return QGate::Custom(U, d);
440}
441
442/**
443 * @brief Return a custom gate descriptor by moving a matrix.
444 * @param U Unitary matrix to move.
445 * @param d Optional duration metadata.
446 * @return Gate descriptor.
447 */
448inline QGate Custom(Matrix&& U, ns3::Time d = ns3::Seconds(0)) {
449 return QGate::Custom(std::move(U), d);
450}
451
452} // namespace gates
453
454} // namespace q2ns
Lightweight gate descriptor used by QState backends.
Definition q2ns-qgate.h:68
QGate(QGateKind k, ns3::Time d=ns3::Seconds(0))
Construct a built-in gate descriptor.
Definition q2ns-qgate.h:80
ns3::Time Duration() const
Return the optional duration metadata.
Definition q2ns-qgate.h:94
ns3::Time duration_
Optional duration metadata.
Definition q2ns-qgate.h:147
static QGate Custom(Matrix &&U, ns3::Time d=ns3::Seconds(0))
Construct a custom gate by moving a matrix.
Definition q2ns-qgate.h:136
QGateKind kind_
Gate kind.
Definition q2ns-qgate.h:145
const Matrix & Unitary() const
Return the custom unitary matrix.
Definition q2ns-qgate.h:107
std::shared_ptr< const Matrix > U_
Custom unitary for Custom gates.
Definition q2ns-qgate.h:146
static QGate Custom(const Matrix &U, ns3::Time d=ns3::Seconds(0))
Construct a custom gate by copying a matrix.
Definition q2ns-qgate.h:120
QGateKind Kind() const
Return the gate kind.
Definition q2ns-qgate.h:86
QGate()=default
Default constructor.
std::complex< double > Complex
Complex scalar type used by matrix-based backends.
Definition q2ns-types.h:37
Eigen::MatrixXcd Matrix
Dynamic complex matrix type used for custom gates and matrix-based states.
Definition q2ns-types.h:43
QGateKind
Enumerates built-in gate kinds.
Definition q2ns-qgate.h:37
@ Custom
Caller-supplied matrix.
const Matrix & MatrixH()
Return the Hadamard matrix.
Definition q2ns-qgate.h:221
const Matrix & MatrixZ()
Return the Pauli-Z matrix.
Definition q2ns-qgate.h:210
const Matrix & MatrixS()
Return the phase gate matrix S = diag(1, i).
Definition q2ns-qgate.h:233
const Matrix & MatrixY()
Return the Pauli-Y matrix.
Definition q2ns-qgate.h:199
const Matrix & MatrixCZ()
Return the CZ matrix.
Definition q2ns-qgate.h:277
const Matrix & MatrixSDG()
Return the inverse phase gate matrix SDG = diag(1, -i).
Definition q2ns-qgate.h:244
const Matrix & MatrixOf(QGateKind k)
Return the built-in matrix for a non-custom gate kind.
Definition q2ns-qgate.h:313
Matrix MakeMatrix(std::initializer_list< std::initializer_list< Complex > > rows)
Build a Matrix from nested initializer lists.
Definition q2ns-qgate.h:155
const Matrix & MatrixCNOT()
Return the CNOT matrix with control qubit 0 and target qubit 1.
Definition q2ns-qgate.h:261
const Matrix & MatrixX()
Return the Pauli-X matrix.
Definition q2ns-qgate.h:188
const Matrix & MatrixI()
Return the 1-qubit identity matrix.
Definition q2ns-qgate.h:177
const Matrix & MatrixSWAP()
Return the SWAP matrix.
Definition q2ns-qgate.h:296