Skip to content

Commit

Permalink
type fix and various missing example warnings fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Jan 31, 2025
1 parent 6b44f44 commit 824e286
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 34 deletions.
20 changes: 16 additions & 4 deletions doc/python_api_reference_vDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -8259,7 +8259,7 @@ def to_numpy(
output_measure_flips: bool | np.ndarray = False,
output_detector_flips: bool | np.ndarray = False,
output_observable_flips: bool | np.ndarray = False,
) -> Tuple[np.ndarray, np.ndarray]:
) -> Optional[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]]:
"""Writes the simulator state into numpy arrays.
Args:
Expand Down Expand Up @@ -8366,16 +8366,18 @@ def to_numpy(
Examples:
>>> import stim
>>> sim = stim.FlipSimulator(batch_size=9)
>>> sim.do(stim.Circuit('M 0 1 2'))
>>> sim.do(stim.Circuit('M(1) 0 1 2'))
>>> ms_buf = np.empty(shape=(9, 1), dtype=np.uint8)
>>> xs, zs, ms, ds, os = sim.to_numpy(
... transpose=True,
... bit_packed=True,
... output_measure_flips=True,
... output_zs=True,
... output_measure_flips=ms_buf,
... )
>>> assert ms is ms_buf
>>> xs
>>> zs
>>> ms
array([[0],
[0],
[0],
Expand All @@ -8385,6 +8387,16 @@ def to_numpy(
[0],
[0],
[0]], dtype=uint8)
>>> ms
array([[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7]], dtype=uint8)
>>> ds
>>> os
"""
Expand Down
20 changes: 16 additions & 4 deletions doc/stim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6519,7 +6519,7 @@ class FlipSimulator:
output_measure_flips: bool | np.ndarray = False,
output_detector_flips: bool | np.ndarray = False,
output_observable_flips: bool | np.ndarray = False,
) -> Tuple[np.ndarray, np.ndarray]:
) -> Optional[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]]:
"""Writes the simulator state into numpy arrays.
Args:
Expand Down Expand Up @@ -6626,16 +6626,18 @@ class FlipSimulator:
Examples:
>>> import stim
>>> sim = stim.FlipSimulator(batch_size=9)
>>> sim.do(stim.Circuit('M 0 1 2'))
>>> sim.do(stim.Circuit('M(1) 0 1 2'))
>>> ms_buf = np.empty(shape=(9, 1), dtype=np.uint8)
>>> xs, zs, ms, ds, os = sim.to_numpy(
... transpose=True,
... bit_packed=True,
... output_measure_flips=True,
... output_zs=True,
... output_measure_flips=ms_buf,
... )
>>> assert ms is ms_buf
>>> xs
>>> zs
>>> ms
array([[0],
[0],
[0],
Expand All @@ -6645,6 +6647,16 @@ class FlipSimulator:
[0],
[0],
[0]], dtype=uint8)
>>> ms
array([[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7]], dtype=uint8)
>>> ds
>>> os
"""
Expand Down
20 changes: 16 additions & 4 deletions glue/python/src/stim/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6519,7 +6519,7 @@ class FlipSimulator:
output_measure_flips: bool | np.ndarray = False,
output_detector_flips: bool | np.ndarray = False,
output_observable_flips: bool | np.ndarray = False,
) -> Tuple[np.ndarray, np.ndarray]:
) -> Optional[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]]:
"""Writes the simulator state into numpy arrays.
Args:
Expand Down Expand Up @@ -6626,16 +6626,18 @@ class FlipSimulator:
Examples:
>>> import stim
>>> sim = stim.FlipSimulator(batch_size=9)
>>> sim.do(stim.Circuit('M 0 1 2'))
>>> sim.do(stim.Circuit('M(1) 0 1 2'))
>>> ms_buf = np.empty(shape=(9, 1), dtype=np.uint8)
>>> xs, zs, ms, ds, os = sim.to_numpy(
... transpose=True,
... bit_packed=True,
... output_measure_flips=True,
... output_zs=True,
... output_measure_flips=ms_buf,
... )
>>> assert ms is ms_buf
>>> xs
>>> zs
>>> ms
array([[0],
[0],
[0],
Expand All @@ -6645,6 +6647,16 @@ class FlipSimulator:
[0],
[0],
[0]], dtype=uint8)
>>> ms
array([[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7]], dtype=uint8)
>>> ds
>>> os
"""
Expand Down
18 changes: 9 additions & 9 deletions src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ std::vector<ExplainedError> py_find_undetectable_logical_error(
return ErrorMatcher::explain_errors_from_circuit(self, &filter, reduce_to_representative);
}

