3
$\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$

2 Answers 2

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$
1
$\begingroup$

With qasm simulator with qiskit and the inner-product quantum oracle (parameterized by the secret bits and leveraging the phase-kickback using the auxiliary qubit at state $|-\rangle$), the BV algorithm can be implemented as follows:

import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit import Aer, execute 
from qiskit.visualization import plot_histogram

def oracle(qc, s):
    n = len(s)
    for i in range(n):
        if s[i] == '1': # phase-kickback
            qc.cx(i, n)
    
def BV_circuit(s): 
    
    s = s[::-1]
    n = len(s)

    # n-qubit quantum register + 1 auxiliary qubit with n classical registers
    qc = QuantumCircuit(n+1, n) 
    for i in range(n):
        qc.h(i)
    qc.x(n)
    qc.h(n)
    
    qc.barrier(range(n))
   
    oracle(qc, s)
    
    qc.barrier(range(n))

    for i in range(n):
        qc.h(i)
    
    qc.measure(list(range(n)), list(range(n)))
    
    return qc, execute(qc,
        Aer.get_backend('qasm_simulator'), shots=1024
    ).result().get_counts()

Running with secret bits 1010, the following figure shows the Hadamard sandwich and the Oracle circuit

s = '1010'
qc, res = BV_circuit(s)
qc.draw('mpl')

enter image description here

Finally, after measurement, it always outputs the secret bits, just 1 run is required as opposed to the classical algorithm requiring n runs to determine the secret bits.

plot_histogram(res) 

enter image description here

$\endgroup$

Your Answer

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

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