|q⟩ Bad Qubits

beginner · Programming · First Quantum Algorithms: Deutsch & Deutsch–Jozsa

Constant vs Balanced Functions

The Deutsch–Jozsa algorithm is one of the earliest demonstrations that a quantum computer can answer a specific question with exponentially fewer queries than any classical algorithm. Before we can appreciate what the algorithm does, we need to be precise about the kind of function it reasons about.

Boolean functions with a single-bit output

A Boolean function on nn bits is any map f:{0,1}n{0,1}f : \{0,1\}^n \to \{0,1\}. It takes an nn-bit string as input and outputs a single bit. The Deutsch–Jozsa problem restricts attention to functions with a particular structure, so we need two definitions.

Constant function. A function ff is constant if it returns the same output for every input:

f(x)=0 for all x{0,1}n,orf(x)=1 for all x{0,1}n.f(x) = 0 \text{ for all } x \in \{0,1\}^n, \quad\text{or}\quad f(x) = 1 \text{ for all } x \in \{0,1\}^n.

There are exactly two constant functions on nn bits — the all-zeros function and the all-ones function.

Balanced function. A function ff is balanced if exactly half of all inputs map to 00 and the other half map to 11. Because there are 2n2^n possible inputs, a balanced function outputs 00 on exactly 2n12^{n-1} of them and 11 on the remaining 2n12^{n-1}.

Why this classification matters

Classically, determining whether an unknown ff is constant or balanced requires querying ff on at least 2n1+12^{n-1}+1 inputs in the worst case. You must see just over half of all outputs before you can be certain: if the first 2n12^{n-1} queries all return 00, the function could still be balanced (the other half might all be 11) or constant (all outputs are 00). Only the next query settles it. For large nn this is an exponential number of queries.

The Deutsch–Jozsa algorithm, by contrast, answers the question with a single quantum query regardless of nn. The separation is as stark as possible: one query versus exponentially many. This is possible because the algorithm uses a quantum-mechanical encoding of ff (called an oracle) that allows all 2n2^n inputs to be queried simultaneously through superposition, then uses interference to read out a global property — constant or balanced — in one shot.

Concrete small examples

For n=2n = 2 the four possible inputs are 0000, 0101, 1010, and 1111.

| Function | f(00)f(00) | f(01)f(01) | f(10)f(10) | f(11)f(11) | Type | |----------|---------|---------|---------|---------|------| | fAf_A | 0 | 0 | 0 | 0 | constant | | fBf_B | 1 | 1 | 1 | 1 | constant | | fCf_C | 0 | 0 | 1 | 1 | balanced | | fDf_D | 0 | 1 | 0 | 1 | balanced |

fCf_C and fDf_D each output 00 on exactly two of the four inputs and 11 on the other two, so both are balanced. The Deutsch–Jozsa algorithm can distinguish the top two rows from the bottom two with a single oracle call, no matter how large the table grows.

From classification to algorithm

The module that follows shows how to encode ff as a quantum oracle — a unitary gate UfU_f that computes ff reversibly — and then how to run the Deutsch–Jozsa circuit. The circuit's output is deterministically 000|00\cdots0\rangle when ff is constant and anything else when ff is balanced, giving a one-shot answer to a question that classically demands exponentially many queries.

Sign in on the full site to ask questions and join the discussion.