Skip to content

[WIP] Add support for comparing DEMs up to instruction ordering#1042

Draft
abbrazi wants to merge 1 commit intoquantumlib:mainfrom
abbrazi:dem-equal-up-to-instruction-ordering
Draft

[WIP] Add support for comparing DEMs up to instruction ordering#1042
abbrazi wants to merge 1 commit intoquantumlib:mainfrom
abbrazi:dem-equal-up-to-instruction-ordering

Conversation

@abbrazi
Copy link
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 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.

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
`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.

Signed-off-by: Abbas Bracken Ziad <abbas.ziad25@imperial.ac.uk>
@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
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
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
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