Skip to content

Add support for comparing DEMs up to instruction ordering#1042

Open
abbrazi wants to merge 5 commits into
quantumlib:mainfrom
abbrazi:dem-equal-up-to-instruction-ordering
Open

Add support for comparing DEMs up to instruction ordering#1042
abbrazi wants to merge 5 commits into
quantumlib:mainfrom
abbrazi:dem-equal-up-to-instruction-ordering

Conversation

@abbrazi
Copy link
Copy Markdown
Contributor

@abbrazi abbrazi commented Feb 26, 2026

The current == overload of the DetectorErrorModel only returns true if the DEMs have the same instruction ordering. It is sometimes useful to be able to compare two DEMs for equality when this is not the case, so I implemented an is_equal_up_to_instruction_ordering member function. I opted not to change the == operator because (a) checking for equality up to instruction ordering requires sorting so turns an O(n) op into an O(n log n) op; and (b) it's possible some of the backend simulation code depends on the ordering constraint.

@abbrazi abbrazi force-pushed the dem-equal-up-to-instruction-ordering branch from 65e7973 to f505004 Compare February 26, 2026 19:01
@Strilanc
Copy link
Copy Markdown
Collaborator

Two major problems with this method as currently implemented.

  1. Instruction order sometimes matters. In particular, shift_detector instructions change the meaning of later error instructions. If you simply sort the instructions without account for this, you will lose this dependence and break the dem.

  2. Not efficient when loops are present. It seems to me that the intent of the method is to basically be graph isomorphism, but that would imply these three dems should all be equal:

error(0.1) D0 D1
shift_detectors 1
error(0.1) D0 D1
shift_detectors 1
error(0.1) D0 D1

and

error(0.1) D0 D1
repeat 2 {
    shift_detectors 1
    error(0.1) D0 D1
}

and

repeat 2 {
    error(0.1) D0 D1
    shift_detectors 1
}
error(0.1) D0 D1

a simple solution would be to first flatten the dem, but that would make the method take far too long when applied to circuits with high repetition counts. There are a few exceptions, but basically all methods in stim are efficient when applied to high-loop-count circuits. This method would also need to meet that bar.

@abbrazi
Copy link
Copy Markdown
Contributor Author

abbrazi commented Feb 26, 2026

Yeah I guess this is a flat world theory :D
It's more interesting than I thought, let me think about it.

@abbrazi abbrazi marked this pull request as draft February 27, 2026 10:14
@abbrazi abbrazi changed the title Add support for comparing DEMs up to instruction ordering [WIP] Add support for comparing DEMs up to instruction ordering Feb 27, 2026
@abbrazi abbrazi force-pushed the dem-equal-up-to-instruction-ordering branch from 1fe660f to a4d2230 Compare May 7, 2026 11:19
@abbrazi abbrazi force-pushed the dem-equal-up-to-instruction-ordering branch from a4d2230 to 4a0ca5d Compare May 7, 2026 11:20
Comment on lines +859 to +862
// check reps
if (structural_op_a.repeat_block_rep_count() != structural_op_b.repeat_block_rep_count()) {
return false;
}
Copy link
Copy Markdown
Contributor Author

@abbrazi abbrazi May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, you might ask why I don't simply check op_a != op_b before handling the dedicated shift/repeat cases. For repeats, that would result in unexpected failures, i.e., where the the DEMs have the same repeat blocks but they are stored in different orders in the backing blocks arrays. That seems like an implementation detail that should not result in this member function reporting inequality.

@abbrazi abbrazi marked this pull request as ready for review May 7, 2026 11:32
@abbrazi abbrazi changed the title [WIP] Add support for comparing DEMs up to instruction ordering Add support for comparing DEMs up to instruction ordering May 7, 2026
@abbrazi
Copy link
Copy Markdown
Contributor Author

abbrazi commented May 7, 2026

Have another look at this PR when you get the chance. I updated the ordering-insensitive comparison to handle shifts/repeats efficiently by treating them as structural barriers (must match exactly) and comparing the instruction segments between barriers order-insensitively, so large repeats don’t need to be unrolled.

I still think the most general isomorphic check would be a semantic flatten + canonicalise + sort + compare (where shifts/repeats only matter via their effects), but that’s expensive.

DEM construction seems to be becoming an active area of research, including tools that try to automatically infer detector/logical observable definitions. Utilities like this are useful for validating those approaches against Stim as a golden reference, using an equality notion that’s less strict than == while still being equivalent from the perspective of decoders.

Feel free to reject the PR if you feel these utilities should live elsewhere. I will anyway keep it on my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants