liveBook · Manning

Parity trick

As explained in Quantum Programming in Depth

The parity trick checks whether counts satisfy a constraint by computing their values modulo two instead of counting them exactly.

The parity trick tests a counting constraint modulo two rather than computing each exact count. In the N queens oracle, odd column parity identifies the required one-queen columns. section 8.2.1

Use the trick when the surrounding constraints make parity sufficient:

  • Assume every row already contains exactly one queen, then check each column for odd parity; any invalid placement has an empty column with even parity. section 8.2.1
  • Skip one column because an invalid column distribution necessarily leaves enough even-parity empty columns to detect the violation. section 8.2.1
  • Do not substitute parity for diagonal counts: zero or one queen on a diagonal may still be valid. section 8.2.1

N queens column check

For each column, compute the XOR of its queen-presence bits. Accept the column when the result is one; an even result identifies an empty column and marks the placement invalid. section 8.2.1

Figure 8.5 Check that each column has exactly one queen, knowing that each row has exactly one queen. We can count the parity of the number of queens in each column and check that it’s odd. Any placement for which this constraint is not satisfied will have at least one empty column with even parity.
figure

A circuit diagram shows how odd column parity replaces exact counting when every row contains one queen. section 8.2.1

How Quantum Programming in Depth covers it

How deep it goes
Derives the parity condition and its skipped-column optimization, but limits it to columns and not diagonal constraints.
Examples
Uses queen counts in columns and contrasts them with diagonal pair checks.
What it assumes
Assumes each row contains exactly one queen and teaches why that invariant makes odd column parity sufficient.
What you can do after
Apply it to replace selected exact-count checks with parity and XOR logic in a constraint-checking oracle.
How it connects
Connects parity to XOR, quantum oracles, and the N queens puzzle.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quantum Programming in Depth. The section links point to the parts of the book it draws on.