@@ -89,7 +89,9 @@ def get_component_obs_matching_undecomposed_obs(
8989
9090
9191def decompose_errors_using_detector_assignment (
92- dem : stim .DetectorErrorModel , detector_component_func : Callable [[int ], int ]
92+ dem : stim .DetectorErrorModel ,
93+ detector_component_func : Callable [[int ], int ],
94+ strip_undecomposable_errors : bool = False ,
9395) -> stim .DetectorErrorModel :
9496 """Decomposes errors in the detector error model `dem` based on an assignment of
9597 detectors to components by the function `detector_component_func`.
@@ -112,6 +114,9 @@ def decompose_errors_using_detector_assignment(
112114 detector_component_func : Callable[[int], int]
113115 A function that maps a detector id to its component. i.e. This could map
114116 a detector index to 0 if it is X-type or to 1 if it is Z-type.
117+ strip_undecomposable_errors : bool
118+ If True, errors that cannot be decomposed due to a missing component error
119+ will be stripped from the output DEM instead of raising a ValueError.
115120
116121 Returns
117122 -------
@@ -150,21 +155,29 @@ def decompose_errors_using_detector_assignment(
150155 dets_by_component = []
151156 obs_options_by_component = []
152157
158+ is_undecomposable = False
153159 for c in unique_components :
154160 component_dets = tuple (
155161 sorted (d for d in detectors if det_components [d ] == c )
156162 )
157163 if component_dets not in single_component_dets_to_obs :
158- raise ValueError (
159- f"The dem error `{ instruction } ` needs to be decomposed into components, however "
160- f"the component with detectors { component_dets } is not present as its own error "
161- "in the dem."
162- )
164+ if strip_undecomposable_errors :
165+ is_undecomposable = True
166+ break
167+ else :
168+ raise ValueError (
169+ f"The dem error `{ instruction } ` needs to be decomposed into components, however "
170+ f"the component with detectors { component_dets } is not present as its own error "
171+ "in the dem."
172+ )
163173 dets_by_component .append (component_dets )
164174 obs_options_by_component .append (
165175 single_component_dets_to_obs [component_dets ]
166176 )
167177
178+ if is_undecomposable :
179+ continue
180+
168181 # Assign observables to each component, such that they are consistent with the
169182 # observables of the undecomposed error
170183 consistent_obs_by_component = get_component_obs_matching_undecomposed_obs (
@@ -202,7 +215,9 @@ def decompose_errors_using_detector_assignment(
202215
203216
204217def decompose_errors_using_detector_coordinate_assignment (
205- dem : stim .DetectorErrorModel , coord_to_component_func : Callable [[list [float ]], int ]
218+ dem : stim .DetectorErrorModel ,
219+ coord_to_component_func : Callable [[list [float ]], int ],
220+ strip_undecomposable_errors : bool = False ,
206221) -> stim .DetectorErrorModel :
207222 """Decomposes errors in the detector error model `dem` based on an assignment of
208223 detectors to components using a function of the detector coordinates.
@@ -225,6 +240,9 @@ def decompose_errors_using_detector_coordinate_assignment(
225240 A function that coordinates of a detector to an integer corresponding to
226241 the index of a component, to be used for the decomposition. The coordinates
227242 are provided as a list of floats.
243+ strip_undecomposable_errors : bool
244+ If True, errors that cannot be decomposed due to a missing component error
245+ will be stripped from the output DEM instead of raising a ValueError.
228246
229247 Returns
230248 -------
@@ -237,7 +255,9 @@ def component_using_coords(detector_id: int) -> int:
237255 return coord_to_component_func (detector_coords [detector_id ])
238256
239257 return decompose_errors_using_detector_assignment (
240- dem = dem , detector_component_func = component_using_coords
258+ dem = dem ,
259+ detector_component_func = component_using_coords ,
260+ strip_undecomposable_errors = strip_undecomposable_errors ,
241261 )
242262
243263
@@ -252,6 +272,7 @@ def detector_coord_to_basis_for_stim_surface_code_convention(coord: tuple[int])
252272
253273def decompose_errors_using_last_coordinate_index (
254274 dem : stim .DetectorErrorModel ,
275+ strip_undecomposable_errors : bool = False ,
255276) -> stim .DetectorErrorModel :
256277 """Decomposes errors in the detector error model `dem` based on an assignment of
257278 detectors to components by the last element of each detector coordinate.
@@ -269,6 +290,9 @@ def decompose_errors_using_last_coordinate_index(
269290 ----------
270291 dem : stim.DetectorErrorModel
271292 The detector error model to decompose.
293+ strip_undecomposable_errors : bool
294+ If True, errors that cannot be decomposed due to a missing component error
295+ will be stripped from the output DEM instead of raising a ValueError.
272296
273297 Returns
274298 -------
@@ -281,12 +305,15 @@ def last_coordinate_component(detector_id: int) -> int:
281305 return detector_coords [detector_id ][- 1 ]
282306
283307 return decompose_errors_using_detector_assignment (
284- dem = dem , detector_component_func = last_coordinate_component
308+ dem = dem ,
309+ detector_component_func = last_coordinate_component ,
310+ strip_undecomposable_errors = strip_undecomposable_errors ,
285311 )
286312
287313
288314def decompose_errors_for_stim_surface_code_coords (
289315 dem : stim .DetectorErrorModel ,
316+ strip_undecomposable_errors : bool = False ,
290317) -> stim .DetectorErrorModel :
291318 """Decomposes the errors in the dem, such that each component
292319 of a decomposed error only triggers detectors of one basis (X or Z)
@@ -302,6 +329,9 @@ def decompose_errors_for_stim_surface_code_coords(
302329 ----------
303330 dem : stim.DetectorErrorModel
304331 The detector error model to decompose
332+ strip_undecomposable_errors : bool
333+ If True, errors that cannot be decomposed due to a missing component error
334+ will be stripped from the output DEM instead of raising a ValueError.
305335
306336 Returns
307337 -------
@@ -316,7 +346,9 @@ def stim_surface_code_det_component(detector_id: int) -> int:
316346 )
317347
318348 return decompose_errors_using_detector_assignment (
319- dem = dem , detector_component_func = stim_surface_code_det_component
349+ dem = dem ,
350+ detector_component_func = stim_surface_code_det_component ,
351+ strip_undecomposable_errors = strip_undecomposable_errors ,
320352 )
321353
322354
0 commit comments