Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-3-teleportation-basic-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-3-teleportation-basic-example.cc
11 * @brief Teleportation without any classical communication
12 *
13 * Demonstrates: Quantum links, CreateBellPair, Send, receive callbacks.
14 * See docs/README-examples.md for detailed walkthrough.
15 */
16
17#include "ns3/q2ns-netcontroller.h"
18#include "ns3/q2ns-qnode.h"
19#include "ns3/q2ns-qubit.h"
20
21#include "ns3/core-module.h"
22#include "ns3/simulator.h"
23
24#include <iostream>
25
26using namespace ns3;
27using namespace q2ns;
28
29int main() {
30 std::cout << "[DEMO] Teleportation (A->B) starting\n";
31
32 ns3::RngSeedManager::SetSeed(1);
33 ns3::RngSeedManager::SetRun(1);
34
35 NetController net;
36 net.SetQStateBackend(QStateBackend::Ket);
37
38 auto A = net.CreateNode();
39 auto B = net.CreateNode();
40
41 auto ch = net.InstallQuantumLink(A, B);
42 ch->SetAttribute("Delay", TimeValue(NanoSeconds(10)));
43
44 // Schedule the send; the qubit arrives at B after the channel delay (10 ns)
45 // Then Alice prepares her special state to teleport and performs a BSM
46 std::pair<int, int> ms;
47 Simulator::Schedule(NanoSeconds(1), [&]() {
48 auto [qA, qBremote] = A->CreateBellPair();
49 bool ok = A->Send(qBremote, B->GetId());
50 std::cout << "[SEND][quantum] A->B: " << (ok ? "ok" : "failed") << "\n";
51
52 auto qAToTeleport = A->CreateQubit();
53 A->Apply(gates::H(), {qAToTeleport});
54 ms = A->MeasureBell(qAToTeleport, qA);
55 std::cout << "[A] BSM results: " << ms.first << ", " << ms.second << "\n";
56 });
57
58 // Bob applies corrections once he receives the qubit
59 B->SetRecvCallback([&](std::shared_ptr<Qubit> q) {
60 std::cout << "[RECV][quantum][B]: yes\n";
61 std::cout << "[B] Applying corrections: Z^" << ms.first << " X^" << ms.second << "|state>\n";
62
63 if (ms.second)
64 B->Apply(gates::X(), {q});
65 if (ms.first)
66 B->Apply(gates::Z(), {q});
67
68 // Verify: Bob should now have |+>
69 // Measure in X-basis: |+> -> 0 deterministically
70 int mx = B->Measure(q, Basis::X);
71 std::cout << "[B][VERIFY] Final state is correct: " << ((mx == 0) ? "yes" : "no") << "\n";
72 });
73
74
75 Simulator::Stop(Seconds(10));
76 Simulator::Run();
77
78 std::cout << "[DONE] Teleportation (A->B) finished\n";
79
80 Simulator::Destroy();
81 return 0;
82}
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.
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 Z(ns3::Time d=ns3::Seconds(0))
Return the Pauli-Z gate descriptor.
Definition q2ns-qgate.h:374