Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Master the historic algorithm that first proved an exponential separation between classical and quantum computing.
Interactive 3D state vector exploration, unitary transformation gates, and Born rule collapse for Topic 7.3
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Input query bitstring | Runs across all possible inputs simultaneously | |
| Output measurement state | Measured bitstring after applying final Hadamard interference gates | |
| Bitwise dot product | Modulo-2 sum of matching bits () | |
| All-zero outcome probability | 1.0 (100%) if f is Constant; exactly 0.0 (0%) if f is Balanced |
Running Deutsch-Jozsa on a 2-qubit balanced function with 4 inputs where f(00)=0, f(01)=0, f(10)=1, f(11)=1:
Result: Because P() = 0, we measure a non-zero state and conclude the function is Balanced with 100% certainty in ONE single query!
Initialize n input qubits to |0> and 1 target ancilla to |1>.
Apply Hadamard to all n+1 qubits, setting target to |->.
Pass through U_f; phase kickback writes (-1)^f(x) onto input superposition.
Apply Hadamard to all n input qubits. If measurement is 00...0, output CONSTANT; otherwise output BALANCED.
Problem: Determining global structural symmetries of massive cryptographic black boxes.
Solution: Deutsch-Jozsa proves exponential speedup for global property testing over local evaluation.
Interactive activity component
Construct the full Deutsch-Jozsa algorithm for a 3-qubit input register and determine whether a function is constant or balanced in 1 query.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install qiskit qiskit-aer1 from qiskit import QuantumCircuit 2 from qiskit_aer import AerSimulator
from qiskit import QuantumCircuit β Circuit module
from qiskit_aer import AerSimulator β Simulator module
1 # 3 input qubits + 1 ancilla qubit = 4 qubits total 2 n = 3 3 dj_circuit = QuantumCircuit(n + 1, n) 4 5 # Step 1: Initialize ancilla to |1> and apply Hadamards to all qubits 6 dj_circuit.x(n) # Ancilla to |1> 7 dj_circuit.h(range(n + 1)) # All qubits to superposition 8 dj_circuit.barrier() 9 10 # Step 2: Balanced Oracle (f(x) = x0 XOR x1 XOR x2) 11 for q in range(n): 12 dj_circuit.cx(q, n) # XOR into ancilla 13 dj_circuit.barrier() 14 15 # Step 3: Interference on input qubits 16 dj_circuit.h(range(n)) 17 dj_circuit.measure(range(n), range(n)) 18 print('Deutsch-Jozsa Circuit Diagram:') 19 print(dj_circuit.draw('text')) 20 21 # Simulate with AerSimulator 22 sim = AerSimulator() 23 counts = sim.run(dj_circuit, shots=1000).result().get_counts() 24 print('\nMeasurement Result (1000 shots):', counts) 25 measured_bitstring = list(counts.keys())[0] 26 if measured_bitstring == '0' * n: 27 print('Verdict: Function is CONSTANT (measured 000)') 28 else: 29 print(f'Verdict: Function is BALANCED (measured {measured_bitstring} != 000) in 1 query!')
# 3 input qubits + 1 ancilla qubit = 4 qubits total
Setup
n = 3
Input size
dj_circuit = QuantumCircuit(n + 1, n)
Circuit with 4 qubits and 3 classical bits
# Step 1: Initialize ancilla to |1> and apply Hadamards to all qubits
Init
dj_circuit.x(n) # Ancilla to |1>
Ancilla flip
dj_circuit.h(range(n + 1)) # All qubits to superposition
H on all wires
dj_circuit.barrier()
Visual barrier
# Step 2: Balanced Oracle (f(x) = x0 XOR x1 XOR x2)
Balanced oracle
for q in range(n):
Attach CNOTs
dj_circuit.cx(q, n) # XOR into ancilla
CNOT from each input
dj_circuit.barrier()
Visual barrier
# Step 3: Interference on input qubits
Interference
dj_circuit.h(range(n))
H on input register
dj_circuit.measure(range(n), range(n))
Measure input qubits
print('Deutsch-Jozsa Circuit Diagram:')
Diagram label
print(dj_circuit.draw('text'))
Print circuit
# Simulate with AerSimulator
Simulation
sim = AerSimulator()
Backend
counts = sim.run(dj_circuit, shots=1000).result().get_counts()
Run 1000 shots
print('\nMeasurement Result (1000 shots):', counts)
Print counts
measured_bitstring = list(counts.keys())[0]
Extract measured bitstring
if measured_bitstring == '0' * n:
Check constant
print('Verdict: Function is CONSTANT (measured 000)')
Constant
else:
Check balanced
print(f'Verdict: Function is BALANCED (measured {measured_bitstring} != 000) in 1 query!')
Balanced
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Implementing the Full Deutsch-Jozsa Algorithm in Qiskit 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install qiskit qiskit-aer 8 9 # Step 2: Import libraries 10 from qiskit import QuantumCircuit 11 from qiskit_aer import AerSimulator 12 13 # Step 3: Run Demonstration 14 # 3 input qubits + 1 ancilla qubit = 4 qubits total 15 n = 3 16 dj_circuit = QuantumCircuit(n + 1, n) 17 18 # Step 1: Initialize ancilla to |1> and apply Hadamards to all qubits 19 dj_circuit.x(n) # Ancilla to |1> 20 dj_circuit.h(range(n + 1)) # All qubits to superposition 21 dj_circuit.barrier() 22 23 # Step 2: Balanced Oracle (f(x) = x0 XOR x1 XOR x2) 24 for q in range(n): 25 dj_circuit.cx(q, n) # XOR into ancilla 26 dj_circuit.barrier() 27 28 # Step 3: Interference on input qubits 29 dj_circuit.h(range(n)) 30 dj_circuit.measure(range(n), range(n)) 31 print('Deutsch-Jozsa Circuit Diagram:') 32 print(dj_circuit.draw('text')) 33 34 # Simulate with AerSimulator 35 sim = AerSimulator() 36 counts = sim.run(dj_circuit, shots=1000).result().get_counts() 37 print('\nMeasurement Result (1000 shots):', counts) 38 measured_bitstring = list(counts.keys())[0] 39 if measured_bitstring == '0' * n: 40 print('Verdict: Function is CONSTANT (measured 000)') 41 else: 42 print(f'Verdict: Function is BALANCED (measured {measured_bitstring} != 000) in 1 query!')
Place 3 input qubits into |+> and ancilla qubit into |->.
Apply CNOT from each input qubit to the ancilla, kicking back negative phases.
Destructive interference cancels '000', resulting in 100% detection of balanced function in 1 step vs 2^(n-1)+1 classical queries.
Deutsch-Jozsa circuit with 3 input qubits and balanced parity oracle.
Measurement counts: {'111': 1000}
Verdict: Function is BALANCED in 1 query (Classically requires 5 queries).Classically, proving a 3-input function is balanced requires testing 5 inputs in the worst case (2^(n-1)+1). Deutsch-Jozsa solves it in exactly ONE quantum query with zero error, establishing definitive quantum speedup.
Oracle, diffusion operator, interference, and quadratic search speedup.
Test your understanding with these questions. Remember, this is for learning, not examination!