Skip to content

Commit

Permalink
⚗️ try to make clang-tidy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
burgholzer committed Nov 9, 2023
1 parent 750eec8 commit c108ec3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 103 deletions.
22 changes: 10 additions & 12 deletions include/Mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Mapper {
/**
* @brief The quantum architecture on which to map the circuit
*/
Architecture& architecture;
Architecture* architecture;

/**
* @brief The resulting quantum circuit after mapping
Expand Down Expand Up @@ -238,7 +238,7 @@ class Mapper {
virtual std::string csv() { return results.csv(); }

std::ostream& printLayering(std::ostream& out) {
out << "---------------- Layering -------------------" << std::endl;
out << "---------------- Layering -------------------\n";
for (auto& layer : layers) {
for (auto& gate : layer) {
if (gate.singleQubit()) {
Expand All @@ -247,33 +247,31 @@ class Mapper {
out << "(" << gate.control << " " << gate.target << ") ";
}
}
out << std::endl;
out << "\n";
}
out << "---------------------------------------------" << std::endl;
out << "---------------------------------------------\n";
return out;
}

std::ostream& printLocations(std::ostream& out) {
out << "---------------- Locations -------------------" << std::endl;
out << "---------------- Locations -------------------\n";
for (std::size_t i = 0; i < qc.getNqubits(); ++i) {
out << locations.at(i) << " ";
}
out << std::endl;
out << "---------------------------------------------" << std::endl;
out << "\n---------------------------------------------\n";
return out;
}
std::ostream& printQubits(std::ostream& out) {
out << "---------------- Qubits -------------------" << std::endl;
for (std::size_t i = 0; i < architecture.getNqubits(); ++i) {
out << "---------------- Qubits -------------------\n";
for (std::size_t i = 0; i < architecture->getNqubits(); ++i) {
out << qubits.at(i) << " ";
}
out << std::endl;
out << "---------------------------------------------" << std::endl;
out << "\n---------------------------------------------\n";
return out;
}

virtual void reset() {
architecture.reset();
architecture->reset();
qc.reset();
layers.clear();
qubits.fill(DEFAULT_POSITION);
Expand Down
4 changes: 2 additions & 2 deletions include/heuristic/HeuristicMapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class HeuristicMapper : public Mapper {
*/
double distanceOnArchitectureOfLogicalQubits(std::uint16_t control,
std::uint16_t target) {
return architecture.distance(
return architecture->distance(
static_cast<std::uint16_t>(locations.at(control)),
static_cast<std::uint16_t>(locations.at(target)));
}
Expand All @@ -189,7 +189,7 @@ class HeuristicMapper : public Mapper {
*/
double distanceOnArchitectureOfPhysicalQubits(std::uint16_t control,
std::uint16_t target) {
return architecture.distance(control, target);
return architecture->distance(control, target);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ void Mapper::initResults() {
countGates(qc, results.input);
results.input.name = qc.getName();
results.input.qubits = static_cast<std::uint16_t>(qc.getNqubits());
results.architecture = architecture.getName();
results.architecture = architecture->getName();
results.output.name = qc.getName() + "_mapped";
results.output.qubits = architecture.getNqubits();
results.output.qubits = architecture->getNqubits();
results.output.gates = std::numeric_limits<std::size_t>::max();
qcMapped.addQubitRegister(architecture.getNqubits());
qcMapped.addQubitRegister(architecture->getNqubits());
}

Mapper::Mapper(qc::QuantumComputation quantumComputation, Architecture& arch)
: qc(std::move(quantumComputation)), architecture(arch) {
: qc(std::move(quantumComputation)), architecture(&arch) {
qubits.fill(DEFAULT_POSITION);
locations.fill(DEFAULT_POSITION);
fidelities.fill(INITIAL_FIDELITY);
Expand Down Expand Up @@ -202,16 +202,16 @@ std::size_t Mapper::getNextLayer(std::size_t idx) {
void Mapper::finalizeMappedCircuit() {
// add additional qubits if the architecture contains more qubits than the
// circuit
if (architecture.getNqubits() > qcMapped.getNqubits()) {
if (architecture->getNqubits() > qcMapped.getNqubits()) {
for (auto logicalQubit = qcMapped.getNqubits();
logicalQubit < architecture.getNqubits(); ++logicalQubit) {
logicalQubit < architecture->getNqubits(); ++logicalQubit) {
std::optional<qc::Qubit> physicalQubit = std::nullopt;

// check if the corresponding physical qubit is already in use
if (qcMapped.initialLayout.find(static_cast<qc::Qubit>(logicalQubit)) !=
qcMapped.initialLayout.end()) {
// get the next unused physical qubit
for (physicalQubit = 0; *physicalQubit < architecture.getNqubits();
for (physicalQubit = 0; *physicalQubit < architecture->getNqubits();
++(*physicalQubit)) {
if (qcMapped.initialLayout.find(*physicalQubit) ==
qcMapped.initialLayout.end()) {
Expand Down Expand Up @@ -239,16 +239,16 @@ void Mapper::finalizeMappedCircuit() {
}

void Mapper::placeRemainingArchitectureQubits() {
if (qc.getNqubits() < architecture.getNqubits()) {
for (auto logical = qc.getNqubits(); logical < architecture.getNqubits();
if (qc.getNqubits() < architecture->getNqubits()) {
for (auto logical = qc.getNqubits(); logical < architecture->getNqubits();
++logical) {
std::optional<qc::Qubit> physical = std::nullopt;

// check if the corresponding physical qubit is already in use
if (qcMapped.initialLayout.find(static_cast<qc::Qubit>(logical)) !=
qcMapped.initialLayout.end()) {
// get the next unused physical qubit
for (physical = 0; *physical < architecture.getNqubits();
for (physical = 0; *physical < architecture->getNqubits();
++(*physical)) {
if (qcMapped.initialLayout.find(*physical) ==
qcMapped.initialLayout.end()) {
Expand Down Expand Up @@ -300,7 +300,7 @@ void Mapper::countGates(decltype(qcMapped.cbegin()) it,

if (g->isStandardOperation()) {
if (g->getType() == qc::SWAP) {
if (architecture.bidirectional()) {
if (architecture->bidirectional()) {
info.gates += GATES_OF_BIDIRECTIONAL_SWAP;
info.cnots += GATES_OF_BIDIRECTIONAL_SWAP;
} else {
Expand Down
66 changes: 33 additions & 33 deletions src/exact/ExactMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,25 @@ void ExactMapper::map(const Configuration& settings) {
}

CouplingMap reducedCouplingMap{};
architecture.getReducedCouplingMap(config.subgraph, reducedCouplingMap);
architecture->getReducedCouplingMap(config.subgraph, reducedCouplingMap);

// check if the subgraph is connected
if (!Architecture::isConnected(config.subgraph, reducedCouplingMap)) {
std::cerr << "The subgraph is not connected." << std::endl;
std::cerr << "The subgraph is not connected.\n";
return;
}

architecture.setCouplingMap(reducedCouplingMap);
architecture->setCouplingMap(reducedCouplingMap);
}

// 2b) If configured to use subsets, collect all k (=m over n) possibilities
// to pick n qubits from m device qubits. Otherwise, consider all qubits.
std::vector<std::uint16_t> qubitRange =
Architecture::getQubitList(architecture.getCouplingMap());
Architecture::getQubitList(architecture->getCouplingMap());

std::vector<QubitChoice> allPossibleQubitChoices{};
if (config.useSubsets) {
allPossibleQubitChoices = architecture.getAllConnectedSubsets(
allPossibleQubitChoices = architecture->getAllConnectedSubsets(
static_cast<std::uint16_t>(qc.getNqubits()));
} else {
allPossibleQubitChoices.emplace_back(qubitRange.begin(), qubitRange.end());
Expand All @@ -100,12 +100,12 @@ void ExactMapper::map(const Configuration& settings) {
std::size_t maxLimit = 0U;
const std::size_t upperLimit = config.swapLimit;
if (config.useSubsets) {
maxLimit = architecture.getCouplingLimit(choice) - 1U;
maxLimit = architecture->getCouplingLimit(choice) - 1U;
} else {
maxLimit = architecture.getCouplingLimit() - 1U;
maxLimit = architecture->getCouplingLimit() - 1U;
}
if (config.swapReduction == SwapReduction::CouplingLimit) {
if (!architecture.bidirectional()) {
if (!architecture->bidirectional()) {
// on a directed architecture, one more SWAP might be needed overall
// due to the directionality of the edges and direction reversal not
// being possible for every gate.
Expand Down Expand Up @@ -150,7 +150,7 @@ void ExactMapper::map(const Configuration& settings) {

// 4) reduce coupling map
CouplingMap reducedCouplingMap = {};
architecture.getReducedCouplingMap(choice, reducedCouplingMap);
architecture->getReducedCouplingMap(choice, reducedCouplingMap);

if (reducedCouplingMap.empty()) {
break;
Expand All @@ -165,7 +165,7 @@ void ExactMapper::map(const Configuration& settings) {
if (config.swapReduction != SwapReduction::None) {
std::cout << "SWAP limit: " << limit;
}
std::cout << std::endl;
std::cout << "\n";
}

// 6) call actual mapping routine
Expand All @@ -175,13 +175,13 @@ void ExactMapper::map(const Configuration& settings) {
if (config.verbose) {
if (!choiceResults.timeout) {
std::cout << "Costs: " << choiceResults.output.swaps << " SWAP(s)";
if (!architecture.bidirectional()) {
if (!architecture->bidirectional()) {
std::cout << ", " << choiceResults.output.directionReverse
<< " direction reverses";
}
std::cout << std::endl;
std::cout << "\n";
} else {
std::cout << "Did not yield a result" << std::endl;
std::cout << "Did not yield a result\n";
}
}

Expand All @@ -199,7 +199,7 @@ void ExactMapper::map(const Configuration& settings) {
}
} while (config.swapReduction == SwapReduction::Increasing &&
(limit <= upperLimit || config.swapLimit == 0) &&
limit < architecture.getCouplingLimit());
limit < architecture->getCouplingLimit());

// stop if a perfect result has been found
if (!results.timeout && results.output.swaps == 0U &&
Expand Down Expand Up @@ -250,14 +250,14 @@ void ExactMapper::map(const Configuration& settings) {

if (settings.verbose) {
std::cout << "Qubits: ";
for (auto q = 0U; q < architecture.getNqubits(); ++q) {
for (auto q = 0U; q < architecture->getNqubits(); ++q) {
std::cout << qubits.at(q) << " ";
}
std::cout << " Locations: ";
for (std::size_t q = 0; q < qc.getNqubits(); ++q) {
std::cout << locations.at(q) << " ";
}
std::cout << std::endl;
std::cout << "\n";
}
++swapsIterator;
}
Expand All @@ -274,7 +274,7 @@ void ExactMapper::map(const Configuration& settings) {
if (gate.singleQubit()) {
if (settings.verbose) {
std::cout << i << ": Added single qubit gate with target: "
<< locations.at(gate.target) << std::endl;
<< locations.at(gate.target) << "\n";
}

qcMapped.emplace_back<qc::StandardOperation>(
Expand All @@ -284,11 +284,11 @@ void ExactMapper::map(const Configuration& settings) {
const Edge cnot = {locations.at(static_cast<std::size_t>(gate.control)),
locations.at(gate.target)};

if (architecture.getCouplingMap().find(cnot) ==
architecture.getCouplingMap().end()) {
if (architecture->getCouplingMap().find(cnot) ==
architecture->getCouplingMap().end()) {
const Edge reverse = {cnot.second, cnot.first};
if (architecture.getCouplingMap().find(reverse) ==
architecture.getCouplingMap().end()) {
if (architecture->getCouplingMap().find(reverse) ==
architecture->getCouplingMap().end()) {
throw QMAPException(
"Invalid CNOT: " + std::to_string(reverse.first) + "-" +
std::to_string(reverse.second));
Expand All @@ -297,7 +297,7 @@ void ExactMapper::map(const Configuration& settings) {
std::cout
<< i
<< ": Added (direction-reversed) cnot with control and target: "
<< cnot.first << " " << cnot.second << std::endl;
<< cnot.first << " " << cnot.second << "\n";
}
qcMapped.h(reverse.first);
qcMapped.h(reverse.second);
Expand All @@ -309,7 +309,7 @@ void ExactMapper::map(const Configuration& settings) {
if (settings.verbose) {
std::cout << i
<< ": Added cnot with control and target: " << cnot.first
<< " " << cnot.second << std::endl;
<< " " << cnot.second << "\n";
}
qcMapped.cx(qc::Control{static_cast<qc::Qubit>(cnot.first)},
cnot.second);
Expand All @@ -332,14 +332,14 @@ void ExactMapper::map(const Configuration& settings) {

if (settings.verbose) {
std::cout << "Qubits: ";
for (auto q = 0U; q < architecture.getNqubits(); ++q) {
for (auto q = 0U; q < architecture->getNqubits(); ++q) {
std::cout << qubits.at(q) << " ";
}
std::cout << " Locations: ";
for (std::size_t q = 0; q < qc.getNqubits(); ++q) {
std::cout << locations.at(q) << " ";
}
std::cout << std::endl;
std::cout << "\n";
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ void ExactMapper::coreMappingRoutine(
//////////////////////////////////////////
if (config.swapLimitsEnabled() && !config.useBDD) {
do {
auto picost = architecture.minimumNumberOfSwaps(
auto picost = architecture->minimumNumberOfSwaps(
pi, static_cast<std::int64_t>(limit));
if (picost > limit) {
skippedPi.insert(piCount);
Expand Down Expand Up @@ -594,7 +594,7 @@ number of variables: (|L|-1) * m!
}

auto coupling = LogicTerm(false);
if (architecture.bidirectional()) {
if (architecture->bidirectional()) {
for (const auto& edge : rcm) {
auto indexFC = x[k][physicalQubitIndex[edge.first]]
[static_cast<std::size_t>(gate.control)];
Expand Down Expand Up @@ -701,8 +701,8 @@ number of variables: (|L|-1) * m!
auto cost = LogicTerm(0);
do {
if (skippedPi.count(piCount) == 0 || !config.swapLimitsEnabled()) {
auto picost = architecture.minimumNumberOfSwaps(pi);
if (architecture.bidirectional()) {
auto picost = architecture->minimumNumberOfSwaps(pi);
if (architecture->bidirectional()) {
picost *= GATES_OF_BIDIRECTIONAL_SWAP;
} else {
picost *= GATES_OF_UNIDIRECTIONAL_SWAP;
Expand All @@ -727,7 +727,7 @@ number of variables: (|L|-1) * m!
}

// cost for reversed directions
if (!architecture.bidirectional()) {
if (!architecture->bidirectional()) {
const auto numLayers = reducedLayerIndices.size();
for (std::size_t k = 0; k < numLayers; ++k) {
for (const auto& gate : layers.at(reducedLayerIndices.at(k))) {
Expand Down Expand Up @@ -818,9 +818,9 @@ number of variables: (|L|-1) * m!
} while (std::next_permutation(pi.begin(), pi.end()));
}

architecture.minimumNumberOfSwaps(pi, swaps.at(k));
architecture->minimumNumberOfSwaps(pi, swaps.at(k));
choiceResults.output.swaps += swaps.at(k).size();
if (architecture.bidirectional()) {
if (architecture->bidirectional()) {
choiceResults.output.gates +=
GATES_OF_BIDIRECTIONAL_SWAP * swaps.at(k).size();
} else {
Expand All @@ -830,7 +830,7 @@ number of variables: (|L|-1) * m!
}

// direction reverse
if (!architecture.bidirectional()) {
if (!architecture->bidirectional()) {
for (std::size_t k = 0; k < reducedLayerIndices.size(); ++k) {
for (const auto& gate : layers.at(reducedLayerIndices.at(k))) {
if (gate.singleQubit()) {
Expand Down
Loading

0 comments on commit c108ec3

Please sign in to comment.