<jupyter_start><jupyter_text>Task Five: Bernstein-Vazirani Algorithm A black-box function $f$, which takes as input a string of bits ($x$), and returns either $0$ or $1$, that is:$$f(\{x_0,x_1,x_2,...\}) \rightarrow 0 \textrm{ or } 1 \textrm{ where } x_n \textrm{ is }0 \textrm{ or } 1  $$ The function is guaranteed to return the bitwise product of the input with some string, $s$.  In other words, given an input $x$, $f(x) = s \cdot x \, \text{(mod 2)} =\ x_0 * s_0+x_1*s_1+x_2*s_2+...\ $ mod 2 The quantum Bernstein-Vazirani Oracle:    1. Initialise the inputs qubits to the $|0\rangle^{\otimes n}$ state, and output qubit to $|{-}\rangle$.2. Apply Hadamard gates to the input register3. Query the oracle4. Apply Hadamard gates to the input register5. Measure  Example Two Qubits:     The register of two qubits is initialized to zero:    $$\lvert \psi_0 \rangle = \lvert 0 0 \rangle$$        Apply a Hadamard gate to both qubits:    $$\lvert \psi_1 \rangle = \frac{1}{2} \left( \lvert 0 0 \rangle + \lvert 0 1 \rangle + \lvert 1 0 \rangle + \lvert 1 1 \rangle \right) $$        For the string $s=11$, the quantum oracle performs the operation:$$|x \rangle \xrightarrow{f_s} (-1)^{x\cdot 11} |x \rangle. $$$$\lvert \psi_2 \rangle = \frac{1}{2} \left( (-1)^{00\cdot 11}|00\rangle + (-1)^{01\cdot 11}|01\rangle + (-1)^{10\cdot 11}|10\rangle + (-1)^{11\cdot 11}|11\rangle \right)$$$$\lvert \psi_2 \rangle = \frac{1}{2} \left( \lvert 0 0 \rangle - \lvert 0 1 \rangle - \lvert 1 0 \rangle + \lvert 1 1 \rangle \right)$$        Apply a Hadamard gate to both qubits:    $$\lvert \psi_3 \rangle = \lvert 1 1 \rangle$$        Measure to find the secret string $s=11$<jupyter_code>import numpy as np
from qiskit import *
%matplotlib inline
from qiskit.tools.visualization import plot_histogram
s = 101011
from qiskit import *<jupyter_output><empty_output><jupyter_text>**Step 1**<jupyter_code>qc = QuantumCircuit(6+1,6)
qc.h([0,1,2,3,4,5])
qc.draw('mpl')<jupyter_output><empty_output><jupyter_text>**Step 2**<jupyter_code>qc.x(6)
qc.h(6)
qc.barrier()
qc.draw('mpl')<jupyter_output><empty_output><jupyter_text>**Step 3**<jupyter_code>qc.cx(5, 6)
qc.cx(3, 6)
qc.cx(1, 6)
qc.cx(0, 6)
qc.barrier()
qc.draw('mpl')<jupyter_output><empty_output><jupyter_text>**Step 4**<jupyter_code>qc.h([0,1,2,3,4,5])
qc.draw('mpl')<jupyter_output><empty_output><jupyter_text>**Step 5**<jupyter_code>qc.measure([0,1,2,3,4,5], [0,1,2,3,4,5])
qc.draw('mpl')
simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, backend = simulator, shots = 1).result()
counts =result.get_counts()
print(counts)<jupyter_output>{'101011': 1}
{'fraction_non_alphanumeric': 0.17766692248656946, 'fraction_numerical': 0.05564082885648503, 'mean_word_length': 4.465408805031447, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '433589', 'license': 'MIT', 'n_tokens_mistral': 1116, 'n_tokens_neox': 990, 'n_words': 282, 'path': 'Guided Project - Programming a Quantum Computer with Qiskit - IBM SDK/Task 5/Task 5 - Bernstein-Vazirani Algorithm.ipynb', 'repo_name': 'bobsub218/exercise-qubit', 'size': '2606'}