Skip to content

Commit fabcd7d

Browse files
🎨 pre-commit fixes
1 parent 5845c87 commit fabcd7d

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

include/na/NAGraphAlgorithms.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class NAGraphAlgorithms {
152152
* @return A tuple of vectors containing the moveable vertices, the fixed
153153
* vertices mapped to their relative x-position in every time step.
154154
*/
155-
[[nodiscard]] static auto computeSequence(const InteractionGraph& g, std::size_t maxSites)
155+
[[nodiscard]] static auto computeSequence(const InteractionGraph& g,
156+
std::size_t maxSites)
156157
-> std::pair<std::vector<std::unordered_map<qc::Qubit, std::int64_t>>,
157158
std::unordered_map<qc::Qubit, std::int64_t>>;
158159
};

src/na/NAGraphAlgorithms.cpp

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,20 @@ auto NAGraphAlgorithms::colorEdges(
184184
std::copy_if(edges.cbegin(), edges.cend(),
185185
std::back_inserter(adjacentEdges),
186186
[&](const Edge& e) { return e.first == v or e.second == v; });
187-
std::sort(adjacentEdges.begin(), adjacentEdges.end(), [&](const Edge& a, const Edge& b) {
187+
std::sort(adjacentEdges.begin(), adjacentEdges.end(),
188+
[&](const Edge& a, const Edge& b) {
188189
const auto u = a.first == v ? a.second : a.first;
189190
const auto w = b.first == v ? b.second : b.first;
190-
return u != w && (partialOrder.isReachable(u, w) ||
191-
(!partialOrder.isReachable(w, u) &&
192-
(nAdjColors[a] > nAdjColors[b] ||
193-
(nAdjColors[a] == nAdjColors[b] &&
194-
(edgeDegree[a] > edgeDegree[b] ||
195-
(edgeDegree[a] == edgeDegree[b] && u < w))))));
196-
// the last line together with the first clause (u != w) is necessary for a
197-
// well-defined compare function to handle edges that compare equally correctly
191+
return u != w &&
192+
(partialOrder.isReachable(u, w) ||
193+
(!partialOrder.isReachable(w, u) &&
194+
(nAdjColors[a] > nAdjColors[b] ||
195+
(nAdjColors[a] == nAdjColors[b] &&
196+
(edgeDegree[a] > edgeDegree[b] ||
197+
(edgeDegree[a] == edgeDegree[b] && u < w))))));
198+
// the last line together with the first clause (u != w) is
199+
// necessary for a well-defined compare function to handle edges
200+
// that compare equally correctly
198201
});
199202
for (const auto& e : adjacentEdges) {
200203
// color the edge
@@ -429,7 +432,8 @@ auto NAGraphAlgorithms::groupByConnectedComponent(
429432
return result;
430433
}
431434

432-
auto NAGraphAlgorithms::computeSequence(const InteractionGraph& g, const std::size_t maxSites)
435+
auto NAGraphAlgorithms::computeSequence(const InteractionGraph& g,
436+
const std::size_t maxSites)
433437
-> std::pair<std::vector<std::unordered_map<qc::Qubit, std::int64_t>>,
434438
std::unordered_map<qc::Qubit, std::int64_t>> {
435439
const auto& maxIndepSet = getMaxIndependentSet(g);
@@ -439,13 +443,14 @@ auto NAGraphAlgorithms::computeSequence(const InteractionGraph& g, const std::si
439443
[&](const auto& u, const auto& v) {
440444
return g.getDegree(u) > g.getDegree(v);
441445
});
442-
auto sequence = groupByConnectedComponent(g, sequenceUngrouped);
446+
auto sequence = groupByConnectedComponent(g, sequenceUngrouped);
443447
const auto& colorEdgesResult =
444448
colorEdges(g, coveredEdges(g, maxIndepSet), sequence);
445-
std::unordered_map<Edge, Color, qc::PairHash<qc::Qubit, qc::Qubit>> coloring = colorEdgesResult.first;
449+
std::unordered_map<Edge, Color, qc::PairHash<qc::Qubit, qc::Qubit>> coloring =
450+
colorEdgesResult.first;
446451
auto partialOrder = colorEdgesResult.second;
447-
auto fixed = partialOrder.orderTopologically();
448-
auto resting = computeRestingPositions(sequence, fixed, coloring);
452+
auto fixed = partialOrder.orderTopologically();
453+
auto resting = computeRestingPositions(sequence, fixed, coloring);
449454
// compute relative x positions of fixed vertices
450455
std::unordered_map<qc::Qubit, std::int64_t> fixedPositions{};
451456
for (std::uint32_t x = 0, i = 0; x < fixed.size(); ++x) {
@@ -455,26 +460,46 @@ auto NAGraphAlgorithms::computeSequence(const InteractionGraph& g, const std::si
455460
}
456461
}
457462

458-
const auto maxSiteUsed = std::max_element(fixedPositions.cbegin(), fixedPositions.cend(), [](const auto& a, const auto& b){ return a.second < b.second; })->second;
463+
const auto maxSiteUsed =
464+
std::max_element(
465+
fixedPositions.cbegin(), fixedPositions.cend(),
466+
[](const auto& a, const auto& b) { return a.second < b.second; })
467+
->second;
459468
const auto maxSitesSigned = static_cast<std::int64_t>(maxSites);
460469
if (maxSiteUsed >= maxSitesSigned) {
461-
// Handle the situation when the entangling zone is not big enough to fit all fixed qubits
462-
for (auto it = fixedPositions.begin(); it != fixedPositions.end(); ) {
470+
// Handle the situation when the entangling zone is not big enough to fit
471+
// all fixed qubits
472+
for (auto it = fixedPositions.begin(); it != fixedPositions.end();) {
463473
if (it->second >= maxSitesSigned) {
464474
it = fixedPositions.erase(it); // erase returns the next iterator
465475
} else {
466476
++it; // move to the next element
467477
}
468478
}
469-
fixed.erase(std::remove_if(fixed.begin(), fixed.end(), [&fixedPositions](const auto& q){ return fixedPositions.find(q) == fixedPositions.end(); }), fixed.end());
470-
for (auto it = coloring.begin(); it != coloring.end(); ) {
471-
if (fixedPositions.find(it->first.first) == fixedPositions.end() && fixedPositions.find(it->first.second) == fixedPositions.end()) {
479+
fixed.erase(std::remove_if(fixed.begin(), fixed.end(),
480+
[&fixedPositions](const auto& q) {
481+
return fixedPositions.find(q) ==
482+
fixedPositions.end();
483+
}),
484+
fixed.end());
485+
for (auto it = coloring.begin(); it != coloring.end();) {
486+
if (fixedPositions.find(it->first.first) == fixedPositions.end() &&
487+
fixedPositions.find(it->first.second) == fixedPositions.end()) {
472488
it = coloring.erase(it); // erase returns the next iterator
473489
} else {
474490
++it; // move to the next element
475491
}
476492
}
477-
sequence.erase(std::remove_if(sequence.begin(), sequence.end(), [&coloring](const auto& q){ return !std::any_of(coloring.cbegin(), coloring.cend(), [q](const auto& elem){ return elem.first.first == q || elem.first.second == q; }); }), sequence.end());
493+
sequence.erase(std::remove_if(sequence.begin(), sequence.end(),
494+
[&coloring](const auto& q) {
495+
return !std::any_of(
496+
coloring.cbegin(), coloring.cend(),
497+
[q](const auto& elem) {
498+
return elem.first.first == q ||
499+
elem.first.second == q;
500+
});
501+
}),
502+
sequence.end());
478503
// recalculate resting positions
479504
resting = computeRestingPositions(sequence, fixed, coloring);
480505
}

src/na/NAMapper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,9 @@ auto NAMapper::map(const qc::QuantumComputation& qc) -> void {
971971
}
972972
const Zone interactionZone =
973973
*arch.getPropertiesOfOperation({qc::OpType::Z, 1}).zones.begin();
974-
const auto sites = arch.getSitesInRow(interactionZone, 0);
975-
const auto& sequence = NAGraphAlgorithms::computeSequence(graph, sites.size());
974+
const auto sites = arch.getSitesInRow(interactionZone, 0);
975+
const auto& sequence =
976+
NAGraphAlgorithms::computeSequence(graph, sites.size());
976977
const auto& moveable = sequence.first;
977978
const auto& fixed = sequence.second;
978979
// 3. move the atoms accordingly and execute the gates

0 commit comments

Comments
 (0)