quantumlib / Stim

A fast stabilizer circuit library.
Apache License 2.0
346 stars 102 forks source link

filter_coords refuses to filter out everything #829

Closed m-mcewen closed 1 month ago

m-mcewen commented 1 month ago

When an empty list is explicitly passed to circuit.diagram, rather than filter out out everything, the diagram filters out nothing. This behavior wasn't what I expected, and made debugging marginally harder. I was constructing filter coord lists based on conditions about the det regions, which happened to be empty for one In that case, the diagram was populated with all detecting regions, rather than showing that no detecting regions matched the criteria I used to construct the filter coords (which happened to be none of them, which is what I expected / wanted to be shown)

# MWE
circuit = stim.Circuit.generated("surface_code:rotated_memory_x", distance=5, rounds=5)
filter_out_everything = []
circuit.diagram('detslice', filter_coords=filter_out_everything)

image

Strilanc commented 1 month ago

This is unfortunately the intended behavior.

The coordinate filter is a prefix match. When you specify an empty list, you are doing the equivalent of saying "include everything whose coordinates start with the empty list". Same as str.startswith("") this returns true for all items. It's sortof a useless corner case, but it's forced by various consistency properties w.r.t. going from n+1 to n coordinates on the prefix.

m-mcewen commented 1 month ago

Ah I was using the ability to give it detector labels, i'd forgotten that filter_coords was originally a filter for the coords... Possibly a good reason to separate out filter_dem_targets given they're not prefix-y