Skip to content

Commit

Permalink
Fix performance hints generated by clang-tidy (#2131)
Browse files Browse the repository at this point in the history
* Fix performance hints generated with clang-tidy

* Run clang format 15 instead of 18

---------

Co-authored-by: Jun Doi <[email protected]>
  • Loading branch information
enum-class and doichanj authored Jul 9, 2024
1 parent 9fe6be7 commit 322e786
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/controllers/state_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ class AerState {
// Operation management
//-----------------------------------------------------------------------
// Buffer Operations::Op
virtual void buffer_op(const Operations::Op &&op);
virtual void buffer_op(Operations::Op &&op);

// Flush buffered Operations::Op
virtual void flush_ops();
Expand All @@ -385,7 +385,7 @@ class AerState {

private:
void initialize_state_controller();
void initialize_qreg_state(std::shared_ptr<QuantumState::Base> state);
void initialize_qreg_state(const std::shared_ptr<QuantumState::Base> &state);
void assert_initialized() const;
void assert_not_initialized() const;
bool is_gpu(bool raise_error) const;
Expand Down Expand Up @@ -658,7 +658,7 @@ void AerState::initialize_state_controller() {
};

void AerState::initialize_qreg_state(
std::shared_ptr<QuantumState::Base> state) {
const std::shared_ptr<QuantumState::Base> &state) {
if (!state) {
if (method_ == Method::statevector) {
if (device_ == Device::CPU)
Expand Down Expand Up @@ -1488,7 +1488,7 @@ std::unordered_map<uint_t, uint_t> AerState::sample_counts(const reg_t &qubits,
//-----------------------------------------------------------------------
// Operation management
//-----------------------------------------------------------------------
void AerState::buffer_op(const Operations::Op &&op) {
void AerState::buffer_op(Operations::Op &&op) {
assert_initialized();
buffer_.ops.push_back(std::move(op));
};
Expand Down
37 changes: 19 additions & 18 deletions src/framework/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ class Circuit {
const std::vector<complex_t> &params,
const std::vector<std::string> &string_params,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
const std::string label = "") {
const std::shared_ptr<Operations::CExpr> &expr = nullptr,
const std::string &label = "") {
ops.push_back(Operations::make_gate(name, qubits, params, string_params,
cond_regidx, expr, label));
check_gate_params(ops.back());
}

void diagonal(const reg_t &qubits, const cvector_t &vec,
const int_t cond_regidx = -1, const std::string label = "") {
const int_t cond_regidx = -1, const std::string &label = "") {
ops.push_back(Operations::make_diagonal(qubits, vec, cond_regidx, label));
}

void unitary(const reg_t &qubits, const cmatrix_t &mat,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
const std::string label = "") {
const std::shared_ptr<Operations::CExpr> &expr = nullptr,
const std::string &label = "") {
ops.push_back(
Operations::make_unitary(qubits, mat, cond_regidx, expr, label));
}
Expand All @@ -168,8 +168,8 @@ class Circuit {

void multiplexer(const reg_t &qubits, const std::vector<cmatrix_t> &mats,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr,
std::string label = "") {
const std::shared_ptr<Operations::CExpr> &expr = nullptr,
const std::string &label = "") {
ops.push_back(
Operations::make_multiplexer(qubits, mats, cond_regidx, expr, label));
}
Expand All @@ -182,7 +182,7 @@ class Circuit {

void superop(const reg_t &qubits, const cmatrix_t &mat,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> &expr = nullptr) {
ops.push_back(Operations::make_superop(qubits, mat, cond_regidx, expr));
}

Expand All @@ -202,19 +202,20 @@ class Circuit {
}

void save_expval(const reg_t &qubits, const std::string &name,
const std::vector<std::string> pauli_strings,
const std::vector<double> coeff_reals,
const std::vector<double> coeff_imags,
const std::vector<std::string> &pauli_strings,
const std::vector<double> &coeff_reals,
const std::vector<double> &coeff_imags,
const std::string &snapshot_type,
const std::string label = "") {
const std::string &label = "") {
ops.push_back(Operations::make_save_expval(qubits, name, pauli_strings,
coeff_reals, coeff_imags,
snapshot_type, label));
}

void set_qerror_loc(const reg_t &qubits, const std::string &label,
const int_t conditional = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
void
set_qerror_loc(const reg_t &qubits, const std::string &label,
const int_t conditional = -1,
const std::shared_ptr<Operations::CExpr> &expr = nullptr) {
ops.push_back(
Operations::make_qerror_loc(qubits, label, conditional, expr));
}
Expand Down Expand Up @@ -254,7 +255,7 @@ class Circuit {

void jump(const reg_t &qubits, const std::vector<std::string> &params,
const int_t cond_regidx = -1,
const std::shared_ptr<Operations::CExpr> expr = nullptr) {
const std::shared_ptr<Operations::CExpr> &expr = nullptr) {
ops.push_back(Operations::make_jump(qubits, params, cond_regidx, expr));
}

Expand Down Expand Up @@ -355,7 +356,7 @@ Circuit::Circuit(const inputdata_t &circ, const json_t &qobj_config,
// without conversion we could call `get_reversed_ops` on the inputdata
// without first converting.
std::vector<Op> converted_ops;
for (auto the_op : input_ops) {
for (const auto &the_op : input_ops) {
converted_ops.emplace_back(Operations::input_to_op(the_op));
}
ops = std::move(converted_ops);
Expand Down Expand Up @@ -610,7 +611,7 @@ void Circuit::set_params(bool truncation) {
op_idx++;
}

for (auto dest : dests) {
for (const auto &dest : dests) {
if (marks.find(dest) == marks.end()) {
std::stringstream msg;
msg << "Invalid jump destination:\"" << dest << "\"." << std::endl;
Expand Down
38 changes: 19 additions & 19 deletions src/framework/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class BoolValue : public ValueExpr {

class UnaryExpr : public CExpr {
public:
UnaryExpr(const UnaryOp op_, const std::shared_ptr<CExpr> operand_)
UnaryExpr(const UnaryOp op_, const std::shared_ptr<CExpr> &operand_)
: CExpr(CExprType::Unary, operand_->type), op(op_), operand(operand_) {}

virtual bool eval_bool(const std::string &memory) {
Expand Down Expand Up @@ -253,8 +253,8 @@ class UnaryExpr : public CExpr {

class BinaryExpr : public CExpr {
public:
BinaryExpr(const BinaryOp op_, const std::shared_ptr<CExpr> left_,
const std::shared_ptr<CExpr> right_)
BinaryExpr(const BinaryOp op_, const std::shared_ptr<CExpr> &left_,
const std::shared_ptr<CExpr> &right_)
: CExpr(CExprType::Binary,
isBoolBinaryOp(op_) ? std::make_shared<Bool>()
: get_wider_type(left_->type, right_->type)),
Expand Down Expand Up @@ -661,7 +661,7 @@ inline std::ostream &operator<<(std::ostream &s, const Op &op) {
}
s << "],[";
first = true;
for (reg_t reg : op.regs) {
for (const reg_t &reg : op.regs) {
if (!first)
s << ",";
s << "[";
Expand Down Expand Up @@ -779,8 +779,8 @@ inline Op make_initialize(const reg_t &qubits,

inline Op make_unitary(const reg_t &qubits, const cmatrix_t &mat,
const int_t conditional = -1,
const std::shared_ptr<CExpr> expr = nullptr,
std::string label = "") {
const std::shared_ptr<CExpr> &expr = nullptr,
const std::string &label = "") {
Op op;
op.type = OpType::matrix;
op.name = "unitary";
Expand Down Expand Up @@ -811,7 +811,7 @@ inline Op make_unitary(const reg_t &qubits, cmatrix_t &&mat,

inline Op make_diagonal(const reg_t &qubits, const cvector_t &vec,
const int_t conditional = -1,
const std::string label = "") {
const std::string &label = "") {
Op op;
op.type = OpType::diagonal_matrix;
op.name = "diagonal";
Expand All @@ -831,7 +831,7 @@ inline Op make_diagonal(const reg_t &qubits, const cvector_t &vec,

inline Op make_diagonal(const reg_t &qubits, cvector_t &&vec,
const int_t conditional = -1,
const std::string label = "") {
const std::string &label = "") {
Op op;
op.type = OpType::diagonal_matrix;
op.name = "diagonal";
Expand All @@ -851,7 +851,7 @@ inline Op make_diagonal(const reg_t &qubits, cvector_t &&vec,

inline Op make_superop(const reg_t &qubits, const cmatrix_t &mat,
const int_t conditional = -1,
const std::shared_ptr<CExpr> expr = nullptr) {
const std::shared_ptr<CExpr> &expr = nullptr) {
Op op;
op.type = OpType::superop;
op.name = "superop";
Expand All @@ -877,7 +877,7 @@ inline Op make_superop(const reg_t &qubits, cmatrix_t &&mat) {

inline Op make_kraus(const reg_t &qubits, const std::vector<cmatrix_t> &mats,
const int_t conditional = -1,
const std::shared_ptr<CExpr> expr = nullptr) {
const std::shared_ptr<CExpr> &expr = nullptr) {
Op op;
op.type = OpType::kraus;
op.name = "kraus";
Expand Down Expand Up @@ -961,12 +961,12 @@ inline Op make_bfunc(const std::string &mask, const std::string &val,
Op make_gate(const std::string &name, const reg_t &qubits,
const std::vector<complex_t> &params,
const std::vector<std::string> &string_params,
const int_t conditional, const std::shared_ptr<CExpr> expr,
const int_t conditional, const std::shared_ptr<CExpr> &expr,
const std::string &label);
Op make_gate(const std::string &name, const reg_t &qubits,
const std::vector<complex_t> &params,
const std::vector<std::string> &string_params,
const int_t conditional, const std::shared_ptr<CExpr> expr,
const int_t conditional, const std::shared_ptr<CExpr> &expr,
const std::string &label) {
Op op;
op.type = OpType::gate;
Expand Down Expand Up @@ -1051,8 +1051,8 @@ inline Op make_store(const reg_t &qubits, const reg_t &clbits,
inline Op make_multiplexer(const reg_t &qubits,
const std::vector<cmatrix_t> &mats,
const int_t conditional = -1,
const std::shared_ptr<CExpr> expr = nullptr,
std::string label = "") {
const std::shared_ptr<CExpr> &expr = nullptr,
const std::string &label = "") {

// Check matrices are N-qubit
auto dim = mats[0].GetRows();
Expand Down Expand Up @@ -1172,9 +1172,9 @@ inline Op make_save_amplitudes(const reg_t &qubits, const std::string &name,
}

inline Op make_save_expval(const reg_t &qubits, const std::string &name,
const std::vector<std::string> pauli_strings,
const std::vector<double> coeff_reals,
const std::vector<double> coeff_imags,
const std::vector<std::string> &pauli_strings,
const std::vector<double> &coeff_reals,
const std::vector<double> &coeff_imags,
const std::string &snapshot_type,
const std::string &label) {

Expand Down Expand Up @@ -1264,7 +1264,7 @@ inline Op make_set_clifford(const reg_t &qubits, const std::string &name,

inline Op make_jump(const reg_t &qubits, const std::vector<std::string> &params,
const int_t conditional,
const std::shared_ptr<CExpr> expr = nullptr) {
const std::shared_ptr<CExpr> &expr = nullptr) {
Op op;
op.type = OpType::jump;
op.name = "jump";
Expand Down Expand Up @@ -1318,7 +1318,7 @@ inline Op make_measure(const reg_t &qubits, const reg_t &memory,

inline Op make_qerror_loc(const reg_t &qubits, const std::string &label,
const int_t conditional = -1,
const std::shared_ptr<CExpr> expr = nullptr) {
const std::shared_ptr<CExpr> &expr = nullptr) {
Op op;
op.type = OpType::qerror_loc;
op.name = label;
Expand Down
1 change: 0 additions & 1 deletion src/framework/opset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ inline std::ostream &operator<<(std::ostream &out,
if (!first)
out << ", ";
out << "\"gates\": " << opset.gates;
first = false;
}
out << "}";
return out;
Expand Down
3 changes: 1 addition & 2 deletions src/framework/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ void split(const matrix<T> &A, matrix<T> &B, matrix<T> &C, uint_t axis) {
throw std::invalid_argument("Utils::split: axis must be 0 or 1");
}
size_t rows = A.GetRows(), cols = A.GetColumns();
matrix<T> temp = A;
if (axis == 0) {
if (rows % 2 != 0) {
throw std::invalid_argument("Utils::split: can't split matrix A by rows");
Expand Down Expand Up @@ -1033,7 +1032,7 @@ std::string &format_hex_inplace(std::string &hex) {
if (prefix != "0x")
hex = "0x" + hex;
// delete leading zeros Eg 0x001 -> 0x1
hex.erase(2, std::min(hex.find_first_not_of("0", 2) - 2, hex.size() - 3));
hex.erase(2, std::min(hex.find_first_not_of('0', 2) - 2, hex.size() - 3));
return hex;
}

Expand Down
6 changes: 3 additions & 3 deletions src/noise/noise_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class NoiseModel {

// Return vector of noise qubits for non local error on specified label and
// qubits If no nonlocal error exists an empty set is returned.
std::set<uint_t> nonlocal_noise_qubits(const std::string label,
std::set<uint_t> nonlocal_noise_qubits(const std::string &label,
const reg_t &qubits) const;

// Return the opset for the noise model
Expand Down Expand Up @@ -951,7 +951,7 @@ std::string NoiseModel::reg2string(const reg_t &reg) const {
reg_t NoiseModel::string2reg(std::string s) const {
reg_t result;
size_t pos = 0;
while ((pos = s.find(",")) != std::string::npos) {
while ((pos = s.find(',')) != std::string::npos) {
result.push_back(std::stoi(s.substr(0, pos)));
s.erase(0, pos + 1);
}
Expand All @@ -962,7 +962,7 @@ reg_t NoiseModel::string2reg(std::string s) const {
// Qubit Remapping
//=========================================================================

std::set<uint_t> NoiseModel::nonlocal_noise_qubits(const std::string label,
std::set<uint_t> NoiseModel::nonlocal_noise_qubits(const std::string &label,
const reg_t &qubits) const {
std::set<uint_t> all_noise_qubits;
// Check if label has noise
Expand Down
4 changes: 2 additions & 2 deletions src/simulators/density_matrix/densitymatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DensityMatrix : public UnitaryMatrix<data_t> {
explicit DensityMatrix(size_t num_qubits);
DensityMatrix(const DensityMatrix &obj){};
DensityMatrix &operator=(const DensityMatrix &obj) = delete;
DensityMatrix &operator=(DensityMatrix &&obj);
DensityMatrix &operator=(DensityMatrix &&obj) noexcept;

//-----------------------------------------------------------------------
// Utility functions
Expand Down Expand Up @@ -186,7 +186,7 @@ DensityMatrix<data_t>::DensityMatrix(size_t num_qubits)

template <typename data_t>
DensityMatrix<data_t> &
DensityMatrix<data_t>::operator=(DensityMatrix<data_t> &&obj) {
DensityMatrix<data_t>::operator=(DensityMatrix<data_t> &&obj) noexcept {
apply_unitary_threshold_ = obj.apply_unitary_threshold_;
BaseMatrix::operator=(std::move(obj));
return *this;
Expand Down
Loading

0 comments on commit 322e786

Please sign in to comment.