I'm just getting started in the space of quantum computing, I've built a 5 qubit program for Bernstein-Vazirani, but am trying to figure out how to go about building an N bit version of Bernstein-Vazirani, and/ or Grover's algorithm given only 5 qubits to work with. I'm currently working in the Bioinformatics field, and for my work I thought it would be a good idea to use Bernstein Vazirani or Grover's to guess/ brute force genomes. If not any suggestions with source code is much appreciated.


2 Answers 2


As mentionned by Michele, with qiskit Aqua algorithms you can easily implement Grover or BV. Here below an example of BV algorithm. Note that the number of qubits is directly related to the size of the hidden number (and so the size of the Oracle). You can easily create Oracle from a thruthtable or logical expression.

 from qiskit import *
 import matplotlib.pyplot as plt
 from qiskit.aqua import QuantumInstance
 from qiskit.aqua.algorithms import BernsteinVazirani
 from qiskit.aqua.components.oracles import TruthTableOracle, LogicalExpressionOracle

 # Set the secret number 
 s='110111' # The secret number
 # Create the thruthTable. 
 print('The Truthtable corresponding to the oracle is:','\n')
 for i in range(2**len(s)):
    var=sum(list(map(int, list(bin(int(s,2) & i)[2:])))) % 2
    print('s.',format(i, '0{}b'.format(len(s))) ,var)
    truth_table_str += str(var)

 # Pass the bistring of the oracle in the TruthTable oracle function with optimization=True to generate the oracle circuit
 oracle = TruthTableOracle(truth_table_str, optimization=True, mct_mode='advanced')

 # Set the backend to qasm_simulator
 backend = Aer.get_backend('qasm_simulator')

 # Create the BernsteinVazirani circuit
 bv = BernsteinVazirani(oracle, backend)

 # you can draw the overall circuit 
 full_circuit = bv.construct_circuit(measurement=True)

enter image description here


Qiskit Aqua includes both algorithms:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.