<jupyter_start><jupyter_text>Berstein-Vazirani Algorithm This implementation of the Berstein-Vazirani model will deal with _n_-qubit black boxes and is specifically designed as a game. The user will input any binary string and the circuit implementing the algorithm will guess it using only 1 query.<jupyter_code>from qiskit import *
from qiskit.tools.visualization import plot_histogram
from qiskit import IBMQ
from qiskit.tools.monitor import job_monitor<jupyter_output><empty_output><jupyter_text>User Input Please type your number here :<jupyter_code>userNum = 7
binaryConvert = bin(userNum)[2:]
noOfBits = len(binaryConvert)
print(binaryConvert)<jupyter_output>111<jupyter_text>Quantum Variant Implementation  Defining the Circuit<jupyter_code>qReg = QuantumRegister(noOfBits + 1, name = 'q')
cReg = ClassicalRegister(noOfBits, name = 'cr')
qCirc = QuantumCircuit(qReg, cReg)<jupyter_output><empty_output><jupyter_text>Initial Hadamards<jupyter_code>qCirc.x(noOfBits)
for i in range(0,noOfBits+1) :
    qCirc.h(i)
qCirc.barrier()<jupyter_output><empty_output><jupyter_text>Querying the Oracle<jupyter_code>for i in range(0,noOfBits) :
    if(binaryConvert[noOfBits - 1 - i] == '1') :
        qCirc.cx(i, noOfBits)
qCirc.barrier()
qCirc.draw('mpl')<jupyter_output><empty_output><jupyter_text>Final Hadamards<jupyter_code>for i in range(0,noOfBits) :
    qCirc.h(i)
qCirc.barrier()<jupyter_output><empty_output><jupyter_text>Measuring the Circuit<jupyter_code>for i in range(0,noOfBits) :
    qCirc.measure(qReg[i],cReg[i])
qCirc.barrier()
qCirc.draw('mpl')<jupyter_output><empty_output><jupyter_text>Simulating Natively<jupyter_code>nativeSim = Aer.get_backend('qasm_simulator')
nativeResult = execute(qCirc, backend = nativeSim, shots = 4096).result()
counts = nativeResult.get_counts()
print(counts)
plot_histogram(counts)<jupyter_output>{'111': 4096}<jupyter_text>SUCCCESSSS!!! We got it done in 1 shot. Yay!  Simulating on a Real Quantum Computer<jupyter_code>IBMQ.load_account()
qProvider = IBMQ.get_provider()
qComp = qProvider.get_backend('ibmq_valencia')
job = execute(qCirc, backend = qComp)
job_monitor(job)
realCounts = job.result().get_counts(qCirc)
plot_histogram(realCounts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11071107110711072, 'fraction_numerical': 0.011701170117011701, 'mean_word_length': 8.185950413223141, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '280344', 'license': 'MIT', 'n_tokens_mistral': 856, 'n_tokens_neox': 805, 'n_words': 157, 'path': 'Berstein-Vazirani Algorithm/Berstein-Vazirani_qiskit.ipynb', 'repo_name': 'kj3moraes/MyQiskitProgramming', 'size': '2222'}