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)
118Trace(
"Bob applies Pauli corrections Z^m1=", bobCtx.m1,
" X^m2=", bobCtx.m2);
122 bob->Apply(
gates::Z(), {bobCtx.eprB});
130 bob->Apply(
gates::X(), {bobCtx.eprB});
137 TraceNodeText(
"Bob",
"Teleportation complete - eprB holds psi state");
140 Trace(
"Bob verifies teleported state (X-basis)");
141 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;
155 TraceNodeText(
"Bob",
"eprB arrived - half of Bell pair received");
156 std::cout <<
"[B][quantum] eprB arrived at t=" << Simulator::Now().GetNanoSeconds() <<
" ns\n";
161 bobRxSocket->SetRecvCallback([&](Ptr<Socket> sock) {
163 while (Ptr<Packet> p = sock->RecvFrom(from)) {
164 uint8_t bits[2] = {0, 0};
165 p->CopyData(bits, 2);
166 bobCtx.m1 = bits[0] & 1;
167 bobCtx.m2 = bits[1] & 1;
168 bobCtx.bitsArrived =
true;
170 Trace(
"Bob receives classical corrections m1=", bobCtx.m1,
" m2=", bobCtx.m2);
172 std::cout <<
"[B][CTRL] Corrections arrived: m1=" << bobCtx.m1 <<
" m2=" << bobCtx.m2 <<
"\n";
182 Simulator::Schedule(MicroSeconds(1), [&]() {
183 Trace(
"Phase 1: Alice creates eprA and eprB");
186 auto eprA = alice->CreateQubit();
187 auto eprB = alice->CreateQubit();
190 std::cout <<
"[A] eprA, eprB created in |0>\n";
193 Simulator::Schedule(kSingleGate, [&, eprA, eprB]() {
194 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+>");
204 TraceNodeText(
"Alice",
"CNOT(eprA, eprB): Bell pair |Phi+> ready");
205 std::cout <<
"[A] Bell pair |Phi+> prepared\n";
208 Simulator::Schedule(kSingleGate, [&, eprA, eprB]() {
209 Trace(
"Alice sends eprB to Bob");
211 Simulator::Now() + kQDelay);
212 bool ok = alice->Send(eprB, bob->GetId());
213 TraceNodeText(
"Alice",
"eprB sent - in transit to Bob (+10ns)");
214 std::cout <<
"[A][quantum] eprB sent: " << (ok ?
"ok" :
"failed") <<
"\n";
220 Simulator::Schedule(MicroSeconds(2), [&, eprA]() {
221 Trace(
"Phase 2: Teleportation begins");
222 TraceNodeText(
"Alice",
"Phase 2: teleportation (entanglement pre-shared)");
224 auto psi = alice->CreateQubit();
226 std::cout <<
"[A] psi created in |0>\n";
229 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
230 Trace(
"Alice applies H to psi -> |+> (state to teleport)");
233 std::cout <<
"[A] psi prepared as |+>\n";
236 Simulator::Schedule(kTwoQGate, [&, eprA, psi]() {
237 Trace(
"Alice BSM step 1: CNOT(psi, eprA)");
240 std::cout <<
"[A][BSM] CNOT(psi, eprA) applied\n";
243 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
244 Trace(
"Alice BSM step 2: H(psi)");
247 std::cout <<
"[A][BSM] H(psi) applied\n";
250 Simulator::Schedule(kSingleGate, [&, eprA, psi]() {
251 Trace(
"Alice BSM step 3: Z-measure psi and eprA");
255 int m1 = alice->Measure(psi);
256 int m2 = alice->Measure(eprA);
258 Trace(
"BSM outcome: m1=",
m1,
" m2=",
m2);
260 std::cout <<
"[A][BSM] m1=" <<
m1 <<
" m2=" <<
m2 <<
"\n";
265 uint8_t corrBits[2] = {(uint8_t) (
m1 & 1), (uint8_t) (
m2 & 1)};
266 aliceTxSocket->Send(Create<Packet>(corrBits, 2));
267 TraceSendCbit(
"m1",
"Alice",
"Bob", Simulator::Now(), Simulator::Now() + kClassical);
268 TraceSendCbit(
"m2",
"Alice",
"Bob", Simulator::Now(), Simulator::Now() + kClassical);
269 TraceSendPacket(
"Alice",
"Bob", Simulator::Now(), Simulator::Now() + kClassical,
270 StrCat(
"Corrections m1=",
m1,
" m2=",
m2),
"udp");
272 std::cout <<
"[A][CTRL] Corrections sent (~5 us delay)\n";
288 Simulator::Stop(MilliSeconds(100));
290 Simulator::Destroy();
294 std::cout <<
"[DONE] Quantum teleportation finished\n";