Skip to content

Commit

Permalink
move applied_rules to QubitRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
zigen committed Dec 27, 2021
1 parent 5e6d1b3 commit eb698fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 75 deletions.
52 changes: 6 additions & 46 deletions quisp/modules/QRSA/RuleEngine/RuleEngine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,46 +541,6 @@ void RuleEngine::Unlock_resource_and_upgrade_stage(unsigned long ruleset_id, uns
}
}

void RuleEngine::updateAppliedRule(IStationaryQubit *qubit, unsigned long rule_id) {
// find there is a key
auto iter = applied_rules.find(qubit);
if (iter != applied_rules.end()) {
// qubit key found;
iter->second.push_back(rule_id);
} else {
std::vector<unsigned long> rule_vector = {rule_id};
applied_rules.insert(std::make_pair(qubit, rule_vector));
}
}

bool RuleEngine::checkAppliedRule(IStationaryQubit *qubit, unsigned long rule_id) {
// check if the rule can be applied (target rule id is not in the applied rules)
for (auto &rules : applied_rules) {
if (rules.first == qubit) {
for (auto &rule : rules.second) {
// if the rule exists, this rule is already applied, so you cannot apply any more.
if (rule == rule_id) {
return false;
}
}
// if not, you can go ahead to apply the rule
return true;
}
}
// completely fresh resource
return true;
}

void RuleEngine::clearAppliedRule(IStationaryQubit *qubit) {
// erase the record when the qubit is initialized
auto iter = applied_rules.find(qubit);
if (iter != applied_rules.end()) {
applied_rules.erase(iter);
} else {
error("No rule record found at clearing");
}
}

