Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-2-qmap-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-qmap-example.cc
11 * @brief Example of using different quantum maps.
12 *
13 * Demonstrates the usage of various quantum maps in q2ns.
14 * Run with different --mode values to see different maps in action:
15 * - loss+depol: a composition of LossQMap and DepolarizingQMap
16 * - randomgate: a RandomGateQMap with X and Z gates
17 * - randomunitary: a RandomUnitaryQMap applying Haar-random unitaries
18 * - conditional: a ConditionalQMap that applies loss only if flight time > 20 ns
19 * - lambda: a QMap from a simple lambda that applies S gate
20 * - lambda-random: a QMap from a lambda that applies X with probability based on a rate and flight
21 * time
22 */
23
24#include "ns3/core-module.h"
25#include "ns3/network-module.h"
26
27#include "ns3/q2ns-netcontroller.h"
28#include "ns3/q2ns-qmap.h"
29#include "ns3/q2ns-qnode.h"
30#include "ns3/q2ns-qstate.h"
31#include "ns3/q2ns-qubit.h"
32#include "ns3/simulator.h"
33
34#include <iostream>
35#include <random>
36#include <string>
37
38using namespace ns3;
39using namespace q2ns;
40
41int main(int argc, char* argv[]) {
42
43 RngSeedManager::SetSeed(std::random_device{}() | 1u);
44
45 std::string mode = "loss+depol"; // default demo
46 double lossP = 0.5;
47 double depolRate = 1e8;
48
49 CommandLine cmd;
50 cmd.AddValue(
51 "mode",
52 "Demo mode: loss+depol | randomgate | randomunitary | conditional | lambda | lambda-random",
53 mode);
54 cmd.AddValue("lossP", "Loss probability used in loss+depol mode", lossP);
55 cmd.AddValue("depolRate", "Depolarizing rate [1/s] used in loss+depol mode", depolRate);
56 cmd.Parse(argc, argv);
57
58 NetController net;
59 auto A = net.CreateNode();
60 auto B = net.CreateNode();
61
62 B->SetRecvCallback([&net](std::shared_ptr<Qubit> q) {
63 std::cout << "Qubit received at " << Simulator::Now() << "\n";
64 std::cout << "In state: " << net.GetState(q) << "\n";
65 });
66
67 Ptr<QMap> map;
68 std::cout << "Running " << mode << "\n";
69
70 if (mode == "loss+depol") {
71 auto loss = CreateObject<LossQMap>();
72 loss->SetAttribute("Probability", DoubleValue(lossP));
73
74 auto depol = CreateObject<DepolarizingQMap>();
75 depol->SetAttribute("Rate", DoubleValue(depolRate));
76
77 map = QMap::Compose({loss, depol});
78 } else if (mode == "randomgate") {
79 auto rg = CreateObject<RandomGateQMap>();
80 rg->AddGate(gates::X(), 1.0);
81 rg->AddGate(gates::Z(), 2.0);
82 rg->SetAttribute("Probability", DoubleValue(1.0));
83 map = rg;
84 } else if (mode == "randomunitary") {
85 auto ru = CreateObject<RandomUnitaryQMap>();
86 ru->SetAttribute("Probability", DoubleValue(1.0));
87 map = ru;
88 } else if (mode == "conditional") {
89 auto loss = CreateObject<LossQMap>();
90 loss->SetAttribute("Probability", DoubleValue(1.0));
91
92 auto cond = CreateObject<ConditionalQMap>();
93 cond->SetQMap(loss);
94 cond->SetCondition([](const std::shared_ptr<Qubit>&, const QMapContext& ctx) {
95 return ctx.elapsedTime > NanoSeconds(20);
96 });
97 map = cond;
98 } else if (mode == "lambda") {
99 map = QMap::FromLambda(
100 [](QNode& node, std::shared_ptr<Qubit>& q) { node.Apply(gates::S(), {q}); });
101 } else if (mode == "lambda-random") {
102 const double rate = 5e6; // 1/s
103 map = QMap::FromLambda([rate](QNode& node, std::shared_ptr<Qubit>& q,
104 Ptr<UniformRandomVariable> u, const QMapContext& ctx) {
105 const double p = QMap::RateToProb(rate, ctx.elapsedTime);
106 if (p > 0.0 && u->GetValue(0.0, 1.0) < p) {
107 node.Apply(gates::X(), {q});
108 }
109 });
110 } else {
111 NS_ABORT_MSG("Unknown mode: " << mode);
112 }
113
114 auto ch = net.InstallQuantumLink(A, B);
115 ch->SetAttribute("Delay", TimeValue(NanoSeconds(10)));
116 ch->SetAttribute("QMap", PointerValue(map));
117
118 // Allocate and prepare the qubit before the simulation starts
119 auto q = A->CreateQubit();
120 A->Apply(gates::H(), {q}); // prepare |+>
121
122 // Schedule the send; the QMap fires at B after the link delay
123 Simulator::Schedule(NanoSeconds(1), [A, B, q]() { A->Send(q, B->GetId()); });
124
125 Simulator::Stop(MilliSeconds(1));
126 Simulator::Run();
127
128 const bool lost = (q->GetLocation().type == LocationType::Lost);
129 std::cout << "Qubit status: " << (lost ? "LOST" : "delivered") << "\n";
130
131 Simulator::Destroy();
132 return 0;
133}
Main user-facing facade for creating and configuring a quantum network.
std::shared_ptr< QState > GetState(const std::shared_ptr< Qubit > &q) const
Convenience helper to get a qubit's current backend state.
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.
static ns3::Ptr< QMap > Compose(const ns3::Ptr< QMap > &a, const ns3::Ptr< QMap > &b)
Compose two QMaps into one sequential composite QMap.
Definition q2ns-qmap.cc:122
static double RateToProb(double rate_per_s, const ns3::Time &t)
Convert a Poisson rate and elapsed time into an event probability.
Definition q2ns-qmap.h:158
static ns3::Ptr< QMap > FromLambda(std::function< void(QNode &, std::shared_ptr< Qubit > &)> f)
Build a QMap from a simple lambda.
Definition q2ns-qmap.cc:140
Main user-facing per-node API for quantum operations and transmission.
Definition q2ns-qnode.h:63
bool Apply(const QGate &gate, const std::vector< std::shared_ptr< Qubit > > &qs)
Apply a gate to one or more local qubits.
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 Z(ns3::Time d=ns3::Seconds(0))
Return the Pauli-Z gate descriptor.
Definition q2ns-qgate.h:374
int main()
Optional per-sample context passed to QMaps.
Definition q2ns-qmap.h:41
ns3::Time elapsedTime
Elapsed time that this map is applied over.
Definition q2ns-qmap.h:42