Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-1-bell-send-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-1-bell-send-example.cc
11 * @brief Bell pair creation and qubit transmission between two nodes.
12 *
13 * Demonstrates: Quantum links, CreateBellPair, Send, receive callbacks.
14 * See docs/README-examples.md for detailed walkthrough.
15 */
16#include "ns3/core-module.h"
17#include "ns3/q2ns-netcontroller.h"
18#include "ns3/q2ns-qnode.h"
19#include "ns3/q2ns-qubit.h"
20#include "ns3/simulator.h"
21
22#include <iostream>
23
24using namespace ns3;
25using namespace q2ns;
26
27int main(int, char**) {
28 std::cout << "[DEMO] Bell send (A->B) starting\n";
29
30 ns3::RngSeedManager::SetSeed(1);
31 ns3::RngSeedManager::SetRun(2);
32
33 NetController net;
34 net.SetQStateBackend(QStateBackend::Stab);
35
36 auto A = net.CreateNode();
37 auto B = net.CreateNode();
38
39 auto ch = net.InstallQuantumLink(A, B);
40 ch->SetAttribute("Delay", TimeValue(NanoSeconds(10)));
41
42 auto [qA, qBremote] = A->CreateBellPair();
43
44 B->SetRecvCallback([&](std::shared_ptr<Qubit>) { std::cout << "[RECV][quantum][B]: yes\n"; });
45
46 // Schedule the send; the qubit arrives at B after the channel delay (10 ns)
47 Simulator::Schedule(NanoSeconds(1), [&]() {
48 bool ok = A->Send(qBremote, B->GetId());
49 std::cout << "[SEND][quantum] A->B: " << (ok ? "ok" : "failed") << "\n";
50 });
51
52 // Schedule measurements after send time + channel delay (1 + 10 + margin = 20 ns)
53 Simulator::Schedule(NanoSeconds(20), [&]() {
54 int mA = A->Measure(qA, q2ns::Basis::Z);
55 int mB = B->Measure(qBremote, q2ns::Basis::Z);
56 std::cout << "[VERIFY] Z-measurements: A=" << mA << " B=" << mB << " (correlated expected)\n";
57 });
58
59 Simulator::Stop(MilliSeconds(1));
60 Simulator::Run();
61
62 std::cout << "[DONE] Bell send (A->B) finished\n";
63
64 Simulator::Destroy();
65 return 0;
66}
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.
ns3::Ptr< QChannel > InstallQuantumLink(ns3::Ptr< QNode > a, ns3::Ptr< QNode > b)
Install a duplex quantum link between two nodes.
void SetQStateBackend(QStateBackend b)
Set the default backend used for newly created quantum states.
int main()