std::string py_shortest_error_sat_problem(const Circuit &self, std::string format) {
std::string py_shortest_error_sat_problem(const Circuit &self, std::string_view format) {
DetectorErrorModel dem = ErrorAnalyzer::circuit_to_detector_error_model(self, false, true, false, 1, false, false);
return stim::shortest_error_sat_problem(dem, format);
}

std::string py_likeliest_error_sat_problem(const Circuit &self, int quantization, std::string format) {
std::string py_likeliest_error_sat_problem(const Circuit &self, int quantization, std::string_view format) {
DetectorErrorModel dem = ErrorAnalyzer::circuit_to_detector_error_model(self, false, true, false, 1, false, false);
return stim::likeliest_error_sat_problem(dem, quantization, format);
}
Expand Down Expand Up @@ -255,7 +255,7 @@ void circuit_append(
std::vector<uint32_t> raw_targets;
try {
raw_targets.push_back(obj_to_gate_target(targets).data);
} catch (const std::invalid_argument &ex) {
} catch (const std::invalid_argument &) {
for (const auto &t : targets) {
raw_targets.push_back(handle_to_gate_target(t).data);
}
Expand All @@ -279,13 +279,13 @@ void circuit_append(
auto d = pybind11::cast<double>(used_arg);
self.safe_append_ua(gate_name, raw_targets, d, tag);
return;
} catch (const pybind11::cast_error &ex) {
} catch (const pybind11::cast_error &) {
}
try {
auto args = pybind11::cast<std::vector<double>>(used_arg);
self.safe_append_u(gate_name, raw_targets, args, tag);
return;
} catch (const pybind11::cast_error &ex) {
} catch (const pybind11::cast_error &) {
}
throw std::invalid_argument("Arg must be a double or sequence of doubles.");
} else if (pybind11::isinstance<PyCircuitInstruction>(obj)) {
Expand Down Expand Up @@ -1329,8 +1329,8 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
double before_measure_flip_probability,
double after_reset_flip_probability) {
auto r = type.find(':');
std::string code;
std::string task;
std::string_view code;
std::string_view task;
if (r == std::string::npos) {
code = "";
task = type;
Expand All @@ -1339,7 +1339,7 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
task = type.substr(r + 1);
}

CircuitGenParameters params(rounds, distance, task);
CircuitGenParameters params(rounds, distance, std::string(task));
params.after_clifford_depolarization = after_clifford_depolarization;
params.after_reset_flip_probability = after_reset_flip_probability;
params.before_measure_flip_probability = before_measure_flip_probability;
Expand Down Expand Up @@ -1920,7 +1920,7 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
[](const Circuit &self, const pybind11::object &obj, double atol) -> bool {
try {
return self.approx_equals(pybind11::cast<Circuit>(obj), atol);
} catch (const pybind11::cast_error &ex) {
} catch (const pybind11::cast_error &) {
return false;
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/stim/search/sat/wcnf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,14 @@ std::string sat_problem_as_wcnf_string(const DetectorErrorModel& model, bool wei
}

// Should ignore weights entirely and minimize the cardinality.
std::string stim::shortest_error_sat_problem(const DetectorErrorModel& model, std::string format) {
std::string stim::shortest_error_sat_problem(const DetectorErrorModel& model, std::string_view format) {
if (format != "WDIMACS") {
throw std::invalid_argument("Unsupported format.");
}
return sat_problem_as_wcnf_string(model, /*weighted=*/false, /*quantization=*/0);
}

std::string stim::likeliest_error_sat_problem(const DetectorErrorModel& model, int quantization, std::string format) {
std::string stim::likeliest_error_sat_problem(const DetectorErrorModel& model, int quantization, std::string_view format) {
if (format != "WDIMACS") {
throw std::invalid_argument("Unsupported format.");
}
Expand Down
8 changes: 2 additions & 6 deletions src/stim/search/sat/wcnf.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef _STIM_SEARCH_SAT_WCNF_H
#define _STIM_SEARCH_SAT_WCNF_H

#include <cstdint>

#include "stim/dem/detector_error_model.h"

namespace stim {
Expand Down Expand Up @@ -33,13 +31,11 @@ namespace stim {
/// users must separately manage the process of selecting and running the solver. This approach is designed to
/// sidestep the need for direct integration with any particular solver and allow
/// for experimentation with different solvers to achieve the best performance.
// std::string shortest_error_problem_as_wcnf_file(
// const DetectorErrorModel &model, bool weighted=false, size_t weight_scale_factor=0);

std::string shortest_error_sat_problem(const DetectorErrorModel& model, std::string format = "WDIMACS");
std::string shortest_error_sat_problem(const DetectorErrorModel& model, std::string_view format = "WDIMACS");

std::string likeliest_error_sat_problem(
const DetectorErrorModel& model, int quantization = 10, std::string format = "WDIMACS");
const DetectorErrorModel& model, int quantization = 10, std::string_view format = "WDIMACS");

} // namespace stim

Expand Down
23 changes: 18 additions & 5 deletions src/stim/simulators/frame_simulator.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ void stim_pybind::pybind_frame_simulator_methods(
pybind11::arg("output_detector_flips") = false,
pybind11::arg("output_observable_flips") = false,
clean_doc_string(R"DOC(
@signature def to_numpy(self, *, bit_packed: bool = False, transpose: bool = False, output_xs: bool | np.ndarray = False, output_zs: bool | np.ndarray = False, output_measure_flips: bool | np.ndarray = False, output_detector_flips: bool | np.ndarray = False, output_observable_flips: bool | np.ndarray = False) -> tuple[np.ndarray, np.ndarray]:
@signature def to_numpy(self, *, bit_packed: bool = False, transpose: bool = False, output_xs: bool | np.ndarray = False, output_zs: bool | np.ndarray = False, output_measure_flips: bool | np.ndarray = False, output_detector_flips: bool | np.ndarray = False, output_observable_flips: bool | np.ndarray = False) -> Optional[Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]]:
Writes the simulator state into numpy arrays.

Args:
Expand Down Expand Up @@ -719,17 +719,19 @@ void stim_pybind::pybind_frame_simulator_methods(

Examples:
>>> import stim
>>> import numpy as np
>>> sim = stim.FlipSimulator(batch_size=9)
>>> sim.do(stim.Circuit('M 0 1 2'))
>>> sim.do(stim.Circuit('M(1) 0 1 2'))

>>> ms_buf = np.empty(shape=(9, 1), dtype=np.uint8)
>>> xs, zs, ms, ds, os = sim.to_numpy(
... transpose=True,
... bit_packed=True,
... output_measure_flips=True,
... output_xs=True,
... output_measure_flips=ms_buf,
... )
>>> assert ms is ms_buf
>>> xs
>>> zs
>>> ms
array([[0],
[0],
[0],
Expand All @@ -739,6 +741,17 @@ void stim_pybind::pybind_frame_simulator_methods(
[0],
[0],
[0]], dtype=uint8)
>>> zs
>>> ms
array([[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7],
[7]], dtype=uint8)
>>> ds
>>> os
)DOC")
Expand Down
Loading

0 comments on commit 824e286

Please sign in to comment.