diff --git a/include/chain.hpp b/include/chain.hpp index 5fd449be..c97f1748 100644 --- a/include/chain.hpp +++ b/include/chain.hpp @@ -78,7 +78,7 @@ class chain { clear(); for (auto &q : c) { data.emplace(q, pair(q, 1)); - minorminer_assert(0 <= q && q < qubit_weight.size()); + minorminer_assert(0 <= q && q < static_cast(qubit_weight.size())); qubit_weight[q]++; } DIAGNOSE_CHAIN(); @@ -96,10 +96,10 @@ class chain { } //! number of qubits in chain - inline int size() const { return data.size(); } + inline size_t size() const { return data.size(); } //! returns 0 if `q` is not contained in `this`, 1 otherwise - inline int count(const int q) const { return data.count(q); } + inline size_t count(const int q) const { return data.count(q); } //! get the qubit, in `this`, which links `this` to the chain of x //!(if x==label, interpret the linking qubit as the chain's root) @@ -229,13 +229,13 @@ class chain { //! store this chain into a `frozen_chain`, unlink all chains from //! this, and clear() - inline int freeze(vector &others, frozen_chain &keep) { + inline size_t freeze(vector &others, frozen_chain &keep) { keep.clear(); for (auto &v_p : links) { keep.links.emplace(v_p); int v = v_p.first; if (v != label) { - minorminer_assert(0 <= v && v < others.size()); + minorminer_assert(0 <= v && v < static_cast(others.size())); int q = others[v].drop_link(label); keep.links.emplace(-v - 1, q); } @@ -260,7 +260,7 @@ class chain { links.emplace(v_p); } else { v = -v - 1; - minorminer_assert(0 <= v && v < others.size()); + minorminer_assert(0 <= v && v < static_cast(others.size())); others[v].set_link(label, v_p.second); } } @@ -279,7 +279,7 @@ class chain { minorminer_assert(q != -1); minorminer_assert(p != -1); - while ((chainsize == 0 || size() < chainsize) && ep.accepts_qubit(label, p)) { + while ((chainsize == 0 || static_cast(size()) < chainsize) && ep.accepts_qubit(label, p)) { int r = other.trim_leaf(p); minorminer_assert(other.size() >= 1); if (r == p) break; diff --git a/include/embedding.hpp b/include/embedding.hpp index 4699c356..35864ff7 100644 --- a/include/embedding.hpp +++ b/include/embedding.hpp @@ -80,7 +80,7 @@ class embedding { auto &c = var_embedding[v]; int root = vC.second[0]; c.set_link(v, root); - int hits = 0; + size_t hits = 0; stack.push_back(root); while (stack.size()) { hits++; @@ -368,7 +368,6 @@ class embedding { int err = 0; vector tmp_weight(num_qubits + num_reserved, 0); int zeros = 0; - int bad_parents = false; for (int v = 0; v < num_vars + num_fixed; v++) { if (!ep.fixed(v)) { for (auto &q : var_embedding.at(v)) { @@ -417,7 +416,6 @@ class embedding { err = 1; } for (int v = num_vars + num_fixed; v--;) { - int n = chainsize(v); vector good_links(num_vars + num_fixed, 0); if (chainsize(v)) { for (auto &u : ep.var_neighbors(v)) { diff --git a/include/embedding_problem.hpp b/include/embedding_problem.hpp index 6606f76c..257b8178 100644 --- a/include/embedding_problem.hpp +++ b/include/embedding_problem.hpp @@ -242,7 +242,7 @@ class embedding_problem_base { if (ultramax_weight < 2) throw MinorMinerException("problem is too large to avoid overflow"); if (ultramax_weight < params.max_fill) - weight_bound = std::floor(ultramax_weight); + weight_bound = static_cast(std::floor(ultramax_weight)); else weight_bound = params.max_fill; @@ -254,9 +254,9 @@ class embedding_problem_base { private: //! computes an upper bound on the distances computed during tearout & replace - unsigned int compute_margin() { + size_t compute_margin() { if (num_q == 0) return 0; - unsigned int max_degree = + size_t max_degree = std::max_element(begin(var_nbrs), end(var_nbrs), [](const vector &a, const vector &b) { return a.size() < b.size(); }) ->size(); @@ -275,7 +275,7 @@ class embedding_problem_base { double base = min(exp2(log2base), min(max_beta, round_beta)); double power = 1; for (int i = 0; i <= max_weight; i++) { - weight_table[i] = power; + weight_table[i] = static_cast(power); power *= base; } for (int i = max_weight + 1; i < 64; i++) weight_table[i] = max_distance; @@ -302,7 +302,7 @@ class embedding_problem_base { //! transposition before returning the reference const vector &var_neighbors(int u, rndswap_first) { if (var_nbrs[u].size() > 2) { - int i = randint(0, var_nbrs[u].size() - 2); + size_t i = randint(0, var_nbrs[u].size() - 2); std::swap(var_nbrs[u][i], var_nbrs[u][i + 1]); } else if (var_nbrs[u].size() == 2) { if (randint(0, 1)) std::swap(var_nbrs[u][0], var_nbrs[u][1]); @@ -387,7 +387,7 @@ class embedding_problem_base { visited[x] = 1; while (front < component.size()) { int x = component[front++]; - unsigned int lastback = component.size(); + size_t lastback = component.size(); for (auto &y : neighbors[x]) { if (!visited[y]) { visited[y] = 1; diff --git a/include/find_embedding.hpp b/include/find_embedding.hpp index 6706f89c..2a0fd770 100644 --- a/include/find_embedding.hpp +++ b/include/find_embedding.hpp @@ -84,9 +84,9 @@ class parameter_processor { } vector _inverse_permutation(vector &f) { - int n = f.size(); + size_t n = f.size(); vector r(n); - for (int i = n; i--;) { + for (size_t i = n; i--;) { r.at(f[i]) = i; } return r; @@ -222,7 +222,7 @@ int findEmbedding(graph::input_graph &var_g, graph::input_graph &qubit_g, option if (params.return_overlap || success) { chains.resize(var_g.num_nodes()); - for (int u = 0; u < var_g.num_nodes(); u++) { + for (size_t u = 0; u < var_g.num_nodes(); u++) { pf.get_chain(u, chains[u]); } } else { diff --git a/include/graph.hpp b/include/graph.hpp index 743ba505..10ffb639 100644 --- a/include/graph.hpp +++ b/include/graph.hpp @@ -61,7 +61,7 @@ class input_graph { // In std::vector edges_aside; std::vector edges_bside; - int _num_nodes; + size_t _num_nodes; //! this method converts a std::vector of sets into a std::vector of sets, ensuring //! that element i is not contained in nbrs[i]. this method is called by @@ -70,7 +70,7 @@ class input_graph { //! in a contiguous memory segment. std::vector> _to_vectorhoods(std::vector>& _nbrs) const { std::vector> nbrs; - for (int i = 0; i < _num_nodes; i++) { + for (size_t i = 0; i < _num_nodes; i++) { std::set& nbrset = _nbrs[i]; nbrset.erase(i); nbrs.emplace_back(std::begin(nbrset), std::end(nbrset)); @@ -104,15 +104,15 @@ class input_graph { int b(const int i) const { return edges_bside[i]; } //! Return the size of the graph in nodes - int num_nodes() const { return _num_nodes; } + size_t num_nodes() const { return _num_nodes; } //! Return the size of the graph in edges - int num_edges() const { return edges_aside.size(); } + size_t num_edges() const { return edges_aside.size(); } //! Add an edge to the graph void push_back(int ai, int bi) { edges_aside.push_back(ai); edges_bside.push_back(bi); - _num_nodes = std::max(_num_nodes, std::max(ai, bi) + 1); + _num_nodes = std::max(_num_nodes, static_cast(std::max(ai, bi) + 1)); } private: @@ -124,7 +124,7 @@ class input_graph { inline std::vector> __get_neighbors(const unaryint& sources, const unaryint& sinks, const unaryint& relabel, const unaryint& mask) const { std::vector> _nbrs(_num_nodes); - for (int i = num_edges(); i--;) { + for (size_t i = num_edges(); i--;) { int ai = a(i), bi = b(i); if (mask(ai)) { int rai = relabel(ai), rbi = relabel(bi); @@ -135,7 +135,7 @@ class input_graph { return _to_vectorhoods(_nbrs); } - //! smash the types throgh unaryint + //! smash the types through unaryint template inline std::vector> _get_neighbors(const T1& sources, const T2& sinks, const T3& relabel = nullptr, const T4& mask = true) const { @@ -197,14 +197,14 @@ class components { to store the parent and rank data for union/find operations. */ std::vector& parent = index; - for (int x = g.num_nodes(); x--;) { + for (size_t x = g.num_nodes(); x--;) { parent[x] = x; } - for (int i = g.num_edges(); i--;) { + for (size_t i = g.num_edges(); i--;) { __init_union(g.a(i), g.b(i)); } - for (int x = g.num_nodes(); x--;) component[__init_find(x)].push_back(x); + for (size_t x = g.num_nodes(); x--;) component[__init_find(x)].push_back(x); sort(std::begin(component), std::end(component), [](const std::vector& a, const std::vector& b) { return a.size() > b.size(); }); @@ -217,13 +217,13 @@ class components { The labels associated with component[c] are the numbers 0 through component[c].size()-1. */ - for (int c = 0; c < g.num_nodes(); c++) { + for (size_t c = 0; c < g.num_nodes(); c++) { std::vector& comp = component[c]; auto back = std::end(comp); for (auto front = std::begin(comp); front < back; front++) while (front < back && reserve(*front)) iter_swap(front, --back); if (comp.size()) { - for (int j = comp.size(); j--;) { + for (size_t j = comp.size(); j--;) { label[comp[j]] = j; index[comp[j]] = c; } @@ -234,7 +234,7 @@ class components { break; } } - for (int i = g.num_edges(); i--;) { + for (size_t i = g.num_edges(); i--;) { int a = g.a(i); int b = g.b(i); component_g[index[a]].push_back(label[a], label[b]); @@ -250,20 +250,20 @@ class components { const std::vector& nodes(int c) const { return component[c]; } //! Get the number of connected components in the graph - int size() const { return component_g.size(); } + size_t size() const { return component_g.size(); } //! returns the number of reserved nodes in a component - int num_reserved(int c) const { return _num_reserved[c]; } + size_t num_reserved(int c) const { return _num_reserved[c]; } //! Get the size (in nodes) of a component - int size(int c) const { return component_g[c].num_nodes(); } + size_t size(int c) const { return component_g[c].num_nodes(); } //! Get a const reference to the graph object of a component const input_graph& component_graph(int c) const { return component_g[c]; } //! Construct a neighborhood list for component c, with reserved nodes as sources std::vector> component_neighbors(int c) const { - return component_g[c].get_neighbors_sources(size(c) - num_reserved(c)); + return component_g[c].get_neighbors_sources(static_cast(size(c)) - static_cast(num_reserved(c))); } //! translate nodes from the input graph, to their labels in component c @@ -321,4 +321,4 @@ class components { std::vector> component; std::vector component_g; }; -} +} // namespace graph diff --git a/include/pathfinder.hpp b/include/pathfinder.hpp index 0765d8dc..2288bda0 100644 --- a/include/pathfinder.hpp +++ b/include/pathfinder.hpp @@ -117,32 +117,36 @@ class pathfinder_base : public pathfinder_public_interface { } } - void set_initial_chains(map> chains) { + //! setter for the initial_chains parameter + virtual void set_initial_chains(map> chains) override { initEmbedding = embedding_t(ep, params.fixed_chains, chains); } virtual ~pathfinder_base() {} //! nonzero return if this is an improvement on our previous best embedding - int check_improvement(const embedding_t &emb) { - int better = 0; + bool check_improvement(const embedding_t &emb) { + bool better = 0; int embedded = emb.statistics(tmp_stats); if (embedded > ep.embedded) { ep.major_info("embedding found.\n"); - better = ep.embedded = 1; + better = true; + ep.embedded = 1; } if (embedded < ep.embedded) return 0; int minorstat = tmp_stats.back(); - int major = best_stats.size() - tmp_stats.size(); + int major = static_cast(best_stats.size()) - static_cast(tmp_stats.size()); int minor = (best_stats.size() == 0) ? 0 : best_stats.back() - minorstat; better |= (major > 0) || (best_stats.size() == 0); if (better) { if (ep.embedded) { - ep.major_info("max chain length %d; num max chains=%d\n", tmp_stats.size() - 1, minorstat); - ep.target_chainsize = tmp_stats.size() - 1; + ep.major_info("max chain length %d; num max chains=%d\n", static_cast(tmp_stats.size()) - 1, + minorstat); + ep.target_chainsize = static_cast(tmp_stats.size()) - 1; } else { - ep.major_info("max qubit fill %d; num maxfull qubits=%d\n", tmp_stats.size() + 1, minorstat); + ep.major_info("max qubit fill %d; num maxfull qubits=%d\n", static_cast(tmp_stats.size()) + 1, + minorstat); } } if ((!better) && (major == 0) && (minor > 0)) { @@ -151,10 +155,10 @@ class pathfinder_base : public pathfinder_public_interface { } else { ep.minor_info(" num max qubits=%d\n", minorstat); } - better = 1; + better = true; } if (!better && (major == 0) && (minor == 0)) { - for (int i = tmp_stats.size(); i--;) { + for (size_t i = tmp_stats.size(); i--;) { if (tmp_stats[i] == best_stats[i]) continue; if (tmp_stats[i] < best_stats[i]) better = 1; break; @@ -190,7 +194,7 @@ class pathfinder_base : public pathfinder_public_interface { inline int check_stops(const int &return_value) { try { params.localInteractionPtr->cancelled(stoptime); - } catch (const TimeoutException &e) { + } catch (const TimeoutException & /*e*/) { ep.major_info("problem timed out"); return -2; } catch (const ProblemCancelledException &e) { @@ -228,7 +232,7 @@ class pathfinder_base : public pathfinder_public_interface { improved |= check_improvement(emb); if (ep.embedded) break; } - return check_stops(improved); + return check_stops(static_cast(improved)); } //! tear up and replace each chain, strictly improving or maintaining the @@ -333,7 +337,7 @@ class pathfinder_base : public pathfinder_public_interface { // will be altered for at least one neighbor per pass. auto &nbrs = ep.var_neighbors(u, rndswap_first{}); if (nbrs.size() > 0) { - int v = nbrs[ep.randint(0, nbrs.size() - 1)]; + int v = nbrs[ep.randint(0, static_cast(nbrs.size() - 1))]; qubit_permutations[u].swap(qubit_permutations[v]); } @@ -342,7 +346,7 @@ class pathfinder_base : public pathfinder_public_interface { // select a random root among those qubits at minimum heuristic distance collectMinima(total_distance, min_list); - int q0 = min_list[ep.randint(0, min_list.size() - 1)]; + int q0 = min_list[ep.randint(0, static_cast(min_list.size()) - 1)]; if (total_distance[q0] == max_distance) return 0; // oops all qubits were overfull or unreachable emb.construct_chain_steiner(u, q0, parents, distances, visited_list); @@ -361,7 +365,7 @@ class pathfinder_base : public pathfinder_public_interface { auto &counts = total_distance; counts.assign(num_qubits, 0); unsigned int best_size = std::numeric_limits::max(); - int q, degree = ep.var_neighbors(u).size(); + int q, degree = static_cast(ep.var_neighbors(u).size()); distance_t d; unsigned int stopcheck = static_cast(max(last_size, target_chainsize)); @@ -511,7 +515,7 @@ class pathfinder_base : public pathfinder_public_interface { public: virtual void quickPass(VARORDER varorder, int chainlength_bound, int overlap_bound, bool local_search, - bool clear_first, double round_beta) { + bool clear_first, double round_beta) override { const vector &vo = ep.var_order(varorder); if (vo.size() == 0) throw BadInitializationException( @@ -523,7 +527,7 @@ class pathfinder_base : public pathfinder_public_interface { } virtual void quickPass(const vector &varorder, int chainlength_bound, int overlap_bound, bool local_search, - bool clear_first, double round_beta) { + bool clear_first, double round_beta) override { int lastsize, got; int old_bound = ep.weight_bound; ep.weight_bound = 1 + overlap_bound; diff --git a/minorminer/src/pyutil.hpp b/minorminer/src/pyutil.hpp index b2aa8631..06be8cc5 100644 --- a/minorminer/src/pyutil.hpp +++ b/minorminer/src/pyutil.hpp @@ -13,7 +13,7 @@ class LocalInteractionPython : public find_embedding::LocalInteraction { virtual void displayErrorImpl(int, const std::string &msg) const { PySys_WriteStderr("%s", msg.c_str()); } virtual bool cancelledImpl() const { - bool cancelled = PyErr_CheckSignals(); + bool cancelled = static_cast(PyErr_CheckSignals()); if (cancelled) PyErr_Clear(); return cancelled; } @@ -38,7 +38,7 @@ class LocalInteractionLogger : public find_embedding::LocalInteraction { } virtual bool cancelledImpl() const { - bool cancelled = PyErr_CheckSignals(); + bool cancelled = static_cast(PyErr_CheckSignals()); if (cancelled) PyErr_Clear(); return cancelled; } diff --git a/tests/test_chain.cpp b/tests/test_chain.cpp index f27fb043..e273ea88 100644 --- a/tests/test_chain.cpp +++ b/tests/test_chain.cpp @@ -14,7 +14,7 @@ struct embedding { class embedding_problem_t { public: - inline bool accepts_qubit(int v, int q) { return true; } + inline bool accepts_qubit(int /*v*/, int /*q*/) { return true; } }; //