Search

The Bernstein-Vazirani Algorithm - Computational Approach

Quantum Computing The Supremacy of Quantum Algorithms cover image

Overview

Secondary School

Mathematics, Computer Science

Quantum Computing

English

Overview

Keywords: Bernstein‑Vazirani algorithm, hidden binary string, Hadamard gates, ⊕-Gate, single query
Age group: 16-18 years
Required knowledge/skills: Students should have done Lesson 2 about Deutsch's algorithm
Time frame: 60 minutes

Author: Wolfgang Geist (CH)

Content

Learning objectives 
Introduction
Mathematical formulation of the problem
Additional tool for Bernstein-Vazirani lesson
Task 1
Task 2
Quantum computer simulation with the IBM Composer - first approach
Task 3
Quantum computer simulation with the IBM Composer - second approach
Task 4
 

Summary

This teaching unit introduces the Bernstein‑Vazirani algorithm as an example where a quantum approach outperforms a classical strategy for finding a hidden binary string. Students analyze the role of Hadamard gates, the ⊕-Gate, and measurement by tracking state evolution step by step and comparing classical and quantum query counts. The focus is on understanding how the algorithm reveals the hidden string with a single query.

Teaser Icon Quantum Algorithms

Learning objectives

The goal of this part of the lesson is for students to deepen their understanding of the Bernstein‑Vazirani algorithm by analyzing its quantum implementation.

By working through a low‑dimensional example, students focus on how the sequence of quantum gates leads to the measurement of the hidden string and how this result can be generalized to larger input sizes.

You can download the worksheet as pdf and docx.

Additional tool for Bernstein-Vazirani lesson

The spreadsheet tool can be used to model any 3‑qubit Bernstein‑Vazirani problem and to illustrate the basic set‑up of the function and the underlying circuitry. By using Hadamard gates, it shows how the hidden string can be determined, and the ai coefficients may be hidden so that students can attempt to deduce the string themselves.

The tool is designed for teacher‑led exposition prior to the exercises in the lesson. While it can be configured for any Bernstein‑Vazirani problem, four versions have been pre‑configured to correspond to the worksheet exercises.

Download the additional Excel tool.

The lesson makes use of several spreadsheet tools, BV Sheet 3 Qubits Task 1.xlsm (etc.), which use embedded macros. Your security settings may block the macros when the file is downloaded. In order to enable them again, you should right click on the file, click “Properties”, and then “Unblock”, as shown below:

 

Introduction

Imagine you’re in an arcade, and you’ve discovered a mysterious slot machine. Here’s how it works: you have three coins – some dimes and some quarters. The machine allows you to throw in these coins in various combinations, and it will either give you nothing or exactly $1. Your goal is to figure out the exact hidden combination of dimes and quarters that unlocks the dollar jackpot.

This arcade game is a playful analogy for the Bernstein-Vazirani algorithm, which solves a similar type of puzzle – but with quantum superpowers! Just as the slot machine has a hidden rule based on your coin choices, this algorithm is all about discovering a secret pattern (a hidden string of 0s and 1s) that determines the output of a special function. The challenge? Doing it as efficiently as possible.

How to play (students and teacher)

  1. Students have three coins to start with, represented as bits (0 for dimes and 1 for quarters).
  2. The slot machine (teacher) has the key (hidden string) and evaluates the input combination and gives the result: 0 (no dollar) or 1 (jackpot!).
  3. Students’ mission is to guess the exact rule (the hidden string) that determines the result.

Mathematical formulation of the problem

Imagine we have a mystery box represented by a function fa. This box is special because when you give a binary number with n bits, it returns either 0 or 1. The function fa is defined using a fixed secret binary number a = a1 a2 an , where each ai is either 0 or 1.

To compute fa (x) , the function follows these steps:

  • Take an n-bit input x = x1 x2 xn , where each xi is either 0 or 1.
  • Multiply each input bit by the corresponding bit in a: a1·x1 + a2·x2 + + an·xn .
  • Check the result of the sum:
    • If the sum is even, the function outputs 0.
    • If the sum is odd, the function outputs 1.

Task 1

Use the hidden binary number a = 1 0 1 for the case n=3 and complete the following table by computing fa (x) .

Input x

 

a·x = a1·x1 + a2·x2 + a3·x3
OddEvenOutput   fa (x)

 

000

 

1·0 + 0·0 + 1·0 =0
 x0

 

001

 

1·0 + 0·0 + 1·1 =1
x 1

 

010
    

 

011
    

 

100
    

 

101
    

 

110
    

 

111
    

 

Analyse the Results: How many and which combinations of quarters and dimes will result in a 1$ win. Notice how the outputs depend on the hidden pattern. This pattern is the key to understanding the function’s behaviour.

 

x1

 

x2

 

x3

 

fa ( x1 , x2 , x3 )
0000
0011
0100
0111
1001
1010
1101
1110

