<jupyter_start><jupyter_text><jupyter_code>!pip install -q qiskit
from qiskit import *
from qiskit.visualization import plot_histogram

%matplotlib inline
a = input("""Please choose a coefficient (in bits) for the function f(x) = ax that maps to {0,1}.
                It is assumed that any input has the same number of bits as a.
                The quantum circuit will decide what it is in one query.
                """)
num_bits = len(a)

qr0 = QuantumRegister(num_bits, "q0")
qr1 = QuantumRegister(1, "q1")
cr = ClassicalRegister(num_bits)
circuit = QuantumCircuit(qr0, qr1, cr)

circuit.x(qr1)
circuit.h(qr0)
circuit.h(qr1)
circuit.barrier()

for i in range(num_bits):
    bit = int(a[i])
    assert(bit == 1 or bit == 0)
    if bit == 1:
        circuit.cx(qr0[num_bits - i - 1], qr1)

circuit.barrier()
circuit.h(qr0)
circuit.measure(qr0, cr)
circuit.draw(output='mpl')
simulator = Aer.get_backend('qasm_simulator')
shots = 100
job = execute(circuit, simulator, shots=shots)
result = job.result()
counts = result.get_counts(circuit)
plot_histogram(counts)
max_counts_bits = ""
max_counts = 0
for bits, c in counts.items():
    if c > max_counts:
        max_counts = counts
        max_counts_bits = bits
        
print("Your chosen coefficient was " + max_counts_bits)<jupyter_output>Your chosen coefficient was 101011
{'fraction_non_alphanumeric': 0.11286681715575621, 'fraction_numerical': 0.022573363431151242, 'mean_word_length': 3.7163120567375887, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '507146', 'license': 'MIT', 'n_tokens_mistral': 508, 'n_tokens_neox': 460, 'n_words': 132, 'path': 'Bernstein_Vazirani_Problem.ipynb', 'repo_name': 'ivanightingale/cirquits', 'size': '1329'}