Skip to content

Commit dfb81d3

Browse files
authored
Add stim.{Circuit,DetectorErrorModel}.without_tags (#911)
- Add `stim.Circuit.without_tags` - Add `stim.CircuitDetectorErrorModel.without_tags` Fixes #884
1 parent 5f96aba commit dfb81d3

File tree

12 files changed

+247
-0
lines changed

12 files changed

+247
-0
lines changed

doc/python_api_reference_vDev.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ API references for stable versions are kept on the [stim github wiki](https://gi
6262
- [`stim.Circuit.to_tableau`](#stim.Circuit.to_tableau)
6363
- [`stim.Circuit.with_inlined_feedback`](#stim.Circuit.with_inlined_feedback)
6464
- [`stim.Circuit.without_noise`](#stim.Circuit.without_noise)
65+
- [`stim.Circuit.without_tags`](#stim.Circuit.without_tags)
6566
- [`stim.CircuitErrorLocation`](#stim.CircuitErrorLocation)
6667
- [`stim.CircuitErrorLocation.__init__`](#stim.CircuitErrorLocation.__init__)
6768
- [`stim.CircuitErrorLocation.flipped_measurement`](#stim.CircuitErrorLocation.flipped_measurement)
@@ -187,6 +188,7 @@ API references for stable versions are kept on the [stim github wiki](https://gi
187188
- [`stim.DetectorErrorModel.rounded`](#stim.DetectorErrorModel.rounded)
188189
- [`stim.DetectorErrorModel.shortest_graphlike_error`](#stim.DetectorErrorModel.shortest_graphlike_error)
189190
- [`stim.DetectorErrorModel.to_file`](#stim.DetectorErrorModel.to_file)
191+
- [`stim.DetectorErrorModel.without_tags`](#stim.DetectorErrorModel.without_tags)
190192
- [`stim.ExplainedError`](#stim.ExplainedError)
191193
- [`stim.ExplainedError.__init__`](#stim.ExplainedError.__init__)
192194
- [`stim.ExplainedError.circuit_error_locations`](#stim.ExplainedError.circuit_error_locations)
@@ -3593,6 +3595,33 @@ def without_noise(
35933595
"""
35943596
```
35953597

3598+
<a name="stim.Circuit.without_tags"></a>
3599+
```python
3600+
# stim.Circuit.without_tags
3601+
3602+
# (in class stim.Circuit)
3603+
def without_tags(
3604+
self,
3605+
) -> stim.Circuit:
3606+
"""Returns a copy of the circuit with all tags removed.
3607+
3608+
Returns:
3609+
A `stim.Circuit` with the same instructions except all tags have been
3610+
removed.
3611+
3612+
Examples:
3613+
>>> import stim
3614+
>>> stim.Circuit('''
3615+
... X[test-tag] 0
3616+
... M[test-tag-2](0.125) 0
3617+
... ''').without_tags()
3618+
stim.Circuit('''
3619+
X 0
3620+
M(0.125) 0
3621+
''')
3622+
"""
3623+
```
3624+
35963625
<a name="stim.CircuitErrorLocation"></a>
35973626
```python
35983627
# stim.CircuitErrorLocation
@@ -7589,6 +7618,31 @@ def to_file(
75897618
"""
75907619
```
75917620

7621+
<a name="stim.DetectorErrorModel.without_tags"></a>
7622+
```python
7623+
# stim.DetectorErrorModel.without_tags
7624+
7625+
# (in class stim.DetectorErrorModel)
7626+
def without_tags(
7627+
self,
7628+
) -> stim.DetectorErrorModel:
7629+
"""Returns a copy of the detector error model with all tags removed.
7630+
7631+
Returns:
7632+
A `stim.DetectorErrorModel` with the same instructions except all tags have
7633+
been removed.
7634+
7635+
Examples:
7636+
>>> import stim
7637+
>>> stim.DetectorErrorModel('''
7638+
... error[test-tag](0.25) D0
7639+
... ''').without_tags()
7640+
stim.DetectorErrorModel('''
7641+
error(0.25) D0
7642+
''')
7643+
"""
7644+
```
7645+
75927646
<a name="stim.ExplainedError"></a>
75937647
```python
75947648
# stim.ExplainedError

doc/stim.pyi

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,26 @@ class Circuit:
27922792
M 0
27932793
''')
27942794
"""
2795+
def without_tags(
2796+
self,
2797+
) -> stim.Circuit:
2798+
"""Returns a copy of the circuit with all tags removed.
2799+
2800+
Returns:
2801+
A `stim.Circuit` with the same instructions except all tags have been
2802+
removed.
2803+
2804+
Examples:
2805+
>>> import stim
2806+
>>> stim.Circuit('''
2807+
... X[test-tag] 0
2808+
... M[test-tag-2](0.125) 0
2809+
... ''').without_tags()
2810+
stim.Circuit('''
2811+
X 0
2812+
M(0.125) 0
2813+
''')
2814+
"""
27952815
class CircuitErrorLocation:
27962816
"""Describes the location of an error mechanism from a stim circuit.
27972817
@@ -5969,6 +5989,24 @@ class DetectorErrorModel:
59695989
>>> contents
59705990
'error(0.25) D2 D3\n'
59715991
"""
5992+
def without_tags(
5993+
self,
5994+
) -> stim.DetectorErrorModel:
5995+
"""Returns a copy of the detector error model with all tags removed.
5996+
5997+
Returns:
5998+
A `stim.DetectorErrorModel` with the same instructions except all tags have
5999+
been removed.
6000+
6001+
Examples:
6002+
>>> import stim
6003+
>>> stim.DetectorErrorModel('''
6004+
... error[test-tag](0.25) D0
6005+
... ''').without_tags()
6006+
stim.DetectorErrorModel('''
6007+
error(0.25) D0
6008+
''')
6009+
"""
59726010
class ExplainedError:
59736011
"""Describes the location of an error mechanism from a stim circuit.
59746012

glue/python/src/stim/__init__.pyi

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,6 +2792,26 @@ class Circuit:
27922792
M 0
27932793
''')
27942794
"""
2795+
def without_tags(
2796+
self,
2797+
) -> stim.Circuit:
2798+
"""Returns a copy of the circuit with all tags removed.
2799+
2800+
Returns:
2801+
A `stim.Circuit` with the same instructions except all tags have been
2802+
removed.
2803+
2804+
Examples:
2805+
>>> import stim
2806+
>>> stim.Circuit('''
2807+
... X[test-tag] 0
2808+
... M[test-tag-2](0.125) 0
2809+
... ''').without_tags()
2810+
stim.Circuit('''
2811+
X 0
2812+
M(0.125) 0
2813+
''')
2814+
"""
27952815
class CircuitErrorLocation:
27962816
"""Describes the location of an error mechanism from a stim circuit.
27972817
@@ -5969,6 +5989,24 @@ class DetectorErrorModel:
59695989
>>> contents
59705990
'error(0.25) D2 D3\n'
59715991
"""
5992+
def without_tags(
5993+
self,
5994+
) -> stim.DetectorErrorModel:
5995+
"""Returns a copy of the detector error model with all tags removed.
5996+
5997+
Returns:
5998+
A `stim.DetectorErrorModel` with the same instructions except all tags have
5999+
been removed.
6000+
6001+
Examples:
6002+
>>> import stim
6003+
>>> stim.DetectorErrorModel('''
6004+
... error[test-tag](0.25) D0
6005+
... ''').without_tags()
6006+
stim.DetectorErrorModel('''
6007+
error(0.25) D0
6008+
''')
6009+
"""
59726010
class ExplainedError:
59736011
"""Describes the location of an error mechanism from a stim circuit.
59746012

src/stim/circuit/circuit.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,22 @@ const Circuit Circuit::aliased_noiseless_circuit() const {
802802
return result;
803803
}
804804

805+
Circuit Circuit::without_tags() const {
806+
Circuit result;
807+
for (CircuitInstruction inst : operations) {
808+
if (inst.gate_type == GateType::REPEAT) {
809+
result.append_repeat_block(
810+
inst.repeat_block_rep_count(),
811+
inst.repeat_block_body(*this).without_tags(),
812+
"");
813+
} else {
814+
inst.tag = "";
815+
result.safe_append(inst);
816+
}
817+
}
818+
return result;
819+
}
820+
805821
Circuit Circuit::without_noise() const {
806822
Circuit result;
807823
for (const auto &op : operations) {

src/stim/circuit/circuit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct Circuit {
147147

148148
/// Returns a copy of the circuit with all noise processes removed.
149149
Circuit without_noise() const;
150+
/// Returns a copy of the circuit with all tags removed.
151+
Circuit without_tags() const;
150152

151153
/// Returns an equivalent circuit without REPEAT or SHIFT_COORDS instructions.
152154
Circuit flattened() const;

src/stim/circuit/circuit.pybind.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,29 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
22362236
)DOC")
22372237
.data());
22382238

2239+
c.def(
2240+
"without_tags",
2241+
&Circuit::without_tags,
2242+
clean_doc_string(R"DOC(
2243+
Returns a copy of the circuit with all tags removed.
2244+
2245+
Returns:
2246+
A `stim.Circuit` with the same instructions except all tags have been
2247+
removed.
2248+
2249+
Examples:
2250+
>>> import stim
2251+
>>> stim.Circuit('''
2252+
... X[test-tag] 0
2253+
... M[test-tag-2](0.125) 0
2254+
... ''').without_tags()
2255+
stim.Circuit('''
2256+
X 0
2257+
M(0.125) 0
2258+
''')
2259+
)DOC")
2260+
.data());
2261+
22392262
c.def(
22402263
"flattened",
22412264
&Circuit::flattened,

src/stim/circuit/circuit.test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,3 +1985,22 @@ TEST(circuit, insert_instruction) {
19851985
X 1
19861986
)CIRCUIT"));
19871987
}
1988+
1989+
TEST(circuit, without_tags) {
1990+
Circuit initial(R"CIRCUIT(
1991+
H[test-1] 0
1992+
REPEAT[test-2] 100 {
1993+
REPEAT[test-3] 100 {
1994+
M[test-4](0.125) 0
1995+
}
1996+
}
1997+
)CIRCUIT");
1998+
ASSERT_EQ(initial.without_tags(), Circuit(R"CIRCUIT(
1999+
H 0
2000+
REPEAT 100 {
2001+
REPEAT 100 {
2002+
M(0.125) 0
2003+
}
2004+
}
2005+
)CIRCUIT"));
2006+
}

src/stim/circuit/circuit_pybind_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,3 +2351,12 @@ def test_append_pauli_string():
23512351
c.append("MPP", object())
23522352
with pytest.raises(ValueError, match="Don't know how to target"):
23532353
c.append("MPP", object())
2354+
2355+
2356+
def test_without_tags():
2357+
circuit = stim.Circuit("""
2358+
H[tag] 5
2359+
""")
2360+
assert circuit.without_tags() == stim.Circuit("""
2361+
H 5
2362+
""")

src/stim/dem/detector_error_model.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,19 @@ std::map<uint64_t, std::vector<double>> DetectorErrorModel::get_detector_coordin
774774

775775
return out;
776776
}
777+
778+
DetectorErrorModel DetectorErrorModel::without_tags() const {
779+
DetectorErrorModel result;
780+
for (DemInstruction inst : instructions) {
781+
if (inst.type == DemInstructionType::DEM_REPEAT_BLOCK) {
782+
result.append_repeat_block(
783+
inst.repeat_block_rep_count(),
784+
inst.repeat_block_body(*this).without_tags(),
785+
"");
786+
} else {
787+
inst.tag = "";
788+
result.append_dem_instruction(inst);
789+
}
790+
}
791+
return result;
792+
}

src/stim/dem/detector_error_model.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ struct DetectorErrorModel {
6363
bool approx_equals(const DetectorErrorModel &other, double atol) const;
6464
std::string str() const;
6565

66+
DetectorErrorModel without_tags() const;
67+
6668
uint64_t total_detector_shift() const;
6769
uint64_t count_detectors() const;
6870
uint64_t count_observables() const;

0 commit comments

Comments
 (0)