Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-2-basis-measurement-example.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-2-basis-measurement-example.cc
11 * @brief Measurement bases, custom circuits, and custom gate definitions in q2ns.
12 *
13 * Demonstrates:
14 * - Preparing eigenstates and measuring in X, Y, and Z bases using the ns-3 scheduler
15 * - Defining a custom gate by composing H and S via matrix product
16 * - Defining the same gate entry-by-entry using MakeMatrix and Complex{re, im}
17 */
18
19#include "ns3/core-module.h"
20#include "ns3/q2ns-netcontroller.h"
21#include "ns3/q2ns-qgate.h"
22#include "ns3/q2ns-qnode.h"
23#include "ns3/q2ns-types.h"
24#include "ns3/rng-seed-manager.h"
25#include <iostream>
26
27using namespace ns3;
28using namespace q2ns;
29
30int main(int, char**) {
31 RngSeedManager::SetSeed(42);
32 RngSeedManager::SetRun(1);
33
34 NetController net;
35 net.SetQStateBackend(QStateBackend::Ket);
36 auto N = net.CreateNode();
37
38 // Allocate all qubits before the simulation starts
39 auto q1 = N->CreateQubit();
40 auto q2 = N->CreateQubit();
41 auto q3 = N->CreateQubit();
42 auto q4 = N->CreateQubit();
43 auto q5 = N->CreateQubit();
44
45 // Pre-compute the custom gate matrices once, before scheduling.
46 // MatrixS() * MatrixH() = S*H (H is rightmost, so it is applied first)
47 auto HS = gates::Custom(MatrixS() * MatrixH());
48
49 // S*H = (1/sqrt(2)) [[1, 1], [i, -i]] -- same gate, written entry-by-entry
50 const double s = 1.0 / std::sqrt(2.0);
51 auto HS_explicit = gates::Custom(MakeMatrix({
52 {Complex{s, 0.0}, Complex{s, 0.0}},
53 {Complex{0.0, s}, Complex{0.0, -s}},
54 }));
55
56 // Test 1 at t=10 us: H|0> -> |+>, measure in X-basis -> always 0
57 Simulator::Schedule(MicroSeconds(10), [N, q1]() {
58 N->Apply(gates::H(), {q1});
59 std::cout << "Test 1 H|0> in X-basis: " << N->Measure(q1, Basis::X)
60 << " (expect 0)\n";
61 });
62
63 // Test 2 at t=20 us: X then H -- |0> -> |1> -> |->, measure in X-basis -> always 1
64 // |-> = (|0> - |1>)/sqrt(2) is the -1 eigenstate of X
65 Simulator::Schedule(MicroSeconds(20), [N, q2]() {
66 N->Apply(gates::X(), {q2}); // |0> -> |1>
67 N->Apply(gates::H(), {q2}); // |1> -> |->
68 std::cout << "Test 2 H|1> in X-basis: " << N->Measure(q2, Basis::X)
69 << " (expect 1)\n";
70 });
71
72 // Test 3 at t=30 us: H then S -- |0> -> |+> -> |+i>, measure in Y-basis -> always 0
73 // S maps |+> to (|0> + i|1>)/sqrt(2) = |+i>, the +1 eigenstate of Y
74 Simulator::Schedule(MicroSeconds(30), [N, q3]() {
75 N->Apply(gates::H(), {q3}); // |0> -> |+>
76 N->Apply(gates::S(), {q3}); // |+> -> |+i>
77 std::cout << "Test 3 S*H|0> in Y-basis: " << N->Measure(q3, Basis::Y)
78 << " (expect 0)\n";
79 });
80
81 // Test 4 at t=40 us: same result using a single custom gate (matrix product form)
82 Simulator::Schedule(MicroSeconds(40), [N, q4, HS]() {
83 N->Apply(HS, {q4});
84 std::cout << "Test 4 Custom HS (S*H)|0> in Y: " << N->Measure(q4, Basis::Y)
85 << " (same as Test 3)\n";
86 });
87
88 // Test 5 at t=50 us: same result using a single custom gate (explicit matrix form)
89 Simulator::Schedule(MicroSeconds(50), [N, q5, HS_explicit]() {
90 N->Apply(HS_explicit, {q5});
91 std::cout << "Test 5 Custom HS (explicit)|0> Y: " << N->Measure(q5, Basis::Y)
92 << " (same as Test 3)\n";
93 });
94
95 Simulator::Stop(MicroSeconds(100));
96 Simulator::Run();
97 Simulator::Destroy();
98 return 0;
99}
Main user-facing facade for creating and configuring a quantum network.
ns3::Ptr< QNode > CreateNode(const std::string &label="")
Create a QNode with an optional human-readable label.
void SetQStateBackend(QStateBackend b)
Set the default backend used for newly created quantum states.
std::complex< double > Complex
Complex scalar type used by matrix-based backends.
Definition q2ns-types.h:37
QGate X(ns3::Time d=ns3::Seconds(0))
Return the Pauli-X gate descriptor.
Definition q2ns-qgate.h:356
QGate H(ns3::Time d=ns3::Seconds(0))
Return the Hadamard gate descriptor.
Definition q2ns-qgate.h:383
QGate S(ns3::Time d=ns3::Seconds(0))
Return the phase gate descriptor.
Definition q2ns-qgate.h:392
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
const Matrix & MatrixH()
Return the Hadamard matrix.
Definition q2ns-qgate.h:221
const Matrix & MatrixS()
Return the phase gate matrix S = diag(1, i).
Definition q2ns-qgate.h:233
Matrix MakeMatrix(std::initializer_list< std::initializer_list< Complex > > rows)
Build a Matrix from nested initializer lists.
Definition q2ns-qgate.h:155
int main()