Skip to content

Commit

Permalink
Use bezier curves for long links in timeslice diagrams (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc authored Mar 29, 2024
1 parent 8c83d9b commit 8f8988a
Show file tree
Hide file tree
Showing 14 changed files with 490 additions and 418 deletions.
1 change: 1 addition & 0 deletions src/stim/diagram/detector_slice/detector_slice_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ FlattenedCoords FlattenedCoords::from(const DetectorSliceSet &set, float desired
}

float characteristic_distance = pick_characteristic_distance(used, result.qubit_coords);
result.unit_distance = desired_unit_distance;
float scale = desired_unit_distance / characteristic_distance;
for (auto &c : result.qubit_coords) {
c *= scale;
Expand Down
1 change: 1 addition & 0 deletions src/stim/diagram/detector_slice/detector_slice_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct FlattenedCoords {
std::vector<Coord<2>> qubit_coords;
std::map<uint64_t, Coord<2>> det_coords;
Coord<2> size;
float unit_distance;

static FlattenedCoords from(const DetectorSliceSet &set, float desired_unit_distance);
};
Expand Down
20 changes: 19 additions & 1 deletion src/stim/diagram/timeline/timeline_svg_drawer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,27 @@ void DiagramTimelineSvgDrawer::reserve_drawing_room_for_targets(SpanRef<const Ga
svg_out << "<path d=\"";
svg_out << "M" << coords[0].xyz[0] << "," << coords[0].xyz[1] << " ";
for (size_t k = 1; k < coords.size(); k++) {
svg_out << "L" << coords[k].xyz[0] << "," << coords[k].xyz[1] << " ";
auto p1 = coords[k - 1];
auto p2 = coords[k];
auto dp = p2 - p1;
if (dp.norm() < coord_sys.unit_distance * 1.1) {
svg_out << "L";
svg_out << p2.xyz[0] << "," << p2.xyz[1] << " ";
} else {
auto dp2 = Coord<2>{-dp.xyz[1], dp.xyz[0]};
if (2 * dp2.xyz[0] + 3 * dp2.xyz[1] < 0) {
dp2 *= -1;
}
auto p3 = p1 + dp * 0.2 + dp2 * 0.2;
auto p4 = p2 + dp * -0.2 + dp2 * 0.2;
svg_out << "C";
svg_out << p3.xyz[0] << " " << p3.xyz[1] << ",";
svg_out << p4.xyz[0] << " " << p4.xyz[1] << ",";
svg_out << p2.xyz[0] << " " << p2.xyz[1] << " ";
}
}
svg_out << "\"";
write_key_val(svg_out, "fill", "none");
write_key_val(svg_out, "stroke", "black");
write_key_val(svg_out, "stroke-width", "5");
svg_out << "/>\n";
Expand Down
19 changes: 19 additions & 0 deletions src/stim/diagram/timeline/timeline_svg_drawer.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,22 @@ TEST(diagram_timeline_svg_drawer, anticommuting_detector_circuit) {
circuit, ss, 0, UINT64_MAX, DiagramTimelineSvgDrawerMode::SVG_MODE_TIME_DETECTOR_SLICE, {&empty_filter});
expect_string_is_identical_to_saved_file(ss.str(), "anticommuting_detslice.svg");
}

TEST(diagram_timeline_svg_drawer, bezier_curves) {
CoordFilter empty_filter;
std::stringstream ss;
auto circuit = Circuit(R"CIRCUIT(
QUBIT_COORDS(0, 0) 0
QUBIT_COORDS(1, 0) 1
QUBIT_COORDS(2, 0) 2
QUBIT_COORDS(3, 0) 3
CX 0 1
CX 2 3
TICK
CX 0 2
CX 1 3
)CIRCUIT");
DiagramTimelineSvgDrawer::make_diagram_write_to(
circuit, ss, 0, UINT64_MAX, DiagramTimelineSvgDrawerMode::SVG_MODE_TIME_SLICE, {&empty_filter});
expect_string_is_identical_to_saved_file(ss.str(), "bezier_time_slice.svg");
}
9 changes: 5 additions & 4 deletions src/stim/stabilizers/tableau_iter.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,30 @@ TEST_EACH_WORD_SIZE_W(tableau_iter, iter_tableau, {
TableauIterator<W> iter1_signs(1, true);
TableauIterator<W> iter2(2, false);
TableauIterator<W> iter3(3, false);
int s1 = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
while (iter1.iter_next()) {
n1++;
}
ASSERT_EQ(n1, 6);

int s1 = 0;
while (iter1_signs.iter_next()) {
s1++;
}
ASSERT_EQ(s1, 24);

int n2 = 0;
while (iter2.iter_next()) {
n2++;
}
ASSERT_EQ(n2, 720);

// Note: disabled because it takes 2-3 seconds.
// int n3 = 0;
// while (iter3.iter_next()) {
// n3++;
// }
// ASSERT_EQ(n3, 1451520); // Note: disabled because it takes 2-3 seconds.
// ASSERT_EQ(n3, 1451520);
})

TEST_EACH_WORD_SIZE_W(tableau_iter, iter_tableau_distinct, {
Expand Down
2 changes: 1 addition & 1 deletion testdata/anticommuting_detslice.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions testdata/bezier_time_slice.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8f8988a

Please sign in to comment.