Task 1.xlsm shows the first problem. The blue cells in column E show the xi inputs to the function, the green cells in column J show the ai coefficients corresponding to the hidden string, and the red cell in column AC shows the output of the function.

The tool may be used to demonstrate the effect of setting the xi values as shown in the first two rows of the table in Exercise 1.

Task 2 

Now we focus on the actual problem to find the hidden string of a function fa ( x1 , x2 , x3 ) from the table below:

 

x1

 

x2

 

x3

 

fa ( x1 , x2 , x3 )
0000
0011
0100
0111
1001
1010
1101
1110

 

Find the most efficient strategy to determine the string a = a1 a2 a3 :

  • How many queries do we need?
  • Which input values x1 x2 x3 would you use to determine the string a = a1 a2 a3 most efficiently?
  • What is the hidden string?

Quantum computer simulation with the IBM Composer

1. In this first approach, we use a classical algorithm to find the hidden string. 

Open https://quantum.ibm.com/composer/files/new . The circuit below implements a particular string, which we are trying to figure out.

Example: Input x1 = 1 0 0 .

  • The input values x1 , x2 , x3 are the inputs from the first three qubits q[0], q[1], q[2] .
  • The function value is the output from q[3] .
  • All qubits are initialized to input value 0. In order to change the input value from 0 to 1, apply  to the respective qubit.
  • Measurement   of the last qubit q[3] gives fa ( x1 , x2 , x3 ) .

The measured value, in this case 1, is the function value fa ( x1 , x2 , x3 ) , which can be read from the graph that shows the simulation of the measurement probability.

We need 3 queries.

Use the combinations x1 = 1 0 0 , x2 = 0 1 0 , x3 = 0 0 1 since

a·x1 = a1

a·x2 = a2

a·x3 = a3

Result:  a = 1 0 1

Task 2.xlsm shows the second problem, with the ai coefficients hidden. It can be used before the second exercise to encourage students to think about their strategy for finding the hidden coefficients.

Students can suggest input values for the xi, observe the effect on the output, and then attempt to guess the hidden ai values. The coefficients can also be changed to different combinations if you do not wish to use the same values as in the exercise.

Task 3 

Build the above quantum circuit and use the quantum composer to complete the table below:

 

 

x1 , x2 , x3
0,0,01,0,00,1,00,0,11,1,01,0,10,1,11,1,1

 

f ( x1 , x2 , x3 )
 1      

2. In this second approach we use the Bernstein Vazirani quantum algorithm to find the hidden string.

The quantum algorithm uses the circuit below. The function is, as before, implemented by the 2 CNOT gates. The hidden output string is obtained after measuring the output of the first 3 qubits. Similar as in the case of the Deutsch Algorithm, the initial Hadamard gates create a superposition of all possible input states.

Build the Quantum algorithm and confirm that the simulation indeed reveals the string you found classically in problem 3.

Notice: It took 3 queries classically, but only one query using the quantum algorithm.

In order to explore how the quantum algorithm solves the problem, let’s look at the two dimensional example, f ( x1 , x2 ) , which state evolution we can easily track with help from an Excel sheet.

Download the Excel sheet.

Task 3.xlsm shows the circuitry that students will implement in the IBM Composer. It can be used to demonstrate the effect that students should observe when changing the states of the input qubits in the Composer.

Task 3.2.xlsm shows the circuitry for finding the hidden string with a single query, using Hadamard gates before and after the function. It is configured for the problem in the sheet, but can be reconfigured for other hidden strings by changing the ai coefficients and redrawing the circuit diagram.

Rows 17–20 of the “BV Game” sheet perform the tensor products and matrix multiplications corresponding to the Hadamard and CNOT gates. These rows may be expanded if the teacher wishes to discuss the detailed effect of the gates on the qubit states.

Task 4

Build the circuit below one gate at a time as you fill in the Excel sheet one gate at a time. Confirm that your Excel agrees with the state evolution from the composer.

 

 

Some help how to get started. Use ket notation, explain that I drop the normalisation in the Excel.

Initially all qubits are in state |0 |q0 |q1 |q2 | q0 , q1 , q2 = | 0,0,0 .

The -Gate is acting on |q2 and flips the total state to | 0,0,1 , which we note in our table:

 

The Hadamard gate acting on |q0 and creates a superposition 1 2 ( |0 + |1 ) . The total state is therefore  1 2 ( |0 + |1 ) |0 |1 = 1 2 ( |0,0,1 + |1,0,1 ) .

For simplicity, we drop the normalisation factor and denote the final state in our table as

 

Finish the Excel table and answer the following questions:

  • a) What do the two Hadamard gates in the beginning accomplish?
  • b) Why is there an -Gate at the beginning of the output qubit? Look what would happen if the -Gate was missing.
  • c) What is the purpose of the last two Hadamard gates?
  • d) What string a1 a2 does the measurement reveal?
Close search