|
14 | 14 |
|
15 | 15 | #include "stim/cmd/command_diagram.pybind.h" |
16 | 16 |
|
| 17 | +#include "stim/arg_parse.h" |
17 | 18 | #include "stim/cmd/command_help.h" |
| 19 | +#include "stim/dem/detector_error_model_target.pybind.h" |
18 | 20 | #include "stim/diagram/base64.h" |
19 | 21 | #include "stim/diagram/crumble.h" |
20 | 22 | #include "stim/diagram/detector_slice/detector_slice_set.h" |
@@ -143,31 +145,68 @@ DiagramHelper stim_pybind::dem_diagram(const DetectorErrorModel &dem, const std: |
143 | 145 | throw std::invalid_argument("Unrecognized diagram type: " + type); |
144 | 146 | } |
145 | 147 | } |
| 148 | + |
| 149 | +CoordFilter item_to_filter_single(const pybind11::handle &obj) { |
| 150 | + if (pybind11::isinstance<ExposedDemTarget>(obj)) { |
| 151 | + CoordFilter filter; |
| 152 | + filter.exact_target = pybind11::cast<ExposedDemTarget>(obj).internal(); |
| 153 | + filter.use_target = true; |
| 154 | + return filter; |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + std::string text = pybind11::cast<std::string>(obj); |
| 159 | + if (text.size() > 1 && text[0] == 'D') { |
| 160 | + CoordFilter filter; |
| 161 | + filter.exact_target = DemTarget::relative_detector_id(parse_exact_uint64_t_from_string(text.substr(1))); |
| 162 | + filter.use_target = true; |
| 163 | + return filter; |
| 164 | + } |
| 165 | + if (text.size() > 1 && text[0] == 'L') { |
| 166 | + CoordFilter filter; |
| 167 | + filter.exact_target = DemTarget::observable_id(parse_exact_uint64_t_from_string(text.substr(1))); |
| 168 | + filter.use_target = true; |
| 169 | + return filter; |
| 170 | + } |
| 171 | + } catch (const pybind11::cast_error &) { |
| 172 | + } catch (const std::invalid_argument &) { |
| 173 | + } |
| 174 | + |
| 175 | + CoordFilter filter; |
| 176 | + for (const auto &c : obj) { |
| 177 | + filter.coordinates.push_back(pybind11::cast<double>(c)); |
| 178 | + } |
| 179 | + return filter; |
| 180 | +} |
| 181 | + |
| 182 | +std::vector<CoordFilter> item_to_filter_multi(const pybind11::object &obj) { |
| 183 | + if (obj.is_none()) { |
| 184 | + return {CoordFilter{}}; |
| 185 | + } |
| 186 | + |
| 187 | + try { |
| 188 | + return {item_to_filter_single(obj)}; |
| 189 | + } catch (const pybind11::cast_error &) { |
| 190 | + } catch (const std::invalid_argument &) { |
| 191 | + } |
| 192 | + |
| 193 | + std::vector<CoordFilter> filters; |
| 194 | + for (const auto &filter_case : obj) { |
| 195 | + filters.push_back(item_to_filter_single(filter_case)); |
| 196 | + } |
| 197 | + return filters; |
| 198 | +} |
| 199 | + |
146 | 200 | DiagramHelper stim_pybind::circuit_diagram( |
147 | 201 | const Circuit &circuit, |
148 | 202 | const std::string &type, |
149 | 203 | const pybind11::object &tick, |
150 | 204 | const pybind11::object &filter_coords_obj) { |
151 | 205 | std::vector<CoordFilter> filter_coords; |
152 | 206 | try { |
153 | | - if (filter_coords_obj.is_none()) { |
154 | | - filter_coords.push_back({}); |
155 | | - } else { |
156 | | - for (const auto &filter_case : filter_coords_obj) { |
157 | | - CoordFilter filter; |
158 | | - if (pybind11::isinstance<DemTarget>(filter_case)) { |
159 | | - filter.exact_target = pybind11::cast<DemTarget>(filter_case); |
160 | | - filter.use_target = true; |
161 | | - } else { |
162 | | - for (const auto &c : filter_case) { |
163 | | - filter.coordinates.push_back(pybind11::cast<double>(c)); |
164 | | - } |
165 | | - } |
166 | | - filter_coords.push_back(std::move(filter)); |
167 | | - } |
168 | | - } |
| 207 | + filter_coords = item_to_filter_multi(filter_coords_obj); |
169 | 208 | } catch (const std::exception &_) { |
170 | | - throw std::invalid_argument("filter_coords wasn't a list of list of floats."); |
| 209 | + throw std::invalid_argument("filter_coords wasn't an Iterable[stim.DemTarget | Iterable[float]]."); |
171 | 210 | } |
172 | 211 |
|
173 | 212 | uint64_t tick_min; |
@@ -198,21 +237,21 @@ DiagramHelper stim_pybind::circuit_diagram( |
198 | 237 | std::stringstream out; |
199 | 238 | out << DiagramTimelineAsciiDrawer::make_diagram(circuit); |
200 | 239 | return DiagramHelper{DIAGRAM_TYPE_TEXT, out.str()}; |
201 | | - } else if (type == "timeline-svg") { |
| 240 | + } else if (type == "timeline-svg" || type == "timeline") { |
202 | 241 | std::stringstream out; |
203 | 242 | DiagramTimelineSvgDrawer::make_diagram_write_to( |
204 | 243 | circuit, out, tick_min, num_ticks, SVG_MODE_TIMELINE, filter_coords); |
205 | 244 | return DiagramHelper{DIAGRAM_TYPE_SVG, out.str()}; |
206 | | - } else if (type == "time-slice-svg" || type == "timeslice-svg") { |
| 245 | + } else if (type == "time-slice-svg" || type == "timeslice-svg" || type == "timeslice" || type == "time-slice") { |
207 | 246 | std::stringstream out; |
208 | 247 | DiagramTimelineSvgDrawer::make_diagram_write_to( |
209 | 248 | circuit, out, tick_min, num_ticks, SVG_MODE_TIME_SLICE, filter_coords); |
210 | 249 | return DiagramHelper{DIAGRAM_TYPE_SVG, out.str()}; |
211 | | - } else if (type == "detslice-svg" || type == "detector-slice-svg") { |
| 250 | + } else if (type == "detslice-svg" || type == "detslice" || type == "detector-slice-svg" || type == "detector-slice") { |
212 | 251 | std::stringstream out; |
213 | 252 | DetectorSliceSet::from_circuit_ticks(circuit, tick_min, num_ticks, filter_coords).write_svg_diagram_to(out); |
214 | 253 | return DiagramHelper{DIAGRAM_TYPE_SVG, out.str()}; |
215 | | - } else if (type == "detslice-with-ops-svg" || type == "time+detector-slice-svg") { |
| 254 | + } else if (type == "detslice-with-ops" || type == "detslice-with-ops-svg" || type == "time+detector-slice-svg") { |
216 | 255 | std::stringstream out; |
217 | 256 | DiagramTimelineSvgDrawer::make_diagram_write_to( |
218 | 257 | circuit, out, tick_min, num_ticks, SVG_MODE_TIME_DETECTOR_SLICE, filter_coords); |
|
0 commit comments