|q⟩ Bad Qubits

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

Quantum Parallelism

A classical computer that wants to know f(0)f(0) and f(1)f(1) for some function ff must call ff twice. Quantum mechanics offers a striking shortcut: by preparing the input in a superposition of every possible value and calling ff only once, both results are encoded in the output state simultaneously. This is the phenomenon called quantum parallelism.

How it works

Set up two registers. The input register is a single qubit starting in 0|0\rangle; the output register is also a single qubit starting in 0|0\rangle. Apply a Hadamard to the input:

H0=12(0+1).H|0\rangle = \frac{1}{\sqrt{2}}\big(|0\rangle + |1\rangle\big).

The combined state of both registers is now

12(0+1)0=12(00+10),\frac{1}{\sqrt{2}}\big(|0\rangle + |1\rangle\big) \otimes |0\rangle = \frac{1}{\sqrt{2}}\big(|00\rangle + |10\rangle\big),

where the first index labels the input and the second labels the output (qubit 0 is the most significant bit in this platform's convention).

A query oracle UfU_f acts on the combined state according to

Ufxy=xyf(x),U_f\,|x\rangle|y\rangle = |x\rangle|y \oplus f(x)\rangle,

where \oplus is addition modulo 2. With y=0y = 0 this simplifies to Ufx0=xf(x)U_f|x\rangle|0\rangle = |x\rangle|f(x)\rangle, so the output register simply stores f(x)f(x).

Applying UfU_f to the superposed state gives

Uf12(00+10)=12(0f(0)+1f(1)).U_f\,\frac{1}{\sqrt{2}}\big(|00\rangle + |10\rangle\big) = \frac{1}{\sqrt{2}}\big(|0\rangle|f(0)\rangle + |1\rangle|f(1)\rangle\big).

Both values f(0)f(0) and f(1)f(1) appear in the state at the same time — the oracle was called only once.

The f(x) = x oracle as a CNOT

For the function f(x)=xf(x) = x the oracle maps x0xx|x\rangle|0\rangle \to |x\rangle|x\rangle. That is exactly the action of a CNOT gate with qubit 0 as control and qubit 1 as target:

CNOT:0000,1011.\text{CNOT}: |00\rangle \to |00\rangle, \quad |10\rangle \to |11\rangle.

After HH on qubit 0 followed by CNOT, the state is

12(00+11),\frac{1}{\sqrt{2}}\big(|00\rangle + |11\rangle\big),

a maximally entangled Bell state. The oracle has evaluated ff on both inputs in one step.

Try it

Build the circuit that places the input register in superposition and then evaluates f(x)=xf(x) = x via a CNOT oracle. The grader checks the full state vector.

Run your code to see the quantum state.

After running, inspect the amplitudes: you should see 120.707\tfrac{1}{\sqrt{2}} \approx 0.707 on 00|00\rangle and 11|11\rangle, and zero everywhere else.

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