Q2NS dev
ns-3 module
Loading...
Searching...
No Matches
q2ns-1-basics-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-basics-example.cc
11 * @brief Basic example demonstrating qubit creation, gate application, and measurement.
12 */
13
14#include "ns3/core-module.h"
15#include "ns3/q2ns-netcontroller.h"
16#include "ns3/q2ns-qgate.h"
17#include "ns3/q2ns-qnode.h"
18#include "ns3/q2ns-qstate.h"
19
20using namespace ns3;
21using namespace q2ns;
22
23int main() {
24
25 ns3::RngSeedManager::SetSeed(45);
26 ns3::RngSeedManager::SetRun(15);
27
28 NetController net;
29 net.SetQStateBackend(QStateBackend::Ket);
30
31 auto node = net.CreateNode();
32 auto q = node->CreateQubit();
33 auto state = net.GetState(q);
34 std::cout << "Created node with ID: " << node->GetId() << "\n";
35 std::cout << "Created qubit with ID: " << state->GetStateId() << "\n";
36 std::cout << " and state: " << state << "\n";
37
38 Simulator::Schedule(MicroSeconds(10), [node, q]() {
39 node->Apply(gates::H(), {q});
40 std::cout << "Applied Hadamard gate to qubit\n";
41 std::cout << "State after gate application: " << node->GetState(q) << "\n";
42 });
43
44 Simulator::Schedule(MicroSeconds(20), [node, q]() {
45 int result = node->Measure(q);
46 std::cout << "Measurement result: " << result << "\n";
47 });
48
49 Simulator::Stop(MilliSeconds(10));
50 Simulator::Run();
51 Simulator::Destroy();
52 return 0;
53}
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.
void SetQStateBackend(QStateBackend b)
Set the default backend used for newly created quantum states.
QGate H(ns3::Time d=ns3::Seconds(0))
Return the Hadamard gate descriptor.
Definition q2ns-qgate.h:383
int main()