void RuleEngine::Unlock_resource_and_discard(unsigned long ruleset_id, unsigned long rule_id, int index) {
bool ok = false;
auto ruleset_result = rp.findById(ruleset_id);
Expand Down Expand Up @@ -1073,16 +1033,14 @@ void RuleEngine::ResourceAllocation(int qnic_type, int qnic_index) {
auto *qubit = provider.getStationaryQubit(qubit_record);
// 3. if the qubit is not allocated yet, and the qubit has not been allocated to this rule,
// if the qubit has already been assigned to the rule, the qubit is not allocatable to that rule
bool allocatable = checkAppliedRule(qubit, (*rule)->rule_id);
// EV<<" allocatable: "<<allocatable<<" : "<<qubit<<"action_partner:"<<action_partner<<"\n";
if (!qubit_record->isAllocated() && allocatable) {
if (!qubit_record->isAllocated() && !qubit_record->isRuleApplied((*rule)->rule_id)) {
if (qubit->entangled_partner == nullptr && qubit->Density_Matrix_Collapsed(0, 0).real() == -111 && !qubit->no_density_matrix_nullptr_entangled_partner_ok) {
error("Freshing qubit wrong");
}
// 5. increment the assined counter and set allocated flag
assigned++;
qubit_record->setAllocated(true);
updateAppliedRule(qubit, (*rule)->rule_id);
qubit_record->markRuleApplied((*rule)->rule_id);
(*rule)->addResource(action_partner, qubit);
}
}
Expand Down Expand Up @@ -1256,8 +1214,10 @@ void RuleEngine::freeConsumedResource(int qnic_index /*Not the address!!!*/, ISt
auto *qubit_record = qnic_store->getQubitRecord(qnic_type, qnic_index, qubit->par("stationaryQubit_address"));
realtime_controller->ReInitialize_StationaryQubit(qubit_record, false);
qubit_record->setBusy(false);
if (qubit_record->isAllocated()) qubit_record->setAllocated(false);
clearAppliedRule(qubit);
if (qubit_record->isAllocated()) {
qubit_record->setAllocated(false);
}
qubit_record->clearAppliedRules();
bell_pair_store.eraseQubit(qubit_record);
}

Expand Down
7 changes: 2 additions & 5 deletions quisp/modules/QRSA/RuleEngine/RuleEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "modules/QRSA/HardwareMonitor/HardwareMonitor.h"
#include "modules/QRSA/RealTimeController/IRealTimeController.h"
#include "modules/QRSA/RoutingDaemon/RoutingDaemon.h"
#include "modules/QRSA/RuleEngine/QubitRecord/IQubitRecord.h"
#include "modules/QUBIT.h"
#include "utils/ComponentProvider.h"

Expand All @@ -30,6 +31,7 @@ namespace quisp {
namespace modules {
using namespace rules;
using qnic_store::IQNicStore;
using qubit_record::IQubitRecord;

/** \class RuleEngine RuleEngine.h
* \note The Connection Manager responds to connection requests received from other nodes.
Expand Down Expand Up @@ -74,8 +76,6 @@ class RuleEngine : public IRuleEngine {
// when the tracker for the qnic is clered by previous BSM trial it goes true
// when the RuleEngine try to start new Photon emittion, it goes false and other BSM trial can't access to it.
std::vector<bool> tracker_accessible;
// tracking the rules that have been applied to the qubit
std::map<IStationaryQubit *, std::vector<unsigned long>> applied_rules; // <qubit, list of rules>

void freeResource(int qnic_index, int qubit_index, QNIC_type qnic_type) override;
void freeConsumedResource(int qnic_index, IStationaryQubit *qubit, QNIC_type qnic_type) override;
Expand Down Expand Up @@ -109,9 +109,6 @@ class RuleEngine : public IRuleEngine {
void Unlock_resource_and_upgrade_stage(unsigned long ruleset_id, unsigned long rule_id, int index);
void Unlock_resource_and_discard(unsigned long ruleset_id, unsigned long rule_id, int index);

void updateAppliedRule(IStationaryQubit *qubit, unsigned long rule_id);
bool checkAppliedRule(IStationaryQubit *qubit, unsigned long rule_id);
void clearAppliedRule(IStationaryQubit *qubit);
void updateResources_EntanglementSwapping(swapping_result swapr);
virtual void updateResources_SimultaneousEntanglementSwapping(swapping_result swapr);

Expand Down
43 changes: 19 additions & 24 deletions quisp/modules/QRSA/RuleEngine/RuleEngine_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,12 @@ class Strategy : public quisp_test::TestComponentProviderStrategy {

class RuleEngineTestTarget : public quisp::modules::RuleEngine {
public:
using quisp::modules::RuleEngine::checkAppliedRule;
using quisp::modules::RuleEngine::clearAppliedRule;
using quisp::modules::RuleEngine::initialize;
using quisp::modules::RuleEngine::par;
using quisp::modules::RuleEngine::qnic_store;
using quisp::modules::RuleEngine::storeCheck_Purification_Agreement;
using quisp::modules::RuleEngine::Unlock_resource_and_discard;
using quisp::modules::RuleEngine::Unlock_resource_and_upgrade_stage;
using quisp::modules::RuleEngine::updateAppliedRule;
using quisp::modules::RuleEngine::updateResources_EntanglementSwapping;

RuleEngineTestTarget(IStationaryQubit* mockQubit, MockRoutingDaemon* routingdaemon, MockHardwareMonitor* hardware_monitor, MockRealTimeController* realtime_controller,
Expand Down Expand Up @@ -161,11 +158,10 @@ TEST(RuleEngineTest, resourceAllocation) {
auto* sim = prepareSimulation();
auto* routingdaemon = new MockRoutingDaemon;
auto* mockHardwareMonitor = new MockHardwareMonitor;
auto* mockQubit1 = new MockQubit(QNIC_E, 3);
auto* qubit_record0 = new QubitRecord(QNIC_E, 3, 0);
auto* qubit_record1 = new QubitRecord(QNIC_E, 3, 1);
auto* qubit_record2 = new QubitRecord(QNIC_E, 3, 2);
auto rule_engine = new RuleEngineTestTarget{mockQubit1, routingdaemon, mockHardwareMonitor, nullptr, qnic_specs};
auto rule_engine = new RuleEngineTestTarget{nullptr, routingdaemon, mockHardwareMonitor, nullptr, qnic_specs};
sim->registerComponent(rule_engine);
rule_engine->callInitialize();
rule_engine->setAllResources(0, qubit_record0);
Expand Down Expand Up @@ -193,7 +189,6 @@ TEST(RuleEngineTest, resourceAllocation) {
EXPECT_EQ(_rule->resources.size(), 1);
delete mockHardwareMonitor;
delete routingdaemon;
delete mockQubit1;
}

TEST(RuleEngineTest, trackerUpdate) {
Expand Down Expand Up @@ -306,13 +301,13 @@ TEST(RuleEngineTest, freeConsumedResource) {
auto* qubit_record = new QubitRecord(QNIC_E, qnic_index, 1);
qubit_record->setBusy(true);
qubit->fillParams();
rule_engine->updateAppliedRule(qubit, 0);
EXPECT_FALSE(rule_engine->checkAppliedRule(qubit, 0));
qubit_record->markRuleApplied(0);
EXPECT_FALSE(!qubit_record->isRuleApplied(0));

EXPECT_CALL(*realtime_controller, ReInitialize_StationaryQubit(qubit_record, false)).Times(1).WillOnce(Return());
EXPECT_CALL(*dynamic_cast<MockQNicStore*>(rule_engine->qnic_store.get()), getQubitRecord(QNIC_E, qnic_index, 1)).Times(1).WillOnce(Return(qubit_record));
rule_engine->freeConsumedResource(qnic_index, qubit, QNIC_E);
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit, 0));
EXPECT_TRUE(!qubit_record->isRuleApplied(0));
EXPECT_FALSE(qubit_record->isBusy());
delete qubit;
delete hardware_monitor;
Expand Down Expand Up @@ -351,7 +346,7 @@ TEST(RuleEngineTest, unlockResourceAndDiscard) {
EXPECT_CALL(*qubit, Unlock()).Times(1);
EXPECT_CALL(*dynamic_cast<MockQNicStore*>(rule_engine->qnic_store.get()), getQubitRecord(QNIC_E, qnic_index, 1)).Times(1).WillOnce(Return(qubit_record));

rule_engine->updateAppliedRule(qubit, 0);
qubit_record->markRuleApplied(0);
EXPECT_EQ(rule1->resources.size(), 1);
EXPECT_EQ(rule2->resources.size(), 0);
EXPECT_CALL(*realtime_controller, ReInitialize_StationaryQubit(qubit_record, false)).Times(1).WillOnce(Return());
Expand Down Expand Up @@ -460,16 +455,14 @@ TEST(RuleEngineTest, updateAndCheckAppliedRule) {
auto* rule_engine = new RuleEngineTestTarget{nullptr, routing_daemon, hardware_monitor, realtime_controller, qnic_specs};
rule_engine->callInitialize();

auto* qubit1 = new MockQubit(QNIC_E, 7);
auto* qubit2 = new MockQubit(QNIC_E, 11);
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit1, 1));
rule_engine->updateAppliedRule(qubit1, 1);
EXPECT_FALSE(rule_engine->checkAppliedRule(qubit1, 1));
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit1, 2));
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit2, 1));
auto* qubit_record1 = new QubitRecord(QNIC_E, 7, 0);
auto* qubit_record2 = new QubitRecord(QNIC_E, 11, 0);
EXPECT_TRUE(!qubit_record1->isRuleApplied(1));
qubit_record1->markRuleApplied(1);
EXPECT_FALSE(!qubit_record1->isRuleApplied(1));
EXPECT_TRUE(!qubit_record1->isRuleApplied(2));
EXPECT_TRUE(!qubit_record2->isRuleApplied(1));

delete qubit1;
delete qubit2;
delete hardware_monitor;
}

Expand All @@ -482,14 +475,16 @@ TEST(RuleEngineTest, checkAppliedRule) {
rule_engine->callInitialize();

auto* qubit = new MockQubit(QNIC_E, 7);
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit, 1));
rule_engine->updateAppliedRule(qubit, 1);
EXPECT_FALSE(rule_engine->checkAppliedRule(qubit, 1));
auto* qubit_record = new QubitRecord(QNIC_E, 7, 1);
EXPECT_TRUE(!qubit_record->isRuleApplied(1));
qubit_record->markRuleApplied(1);
EXPECT_FALSE(!qubit_record->isRuleApplied(1));

rule_engine->clearAppliedRule(qubit);
EXPECT_TRUE(rule_engine->checkAppliedRule(qubit, 1));
qubit_record->clearAppliedRules();
EXPECT_TRUE(!qubit_record->isRuleApplied(1));

delete qubit;
delete qubit_record;
delete hardware_monitor;
}

Expand Down

0 comments on commit eb698fe

Please sign in to comment.