Bernstein Vazirani algorithm

We first create an oracle embedding the secret number 14; we can do this in two equivalent ways:

  • using secret_oracle() function

  • writing the oracle from scratch in python

from qlasskit import qlassf, Qint


@qlassf
def oracle(x: Qint[4]) -> bool:
    s = Qint4(14)
    return (x[0] & s[0]) ^ (x[1] & s[1]) ^ (x[2] & s[2]) ^ (x[3] & s[3])


oracle.export("qiskit").draw("mpl")
_images/e017f3e106e1cd0f02b1307f42b531383710dab1353d524ef5e4ddbf761ef27a.png
from qlasskit.algorithms import secret_oracle

oracle = secret_oracle(4, 14)
oracle.export("qiskit").draw("mpl")
_images/e017f3e106e1cd0f02b1307f42b531383710dab1353d524ef5e4ddbf761ef27a.png

Now we can use BernsteinVazirani to discover the secret embedded in the oracle.

from qlasskit.algorithms import BernsteinVazirani

q_algo = BernsteinVazirani(oracle)
qc = q_algo.export("qiskit")
qc.draw("mpl")
_images/a8917ace04cb0420613da4cfd474deb06536a17cf19a713c5f187839fee6a0e4.png
from qiskit import QuantumCircuit
from qiskit.visualization import plot_histogram
from qiskit_aer import AerSimulator

qc.measure_all()
simulator = AerSimulator()
result = simulator.run(qc).result()
counts = result.get_counts(qc)

counts_readable = q_algo.decode_counts(counts)
plot_histogram(counts_readable)
_images/38de6c110ed81ea81303c7d605b59843cce73ba51cc6f4e85a6ff5669271c5a2.png