The Search Algorithm

Learn the concept of search algorithm with its implementation.

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.

Press + to interact
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

Press + to interact
results = execute(qc,Aer.get_backend('statevector_simulator')).result()
plot_histogram(results.get_counts())

Our algorithm measures state 0010000100 with the highest probability of ...