Skip to content

Commit f0367eb

Browse files
authored
Adding the clang format to the CI (#37)
* Applied the clang format to the files we have in `src/` * No code change
1 parent c7a0cf5 commit f0367eb

19 files changed

+405
-530
lines changed

.clang-format

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
BasedOnStyle: Google
22
IndentWidth: 2
3-
ColumnLimit: 80
3+
ColumnLimit: 100
44
AllowShortFunctionsOnASingleLine: Empty
55
PointerAlignment: Left
6-
Standard: C++20
76
SortIncludes: true

.github/workflows/ci.yml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
name: ci
2+
23
on:
34
push:
45
branches:
@@ -15,8 +16,26 @@ concurrency:
1516
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
1617
cancel-in-progress: true
1718

18-
1919
jobs:
20+
clang-format-check:
21+
name: Clang code formatting
22+
runs-on: ubuntu-latest
23+
24+
steps:
25+
- name: Checkout repository
26+
uses: actions/checkout@v4
27+
28+
- name: Install clang-format
29+
run: |
30+
sudo apt-get update
31+
sudo apt-get install -y clang-format
32+
33+
- name: Run clang-format check
34+
run: |
35+
clang-format --version
36+
files=$(git ls-files '*.cc' '*.cpp' '*.h' '*.hpp')
37+
[ -z "$files" ] || clang-format --dry-run --Werror $files
38+
2039
build:
2140
strategy:
2241
matrix:
@@ -35,5 +54,22 @@ jobs:
3554
disk-cache: ${{ github.workflow }}
3655
repository-cache: true
3756

57+
- name: Install clang-format (Linux)
58+
if: runner.os == 'Linux'
59+
run: |
60+
sudo apt-get update
61+
sudo apt-get install -y clang-format
62+
63+
- name: Install clang-format (macOS)
64+
if: runner.os == 'macOS'
65+
run: |
66+
brew install clang-format
67+
68+
- name: Check formatting (redundant but allowed)
69+
run: |
70+
clang-format --version
71+
files=$(git ls-files '*.cc' '*.cpp' '*.h' '*.hpp')
72+
[ -z "$files" ] || clang-format --dry-run --Werror $files
73+
3874
- name: Bazel tests
3975
run: bazel test src:all

src/common.cc

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ common::Error::Error(const stim::DemInstruction& error) {
5252
}
5353

5454
std::string common::Error::str() {
55-
return "Error{cost=" + std::to_string(likelihood_cost) +
56-
", symptom=" + symptom.str() + "}";
55+
return "Error{cost=" + std::to_string(likelihood_cost) + ", symptom=" + symptom.str() + "}";
5756
}
5857

59-
std::vector<stim::DemTarget> common::Symptom::as_dem_instruction_targets()
60-
const {
58+
std::vector<stim::DemTarget> common::Symptom::as_dem_instruction_targets() const {
6159
std::vector<stim::DemTarget> targets;
6260
for (int d : detectors) {
6361
targets.push_back(stim::DemTarget::relative_detector_id(d));
@@ -72,8 +70,7 @@ std::vector<stim::DemTarget> common::Symptom::as_dem_instruction_targets()
7270
return targets;
7371
}
7472

75-
stim::DetectorErrorModel common::merge_identical_errors(
76-
const stim::DetectorErrorModel& dem) {
73+
stim::DetectorErrorModel common::merge_identical_errors(const stim::DetectorErrorModel& dem) {
7774
stim::DetectorErrorModel out_dem;
7875

7976
// Map to track the distinct symptoms
@@ -89,11 +86,9 @@ stim::DetectorErrorModel common::merge_identical_errors(
8986
// Merge with existing error with the same symptom (if applicable)
9087
if (errors_by_symptom.find(error.symptom) != errors_by_symptom.end()) {
9188
double p0 = errors_by_symptom[error.symptom].probability;
92-
error.probability =
93-
p0 * (1 - error.probability) + (1 - p0) * error.probability;
89+
error.probability = p0 * (1 - error.probability) + (1 - p0) * error.probability;
9490
}
95-
error.likelihood_cost =
96-
-1 * std::log(error.probability / (1 - error.probability));
91+
error.likelihood_cost = -1 * std::log(error.probability / (1 - error.probability));
9792
errors_by_symptom[error.symptom] = error;
9893
break;
9994
}
@@ -106,9 +101,9 @@ stim::DetectorErrorModel common::merge_identical_errors(
106101
}
107102
}
108103
for (const auto& it : errors_by_symptom) {
109-
out_dem.append_error_instruction(
110-
it.second.probability, it.second.symptom.as_dem_instruction_targets(),
111-
/*tag=*/"");
104+
out_dem.append_error_instruction(it.second.probability,
105+
it.second.symptom.as_dem_instruction_targets(),
106+
/*tag=*/"");
112107
}
113108
return out_dem;
114109
}
@@ -136,19 +131,17 @@ stim::DetectorErrorModel common::remove_zero_probability_errors(
136131
return out_dem;
137132
}
138133

139-
stim::DetectorErrorModel common::dem_from_counts(
140-
stim::DetectorErrorModel& orig_dem, const std::vector<size_t>& error_counts,
141-
size_t num_shots) {
134+
stim::DetectorErrorModel common::dem_from_counts(stim::DetectorErrorModel& orig_dem,
135+
const std::vector<size_t>& error_counts,
136+
size_t num_shots) {
142137
if (orig_dem.count_errors() != error_counts.size()) {
143138
throw std::invalid_argument(
144139
"Error hits array must be the same size as the number of errors in the "
145140
"original DEM.");
146141
}
147142

148-
for (const stim::DemInstruction& instruction :
149-
orig_dem.flattened().instructions) {
150-
if (instruction.type == stim::DemInstructionType::DEM_ERROR &&
151-
instruction.arg_data[0] == 0) {
143+
for (const stim::DemInstruction& instruction : orig_dem.flattened().instructions) {
144+
if (instruction.type == stim::DemInstructionType::DEM_ERROR && instruction.arg_data[0] == 0) {
152145
throw std::invalid_argument(
153146
"dem_from_counts requires DEMs without zero-probability errors. Use"
154147
" remove_zero_probability_errors first.");
@@ -157,17 +150,14 @@ stim::DetectorErrorModel common::dem_from_counts(
157150

158151
stim::DetectorErrorModel out_dem;
159152
size_t ei = 0;
160-
for (const stim::DemInstruction& instruction :
161-
orig_dem.flattened().instructions) {
153+
for (const stim::DemInstruction& instruction : orig_dem.flattened().instructions) {
162154
switch (instruction.type) {
163155
case stim::DemInstructionType::DEM_SHIFT_DETECTORS:
164156
assert(false && "unreachable");
165157
break;
166158
case stim::DemInstructionType::DEM_ERROR: {
167-
double est_probability =
168-
double(error_counts.at(ei)) / double(num_shots);
169-
out_dem.append_error_instruction(est_probability,
170-
instruction.target_data, /*tag=*/"");
159+
double est_probability = double(error_counts.at(ei)) / double(num_shots);
160+
out_dem.append_error_instruction(est_probability, instruction.target_data, /*tag=*/"");
171161
++ei;
172162
break;
173163
}

src/common.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@ struct Error {
5151
Symptom symptom;
5252
std::vector<bool> dets_array;
5353
Error() = default;
54-
Error(double likelihood_cost, std::vector<int>& detectors,
55-
ObservablesMask observables, std::vector<bool>& dets_array)
56-
: likelihood_cost(likelihood_cost),
57-
symptom{detectors, observables},
58-
dets_array(dets_array) {}
54+
Error(double likelihood_cost, std::vector<int>& detectors, ObservablesMask observables,
55+
std::vector<bool>& dets_array)
56+
: likelihood_cost(likelihood_cost), symptom{detectors, observables}, dets_array(dets_array) {}
5957
Error(double likelihood_cost, double probability, std::vector<int>& detectors,
6058
ObservablesMask observables, std::vector<bool>& dets_array)
6159
: likelihood_cost(likelihood_cost),
@@ -68,21 +66,18 @@ struct Error {
6866

6967
// Makes a new (flattened) dem where identical error mechanisms have been
7068
// merged.
71-
stim::DetectorErrorModel merge_identical_errors(
72-
const stim::DetectorErrorModel& dem);
69+
stim::DetectorErrorModel merge_identical_errors(const stim::DetectorErrorModel& dem);
7370

7471
// Returns a copy of the given error model with any zero-probability DEM_ERROR
7572
// instructions removed.
76-
stim::DetectorErrorModel remove_zero_probability_errors(
77-
const stim::DetectorErrorModel& dem);
73+
stim::DetectorErrorModel remove_zero_probability_errors(const stim::DetectorErrorModel& dem);
7874

7975
// Makes a new dem where the probabilities of errors are estimated from the
8076
// fraction of shots they were used in.
8177
// Throws std::invalid_argument if `orig_dem` contains zero-probability errors;
8278
// call remove_zero_probability_errors first.
83-
stim::DetectorErrorModel dem_from_counts(
84-
stim::DetectorErrorModel& orig_dem, const std::vector<size_t>& error_counts,
85-
size_t num_shots);
79+
stim::DetectorErrorModel dem_from_counts(stim::DetectorErrorModel& orig_dem,
80+
const std::vector<size_t>& error_counts, size_t num_shots);
8681

8782
} // namespace common
8883

src/common.pybind.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ void add_common_module(py::module &root) {
3232

3333
py::class_<common::Symptom>(m, "Symptom")
3434
.def(py::init<std::vector<int>, common::ObservablesMask>(),
35-
py::arg("detectors") = std::vector<int>(),
36-
py::arg("observables") = 0)
35+
py::arg("detectors") = std::vector<int>(), py::arg("observables") = 0)
3736
.def_readwrite("detectors", &common::Symptom::detectors)
3837
.def_readwrite("observables", &common::Symptom::observables)
3938
.def("__str__", &common::Symptom::str)
@@ -51,25 +50,22 @@ void add_common_module(py::module &root) {
5150
.def_readwrite("symptom", &common::Error::symptom)
5251
.def("__str__", &common::Error::str)
5352
.def(py::init<>())
54-
.def(py::init<double, std::vector<int> &, common::ObservablesMask,
55-
std::vector<bool> &>(),
56-
py::arg("likelihood_cost"), py::arg("detectors"),
57-
py::arg("observables"), py::arg("dets_array"))
53+
.def(py::init<double, std::vector<int> &, common::ObservablesMask, std::vector<bool> &>(),
54+
py::arg("likelihood_cost"), py::arg("detectors"), py::arg("observables"),
55+
py::arg("dets_array"))
5856
.def(py::init<double, double, std::vector<int> &, common::ObservablesMask,
5957
std::vector<bool> &>(),
60-
py::arg("likelihood_cost"), py::arg("probability"),
61-
py::arg("detectors"), py::arg("observables"), py::arg("dets_array"))
58+
py::arg("likelihood_cost"), py::arg("probability"), py::arg("detectors"),
59+
py::arg("observables"), py::arg("dets_array"))
6260
.def(py::init([](stim_pybind::ExposedDemInstruction edi) {
6361
return new common::Error(edi.as_dem_instruction());
6462
}),
6563
py::arg("error"));
6664

67-
m.def("merge_identical_errors", &common::merge_identical_errors,
68-
py::arg("dem"));
69-
m.def("remove_zero_probability_errors",
70-
&common::remove_zero_probability_errors, py::arg("dem"));
71-
m.def("dem_from_counts", &common::dem_from_counts, py::arg("orig_dem"),
72-
py::arg("error_counts"), py::arg("num_shots"));
65+
m.def("merge_identical_errors", &common::merge_identical_errors, py::arg("dem"));
66+
m.def("remove_zero_probability_errors", &common::remove_zero_probability_errors, py::arg("dem"));
67+
m.def("dem_from_counts", &common::dem_from_counts, py::arg("orig_dem"), py::arg("error_counts"),
68+
py::arg("num_shots"));
7369
}
7470

7571
#endif

src/common.test.cc

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ TEST(common, DemFromCountsRejectsZeroProbabilityErrors) {
3838

3939
std::vector<size_t> counts{1, 7, 4};
4040
size_t num_shots = 10;
41-
EXPECT_THROW({
42-
common::dem_from_counts(dem, counts, num_shots);
43-
}, std::invalid_argument);
41+
EXPECT_THROW({ common::dem_from_counts(dem, counts, num_shots); }, std::invalid_argument);
4442

4543
stim::DetectorErrorModel cleaned = common::remove_zero_probability_errors(dem);
4644
stim::DetectorErrorModel out_dem =
@@ -50,11 +48,9 @@ TEST(common, DemFromCountsRejectsZeroProbabilityErrors) {
5048
ASSERT_EQ(out_dem.count_errors(), 2);
5149
ASSERT_GE(flat.instructions.size(), 2);
5250

53-
EXPECT_EQ(flat.instructions[0].type,
54-
stim::DemInstructionType::DEM_ERROR);
51+
EXPECT_EQ(flat.instructions[0].type, stim::DemInstructionType::DEM_ERROR);
5552
EXPECT_NEAR(flat.instructions[0].arg_data[0], 0.1, 1e-9);
56-
ASSERT_EQ(flat.instructions[1].type,
57-
stim::DemInstructionType::DEM_ERROR);
53+
ASSERT_EQ(flat.instructions[1].type, stim::DemInstructionType::DEM_ERROR);
5854
EXPECT_NEAR(flat.instructions[1].arg_data[0], 0.4, 1e-9);
5955
}
6056

@@ -68,18 +64,15 @@ TEST(common, DemFromCountsSimpleTwoErrors) {
6864

6965
std::vector<size_t> counts{5, 7};
7066
size_t num_shots = 20;
71-
stim::DetectorErrorModel out_dem =
72-
common::dem_from_counts(dem, counts, num_shots);
67+
stim::DetectorErrorModel out_dem = common::dem_from_counts(dem, counts, num_shots);
7368

7469
auto flat = out_dem.flattened();
7570
ASSERT_EQ(out_dem.count_errors(), 2);
7671

7772
ASSERT_GE(flat.instructions.size(), 2);
78-
EXPECT_EQ(flat.instructions[0].type,
79-
stim::DemInstructionType::DEM_ERROR);
73+
EXPECT_EQ(flat.instructions[0].type, stim::DemInstructionType::DEM_ERROR);
8074
EXPECT_NEAR(flat.instructions[0].arg_data[0], 0.25, 1e-9);
81-
EXPECT_EQ(flat.instructions[1].type,
82-
stim::DemInstructionType::DEM_ERROR);
75+
EXPECT_EQ(flat.instructions[1].type, stim::DemInstructionType::DEM_ERROR);
8376
EXPECT_NEAR(flat.instructions[1].arg_data[0], 0.35, 1e-9);
8477
}
8578

@@ -93,15 +86,12 @@ TEST(common, RemoveZeroProbabilityErrors) {
9386
detector(0, 0, 0) D2
9487
)DEM");
9588

96-
stim::DetectorErrorModel cleaned =
97-
common::remove_zero_probability_errors(dem);
89+
stim::DetectorErrorModel cleaned = common::remove_zero_probability_errors(dem);
9890

9991
EXPECT_EQ(cleaned.count_errors(), 2);
10092
auto flat = cleaned.flattened();
101-
ASSERT_EQ(flat.instructions[0].type,
102-
stim::DemInstructionType::DEM_ERROR);
93+
ASSERT_EQ(flat.instructions[0].type, stim::DemInstructionType::DEM_ERROR);
10394
EXPECT_NEAR(flat.instructions[0].arg_data[0], 0.1, 1e-9);
104-
ASSERT_EQ(flat.instructions[1].type,
105-
stim::DemInstructionType::DEM_ERROR);
95+
ASSERT_EQ(flat.instructions[1].type, stim::DemInstructionType::DEM_ERROR);
10696
EXPECT_NEAR(flat.instructions[1].arg_data[0], 0.2, 1e-9);
10797
}

0 commit comments

Comments
 (0)