diff --git a/.clang-format b/.clang-format index 9a036c2..0995233 100644 --- a/.clang-format +++ b/.clang-format @@ -8,3 +8,11 @@ UseTab: Never PointerAlignment: Left IndentCaseLabels: true AlwaysBreakTemplateDeclarations: Yes +InsertBraces: true +SortIncludes: true +# system headers come first +IncludeCategories: + - Regex: "^<.*>" + Priority: 1 + - Regex: '^".*\.(h|hpp)"' + Priority: 2 diff --git a/prettyprint.sh b/prettyprint.sh index b0976d7..a14ac3a 100755 --- a/prettyprint.sh +++ b/prettyprint.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash +# $@ - List of directories + # Code beautifier with clang-format -# Recursively parses the folder(s) passed as command line argument(s) +# Recursively parses the directories passed as command line arguments if test -z "$CLANG_FORMAT"; then echo "Please set the CLANG_FORMAT environment variable to point to the \ diff --git a/qasmtools/include/qasmtools/ast/decl.hpp b/qasmtools/include/qasmtools/ast/decl.hpp index ad83809..da4ee36 100644 --- a/qasmtools/include/qasmtools/ast/decl.hpp +++ b/qasmtools/include/qasmtools/ast/decl.hpp @@ -162,8 +162,9 @@ class GateDecl final : public Stmt, public Decl { * \param f A void function taking a reference to a Gate */ void foreach_stmt(std::function f) { - for (auto it = body_.begin(); it != body_.end(); it++) + for (auto it = body_.begin(); it != body_.end(); it++) { f(**it); + } } /** @@ -183,8 +184,9 @@ class GateDecl final : public Stmt, public Decl { void accept(Visitor& visitor) override { visitor.visit(*this); } std::ostream& pretty_print(std::ostream& os, bool suppress_std) const override { - if (suppress_std && is_std_qelib(id_)) + if (suppress_std && is_std_qelib(id_)) { return os; + } os << (opaque_ ? "opaque " : "gate ") << id_; if (c_params_.size() > 0) { @@ -381,8 +383,9 @@ class AncillaDecl final : public Gate, public Decl { void accept(Visitor& visitor) override { visitor.visit(*this); } std::ostream& pretty_print(std::ostream& os, bool) const override { - if (dirty_) + if (dirty_) { os << "dirty "; + } os << "ancilla " << id_ << "[" << size_ << "];\n"; return os; } diff --git a/qasmtools/include/qasmtools/ast/expr.hpp b/qasmtools/include/qasmtools/ast/expr.hpp index fbf6ed0..6dd98a3 100644 --- a/qasmtools/include/qasmtools/ast/expr.hpp +++ b/qasmtools/include/qasmtools/ast/expr.hpp @@ -200,8 +200,9 @@ class BExpr final : public Expr { auto lexp = lexp_->constant_eval(); auto rexp = rexp_->constant_eval(); - if (!lexp || !rexp) + if (!lexp || !rexp) { return std::nullopt; + } switch (op_) { case BinaryOp::Plus: @@ -293,8 +294,9 @@ class UExpr final : public Expr { std::optional constant_eval() const override { auto expr = exp_->constant_eval(); - if (!expr) + if (!expr) { return std::nullopt; + } switch (op_) { case UnaryOp::Neg: @@ -320,9 +322,9 @@ class UExpr final : public Expr { (void)ctx; os << op_; - if (op_ == UnaryOp::Neg) + if (op_ == UnaryOp::Neg) { exp_->pretty_print(os, true); - else { + } else { os << "("; exp_->pretty_print(os, false); os << ")"; diff --git a/qasmtools/include/qasmtools/ast/program.hpp b/qasmtools/include/qasmtools/ast/program.hpp index bca5b35..ddac85c 100644 --- a/qasmtools/include/qasmtools/ast/program.hpp +++ b/qasmtools/include/qasmtools/ast/program.hpp @@ -99,8 +99,9 @@ class Program : public ASTNode { * \param f Void function accepting a reference to a statement */ void foreach_stmt(std::function f) { - for (auto it = body_.begin(); it != body_.end(); it++) + for (auto it = body_.begin(); it != body_.end(); it++) { f(**it); + } } /** @@ -120,8 +121,9 @@ class Program : public ASTNode { void accept(Visitor& visitor) override { visitor.visit(*this); } std::ostream& pretty_print(std::ostream& os) const override { os << "OPENQASM 2.0;\n"; - if (std_include_) + if (std_include_) { os << "include \"qelib1.inc\";\n"; + } os << "\n"; for (auto it = body_.begin(); it != body_.end(); it++) { (*it)->pretty_print(os, std_include_); diff --git a/qasmtools/include/qasmtools/ast/replacer.hpp b/qasmtools/include/qasmtools/ast/replacer.hpp index 40ee4b7..8838d31 100644 --- a/qasmtools/include/qasmtools/ast/replacer.hpp +++ b/qasmtools/include/qasmtools/ast/replacer.hpp @@ -359,10 +359,11 @@ class GateReplacer final : public Replacer { std::optional>> replace_gate(Gate& gate) { auto it = replacements_.find(gate.uid()); - if (it != replacements_.end()) + if (it != replacements_.end()) { return std::move(it->second); - else + } else { return std::nullopt; + } } }; diff --git a/qasmtools/include/qasmtools/ast/semantic.hpp b/qasmtools/include/qasmtools/ast/semantic.hpp index 2c68357..75d842f 100644 --- a/qasmtools/include/qasmtools/ast/semantic.hpp +++ b/qasmtools/include/qasmtools/ast/semantic.hpp @@ -333,8 +333,9 @@ class SemanticChecker final : public Visitor { * \param typ The type of the symbol */ void set(const ast::symbol& id, Type typ) { - if (symbol_table_.empty()) + if (symbol_table_.empty()) { throw std::logic_error("No current symbol table!"); + } symbol_table_.front()[id] = typ; } @@ -467,8 +468,9 @@ class SemanticChecker final : public Visitor { */ inline void check_source(Program& prog) { SemanticChecker analysis; - if (analysis.run(prog)) + if (analysis.run(prog)) { throw SemanticError(); + } } } /* namespace ast */ diff --git a/qasmtools/include/qasmtools/ast/stmt.hpp b/qasmtools/include/qasmtools/ast/stmt.hpp index 0b3b93c..e4ae2ef 100644 --- a/qasmtools/include/qasmtools/ast/stmt.hpp +++ b/qasmtools/include/qasmtools/ast/stmt.hpp @@ -499,8 +499,9 @@ class BarrierGate final : public Gate { * \param f Void function accepting a reference to the argument */ void foreach_arg(std::function f) { - for (auto it = args_.begin(); it != args_.end(); it++) + for (auto it = args_.begin(); it != args_.end(); it++) { f(*it); + } } /** @@ -612,8 +613,9 @@ class DeclaredGate final : public Gate { * \param f Void function accepting an expression reference */ void foreach_carg(std::function f) { - for (auto it = c_args_.begin(); it != c_args_.end(); it++) + for (auto it = c_args_.begin(); it != c_args_.end(); it++) { f(**it); + } } /** @@ -622,8 +624,9 @@ class DeclaredGate final : public Gate { * \param f Void function accepting a reference to an argument */ void foreach_qarg(std::function f) { - for (auto it = q_args_.begin(); it != q_args_.end(); it++) + for (auto it = q_args_.begin(); it != q_args_.end(); it++) { f(*it); + } } /** diff --git a/qasmtools/include/qasmtools/ast/traversal.hpp b/qasmtools/include/qasmtools/ast/traversal.hpp index 794a3ef..e552234 100644 --- a/qasmtools/include/qasmtools/ast/traversal.hpp +++ b/qasmtools/include/qasmtools/ast/traversal.hpp @@ -83,27 +83,32 @@ class Traverse : public Visitor { gate.tgt().accept(*this); } void visit(BarrierGate& gate) override { - for (int i = 0; i < gate.num_args(); i++) + for (int i = 0; i < gate.num_args(); i++) { gate.arg(i).accept(*this); + } } void visit(DeclaredGate& gate) override { - for (int i = 0; i < gate.num_cargs(); i++) + for (int i = 0; i < gate.num_cargs(); i++) { gate.carg(i).accept(*this); - for (int i = 0; i < gate.num_qargs(); i++) + } + for (int i = 0; i < gate.num_qargs(); i++) { gate.qarg(i).accept(*this); + } } void visit(GateDecl& decl) override { - for (auto it = decl.begin(); it != decl.end(); it++) + for (auto it = decl.begin(); it != decl.end(); it++) { (**it).accept(*this); + } } void visit(OracleDecl& decl) override {} void visit(RegisterDecl& decl) override {} void visit(AncillaDecl& decl) override {} void visit(Program& prog) override { - for (auto it = prog.begin(); it != prog.end(); it++) + for (auto it = prog.begin(); it != prog.end(); it++) { (**it).accept(*this); + } } }; diff --git a/qasmtools/include/qasmtools/ast/var.hpp b/qasmtools/include/qasmtools/ast/var.hpp index 3f152bd..8301544 100644 --- a/qasmtools/include/qasmtools/ast/var.hpp +++ b/qasmtools/include/qasmtools/ast/var.hpp @@ -113,10 +113,11 @@ class VarAccess final : public ASTNode { * Used to allow variable accesses as keys in ordered maps */ bool operator<(const VarAccess& v) const { - if (var_ == v.var_) + if (var_ == v.var_) { return offset_ < v.offset_; - else + } else { return var_ < v.var_; + } } /** @@ -130,10 +131,11 @@ class VarAccess final : public ASTNode { * \return true if the variable access contains v */ bool contains(const VarAccess& v) const { - if (offset_) + if (offset_) { return *this == v; - else + } else { return v.var_ == var_; + } } /** @@ -157,8 +159,9 @@ class VarAccess final : public ASTNode { void accept(Visitor& visitor) override { visitor.visit(*this); } std::ostream& pretty_print(std::ostream& os) const override { os << var_; - if (offset_) + if (offset_) { os << "[" << *offset_ << "]"; + } return os; } diff --git a/qasmtools/include/qasmtools/parser/lexer.hpp b/qasmtools/include/qasmtools/parser/lexer.hpp index 3715847..5343d7c 100644 --- a/qasmtools/include/qasmtools/parser/lexer.hpp +++ b/qasmtools/include/qasmtools/parser/lexer.hpp @@ -95,10 +95,11 @@ class Lexer { } pos_.advance_column(consumed); - if (consumed != 0) + if (consumed != 0) { return true; - else + } else { return false; + } } /** diff --git a/qasmtools/include/qasmtools/parser/parser.hpp b/qasmtools/include/qasmtools/parser/parser.hpp index 11cbd6c..4b21cc4 100644 --- a/qasmtools/include/qasmtools/parser/parser.hpp +++ b/qasmtools/include/qasmtools/parser/parser.hpp @@ -84,12 +84,14 @@ class Parser { ast::ptr parse(bool check = true) { // Parse the program auto result = parse_program(); - if (error_) + if (error_) { throw ParseError(); + } // Perform semantic analysis before returning - if (check) + if (check) { ast::check_source(*result); + } return result; } @@ -102,8 +104,9 @@ class Parser { */ void consume_token(bool reset = false) { current_token_ = pp_lexer_.next_token(); - if (reset) + if (reset) { supress_errors_ = false; + } } /** diff --git a/qasmtools/include/qasmtools/parser/preprocessor.hpp b/qasmtools/include/qasmtools/parser/preprocessor.hpp index 8d77d05..2b806da 100644 --- a/qasmtools/include/qasmtools/parser/preprocessor.hpp +++ b/qasmtools/include/qasmtools/parser/preprocessor.hpp @@ -248,8 +248,9 @@ class Preprocessor { } auto target = token.as_string(); - if (target == "qelib1.inc") + if (target == "qelib1.inc") { std_include_ = true; + } token = current_lexer_->next_token(); if (token.is_not(Token::Kind::semicolon)) { diff --git a/qasmtools/include/qasmtools/tools/ast_printer.hpp b/qasmtools/include/qasmtools/tools/ast_printer.hpp index 53e562e..50c2dfa 100644 --- a/qasmtools/include/qasmtools/tools/ast_printer.hpp +++ b/qasmtools/include/qasmtools/tools/ast_printer.hpp @@ -47,8 +47,9 @@ class ASTPrinter final : public ast::Visitor { // Variables void visit(ast::VarAccess& ap) { os_ << prefix_ << "|- Var(" << ap.var(); - if (ap.offset()) + if (ap.offset()) { os_ << "[" << *ap.offset() << "]"; + } os_ << ")\n"; } @@ -153,14 +154,17 @@ class ASTPrinter final : public ast::Visitor { // Declarations void visit(ast::GateDecl& decl) { os_ << prefix_ << "|- Gate Decl(" << decl.id() << "("; - for (auto& param : decl.c_params()) + for (auto& param : decl.c_params()) { os_ << param << ","; + } os_ << ")["; - for (auto& param : decl.q_params()) + for (auto& param : decl.q_params()) { os_ << param << ","; + } os_ << "]"; - if (decl.is_opaque()) + if (decl.is_opaque()) { os_ << ", opaque"; + } os_ << ")\n"; prefix_ += " "; @@ -170,24 +174,27 @@ class ASTPrinter final : public ast::Visitor { void visit(ast::OracleDecl& decl) { os_ << prefix_ << "|- Oracle Decl(" << decl.id() << "["; - for (auto& param : decl.params()) + for (auto& param : decl.params()) { os_ << param << ","; + } os_ << "] = " << decl.fname() << ")\n"; } void visit(ast::RegisterDecl& decl) { os_ << prefix_ << "|- Register Decl(" << decl.id() << "[" << decl.size() << "]"; - if (decl.is_quantum()) + if (decl.is_quantum()) { os_ << ", quantum"; + } os_ << ")\n"; } void visit(ast::AncillaDecl& decl) { os_ << prefix_ << "|- Ancilla Decl(" << decl.id() << "[" << decl.size() << "]"; - if (decl.is_dirty()) + if (decl.is_dirty()) { os_ << ", dirty"; + } os_ << ")\n"; } diff --git a/qasmtools/include/qasmtools/utils/angle.hpp b/qasmtools/include/qasmtools/utils/angle.hpp index 314a8d7..929e784 100644 --- a/qasmtools/include/qasmtools/utils/angle.hpp +++ b/qasmtools/include/qasmtools/utils/angle.hpp @@ -66,16 +66,18 @@ class Angle { public: constexpr Angle(int n, int d) : value_(std::make_pair(n, d)) { - if (d == 0) + if (d == 0) { throw std::invalid_argument( "Trying to construct angle with denominator 0"); + } normalize(); } constexpr Angle(fraction angle) : value_(angle) { - if (angle.second == 0) + if (angle.second == 0) { throw std::invalid_argument( "Trying to construct angle with denominator 0"); + } normalize(); } diff --git a/src/AffineState.cpp b/src/AffineState.cpp index 110ab9c..f4a8713 100644 --- a/src/AffineState.cpp +++ b/src/AffineState.cpp @@ -61,8 +61,9 @@ std::vector AffineState::A_col_nonzeros(int col) { std::vector ones; ones.reserve(n_); for (int row = 0; row < n_; ++row) { - if (A_(row, col) == 1) + if (A_(row, col) == 1) { ones.push_back(row); + } } return ones; } @@ -74,8 +75,9 @@ std::vector AffineState::Q_nonzeros() { std::vector ones; ones.reserve(r_ - 1); for (int row = 0; row < r_ - 1; ++row) { - if (Q_(row, r_ - 1) != 0) + if (Q_(row, r_ - 1) != 0) { ones.push_back(row); + } } return ones; } @@ -87,8 +89,9 @@ std::vector AffineState::A_row_nonzeros(int row) { std::vector ones; ones.reserve(r_); for (int col = 0; col < r_; ++col) { - if (A_(row, col) != 0) + if (A_(row, col) != 0) { ones.push_back(col); + } } return ones; } @@ -106,8 +109,9 @@ void AffineState::ReindexSubtColumn(int k, int c, // Applies the update Column k of A <--- Column k - Column c assert(c != k); - for (int row : col_c_nonzeros) + for (int row : col_c_nonzeros) { A_(row, k) ^= 1; + } // These three lines could potentially be optimized but I do not anticipate // a significant speedup, so leaving as is for clarity @@ -164,8 +168,9 @@ void AffineState::FixFinalBit(int z) { int u = Q_(r_ - 1, r_ - 1) % 4; // Step 2: - for (int row : a_ones) + for (int row : a_ones) { A_(row, r_ - 1) = 0; + } for (int rowcol : q_ones) { Q_(rowcol, r_ - 1) = 0; @@ -175,10 +180,12 @@ void AffineState::FixFinalBit(int z) { // Step 3: if (z == 1) { - for (int i : q_ones) + for (int i : q_ones) { Q_(i, i) = (Q_(i, i) + 2) % 4; - for (int row : a_ones) + } + for (int row : a_ones) { b_(row) ^= 1; + } phase_ = (phase_ + 2 * u) % 8; } @@ -227,8 +234,9 @@ void AffineState::ZeroColumnElim(int c) { std::vector col_ell_nonzeros = A_col_nonzeros(ell); for (int col : q_ones) { - if (col != ell) + if (col != ell) { ReindexSubtColumn(col, ell, col_ell_nonzeros); + } } ReindexSwapColumns(ell); @@ -240,10 +248,11 @@ int AffineState::piv_col(int row_number) { // Given a row number, return the index of the column j for in which that // row has a pivot, and return -1 otherwise. auto it = std::find(pivots_.begin(), pivots_.end(), row_number); - if (it != pivots_.end()) + if (it != pivots_.end()) { return it - pivots_.begin(); - else + } else { return -1; + } } // GATES: @@ -280,8 +289,9 @@ void AffineState::H(int j) { b_(j) = 0; // Step 7: - if (c > -1) + if (c > -1) { ZeroColumnElim(c); + } } void AffineState::CZ(int j, int k) { @@ -300,19 +310,22 @@ void AffineState::CZ(int j, int k) { Q_(jj, jj) = (Q_(jj, jj) + 2 * b_(k)) % 4; } - for (int kk : Ak_ones) + for (int kk : Ak_ones) { Q_(kk, kk) = (Q_(kk, kk) + 2 * b_(j)) % 4; + } phase_ = (phase_ + 4 * b_(j) * b_(k)) % 8; } void AffineState::CX(int j, int k) { int c = piv_col(k); - for (int col : A_row_nonzeros(j)) + for (int col : A_row_nonzeros(j)) { A_(k, col) ^= 1; + } b_(k) ^= b_(j); - if (c != -1) + if (c != -1) { ReselectPrincipalRow(-1, c); + } } void AffineState::SWAP(int j, int k) { @@ -321,10 +334,12 @@ void AffineState::SWAP(int j, int k) { auto it_j = std::find(pivots_.begin(), pivots_.end(), j); auto it_k = std::find(pivots_.begin(), pivots_.end(), k); - if (it_j != pivots_.end()) + if (it_j != pivots_.end()) { *it_j = k; - if (it_k != pivots_.end()) + } + if (it_k != pivots_.end()) { *it_k = j; + } } void AffineState::S_or_SDG(int j, bool dg) { @@ -400,14 +415,16 @@ int AffineState::MeasureZ(int j, bool postselect, int postselected_outcome) { std::vector AffineState::MeasureAll() const { vec_u_t x(r_); - for (int i = 0; i < r_; ++i) + for (int i = 0; i < r_; ++i) { x(i) = random_bit(); + } vec_u_t tmp = ReduceMod2(A_.block(0, 0, n_, r_) * x + b_); std::vector result(n_); - for (int i = 0; i < n_; ++i) + for (int i = 0; i < n_; ++i) { result[i] = tmp(i); + } return result; } @@ -422,8 +439,9 @@ std::map, int> AffineState::Sample(int nreps) const { void AffineState::Reset(int j) { int tmp = MeasureZ(j); - if (tmp == 1) + if (tmp == 1) { X(j); + } } // Eigen::VectorXcd AffineState::to_ket() const { diff --git a/unit_tests/tests/tests.cpp b/unit_tests/tests/tests.cpp index 24b710c..47a4b2d 100644 --- a/unit_tests/tests/tests.cpp +++ b/unit_tests/tests/tests.cpp @@ -32,7 +32,7 @@ std::string random_qasm(int nq, bool measure) { } // Now add n^3 random gates - int ngates = std::pow(nq,3); + int ngates = std::pow(nq, 3); for (int gatenumber = 0; gatenumber < ngates; ++gatenumber) { int randno = random_integer(0, 8); std::string gate = gates[randno]; // Select random number @@ -79,7 +79,7 @@ std::string random_identity(int nq) { std::string inverse{}; // Now add some gates - int ngates = std::pow(nq,3); + int ngates = std::pow(nq, 3); for (int gatenumber = 0; gatenumber < ngates; ++gatenumber) { int randno = random_integer(0, 8); std::string gate = gates[randno]; @@ -131,7 +131,7 @@ get_random_circuits(int nmin, int nmax) { // We will use a bunch of random circuits. We can save time by only generating // them once const std::pair, std::vector> circs = - get_random_circuits(1, 15); + get_random_circuits(1, 15); const std::vector circs_without = circs.first; const std::vector circs_with = circs.second; } // namespace