Search⌘ K

The Search Algorithm

Explore the design and application of a quantum search algorithm utilizing Hadamard gates, oracles, and amplifiers. Understand how repeating amplification enhances the probability of identifying searched states in quantum circuits. This lesson helps you build flexible quantum circuits, scale them for multiple qubits, and grasp the computational demands on classical systems.

The search algorithm

Thanks to our preparation, our main algorithm is relatively small. We define a QuantumCircuit with two QuantumRegisters in line 1. We apply Hadamard gates on all qubits inside the q_pass register in line 4. Then, we apply the oracle in line 7 and the amplifier in line 10 by appending them to our main circuit. The append function takes two parameters. The first is the gate that we create in our convenience functions. The second is a list of qubits that we want to apply to this gate to. The oracle uses all qubits. The amplifier only uses the passenger qubits.

Javascript (babel-node)
qc = QuantumCircuit(q_pass, q_rules)
# put passenger qubits into superposition
qc.h(q_pass)
# Apply the oracle
qc.append(oracle(current_passenger, group, q_pass, q_rules), [*q_pass, *q_rules])
# Apply the amplifier
qc.append(amplifier(current_passenger, q_pass), q_pass)
print(qc.draw())

If you’re curious to see how it works, let’s run this circuit.

Result of the search algorithm

Javascript (babel-node)
results = execute(qc,Aer.get_backend('statevector_simulator')).result()
plot_histogram(results.get_counts())

Our algorithm measures state 0010000100 with the highest probability of 0.7810.781. This is the state representing the searched relative of Mr. Rev. Ernest Courtenay Carter. It is Mrs. Ernest Courtenay (Lilian Hughes) Carter.

Even though the result is quite evident, there’s one thing missing. At the end of the lesson Using Grover’s Algorithm, we mentioned we need to repeat the amplification more ...