I have this implementation of the Bernstein-Vazirani Algorithm for Qiskit Aer:
from qiskit_aer import AerSimulator
from qiskit import QuantumCircuit
nbits = 5
a = "11010"
qu = QuantumCircuit(nbits+1)
qu.reset(range(nbits+1))
for i,j in enumerate(a, start=1):
if j == "1":
qu.cx(i,0)
Uf = qu.to_instruction()
qc = QuantumCircuit(nbits+1,nbits)
qc.reset(range(nbits+1))
qc.x(0)
qc.barrier()
qc.h(range(nbits+1))
qc.append(Uf, range(nbits+1))
qc.h(range(nbits+1))
qc.measure(range(1,nbits+1), range(nbits))
from collections import Counter
simulator = AerSimulator()
result = simulator.run(
qc.decompose(),
shots=100
).result().get_counts()
print(Counter(result))
I understand that the algorithm is meant to output an answer with a single shot. However, I would expect it to be the same answer even if I decided to run it multiple times, which is not what I'm getting from this code.
Here's how the circuits look like:
The content of the black box is:
When I run it, I get all sorts of random answers, here's one example:
Counter({'10100': 7, '01101': 5, '00011': 5, '11000': 5, '10000': 5, '10110': 5, '10011': 5, '11110': 5, '11001': 4, '00000': 4, '10111': 4, '01011': 3, '00110': 3, '11100': 3, '01110': 3, '11111': 3, '11011': 3, '01001': 3, '10001': 3, '01111': 3, '11010': 2, '00111': 2, '10010': 2, '00001': 2, '10101': 2, '00101': 2, '01000': 2, '01100': 2, '11101': 2, '00100': 1})
Instead, I would expect it to be either {'11010:100'}
or {'01011':100}
.