liveBook · Manning

N-queens problem

As explained in Quantum Programming in Depth

The N queens puzzle is the problem of placing N chess queens on an N-by-N board so that no two queens attack each other.

The N-queens problem asks for placements of N queens on an N × N chessboard such that no two attack each other. A valid placement therefore has no shared row, column, or diagonal. For four queens, there are two solutions, related by reflection; larger boards have more solutions and a harder search task. section 8.0

As a constraint-satisfaction search problem, it is defined by properties of acceptable placements rather than by a precomputed list of answers. A Grover-search solution must choose an input representation, prepare an equal superposition over its candidate inputs, and implement a marking oracle that recognizes valid placements. section 8.0

Three representations expose different tradeoffs:

  • A naive board encoding uses one bit per cell, requiring N² bits. Its oracle must reject every attacking pair of cells in a common row, column, or diagonal; reversible checking can require extra workspace for intermediate constraints. section 8.1
  • A restricted board encoding still uses one bit per cell, but prepares only states with exactly one queen in each row. The search space becomes a tensor product of row-wise W states, removing row-conflict checks from the oracle. section 8.2.1
  • A compact row encoding stores each queen’s column index using ⌈log₂ N⌉ bits per row. It retains Nᴺ candidates with one queen per row while reducing the input register from N² to N⌈log₂ N⌉ bits; column and diagonal conflicts are checked between row pairs. section 8.3.1

Four queens

On a 4 × 4 board, a placement is valid only when every row and column contains one queen and no diagonal contains two. A cell-bit picture makes invalid cases visible: one can fail through a repeated column, another through a repeated diagonal. section 8.0

Figure 8.7 The new input encoding relies on the fact that only inputs in which each row has exactly one queen can be solutions to the puzzle. We represent each row of the board as just one integer, the column index of the queen in that row.
figure

The row-index representation makes the key reduction concrete: each row is stored as one column number rather than as N separate cell bits. section 8.3.1

Encoding choices affect practical quantum cost. For N = 10, resource estimates compare the cell-bit and column-index approaches over the same Nᴺ search space; the compact encoding uses fewer estimated physical qubits and has a shorter estimated runtime. The comparison also cautions that these quantum approaches fare unfavorably against the classical solutions considered. section 9.5

How Quantum Programming in Depth covers it

How deep it goes
Derives three encodings, their search spaces and oracle checks, then compares resource estimates for a 10-queen instance.
Examples
Uses valid and invalid 4 × 4 placements and evaluates alternative quantum encodings for 10 queens.
What you can do after
Evaluate encoding tradeoffs by relating constraint structure to oracle complexity, register size, and estimated quantum resources.
How it connects
Connects Grover’s algorithm, marking oracles, reversible computation, search spaces, W states, and resource estimation.

Also explained in

Compare how 2 books explain n-queens problem →

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.