46int main(
int argc,
char** argv) {
47 std::cout <<
"[DEMO] Quantum teleportation starting\n";
49 RngSeedManager::SetSeed(1);
50 RngSeedManager::SetRun(1);
53 cmd.Parse(argc, argv);
57 Time::SetResolution(Time::NS);
67 const Time kSingleGate = NanoSeconds(100);
68 const Time kTwoQGate = NanoSeconds(300);
69 const Time kQDelay = NanoSeconds(10);
70 const Time kClassical = kQDelay;
74 ch->SetAttribute(
"Delay", TimeValue(kQDelay));
81 InternetStackHelper internet;
82 internet.Install(alice);
83 internet.Install(bob);
85 PointToPointHelper p2p;
86 p2p.SetDeviceAttribute(
"DataRate", StringValue(
"100Mbps"));
87 p2p.SetChannelAttribute(
"Delay", StringValue(
"10ns"));
88 NetDeviceContainer devices = p2p.Install(StaticCast<Node>(alice), StaticCast<Node>(bob));
91 ip.SetBase(
"10.1.1.0",
"255.255.255.0");
92 auto ifs = ip.Assign(devices);
93 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
97 Ptr<Socket> bobRxSocket = Socket::CreateSocket(bob, UdpSocketFactory::GetTypeId());
98 bobRxSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(),
kCtrlPort));
100 Ptr<Socket> aliceTxSocket = Socket::CreateSocket(alice, UdpSocketFactory::GetTypeId());
101 aliceTxSocket->Connect(InetSocketAddress(ifs.GetAddress(1),
kCtrlPort));
105 std::shared_ptr<Qubit> eprB;
106 bool qubitArrived =
false;
107 bool bitsArrived =
false;
114 auto tryCorrections = [&]() {
115 if (!bobCtx.qubitArrived || !bobCtx.bitsArrived)
118 Trace(
"Bob applies Pauli corrections Z^m", bobCtx.m1,
" X^m", bobCtx.m2);
122 bob->Apply(
gates::Z(), {bobCtx.eprB});
130 bob->Apply(
gates::X(), {bobCtx.eprB});
138 TraceNodeText(
"Bob",
"Teleportation complete - eprB holds psi state");
141 Trace(
"Bob verifies teleported state (X-basis)");
142 int mx = bob->Measure(bobCtx.eprB, Basis::X);
145 std::cout <<
"[VERIFY] Bob's X-measure: " << mx <<
" (expected 0)\n";
151 bob->SetRecvCallback([&](std::shared_ptr<Qubit> q) {
152 Trace(
"Bob receives eprB");
154 bobCtx.qubitArrived =
true;
156 TraceNodeText(
"Bob",
"eprB arrived - half of Bell pair received");
157 std::cout <<
"[B][quantum] eprB arrived at t=" << Simulator::Now().GetNanoSeconds() <<
" ns\n";
162 bobRxSocket->SetRecvCallback([&](Ptr<Socket> sock) {
164 while (Ptr<Packet> p = sock->RecvFrom(from)) {
165 uint8_t bits[2] = {0, 0};
166 p->CopyData(bits, 2);
167 bobCtx.m1 = bits[0] & 1;
168 bobCtx.m2 = bits[1] & 1;
169 bobCtx.bitsArrived =
true;
171 Trace(
"Bob receives classical corrections m1=", bobCtx.m1,
" m2=", bobCtx.m2);
173 std::cout <<
"[B][CTRL] Corrections arrived: m1=" << bobCtx.m1 <<
" m2=" << bobCtx.m2 <<
"\n";
181 Simulator::Schedule(MicroSeconds(1), [&]() {
182 Trace(
"Phase 1: Alice creates eprA and eprB");
185 auto eprA = alice->CreateQubit();
186 auto eprB = alice->CreateQubit();
189 std::cout <<
"[A] eprA, eprB created in |0>\n";
192 Simulator::Schedule(kSingleGate, [&, eprA, eprB]() {
193 Trace(
"Alice applies H to eprA");
197 std::cout <<
"[A] H applied to eprA\n";
200 Simulator::Schedule(kTwoQGate, [&, eprA, eprB]() {
201 Trace(
"Alice applies CNOT(eprA, eprB) -> Bell pair |Phi+>");
206 TraceNodeText(
"Alice",
"CNOT(eprA, eprB): Bell pair |Phi+> ready");
207 std::cout <<
"[A] Bell pair |Phi+> prepared\n";
210 Simulator::Schedule(kSingleGate, [&, eprA, eprB]() {
211 Trace(
"Alice sends eprB to Bob");
212 TraceSendBit(
"bob_epr",
"Alice",
"Bob",
"quantum", Simulator::Now(),
213 Simulator::Now() + kQDelay);
214 bool ok = alice->Send(eprB, bob->GetId());
215 TraceNodeText(
"Alice",
"eprB sent - in transit to Bob (+10ns)");
216 std::cout <<
"[A][quantum] eprB sent: " << (ok ?
"ok" :
"failed") <<
"\n";
222 Simulator::Schedule(MicroSeconds(2), [&, eprA]() {
223 Trace(
"Phase 2: Teleportation begins");
224 TraceNodeText(
"Alice",
"Phase 2: teleportation (entanglement pre-shared)");
226 auto psi = alice->CreateQubit();
228 std::cout <<
"[A] psi created in |0>\n";
231 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
232 Trace(
"Alice applies H to psi -> |+> (state to teleport)");
236 std::cout <<
"[A] psi prepared as |+>\n";
239 Simulator::Schedule(kTwoQGate, [&, eprA, psi]() {
240 Trace(
"Alice BSM step 1: CNOT(psi, eprA)");
243 std::cout <<
"[A][BSM] CNOT(psi, eprA) applied\n";
246 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
247 Trace(
"Alice BSM step 2: H(psi)");
250 std::cout <<
"[A][BSM] H(psi) applied\n";
253 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
254 Trace(
"Alice BSM step 3: Z-measure psi and eprA");
258 int m1 = alice->Measure(psi);
259 int m2 = alice->Measure(eprA);
261 Trace(
"BSM outcome: m1=",
m1,
" m2=",
m2);
265 std::cout <<
"[A][BSM] m1=" <<
m1 <<
" m2=" <<
m2 <<
"\n";
268 uint8_t corrBits[2] = {(uint8_t) (
m1 & 1), (uint8_t) (
m2 & 1)};
269 aliceTxSocket->Send(Create<Packet>(corrBits, 2));
270 TraceSendPacket(
"Alice",
"Bob", Simulator::Now(), Simulator::Now() + kClassical,
273 std::cout <<
"[A][CTRL] Corrections sent (~5 us delay)\n";
289 Simulator::Stop(MilliSeconds(100));
291 Simulator::Destroy();
295 std::cout <<
"[DONE] Quantum teleportation finished\n";