• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sfc-aqua / quisp / 4329255112

pending completion
4329255112

Pull #504

github

GitHub
Merge 948c96a03 into 3166d7e57
Pull Request #504: Integrate Message Exchange (Purification/Entanglement Swapping) into RuleSet and Runtime

957 of 957 new or added lines in 22 files covered. (100.0%)

2287 of 5806 relevant lines covered (39.39%)

46937.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/quisp/modules/PhysicalConnection/BSA/BSAController.cc
1
/** \file BSA_Controller.cc
2
 *
3
 *  \brief BSAController
4
 */
5
#include "BSAController.h"
6

7
#include <cstring>
8
#include <stdexcept>
9

10
namespace quisp::modules {
11

12
Define_Module(BSAController);
13

14
BSAController::BSAController() : provider(utils::ComponentProvider{this}) {}
×
15

16
BSAController::~BSAController() { cancelAndDelete(time_out_message); }
×
17

18
void BSAController::finish() { std::cout << "last BSM message that was sent " << last_result_send_time << "\n"; }
×
19

20
void BSAController::initialize() {
×
21
  bsa = check_and_cast<BellStateAnalyzer *>(getParentModule()->getSubmodule("bsa"));
×
22
  // if this BSA is internal set left to be self node
23
  if (strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
24
    address = provider.getQNode()->par("address").intValue();
×
25
    left_qnic.address = provider.getQNode()->par("address").intValue();
×
26
    left_qnic.index = getParentModule()->par("self_qnic_index").intValue();
×
27
    left_qnic.type = QNIC_R;
×
28
  } else {
×
29
    address = getParentModule()->par("address").intValue();
×
30
    left_qnic = getExternalQNICInfoFromPort(0);
×
31
  }
×
32
  time_interval_between_photons = SimTime(1, SIMTIME_S) / SimTime(getParentModule()->getSubmodule("bsa")->par("photon_detection_per_second").intValue(), SIMTIME_S);
×
33
  simtime_t first_notification_timer = SimTime(par("initial_notification_timing_buffer").doubleValue());
×
34
  right_qnic = getExternalQNICInfoFromPort(1);
×
35
  offset_time_for_first_photon = calculateOffsetTimeFromDistance();
×
36
  left_travel_time = getTravelTimeFromPort(0);
×
37
  right_travel_time = getTravelTimeFromPort(1);
×
38
  time_out_count = 0;
×
39
  time_out_message = new BSMNotificationTimeout("bsm_notification_timeout");
×
40
  scheduleAt(first_notification_timer, time_out_message);
×
41
}
×
42

43
void BSAController::handleMessage(cMessage *msg) {
×
44
  if (msg == time_out_message) {
×
45
    send(generateFirstNotificationTiming(true), "to_router");
×
46
    send(generateFirstNotificationTiming(false), "to_router");
×
47
    bsa->resetState();
×
48
    // set timeout to be twice the travel time plus number of no response
49
    time_out_count++;
×
50
    scheduleAt(simTime() + (2 + time_out_count) * (offset_time_for_first_photon), msg);
×
51
    return;
×
52
  }
×
53

54
  if (dynamic_cast<CancelBSMTimeOutMsg *>(msg)) {
×
55
    cancelBSMTimeOut();
×
56
    delete msg;
×
57
    return;
×
58
  }
×
59

60
  if (auto *batch_click_msg = dynamic_cast<BatchClickEvent *>(msg)) {
×
61
    sendMeasurementResults(batch_click_msg);
×
62
    delete msg;
×
63
    return;
×
64
  }
×
65
  // a more realistic way of execution would be to send every click events through here.
66
  // but we opt for a better performance, since we are more interested in protocols
67
  // no emulating physical hardwares.
68
}
×
69

70
void BSAController::sendMeasurementResults(BatchClickEvent *batch_click_msg) {
×
71
  // we will apply corrections at right nodes
72
  auto *leftpk = generateNextNotificationTiming(true);
×
73
  auto *rightpk = generateNextNotificationTiming(false);
×
74
  for (int index = 0; index < batch_click_msg->numberOfClicks(); index++) {
×
75
    if (!batch_click_msg->getClickResults(index).success) continue;
×
76
    leftpk->appendSuccessIndex(index);
×
77
    leftpk->appendCorrectionOperation(PauliOperator::I);
×
78
    leftpk->setNeighborAddress(right_qnic.address);
×
79
    rightpk->appendSuccessIndex(index);
×
80
    rightpk->appendCorrectionOperation(batch_click_msg->getClickResults(index).correction_operation);
×
81
    rightpk->setNeighborAddress(left_qnic.address);
×
82
  }
×
83
  send(leftpk, "to_router");
×
84
  send(rightpk, "to_router");
×
85
  last_result_send_time = simTime();
×
86

87
  scheduleAt(simTime() + 1.1 * offset_time_for_first_photon, time_out_message);
×
88
}
×
89

90
BSMTimingNotification *BSAController::generateFirstNotificationTiming(bool is_left) {
×
91
  int destination = (is_left) ? left_qnic.address : right_qnic.address;
×
92
  int qnic_index = (is_left) ? left_qnic.index : right_qnic.index;
×
93
  auto qnic_type = (is_left) ? left_qnic.type : right_qnic.type;
×
94
  auto *notification_packet = new BSMTimingNotification();
×
95
  auto travel_time = (is_left) ? left_travel_time : right_travel_time;
×
96

97
  // The node should emit at <arrival_time - travel_time>
98
  simtime_t arrival_time = simTime() + offset_time_for_first_photon;
×
99
  simtime_t emit_time = arrival_time - travel_time;
×
100

101
  notification_packet->setSrcAddr(address);
×
102
  notification_packet->setDestAddr(destination);
×
103
  notification_packet->setFirstPhotonEmitTime(emit_time);
×
104
  notification_packet->setInterval(time_interval_between_photons);
×
105
  notification_packet->setQnicIndex(qnic_index);
×
106
  notification_packet->setQnicType(qnic_type);
×
107
  return notification_packet;
×
108
}
×
109

110
CombinedBSAresults *BSAController::generateNextNotificationTiming(bool is_left) {
×
111
  int destination = (is_left) ? left_qnic.address : right_qnic.address;
×
112
  int qnic_index = (is_left) ? left_qnic.index : right_qnic.index;
×
113
  auto qnic_type = (is_left) ? left_qnic.type : right_qnic.type;
×
114
  auto *notification_packet = new CombinedBSAresults();
×
115
  auto travel_time = (is_left) ? left_travel_time : right_travel_time;
×
116

117
  // The node should emit at <arrival_time - travel_time>
118
  simtime_t arrival_time = simTime() + offset_time_for_first_photon;
×
119
  simtime_t emit_time = arrival_time - travel_time;
×
120

121
  notification_packet->setSrcAddr(address);
×
122
  notification_packet->setDestAddr(destination);
×
123
  notification_packet->setFirstPhotonEmitTime(emit_time);
×
124
  notification_packet->setInterval(time_interval_between_photons);
×
125
  notification_packet->setQnicIndex(qnic_index);
×
126
  notification_packet->setQnicType(qnic_type);
×
127
  return notification_packet;
×
128
}
×
129

130
simtime_t BSAController::calculateOffsetTimeFromDistance() {
×
131
  auto one_way_longer_travel_time = std::max(getTravelTimeFromPort(0), getTravelTimeFromPort(1));
×
132
  // we add 10 times the photon interval to offset the travel time for safety in case RuleEngine has internal delay;
133
  return 2 * one_way_longer_travel_time + time_interval_between_photons * 10;
×
134
}
×
135

136
int BSAController::getExternalAdressFromPort(int port) {
×
137
  if (port == 0 && strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
138
    throw cRuntimeError("Trying to get external QNIC information from a port connecting to internal QNIC_R. Address %d, BSAController port %d", address, port);
×
139
  }
×
140

141
  // this BSAController is inside QNIC_R but the port is connecting to outside
142
  if (port != 0 && strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
143
    return getParentModule()
×
144
        ->getSubmodule("bsa")
×
145
        ->gate("quantum_port$i", port)
×
146
        ->getPreviousGate()  // qnic_quantum_port
×
147
        ->getPreviousGate()  // QNode quantum_port_receiver
×
148
        ->getPreviousGate()  // another QNode quantum_port
×
149
        ->getOwnerModule()
×
150
        ->par("address");
×
151
  }
×
152

153
  // this BSAController is in a stand-alone BSANode
154
  return getParentModule()
×
155
      ->getSubmodule("bsa")
×
156
      ->gate("quantum_port$i", port)
×
157
      ->getPreviousGate()  // BSANode quantum_port
×
158
      ->getPreviousGate()  // QNode quantum_port
×
159
      ->getOwnerModule()  // QNode
×
160
      ->par("address");
×
161
}
×
162

163
int BSAController::getExternalQNICIndexFromPort(int port) {
×
164
  if (port == 0 && strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
165
    throw cRuntimeError("Trying to get external QNIC information from a port connecting to internal QNIC_R. Address %d, BSAController port %d", address, port);
×
166
  }
×
167

168
  // this BSAController is inside QNIC_R but the port is connecting to outside
169
  if (port != 0 && strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
170
    return getParentModule()
×
171
        ->getSubmodule("bsa")
×
172
        ->gate("quantum_port$i", port)
×
173
        ->getPreviousGate()  // qnic_quantum_port
×
174
        ->getPreviousGate()  // QNode quantum_port_receiver
×
175
        ->getPreviousGate()  // another QNode quantum_port
×
176
        ->getPreviousGate()  // QNIC quantum port
×
177
        ->getOwnerModule()
×
178
        ->par("self_qnic_index");
×
179
  }
×
180

181
  // this BSAController is in a stand-alone BSANode
182
  return getParentModule()
×
183
      ->getSubmodule("bsa")
×
184
      ->gate("quantum_port$i", port)
×
185
      ->getPreviousGate()  // BSANode quantum_port
×
186
      ->getPreviousGate()  // QNode quantum_port
×
187
      ->getPreviousGate()  // QNIC quantum port
×
188
      ->getOwnerModule()
×
189
      ->par("self_qnic_index");
×
190
}
×
191

192
simtime_t BSAController::getTravelTimeFromPort(int port) {
×
193
  cChannel *channel;
×
194
  // this port connects to internal QNIC
195
  // since only port 0 is supposed to be connected to internal QNIC
196
  if (port == 0 && strcmp(getParentModule()->getName(), "qnic_r") == 0) {
×
197
    return 0;
×
198
  } else {
×
199
    // this port connects to outside QNode
200
    channel = getParentModule()->getSubmodule("bsa")->gate("quantum_port$i", port)->getIncomingTransmissionChannel();
×
201
  }
×
202
  double distance = channel->par("distance").doubleValue();  // km
×
203
  double speed_of_light_in_channel = channel->par("speed_of_light_in_fiber").doubleValue();  // km/sec
×
204
  return SimTime(distance / speed_of_light_in_channel);
×
205
}
×
206

207
QNIC_id BSAController::getExternalQNICInfoFromPort(int port) {
×
208
  QNIC_id qid;
×
209
  qid.address = getExternalAdressFromPort(port);
×
210
  qid.index = getExternalQNICIndexFromPort(port);
×
211
  qid.type = QNIC_E;
×
212
  return qid;
×
213
}
×
214

215
void BSAController::cancelBSMTimeOut() {
×
216
  cancelEvent(time_out_message);
×
217
  time_out_count = 0;
×
218
}
×
219

220
}  // namespace quisp::modules
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc