Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cpp-linter action #199

Merged
merged 50 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ccf22bc
Add cpp-linter action
hillmich Mar 1, 2023
37715df
Run tests on all files
hillmich Mar 1, 2023
a50f8bd
Fixed first round of cpp-linter warnings
hillmich Mar 1, 2023
ed9df72
Fixed few more things
hillmich Mar 1, 2023
102e4e6
Add assertion
hillmich Mar 1, 2023
62e8893
More fixes + OpenCV
hillmich Mar 1, 2023
09f8584
More fixes
hillmich Mar 1, 2023
4c4b5e5
More fixes
hillmich Mar 1, 2023
3ef8820
More fixes
hillmich Mar 1, 2023
fa3c0a6
Clang-Tidy for Bindings
hillmich Mar 2, 2023
325fcdf
Fixed warning for benchmark.cpp
hillmich Mar 2, 2023
16adb16
Clang-Tidy for test_det_noise_sim.cpp
hillmich Mar 2, 2023
b422359
Clang-Format frqi.cpp
hillmich Mar 2, 2023
f45bc33
Clang-Tidy primebases.cpp
hillmich Mar 2, 2023
c35d452
Cland-tidy vectors.cpp
hillmich Mar 2, 2023
b6b9547
Cland-tidy noise_aware.cpp
hillmich Mar 2, 2023
0da4ebb
Fix overly eager rename
hillmich Mar 2, 2023
40423a2
Clang-Tidy fix test_det_noise_sim.cpp
hillmich Mar 2, 2023
b2e799d
Clang-Tidy noisy sim headers
hillmich Mar 2, 2023
5a0869c
Add to .gitignore
hillmich Mar 2, 2023
d6f90e7
Clang-Tidy Hybrid SchrFeyn header
hillmich Mar 2, 2023
d186452
Clang-Tidy UnitarySim header
hillmich Mar 2, 2023
eeb9926
Clang-Tidy simulator header and some fixes
hillmich Mar 2, 2023
c109b84
Clang-Tidy grover header
hillmich Mar 2, 2023
b5d1796
Clang-tidy path sim header
hillmich Mar 2, 2023
e80c3d7
Clang-Tidy fast shor header
hillmich Mar 2, 2023
2860a69
Fix warning on ShorFastSim
hillmich Mar 2, 2023
3e1e576
Clang-tidy shor sim
hillmich Mar 2, 2023
2375b93
Clang-tidy shor fast sim
hillmich Mar 2, 2023
1e63620
Clang-tidy hybrid sim
hillmich Mar 2, 2023
51147b0
Clang-tidy circuit sim
hillmich Mar 2, 2023
66f2e36
Clang-tidy base sim
hillmich Mar 2, 2023
61d5bf6
Clang-tidy grover sim
hillmich Mar 2, 2023
06b11f9
Clang-tidy det noise sim
hillmich Mar 2, 2023
8b811bc
Clang-tidy path sim
hillmich Mar 2, 2023
730d24f
Possible fix for div by zero warning
hillmich Mar 2, 2023
533d6f8
Clang-tidy stoch sim
hillmich Mar 2, 2023
1b3bcc6
Added tests/coverage
hillmich Mar 2, 2023
26dd1d8
Fixes clang-format and codeql
hillmich Mar 2, 2023
a30b677
Another fix for wrong dead code detection
hillmich Mar 2, 2023
298c2c5
Another fix for wrong dead code detection
hillmich Mar 2, 2023
384c622
Added more tests for shor sim
hillmich Mar 2, 2023
e229f7b
Fixed flaky test
hillmich Mar 2, 2023
8e16daf
Add nolint and fix test
hillmich Mar 3, 2023
dad970a
Refactored ShorSim to increase coverage
hillmich Mar 3, 2023
aae48a8
cpp-linter on changes only
hillmich Mar 3, 2023
62d6c0d
Remove non-emulated shor sim (see issue #26
hillmich Mar 3, 2023
3497574
Use plain std::unique_ptr
hillmich Mar 7, 2023
08f5949
Move semantics are hard
hillmich Mar 7, 2023
b3be5b8
Also moving from moved object is to be avoided
hillmich Mar 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
FormatStyle: file

Checks: |
clang-diagnostic-*,
clang-analyzer-*,
boost-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
clang-analyzer-*,
cppcoreguidelines-*,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-special-member-functions,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-macro-usage,
google-*,
-google-readability-todo,
-google-build-using-namespace,
misc-*,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
portability-*,
readability-*,
-readability-identifier-length,
-readability-magic-numbers,
-readability-function-cognitive-complexity
CheckOptions:
- key: readability-identifier-naming.ClassCase
value: CamelCase
- key: readability-identifier-naming.ClassIgnoredRegexp
value: ".*ZX.*|.*SWAP.*|.*CEX.*|.*DD.*|.*EQ.*"
- key: readability-identifier-naming.ConstantParameterCase
value: camelBack
- key: readability-identifier-naming.EnumCase
value: CamelCase
- key: readability-identifier-naming.EnumConstantCase
value: CamelCase
- key: readability-identifier-naming.FunctionCase
value: camelBack
- key: readability-identifier-naming.FunctionIgnoredRegexp
value: ".*ZX.*|.*SWAP.*|.*CEX.*|.*DD.*|.*EQ.*"
- key: readability-identifier-naming.GlobalConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.IgnoreMainLikeFunctions
value: "true"
- key: readability-identifier-naming.LocalConstantCase
value: camelBack
- key: readability-identifier-naming.LocalVariableCase
value: camelBack
- key: readability-identifier-naming.MemberCase
value: camelBack
- key: readability-identifier-naming.MemberIgnoredRegexp
value: ".*ZX.*|.*SWAP.*|.*CEX.*|.*DD.*|.*EQ.*"
- key: readability-identifier-naming.MethodCase
value: camelBack
- key: readability-identifier-naming.ParameterCase
value: camelBack
- key: readability-identifier-naming.ParameterIgnoredRegexp
value: ".*ZX.*|.*SWAP.*|.*CEX.*|.*DD.*|.*EQ.*|.*_"
- key: readability-identifier-naming.ConstantParameterIgnoredRegexp
value: ".*ZX.*|.*SWAP.*|.*CEX.*|.*DD.*|.*EQ.*|.*_"
- key: readability-identifier-naming.NamespaceCase
value: lower_case
- key: readability-identifier-naming.StaticConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.StructCase
value: CamelCase
- key: readability-identifier-naming.VariableCase
value: camelBack
41 changes: 41 additions & 0 deletions .github/workflows/cpp-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: cpp-linter

on:
pull_request:
merge_group:
push:
branches:
- main
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
cpp-linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install OpenCV
run: sudo apt install libopencv-dev
- name: Generate compilation database
run: CC=clang-14 CXX=clang++-14 cmake -S . -B build -DBINDINGS=ON -DBUILD_DDSIM_TESTS=ON
- name: Run cpp-linter
id: linter
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
pipx run cpp-linter \
--version=14 \
--style="file" \
--tidy-checks="" \
--thread-comments=true \
--files-changed-only=false \
--ignore="build" \
--database=build
- name: Fail if linter found errors
if: steps.linter.outputs.checks-failed > 0
run: echo "Linter found errors" && exit 1
46 changes: 23 additions & 23 deletions apps/primebases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <iostream>
#include <string>

static unsigned long long gcd(unsigned long long a, unsigned long long b) {
unsigned long long c;
static std::uint64_t gcd(std::uint64_t a, std::uint64_t b) {
std::uint64_t c{};
while (a != 0) {
c = a;
a = b % a;
Expand All @@ -14,40 +14,40 @@ static unsigned long long gcd(unsigned long long a, unsigned long long b) {
return b;
}

static bool is_prime(unsigned int number) {
const unsigned int upper_limit = std::floor(std::sqrt(number));
static bool isPrime(std::uint64_t number) {
const auto upperLimit = static_cast<std::uint64_t>(std::floor(std::sqrt(number)));

for (unsigned int a = 2; a <= upper_limit; a++) {
for (std::uint64_t a = 2; a <= upperLimit; a++) {
if (number % a == 0) {
return false;
}
}
return true;
}

void output_coprimes(const unsigned int composite_number, const unsigned int length) {
unsigned output_length = 0;
unsigned current_number = 2;
void outputCoprimes(const std::uint64_t compositeNumber, const std::uint64_t length) {
std::uint64_t outputLength = 0;
std::uint64_t currentNumber = 2;

while (output_length < length) {
if (gcd(current_number, composite_number) == 1) {
std::cout << current_number << "\n";
output_length++;
while (outputLength < length) {
if (gcd(currentNumber, compositeNumber) == 1) {
std::cout << currentNumber << "\n";
outputLength++;
}
current_number++;
currentNumber++;
}
}

void output_primes(const unsigned int composite_number, const unsigned int length) {
unsigned output_length = 0;
unsigned current_number = 2;
void outputPrimes(const std::uint64_t compositeNumber, const std::uint64_t length) {
std::uint64_t outputLength = 0;
std::uint64_t currentNumber = 2;

while (output_length < length) {
if (is_prime(current_number) && gcd(current_number, composite_number) == 1) {
std::cout << current_number << "\n";
output_length++;
while (outputLength < length) {
if (isPrime(currentNumber) && gcd(currentNumber, compositeNumber) == 1) {
std::cout << currentNumber << "\n";
outputLength++;
}
current_number++;
currentNumber++;
}
}

Expand All @@ -68,9 +68,9 @@ int main(int argc, char** argv) {
}

if (vm["strategy"].as<std::string>() == "coprimes") {
output_coprimes(vm["composite_number"].as<unsigned int>(), vm["length"].as<unsigned int>());
outputCoprimes(vm["composite_number"].as<unsigned int>(), vm["length"].as<unsigned int>());
} else if (vm["strategy"].as<std::string>() == "primes") {
output_primes(vm["composite_number"].as<unsigned int>(), vm["length"].as<unsigned int>());
outputPrimes(vm["composite_number"].as<unsigned int>(), vm["length"].as<unsigned int>());
} else {
std::cerr << "Invalid strategy.\n";
}
Expand Down
96 changes: 48 additions & 48 deletions apps/simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

namespace nl = nlohmann;

int main(int argc, char** argv) {
int main(int argc, char** argv) { // NOLINT(bugprone-exception-escape)
cxxopts::Options options("MQT DDSIM", "for more information see https://www.cda.cit.tum.de/");
// clang-format off
options.add_options()
("h,help", "produce help message")
("seed", "seed for random number generator (default zero is possibly directly used as seed!)", cxxopts::value<unsigned long long>()->default_value("0"))
("seed", "seed for random number generator (default zero is possibly directly used as seed!)", cxxopts::value<std::uint64_t>()->default_value("0"))
("shots", "number of measurements (if the algorithm does not contain non-unitary gates, weak simulation is used)", cxxopts::value<unsigned int>()->default_value("0"))
("pv", "display the state vector")
("ps", "print simulation stats (applied gates, sim. time, and maximal size of the DD)")
Expand Down Expand Up @@ -53,12 +53,12 @@ int main(int argc, char** argv) {
// clang-format on

auto vm = options.parse(argc, argv);
if (vm.count("help")) {
if (vm.count("help") > 0) {
std::cout << options.help();
std::exit(0);
}

const auto seed = vm["seed"].as<unsigned long long>();
const auto seed = vm["seed"].as<std::uint64_t>();
const auto shots = vm["shots"].as<unsigned int>();
const auto nthreads = vm["nthreads"].as<unsigned int>();
const auto approxSteps = vm["steps"].as<unsigned int>();
Expand All @@ -70,17 +70,17 @@ int main(int argc, char** argv) {

std::unique_ptr<qc::QuantumComputation> quantumComputation;
std::unique_ptr<Simulator<dd::DDPackageConfig>> ddsim{nullptr};
ApproximationInfo approximationInfo(stepFidelity, approxSteps, strategy);
const ApproximationInfo approximationInfo(stepFidelity, approxSteps, strategy);
const bool verbose = vm.count("verbose") > 0;

if (vm.count("simulate_file")) {
if (vm.count("simulate_file") > 0) {
const std::string fname = vm["simulate_file"].as<std::string>();
quantumComputation = std::make_unique<qc::QuantumComputation>(fname);
ddsim = std::make_unique<CircuitSimulator<dd::DDPackageConfig>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_file_hybrid")) {
} else if (vm.count("simulate_file_hybrid") > 0) {
const std::string fname = vm["simulate_file_hybrid"].as<std::string>();
quantumComputation = std::make_unique<qc::QuantumComputation>(fname);
if (vm.count("hybrid_mode")) {
if (vm.count("hybrid_mode") > 0) {
const std::string mname = vm["hybrid_mode"].as<std::string>();
if (mname == "amplitude") {
mode = HybridSchrodingerFeynmanSimulator<dd::DDPackageConfig>::Mode::Amplitude;
Expand All @@ -93,40 +93,40 @@ int main(int argc, char** argv) {
} else {
ddsim = std::make_unique<HybridSchrodingerFeynmanSimulator<dd::DDPackageConfig>>(std::move(quantumComputation), mode, nthreads);
}
} else if (vm.count("simulate_qft")) {
const unsigned int n_qubits = vm["simulate_qft"].as<unsigned int>();
quantumComputation = std::make_unique<qc::QFT>(n_qubits);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_fast_shor")) {
const unsigned int composite_number = vm["simulate_fast_shor"].as<unsigned int>();
const unsigned int coprime = vm["simulate_fast_shor_coprime"].as<unsigned int>();
} else if (vm.count("simulate_qft") > 0) {
const unsigned int nQubits = vm["simulate_qft"].as<unsigned int>();
quantumComputation = std::make_unique<qc::QFT>(nQubits);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_fast_shor") > 0) {
const unsigned int compositeNumber = vm["simulate_fast_shor"].as<unsigned int>();
const unsigned int coprime = vm["simulate_fast_shor_coprime"].as<unsigned int>();
if (seed == 0) {
ddsim = std::make_unique<ShorFastSimulator<dd::DDPackageConfig>>(composite_number, coprime, verbose);
ddsim = std::make_unique<ShorFastSimulator<dd::DDPackageConfig>>(compositeNumber, coprime, verbose);
} else {
ddsim = std::make_unique<ShorFastSimulator<dd::DDPackageConfig>>(composite_number, coprime, seed, verbose);
ddsim = std::make_unique<ShorFastSimulator<dd::DDPackageConfig>>(compositeNumber, coprime, seed, verbose);
}
} else if (vm.count("simulate_shor")) {
const unsigned int composite_number = vm["simulate_shor"].as<unsigned int>();
const unsigned int coprime = vm["simulate_shor_coprime"].as<unsigned int>();
const bool emulate = vm.count("simulate_shor_no_emulation") == 0;
} else if (vm.count("simulate_shor") > 0) {
const unsigned int compositeNumber = vm["simulate_shor"].as<unsigned int>();
const unsigned int coprime = vm["simulate_shor_coprime"].as<unsigned int>();
const bool emulate = vm.count("simulate_shor_no_emulation") == 0;
if (seed == 0) {
ddsim = std::make_unique<ShorSimulator<dd::DDPackageConfig>>(composite_number, coprime, emulate, verbose, stepFidelity < 1);
ddsim = std::make_unique<ShorSimulator<dd::DDPackageConfig>>(compositeNumber, coprime, emulate, verbose, stepFidelity < 1);
} else {
ddsim = std::make_unique<ShorSimulator<dd::DDPackageConfig>>(composite_number, coprime, seed, emulate, verbose,
ddsim = std::make_unique<ShorSimulator<dd::DDPackageConfig>>(compositeNumber, coprime, seed, emulate, verbose,
stepFidelity < 1);
}
} else if (vm.count("simulate_grover")) {
const unsigned int n_qubits = vm["simulate_grover"].as<unsigned int>();
quantumComputation = std::make_unique<qc::Grover>(n_qubits, seed);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_grover_emulated")) {
} else if (vm.count("simulate_grover") > 0) {
const unsigned int nQubits = vm["simulate_grover"].as<unsigned int>();
quantumComputation = std::make_unique<qc::Grover>(nQubits, seed);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_grover_emulated") > 0) {
ddsim = std::make_unique<GroverSimulator<dd::DDPackageConfig>>(vm["simulate_grover_emulated"].as<unsigned int>(), seed);
} else if (vm.count("simulate_grover_oracle_emulated")) {
} else if (vm.count("simulate_grover_oracle_emulated") > 0) {
ddsim = std::make_unique<GroverSimulator<dd::DDPackageConfig>>(vm["simulate_grover_oracle_emulated"].as<std::string>(), seed);
} else if (vm.count("simulate_ghz")) {
const unsigned int n_qubits = vm["simulate_ghz"].as<unsigned int>();
quantumComputation = std::make_unique<qc::Entanglement>(n_qubits);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else if (vm.count("simulate_ghz") > 0) {
const unsigned int nQubits = vm["simulate_ghz"].as<unsigned int>();
quantumComputation = std::make_unique<qc::Entanglement>(nQubits);
ddsim = std::make_unique<CircuitSimulator<>>(std::move(quantumComputation), approximationInfo, seed);
} else {
std::cerr << "Did not find anything to simulate. See help below.\n"
<< options.help() << "\n";
Expand All @@ -141,9 +141,9 @@ int main(int argc, char** argv) {
auto m = ddsim->Simulate(shots);
auto t2 = std::chrono::high_resolution_clock::now();

std::chrono::duration<float> duration_simulation = t2 - t1;
const std::chrono::duration<float> durationSimulation = t2 - t1;

if (vm.count("approx_state")) {
if (vm.count("approx_state") > 0) {
// TargetFidelity
ddsim->ApproximateByFidelity(1 / 100.0, false, false, true);
ddsim->ApproximateByFidelity(2 / 100.0, false, false, true);
Expand Down Expand Up @@ -206,19 +206,19 @@ int main(int argc, char** argv) {
}
}

nl::json output_obj;
nl::json outputObj;

if (vm.count("pm")) {
output_obj["measurement_results"] = m;
if (vm.count("pm") > 0) {
outputObj["measurement_results"] = m;
}

if (vm.count("pv")) {
output_obj["state_vector"] = ddsim->getVectorPair();
if (vm.count("pv") > 0) {
outputObj["state_vector"] = ddsim->getVectorPair();
}

if (vm.count("ps")) {
output_obj["statistics"] = {
{"simulation_time", duration_simulation.count()},
if (vm.count("ps") > 0) {
outputObj["statistics"] = {
{"simulation_time", durationSimulation.count()},
{"benchmark", ddsim->getName()},
{"n_qubits", +ddsim->getNumberOfQubits()},
{"applied_gates", ddsim->getNumberOfOps()},
Expand All @@ -229,19 +229,19 @@ int main(int argc, char** argv) {
};

for (const auto& [stat, value]: ddsim->AdditionalStatistics()) {
output_obj["statistics"][stat] = value;
outputObj["statistics"][stat] = value;
}
}

if (vm.count("pcomplex")) {
output_obj["complex_stats"] = ddsim->dd->cn.complexTable.getStatistics();
if (vm.count("pcomplex") > 0) {
outputObj["complex_stats"] = ddsim->dd->cn.complexTable.getStatistics();
}

if (vm.count("dump_complex")) {
if (vm.count("dump_complex") > 0) {
auto filename = vm["dump_complex"].as<std::string>();
auto ostream = std::fstream(filename, std::fstream::out);
dd::exportEdgeWeights(ddsim->rootEdge, ostream);
}

std::cout << std::setw(2) << output_obj << std::endl;
std::cout << std::setw(2) << outputObj << std::endl;
}
2 changes: 1 addition & 1 deletion apps/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char** argv) {

std::cout << "Lines: " << lines << " (Should be a power of two)\n";
std::cout << "Fidelity: " << fidelity << "\n";
if (std::abs(1.0l - fidelity) < 0.001) {
if (std::abs(1.0l - fidelity) < 0.001L) {
return 0;
} else {
return 1;
Expand Down
Loading