2
$\begingroup$

I'm new to quantum computing and Qiskit, so please be patient with me. I'm going through the info on Bernstein Vazirani. Here is the link to the section: https://qiskit.org/textbook/ch-algorithms/bernstein-vazirani.html. About midway down the page, at the end of the example section, is a widget: bv_widget. I have attempted to run the widget and apply the steps mentioned in the section. I apply the hadamards, then the oracle and then the hadamards again. But there seems to be no way of performing a measurement to determine the result. I tried placing bv_widget.measure(2,2) as the last line in the code sample, but that didn't work. Neither did just using the word measure(2,2).

What am I missing here? Is there a way to perform a measurement?

PS A similar piece of code is at the end of the chapter, and I'm running into the same problem with it.

Thanks for your help.

Doug

$\endgroup$
1
$\begingroup$

I think the widget is supposed to only show you the initialization and Oracle part of the circuit and not actually implement the measurement process into it. Thus, that is why you don't see the measurement step.

However, it is quite simple to write a function for perform the Bernstein-Vazirani algorithm. You can just use the script below:

import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit import IBMQ, Aer, execute 
provider = IBMQ.load_account()

def BV_circuit(secret_bitstring): 
    num_qubits = len(secret_bitstring) + 1
    circuit = QuantumCircuit(num_qubits,num_qubits)
    for i in range(num_qubits):
        circuit.h(i)
    circuit.z(num_qubits-1)
    for i in range(num_qubits-1):
        if secret_bitstring[i] == '1':
            circuit.cx(i,num_qubits-1)
    circuit.barrier(range(num_qubits))
    for i in range(num_qubits):
        circuit.h(i)
    circuit.measure([i for i in range(num_qubits-1)], [i for i in range(num_qubits-1)])
    return circuit

For example: If I pick a secret bitstring to be 1111 then I have the following:

secret_bitstring = '1111'    
QC = BV_circuit(secret_bitstring)
QC.draw( 'mpl',style={'name': 'bw'}, plot_barriers= False, initial_state = True, scale = 1)

enter image description here

You can check that this this will generate the same circuit as the one you have using the qiskit's Widget. And if you want to run it on hardware, just use the following script:

backend = provider.backends.ibmq_16_melbourne
job = execute(QC, backend, shots=8192)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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