Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-2-fidelity-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-fidelity-example.cc
11 * @brief Fidelity sweep over depolarizing probability.
12 *
13 * Demonstrates q2ns::analysis::Fidelity by sending |+> through a
14 * DepolarizingQMap at several probability values and printing the
15 * fidelity against the ideal |+> reference for each trial.
16 *
17 * Because DepolarizingQMap is a trajectory model (it applies one
18 * specific Pauli per shot), the per-trial fidelity is 0 or 1.
19 * Run multiple times with different seeds to see the stochastic nature.
20 */
21
22#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24
25#include "ns3/q2ns-analysis.h"
26#include "ns3/q2ns-netcontroller.h"
27#include "ns3/q2ns-qmap.h"
28#include "ns3/q2ns-qnode.h"
29#include "ns3/q2ns-qstate.h"
30#include "ns3/q2ns-qubit.h"
31#include "ns3/simulator.h"
32
33#include <iomanip>
34#include <iostream>
35#include <random>
36#include <vector>
37
38using namespace ns3;
39using namespace q2ns;
40
41int main() {
42 RngSeedManager::SetSeed(std::random_device{}() | 1u);
43
44 NetController net;
45
46 // Build the ideal |+> reference
47 auto ref_node = net.CreateNode();
48 auto qref = ref_node->CreateQubit();
49 ref_node->Apply(gates::H(), {qref});
50 const auto ideal = net.GetState(qref);
51
52 // Experiment topology: A -> [DepolarizingQMap] -> B
53 auto A = net.CreateNode();
54 auto B = net.CreateNode();
55
56 auto depol = CreateObject<DepolarizingQMap>();
57 auto ch = net.InstallQuantumLink(A, B);
58 ch->SetAttribute("Delay", TimeValue(NanoSeconds(10)));
59 ch->SetAttribute("QMap", PointerValue(depol));
60
61 // Receive callback installed once; `received` is reset at the start of each trial.
62 std::shared_ptr<QState> received;
63 B->SetRecvCallback([&net, &received](std::shared_ptr<Qubit> q) { received = net.GetState(q); });
64
65 const std::vector<double> probs = {0.0, 0.10, 0.25, 0.50, 0.75, 1.0};
66
67 std::cout << std::fixed << std::setprecision(4);
68 std::cout << "Depolarizing fidelity sweep (one trial per point)\n";
69 std::cout << " p F\n";
70
71 for (double p : probs) {
72 received = nullptr;
73 depol->SetAttribute("Probability", DoubleValue(p));
74
75 auto q = A->CreateQubit();
76 A->Apply(gates::H(), {q});
77
78 Simulator::Schedule(NanoSeconds(1), [A, B, q]() { A->Send(q, B->GetId()); });
79
80 // Advance the simulation by 1 ms -- well past the 10 ns channel delay.
81 Simulator::Stop(Simulator::Now() + MilliSeconds(1));
82 Simulator::Run();
83
84 const double f = received ? q2ns::analysis::Fidelity(*ideal, *received) : -1.0;
85 std::cout << " " << p << " " << f << "\n";
86 }
87
88 Simulator::Destroy();
89 return 0;
90}
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.
double Fidelity(const QState &a, const QState &b)
Compute fidelity between two QState objects of the same backend type.
QGate H(ns3::Time d=ns3::Seconds(0))
Return the Hadamard gate descriptor.
Definition q2ns-qgate.h:383