Use this file to discover all available pages before exploring further.
View on GitHub
Open this notebook in GitHub to run it yourself
The Bernstein-Vazirani (BV) algorithm[1], [2], introduced by Ethan Bernstein and Umesh Vazirani, is a fundamental quantum algorithm that addresses a special case of the hidden-shift problem. It employs the same functional circuit structure as the Deutsch-Jozsa algorithm and achieves a linear speedup over its classical counterpart in the oracle query model.The algorithm treats the following problem:
Input: A Boolean function f:{0,1}n→{0,1} defined as
f(x)≡(x⋅a)mod2,where ⋅ refers to a bitwise dot operation, and a is a binary string of length n.
Promise:
Output: Returns the secret string a with minimum inquiries of the function.
Complexity: The quantum approach requires a single query call, therefore, the quantum complexity query is O(1). In contrast, Classically, the minimum inquiries of the function f for determining the secret string is n: f is called with these strings:
f(100…0)f(010…0)⋮f(00…01)=a0,=a1,=an−1,which reveals the secret string, one bit at a time. If one allows for randomness, the expected number of queries remains linear in n. >
The BV algorithm contains three function blocks: an oracle for the predicate f, “sandwiched” between two Hadamard transforms.The resulting state corresponds to the secret string.The full mathematical derivation is at the end of this notebook.
A simple quantum implementation of the binary function f(x) applies a series of controlled-X operations: starting with the state ∣f⟩=∣0⟩, we apply an X gate, controlled on the ∣xi⟩ state, for all i such that ai=1:∣x0…xn−1⟩∣0⟩f→Πi:ai=1CX(xi,f)∣x0…xn−1⟩∣0⟩f=∣x0…xn−1⟩Xa⋅x∣0⟩f=∣x0…xn−1⟩∣a⋅x( mod 2)⟩f.
The quantum part of the BV algorithm is essentially identical to the deutsch_jozsa function in the Deutsch-Jozsa notebook. However, in contrast to the latter, the predicate function implementation is fixed, depending solely on the secret string a. Hereafter, we refer to the secret string as a secret integer, defined as an integer argument for the bv_function:
We construct a model for a specific example, setting the secret integer to a=13 and n=5.The algorithm requires only a single application of the quantum circuit, as in an ideal noiseless execution, the resulting quantum state corresponds exactly to the hidden integer (see the last Eq. (1) below).Even in the presence of noise, the idea of using only a single query call of f has demonstrated algorithmic speedup in Ref. [3], where the authors considered a modified version of the BV algorithm in which the secret integer changes after every inquiry.In this example, we take num_shots=1000 to highlight the fact that the resulting state is purely the secret string.
import numpy as npfrom classiq.execution import ExecutionPreferencesSECRET_INT = 13STRING_LENGTH = 5NUM_SHOTS = 1000assert ( np.floor(np.log2(SECRET_INT) + 1) <= STRING_LENGTH), "The STRING_LENGTH cannot be smaller than secret string length"@qfuncdef main(x: Output[QNum[STRING_LENGTH]]): allocate(x) bv_function(SECRET_INT, x)qprog = synthesize(main)
We can now visualize the circuit:
show(qprog)
Output:
Quantum program link: https://platform.classiq.io/circuit/34kDHZcHQhQ2YaaFJJLVMsE8En4
We execute and extract the result:
result = execute(qprog).result_value()
secret_integer_q = result.dataframe["x"][0]print("The secret integer is:", secret_integer_q)print( "The probability for measuring the secret integer is:", result.dataframe["probability"][0],)assert int(secret_integer_q) == SECRET_INT
Output:
The secret integer is: 13 The probability for measuring the secret integer is: 1.0
Here is a brief summary of the linear algebra behind the Bernstein-Vazirani algorithm.The first Hadamard transformation generates an equal superposition over all the standard basis elements:∣0⟩nH⊗n2n/21j=0∑2n−1∣j⟩n.The oracle gets the Boolean Bernstein-Vazirani predicate and adds an eπi=−1 phase to all states for which the function returns true:2n/21j=0∑2n−1∣j⟩nOracle(f(j))2n/21j=0∑2n−1(−1)a⋅j∣j⟩n.Finally, application of the Hadamard transform, which can be written as H⊗n≡2n/21∑k,l=02n−1(−1)k⋅l∣k⟩⟨l∣, gives2n/21j=0∑2n−1(−1)a⋅j∣j⟩H⊗nk=0∑2n−1(2n1j=0∑2n−1(−1)j⋅(k⊕a))∣k⟩.The final expression represents a superposition over all basis states ∣k⟩; however, we can verify that the amplitude of the state ∣k⟩=∣a⟩ is simply one, as a⊕a=0:(2n1j=0∑2n−11)∣a⟩+k=a∑2n−10∣k⟩=∣a⟩.(1)Therefore, the final state is the secret string.