Skip to content

Commit

Permalink
Add skip_detectors and mark arguments to `stim.Circuit.to_crumble…
Browse files Browse the repository at this point in the history
…_url` (#835)

Fixes #806
  • Loading branch information
Strilanc authored Sep 21, 2024
1 parent d7b471e commit 3b827fe
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 29 deletions.
35 changes: 35 additions & 0 deletions doc/python_api_reference_vDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,9 @@ def time_reversed_for_flows(
# (in class stim.Circuit)
def to_crumble_url(
self,
*,
skip_detectors: bool = False,
mark: Optional[Dict[int, List[stim.ExplainedError]]] = None,
) -> str:
"""Returns a URL that opens up crumble and loads this circuit into it.
Expand All @@ -3244,6 +3247,15 @@ def to_crumble_url(
at https://algassert.com/crumble, which is what the URL returned by this
method will point to.
Args:
skip_detectors: Defaults to False. If set to True, detectors from the
circuit aren't included in the crumble URL. This can reduce visual
clutter in crumble, and improve its performance, since it doesn't
need to indicate or track the sensitivity regions of detectors.
mark: Defaults to None (no marks). If set to a dictionary from int to
errors, such as `mark={1: circuit.shortest_graphlike_error()}`,
then the errors will be highlighted and tracked forward by crumble.
Returns:
A URL that can be opened in a web browser.
Expand All @@ -3255,6 +3267,16 @@ def to_crumble_url(
... S 1
... ''').to_crumble_url()
'https://algassert.com/crumble#circuit=H_0;CX_0_1;S_1'
>>> circuit = stim.Circuit('''
... M(0.25) 0 1 2
... DETECTOR rec[-1] rec[-2]
... DETECTOR rec[-2] rec[-3]
... OBSERVABLE_INCLUDE(0) rec[-1]
... ''')
>>> err = circuit.shortest_graphlike_error(canonicalize_circuit_errors=True)
>>> circuit.to_crumble_url(skip_detectors=True, mark={1: err})
'https://algassert.com/crumble#circuit=;TICK;MARKX(1)1;MARKX(1)2;MARKX(1)0;TICK;M(0.25)0_1_2;OI(0)rec[-1]'
"""
```

Expand Down Expand Up @@ -4926,6 +4948,19 @@ def sample(
(dets[s, m // 8] >> (m % 8)) & 1
The bit for observable `m` in shot `s` is at
(obs[s, m // 8] >> (m % 8)) & 1
Examples:
>>> import stim
>>> c = stim.Circuit('''
... H 0
... CNOT 0 1
... X_ERROR(1.0) 0
... M 0 1
... DETECTOR rec[-1] rec[-2]
... ''')
>>> s = c.compile_detector_sampler()
>>> s.sample(shots=1)
array([[ True]])
"""
```

Expand Down
35 changes: 35 additions & 0 deletions doc/stim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,9 @@ class Circuit:
"""
def to_crumble_url(
self,
*,
skip_detectors: bool = False,
mark: Optional[Dict[int, List[stim.ExplainedError]]] = None,
) -> str:
"""Returns a URL that opens up crumble and loads this circuit into it.
Expand All @@ -2492,6 +2495,15 @@ class Circuit:
at https://algassert.com/crumble, which is what the URL returned by this
method will point to.
Args:
skip_detectors: Defaults to False. If set to True, detectors from the
circuit aren't included in the crumble URL. This can reduce visual
clutter in crumble, and improve its performance, since it doesn't
need to indicate or track the sensitivity regions of detectors.
mark: Defaults to None (no marks). If set to a dictionary from int to
errors, such as `mark={1: circuit.shortest_graphlike_error()}`,
then the errors will be highlighted and tracked forward by crumble.
Returns:
A URL that can be opened in a web browser.
Expand All @@ -2503,6 +2515,16 @@ class Circuit:
... S 1
... ''').to_crumble_url()
'https://algassert.com/crumble#circuit=H_0;CX_0_1;S_1'
>>> circuit = stim.Circuit('''
... M(0.25) 0 1 2
... DETECTOR rec[-1] rec[-2]
... DETECTOR rec[-2] rec[-3]
... OBSERVABLE_INCLUDE(0) rec[-1]
... ''')
>>> err = circuit.shortest_graphlike_error(canonicalize_circuit_errors=True)
>>> circuit.to_crumble_url(skip_detectors=True, mark={1: err})
'https://algassert.com/crumble#circuit=;TICK;MARKX(1)1;MARKX(1)2;MARKX(1)0;TICK;M(0.25)0_1_2;OI(0)rec[-1]'
"""
def to_file(
self,
Expand Down Expand Up @@ -3810,6 +3832,19 @@ class CompiledDetectorSampler:
(dets[s, m // 8] >> (m % 8)) & 1
The bit for observable `m` in shot `s` is at
(obs[s, m // 8] >> (m % 8)) & 1
Examples:
>>> import stim
>>> c = stim.Circuit('''
... H 0
... CNOT 0 1
... X_ERROR(1.0) 0
... M 0 1
... DETECTOR rec[-1] rec[-2]
... ''')
>>> s = c.compile_detector_sampler()
>>> s.sample(shots=1)
array([[ True]])
"""
def sample_bit_packed(
self,
Expand Down
35 changes: 35 additions & 0 deletions glue/python/src/stim/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,9 @@ class Circuit:
"""
def to_crumble_url(
self,
*,
skip_detectors: bool = False,
mark: Optional[Dict[int, List[stim.ExplainedError]]] = None,
) -> str:
"""Returns a URL that opens up crumble and loads this circuit into it.
Expand All @@ -2492,6 +2495,15 @@ class Circuit:
at https://algassert.com/crumble, which is what the URL returned by this
method will point to.
Args:
skip_detectors: Defaults to False. If set to True, detectors from the
circuit aren't included in the crumble URL. This can reduce visual
clutter in crumble, and improve its performance, since it doesn't
need to indicate or track the sensitivity regions of detectors.
mark: Defaults to None (no marks). If set to a dictionary from int to
errors, such as `mark={1: circuit.shortest_graphlike_error()}`,
then the errors will be highlighted and tracked forward by crumble.
Returns:
A URL that can be opened in a web browser.
Expand All @@ -2503,6 +2515,16 @@ class Circuit:
... S 1
... ''').to_crumble_url()
'https://algassert.com/crumble#circuit=H_0;CX_0_1;S_1'
>>> circuit = stim.Circuit('''
... M(0.25) 0 1 2
... DETECTOR rec[-1] rec[-2]
... DETECTOR rec[-2] rec[-3]
... OBSERVABLE_INCLUDE(0) rec[-1]
... ''')
>>> err = circuit.shortest_graphlike_error(canonicalize_circuit_errors=True)
>>> circuit.to_crumble_url(skip_detectors=True, mark={1: err})
'https://algassert.com/crumble#circuit=;TICK;MARKX(1)1;MARKX(1)2;MARKX(1)0;TICK;M(0.25)0_1_2;OI(0)rec[-1]'
"""
def to_file(
self,
Expand Down Expand Up @@ -3810,6 +3832,19 @@ class CompiledDetectorSampler:
(dets[s, m // 8] >> (m % 8)) & 1
The bit for observable `m` in shot `s` is at
(obs[s, m // 8] >> (m % 8)) & 1
Examples:
>>> import stim
>>> c = stim.Circuit('''
... H 0
... CNOT 0 1
... X_ERROR(1.0) 0
... M 0 1
... DETECTOR rec[-1] rec[-2]
... ''')
>>> s = c.compile_detector_sampler()
>>> s.sample(shots=1)
array([[ True]])
"""
def sample_bit_packed(
self,
Expand Down
31 changes: 30 additions & 1 deletion src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3449,8 +3449,18 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci

c.def(
"to_crumble_url",
&export_crumble_url,
[](const Circuit &self, bool skip_detectors, pybind11::object &obj_mark) {
std::map<int, std::vector<ExplainedError>> mark;
if (!obj_mark.is_none()) {
mark = pybind11::cast<std::map<int, std::vector<ExplainedError>>>(obj_mark);
}
return export_crumble_url(self, skip_detectors, mark);
},
pybind11::kw_only(),
pybind11::arg("skip_detectors") = false,
pybind11::arg("mark") = pybind11::none(),
clean_doc_string(R"DOC(
@signature def to_crumble_url(self, *, skip_detectors: bool = False, mark: Optional[dict[int, list[stim.ExplainedError]]] = None) -> str:
Returns a URL that opens up crumble and loads this circuit into it.
Crumble is a tool for editing stabilizer circuits, and visualizing their
Expand All @@ -3459,6 +3469,15 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
at https://algassert.com/crumble, which is what the URL returned by this
method will point to.
Args:
skip_detectors: Defaults to False. If set to True, detectors from the
circuit aren't included in the crumble URL. This can reduce visual
clutter in crumble, and improve its performance, since it doesn't
need to indicate or track the sensitivity regions of detectors.
mark: Defaults to None (no marks). If set to a dictionary from int to
errors, such as `mark={1: circuit.shortest_graphlike_error()}`,
then the errors will be highlighted and tracked forward by crumble.
Returns:
A URL that can be opened in a web browser.
Expand All @@ -3470,6 +3489,16 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
... S 1
... ''').to_crumble_url()
'https://algassert.com/crumble#circuit=H_0;CX_0_1;S_1'
>>> circuit = stim.Circuit('''
... M(0.25) 0 1 2
... DETECTOR rec[-1] rec[-2]
... DETECTOR rec[-2] rec[-3]
... OBSERVABLE_INCLUDE(0) rec[-1]
... ''')
>>> err = circuit.shortest_graphlike_error(canonicalize_circuit_errors=True)
>>> circuit.to_crumble_url(skip_detectors=True, mark={1: err})
'https://algassert.com/crumble#circuit=;TICK;MARKX(1)1;MARKX(1)2;MARKX(1)0;TICK;M(0.25)0_1_2;OI(0)rec[-1]'
)DOC")
.data());

Expand Down
13 changes: 13 additions & 0 deletions src/stim/py/compiled_detector_sampler.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ void stim_pybind::pybind_compiled_detector_sampler_methods(
(dets[s, m // 8] >> (m % 8)) & 1
The bit for observable `m` in shot `s` is at
(obs[s, m // 8] >> (m % 8)) & 1
Examples:
>>> import stim
>>> c = stim.Circuit('''
... H 0
... CNOT 0 1
... X_ERROR(1.0) 0
... M 0 1
... DETECTOR rec[-1] rec[-2]
... ''')
>>> s = c.compile_detector_sampler()
>>> s.sample(shots=1)
array([[ True]])
)DOC")
.data());

Expand Down
Loading

0 comments on commit 3b827fe

Please sign in to comment.