Skip to content

Commit 04d119b

Browse files
authored
Fix some flaky tests (#488)
- Use stable_sort in diagrams to avoid accidental reordering - Fix re-checking for circuit operation fusing after fusing was done - Increase sample counts in statistical tests - Fix using &vector[vector.size()] instead of vector.data() + vector.size() to get end ptr of vector data
1 parent 0cb6adc commit 04d119b

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

src/stim/circuit/circuit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ void circuit_read_operations(Circuit &circuit, SOURCE read_char, READ_CONDITION
608608
}
609609

610610
// Fuse operations.
611-
while (ops.size() > 1 && ops[ops.size() - 2].can_fuse(new_op)) {
611+
if (ops.size() > 1 && ops[ops.size() - 2].can_fuse(new_op)) {
612612
fuse_data(ops[ops.size() - 2].target_data.targets, new_op.target_data.targets, circuit.target_buf);
613613
ops.pop_back();
614614
}

src/stim/cmd/command_sample.test.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TEST(command_sample, basic_distributions) {
161161
ASSERT_EQ(
162162
"",
163163
deviation(
164-
run_captured_stim_main({"--sample=1000"}, R"input(
164+
run_captured_stim_main({"--sample=10000"}, R"input(
165165
H 0
166166
CNOT 0 1
167167
M 0 1
@@ -171,7 +171,7 @@ M 0 1
171171
ASSERT_EQ(
172172
"",
173173
deviation(
174-
run_captured_stim_main({"--sample=1000"}, R"input(
174+
run_captured_stim_main({"--sample=10000"}, R"input(
175175
H 0
176176
CNOT 0 1
177177
SQRT_X 0 1
@@ -182,7 +182,7 @@ M 0 1
182182
ASSERT_EQ(
183183
"",
184184
deviation(
185-
run_captured_stim_main({"--sample=1000"}, R"input(
185+
run_captured_stim_main({"--sample=10000"}, R"input(
186186
H 0
187187
CNOT 0 1
188188
SQRT_Y 0 1
@@ -195,7 +195,7 @@ TEST(command_sample, sample_x_error) {
195195
ASSERT_EQ(
196196
"",
197197
deviation(
198-
run_captured_stim_main({"--sample=10000"}, R"input(
198+
run_captured_stim_main({"--sample=100000"}, R"input(
199199
X_ERROR(0.1) 0 1
200200
M 0 1
201201
)input"),
@@ -217,7 +217,7 @@ TEST(command_sample, sample_z_error) {
217217
ASSERT_EQ(
218218
"",
219219
deviation(
220-
run_captured_stim_main({"--sample=10000"}, R"input(
220+
run_captured_stim_main({"--sample=100000"}, R"input(
221221
H 0 1
222222
Z_ERROR(0.1) 0 1
223223
H 0 1
@@ -239,7 +239,7 @@ TEST(command_sample, sample_y_error) {
239239
ASSERT_EQ(
240240
"",
241241
deviation(
242-
run_captured_stim_main({"--sample=10000"}, R"input(
242+
run_captured_stim_main({"--sample=100000"}, R"input(
243243
Y_ERROR(0.1) 0 1
244244
M 0 1
245245
)input"),
@@ -261,7 +261,7 @@ TEST(command_sample, sample_depolarize1_error) {
261261
ASSERT_EQ(
262262
"",
263263
deviation(
264-
run_captured_stim_main({"--sample=10000"}, R"input(
264+
run_captured_stim_main({"--sample=100000"}, R"input(
265265
DEPOLARIZE1(0.3) 0 1
266266
M 0 1
267267
)input"),
@@ -270,7 +270,7 @@ M 0 1
270270
ASSERT_EQ(
271271
"",
272272
deviation(
273-
run_captured_stim_main({"--sample=10000"}, R"input(
273+
run_captured_stim_main({"--sample=100000"}, R"input(
274274
H 0 1
275275
DEPOLARIZE1(0.3) 0 1
276276
H 0 1
@@ -281,7 +281,7 @@ M 0 1
281281
ASSERT_EQ(
282282
"",
283283
deviation(
284-
run_captured_stim_main({"--sample=10000"}, R"input(
284+
run_captured_stim_main({"--sample=100000"}, R"input(
285285
H_YZ 0 1
286286
DEPOLARIZE1(0.3) 0 1
287287
H_YZ 0 1
@@ -294,7 +294,7 @@ TEST(command_sample, sample_depolarize2_error) {
294294
ASSERT_EQ(
295295
"",
296296
deviation(
297-
run_captured_stim_main({"--sample=10000"}, R"input(
297+
run_captured_stim_main({"--sample=100000"}, R"input(
298298
DEPOLARIZE2(0.1) 0 1
299299
M 0 1
300300
)input"),
@@ -303,7 +303,7 @@ M 0 1
303303
ASSERT_EQ(
304304
"",
305305
deviation(
306-
run_captured_stim_main({"--sample=10000"}, R"input(
306+
run_captured_stim_main({"--sample=100000"}, R"input(
307307
H 0
308308
H_YZ 1
309309
DEPOLARIZE2(0.3) 0 1

src/stim/diagram/circuit_timeline_helper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ConstPointerRange<double> CircuitTimelineHelper::shifted_coordinates_in_workspac
125125
coord_workspace[k] += cur_coord_shift[k];
126126
}
127127
}
128-
return {&coord_workspace[0], &coord_workspace[coords.size()]};
128+
return {coord_workspace.data(), coord_workspace.data() + coords.size()};
129129
}
130130

131131
void CircuitTimelineHelper::do_detector(const Operation &op) {

src/stim/diagram/detector_slice/detector_slice_set.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ void _draw_observable(
562562
size_t scale) {
563563
std::vector<GateTarget> terms_copy;
564564
terms_copy.insert(terms_copy.end(), terms.begin(), terms.end());
565-
std::sort(terms_copy.begin(), terms_copy.end(), [&](GateTarget a, GateTarget b) {
565+
std::stable_sort(terms_copy.begin(), terms_copy.end(), [&](GateTarget a, GateTarget b) {
566566
auto a2 = inv_space_fill_transform(unscaled_coords(a.qubit_value()));
567567
auto b2 = inv_space_fill_transform(unscaled_coords(b.qubit_value()));
568568
if (a2 != b2) {
@@ -647,7 +647,7 @@ void _start_many_body_svg_path(
647647
pts_workspace.push_back(coords(tick, term.qubit_value()));
648648
}
649649
auto center = pick_polygon_center(pts_workspace);
650-
std::sort(pts_workspace.begin(), pts_workspace.end(), [&](Coord<2> a, Coord<2> b) {
650+
std::stable_sort(pts_workspace.begin(), pts_workspace.end(), [&](Coord<2> a, Coord<2> b) {
651651
return angle_from_to(center, a) < angle_from_to(center, b);
652652
});
653653

src/stim/mem/fixed_cap_vector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ class FixedCapVector {
6161
return &data[0];
6262
}
6363
T *end() {
64-
return &data[num_used];
64+
return &data[0] + num_used;
6565
}
6666
const T *end() const {
67-
return &data[num_used];
67+
return &data[0] + num_used;
6868
}
6969
const T *begin() const {
7070
return &data[0];

src/stim/stabilizers/conversions.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ std::vector<std::vector<std::complex<float>>> stim::tableau_to_unitary(const Tab
163163
size_t n = 1 << tableau.num_qubits;
164164
for (size_t row = 0; row < n; row++) {
165165
result.push_back({});
166-
result.back().insert(result.back().end(), &flat[row * n], &flat[row * n + n]);
166+
auto &back = result.back();
167+
std::complex<float> *start = &flat[row * n];
168+
back.insert(back.end(), start, start + n);
167169
}
168170
return result;
169171
}

0 commit comments

Comments
 (0)