<jupyter_start><jupyter_text>Bernstein-Vazirani algorithm Example of the Bernstein-Vazirani algorithm without entanglement. It takes in a hidden integer which can be revealed by querying the oracle. For example:Secret number = 101001Taking the dot product of the secret number tells you that there are three 1's but not their location. Classical algorithms work out the position of the secret number by guessing each configuration (eg. 000001, 000010..) until the correct one is found.The quantum algorithm is able to get the secret number in one go.The algorithm assumes that someone has handed you a black box that performs a function however you cannot look inside the oracle to see what it does. For more detail see: https://www.youtube.com/watch?v=sqJIpHYl7oo<jupyter_code>from qiskit import QuantumCircuit, IBMQ, Aer
# tool to plot output histogram of states
from qiskit.visualization import plot_histogram
# used to look at progress of job (when sent to real device)
from qiskit.tools.monitor import job_monitor
from qiskit.providers.ibmq import least_busy
secret_number = '1010'
len_secret_no = len(secret_number)

# creating the quantum circuit
# QuantumCircuit(no of quantum bits, no of classical bits)
circuit = QuantumCircuit(len_secret_no + 1, len_secret_no)
circuit.draw(output='mpl')
circuit.x(len_secret_no)
# adding a barrier to the circuit 
# just to divide sections, for visualisation purposes
circuit.barrier()

# applying a Hadamard gate to each qubit
circuit.h(range(len_secret_no + 1))
circuit.barrier()

# for each 1 in the secret number a CNOT gate is added to 
# the respective qubit
for indx, value in enumerate(reversed(secret_number)):
    if value == '1':
        circuit.cx(indx, len_secret_no)
circuit.barrier()

# applying a Hadamard gate to each qubit
circuit.h(range(len_secret_no + 1))
circuit.barrier()

# adding measurement gates to the end of each qubit
circuit.measure(range(len_secret_no), range(len_secret_no))
# drawing the circuit
circuit.draw(output='mpl')
# running the circuit on the quantum simulator
simulator = Aer.get_backend('qasm_simulator')
result = execute(circuit, backend=simulator, shots=1).result()
# plotting histogram of results
plot_histogram(result.get_counts(circuit))
# running the circuit on the real quantum device
backend = least_busy(IBMQ.backends(simulator=False))
compiled = transpile(circuit, backend=backend, optimization_level=1)
job = execute(compiled, backend=backend, shots=2048)
job_monitor(job)
results = job.result()
answer = results.get_counts(circuit)
plot_histogram(answer)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07385924207269914, 'fraction_numerical': 0.013534416086620264, 'mean_word_length': 5.9171122994652405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '316767', 'license': 'MIT', 'n_tokens_mistral': 807, 'n_tokens_neox': 732, 'n_words': 288, 'path': 'images/intro-qc/.ipynb_checkpoints/Bernstein-Vazirani-checkpoint.ipynb', 'repo_name': 'desireevl/desireevl.github.io', 'size': '2586'}