`are_programs_equivalent` ignores `naturally_flipped` check parity under `(ProgEq 3)`

Open Beginner friendly
#300 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start in deq/deq/spec/program_equivalence.py at _canonical_check_linear_map, around the GF(2) solve loop. Compare each canonical check's naturally_flipped flag with the XOR of the peer checks selected by solution. The reproducer builds two one-check libraries and should return Violations with the (ProgEq 3) message. Run the relevant deq unit tests plus that snippet; done means polarity mismatches are rejected while equivalent programs still map.

Written by the indexing model from the issue text.

Description

Description

are_programs_equivalent checks whether each canonical check's measurement-index vector lies in the GF(2) span of the peer library's checks, and verifies error probabilities, but _canonical_check_linear_map never checks the naturally_flipped polarity field. As a result, a check with naturally_flipped=False and an otherwise identical check with naturally_flipped=True are falsely certified as equivalent (EquivalenceMap).

Steps to Reproduce

Run on main (60dd1134c53d1a7dc792516014fa1a021aa10a61), Python 3.12.14:

import deq.proto.deq_bin_pb2 as pb
import deq.proto.util_pb2 as util_pb
from deq.spec.program_equivalence import are_programs_equivalent
from deq.spec.violations import Violations

def make_lib(nf: bool) -> pb.Library:
    return pb.Library(
        port_types=[pb.PortType(ptype=1, n=1, observables=[pb.PortType.Observable()])],
        gadget_types=[pb.GadgetType(
            gtype=1, measurements=[pb.GadgetType.Measurement()], outputs=[pb.GadgetType.Port(ptype=1)],
            correction_propagation=util_pb.BitMatrix(rows=1, cols=1), physical_correction=util_pb.BitMatrix(rows=1, cols=1),
        )],
        check_model_types=[pb.CheckModelType(ctype=1, gtype=1, checks=[pb.CheckModelType.Check(
            naturally_flipped=nf, measurements=[pb.CheckModelType.RemoteMeasurement(measurement_index=0)],
        )])],
        error_model_types=[pb.ErrorModelType(etype=1, ctype=1, errors=[pb.ErrorModelType.Error(
            probability=0.05, checks=[pb.ErrorModelType.RemoteCheck(check_index=0)],
        )])],
        program=[pb.Instruction(gadget=pb.Gadget(gtype=1)), pb.Instruction(check_model=pb.CheckModel(ctype=1, gid=1)), pb.Instruction(error_model=pb.ErrorModel(etype=1, cid=1))],
    )

res = are_programs_equivalent(make_lib(False), make_lib(True))
assert isinstance(res, Violations), f"Expected Violations for opposite naturally_flipped, got {res}"
print("OK")

Actual Behavior

AssertionError: Expected Violations for opposite naturally_flipped, got EquivalenceMap(check_map=LinearMap(atob={CheckIndex(cid=1, check_index=0): {CheckIndex(cid=1, check_index=0)}}, btoa={CheckIndex(cid=1, check_index=0): {CheckIndex(cid=1, check_index=0)}}))

Expected Behavior

are_programs_equivalent should return Violations("(ProgEq 3) naturally_flipped mismatch ...") whenever a check's naturally_flipped parity differs from the XOR parity of the checks it maps to.

Root Cause & Suggested Minimal Fix

  • Location: deq/deq/spec/program_equivalence.py#L165-L202
  • Details: _canonical_check_linear_map solves the GF(2) linear system linear_solve(peer_matrix, b) for measurement incidence vectors without checking this_nf == functools.reduce(operator.xor, peer_nf).
Suggested minimal diff (verified against `deq` unit tests)
diff --git a/deq/deq/spec/program_equivalence.py b/deq/deq/spec/program_equivalence.py
index 3c67f64..61fb436 100644
--- a/deq/deq/spec/program_equivalence.py
+++ b/deq/deq/spec/program_equivalence.py
@@ -165,9 +165,9 @@ def _canonical_check_linear_map(
 
     # check if all the checks can be formed by linear combination
     check_map: LinearMap[CheckIndex] = LinearMap()
-    for this_id, this_matrix, peer_id, peer_matrix, mapper in (
-        (1, check_matrix_1, 2, check_matrix_2, check_map.atob),
-        (2, check_matrix_2, 1, check_matrix_1, check_map.btoa),
+    for this_id, this_canonical, this_matrix, peer_id, peer_canonical, peer_matrix, mapper in (
+        (1, canonical1, check_matrix_1, 2, canonical2, check_matrix_2, check_map.atob),
+        (2, canonical2, check_matrix_2, 1, canonical1, check_matrix_1, check_map.btoa),
     ):
         for check_index in range(this_matrix.column_count):
             b = BitVector.zeros(this_matrix.row_count)
@@ -180,11 +180,23 @@ def _canonical_check_linear_map(
                     + f"check {check_index} in canonical{this_id} "
                     + f"cannot be formed by linear combination of checks in canonical{peer_id}"
                 )
-            mapper[CheckIndex(cid=1, check_index=check_index)] = {
+            mapped_checks = {
                 CheckIndex(cid=1, check_index=ci)
                 for ci in range(peer_matrix.column_count)
                 if solution[ci]
             }
+            this_nf = bool(this_canonical.check_model_type.checks[check_index].naturally_flipped)
+            mapped_nf = False
+            for m_ci in mapped_checks:
+                mapped_nf ^= bool(
+                    peer_canonical.check_model_type.checks[m_ci.check_index].naturally_flipped
+                )
+            if this_nf != mapped_nf:
+                return Violations(
+                    f"(ProgEq 3) naturally_flipped mismatch for check {check_index} "
+                    + f"in canonical{this_id}: {this_nf} != {mapped_nf}"
+                )
+            mapper[CheckIndex(cid=1, check_index=check_index)] = mapped_checks
 
     return check_map
Dominant language
Rust
Stars
69
Forks
25
Avg merge
2d 22h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/qdk-ec

All issues in microsoft/qdk-ec

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.