Search

The Bernstein-Vazirani Algorithm - Mathematical approach

Quantum Computing The Supremacy of Quantum Algorithms cover image

Overview

Secondary School

Physics, Mathematics

Quantum Computing

English

Overview

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

Author: Sergio Rivera Hernandez (DE)

Content

Learning objectives
Lesson plan
Worksheet 1: Student tasks and solutions
  • The problem
  • Task 1, Task 2, Task 3
  • Quantum Solution: The Bernstein‑Vazirani Algorithm
  • Task 4
Worksheet 2: Testing the algorithm for the case n=2 with solutions 
  • Task 1, Task 2, Task 3, Task 4
  • Optional tasks

Summary

This teaching unit introduces the Bernstein‑Vazirani algorithm as an example where a quantum approach massively outperforms the classical approach for finding a hidden binary string. Students work through the algorithm step by step for small cases, using tables, Hadamard gates, and state transformations to see how the hidden string is obtained with a single query. The unit then connects the theoretical calculations to a concrete circuit implementation using the IBM Quantum Composer.

Teaser Icon Quantum Algorithms

Learning objectives

The goal of this lesson is for students to apply and deepen their understanding of the general physical principles introduced in Lesson 1 (Superposition, Interference and Quantum Function Evaluation), now in the context of the Bernstein-Vazirani Algorithm.

First, students will explore the general idea of the algorithm, as they did in Lesson 2. Then, they will apply the algorithm to the n = 2 case.

It is important that, throughout the lesson, students understand that for n≥3, calculations become increasingly tedious. However, understanding the n=2 case allows them to generalize the algorithm for larger values of n. The choice of n=2 is based on time constraints and the fact that solving larger cases is not necessary to grasp the core principles of the algorithm.

This lesson will also reinforce the fundamental structure underlying many quantum algorithms, known as the Golden Circuit Pattern.

Lesson plan

Timing
in minutes
Lesson phasesActivityMaterial
0–5IntroductionIn this phase, the teacher will explain that this lesson will cover a more complex algorithm, one that solves one of the search algorithms [FU1.1][SR1.2]from Lesson 1 in a single step.Worksheet & Spreadsheet tool
5–20Work Phase IIn this phase, students will receive the worksheet, work on Tasks 1,2 and 3, and read through the three steps of the algorithm.Worksheet 1
15–45Intermediate Reflection and Work Phse IIIn this phase, the results of the first three tasks will be reviewed to ensure that students have understood the general idea before proceeding with the concrete application of the algorithm.
The teacher will explain the algorithm in detail to ensure that students fully understand it, after which they will proceed to solve Task 4.
 
30-40Work Phase IIIIn this phase, students will receive the next work-sheet and work on Exercises 1–4. Students who finish early can proceed to using a computer and continue with the optional task.Worksheet 2
75–90ClosureDiscussion of results. In this section, the teacher will once again emphasize the physical principles behind the algorithm and highlight that these principles will be needed again in the next lesson.  

 

Note for Teachers: This lesson is designed as a double period (approx. 90 minutes). If your schedule uses 45-minute slots, Task 4 and the IBM Composer activity can be assigned as homework or covered in a second session

You can download both worksheets to use with your students:

Worksheet 1

The Bernstein-Vazirani algorithm, developed in 1993, builds upon Deutsch’s algorithm and provides another example of how quantum computing can solve certain problems more efficiently than classical computing.
In Lesson 1, you solved a task trying to discover a hidden binary string by asking Yes/No questions one at a time. Using a classical approach as you did, one needs to ask one question per bit to determine the entire string. This means that for a hidden string of n bits, you required n queries in the worst case.
However, quantum computing leverages the Bernstein-Vazirani algorithm to find the hidden string in just one query, regardless of how many bits it contains: Yes, it reduces n queries to a single one.
In this lesson, you will first explore how this quantum speedup is achieved using an example where the hidden string consists of three bits. Then, you will generalize the concept to n bits.

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 a n-bit input x = x1 x2 xn , where each  xi is  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.

This type of addition is called addition modulo 2. It means: if the total number of 1s is even, the result is 0; if it is odd, the result is 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
    

Computing  fa (x) for  a = 1 0 1 and  n = 3

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

 

1·0 +0·1 +1·0 =0
 X0

 

011

 

1·0 +0·1 +1·1 =1
X 1

 

100

 

1·1 +0·0 +1·0 =1
X 1

 

101

 

1·1 +0·0 +1·1 =2
 X0

 

110

 

1·1 +0·1 +1·0 =1
X 1

 

111

 

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

Understanding the challenge

Our goal is to discover the secret binary string a used by the function fa (x) . To achieve this, we can ask the function questions by giving it different binary strings x = x1 x2 xn and observing what it returns.

Classical Solution

Task 2 

Determine the minimum number of questions you need to ask to fully uncover the binary string a = a1 a2 a3 . Justify your answer.

Hint: Think back to the task you solved in Lesson 1, where you had to find a hidden binary string by asking Yes/No questions. How is that related to the problem we are facing now?

Classical Approach for finding a

Minimum number of queries needed: 3 (one for each bit).

Justification: Each bit ai is only affected by xi in the function fa (x) , meaning we can reveal a by choosing queries carefully:

  • Query x = 1 0 0 reveals a1.
  • Query x = 0 1 0 reveals a2.
  • Query x = 0 0 1 reveals a3.

Task 3 

Now, consider the case where the binary string has n bits: a = a1 a2 an .

Determine the number of questions needed in this general case. Justify your answer.

Number of queries required in the classical case: n

Justification: The best strategy is to query one bit at a time:

  • x = 1 0 0 0 0 reveals a1
  • x = 0 1 0 0 0 reveals a2
  • x = 0 0 1 0 0 reveals a3, etc.

Quantum Solution: The Bernstein-Vazirani Algorithm

Instead of querying the function multiple times as in the classical method, the Bernstein–Vazirani algorithm extracts the entire hidden string in a single query using the circuit below:

 

It follows these steps:

Step I: Creating superposition

Prepare n qubis in state |0 in the first register. Apply a Hadamard gate to all of them. This creates a superposition of all possible states on the n qubits:

H|0 H|0 H|0 = 1 2n ( |000 + |001 + + |111 )

Step II: Applying the Function fa and Introducing Phases

Apply the function fa to the obtained state in Step I, using the second register. This operation adds a phase factor of either (-1)0 or (-1)1 to each term of the superposition:

1 2n [ (-1) fa (000) |000 + (-1) fa (001) |001 + + (-1) fa (111) |111 ]

Step III: Applying Hadamard Gates

Apply a Hadamard gate to each qubit in the first register:

1 2n [ (-1) fa (000) H|0 H|0 H|0 + (-1) fa (001) H|0 H|0 H|1 + (-1) fa (111) H|1 H|1 H|1 ]

Note: The fact that the function introduces these phase shifts in the first register without affecting the second register directly is a fundamental concept in quantum computing. If you would like to explore in detail why and how these phase shifts occur, check the Quantum Function Evaluation worksheet.

Final Measurement: Measuring the first register now reveals a with 100% probability. Unlike the classical case, where multiple queries are required, the quantum algorithm finds a in one query. After measurement, we obtain |a = |a1a2an .

Task 4

Compare the circuit used in the Deutsch Algorithm (see the figure) with the circuit used in the Bernstein-Vazirani Algorithm and answer the following questions:

Explain the similarities and differences between the two algorithms. How does the Deutsch Algorithm relate to the Bernstein-Vazirani Algorithm?

 

Circuit used in the Deutsch Algorithm

Similarities & Differences

AspectDeutsch AlgorithmBernstein-Vazirani Algorithm
Problem SolvedDetermines if  f(x) is constant or balancedFinds the hidden bit-string a
Number of Qubits2 qubits (1 input + 1 auxiliary) n+1

 qubits (n input + 1 auxiliary)

Quantum SpeedupReduces queries from 2 to 1Reduces queries from n to 1
Key DifferenceSolves a binary classification problemExtracts full information about a

Worksheet 2

Testing the algorithm for the case n=2

Now, you will analyze each step of the algorithm to understand how the quantum principles behind it allow us to solve the problem with a single query for the case n=2. Here is the circuit for this case:

Task 1

Step I: Show that after applying a Hadamard gate to each |0 in the first register, the first register transforms into:

H|0 H|0 = 1 2 ( |00 + |01 + |10 + |11 )

Hint: The Hadamard operator can be expressed as  H = 1 2 ( |0 0| + |0 1| + |1 0| - |1 1| ) .

We apply a Hadamard gate to each qubit in the first register, which transforms the initial state as follows:

H|0 H|0 = 1 2 ( |00 + |01 + |10 + |11 )

Explanation:

  • The Hadamard gate applied to |0 creates an equal superposition of |0 and |1.
  • Since there are two qubits, applying Hadamard gates to both results in a uniform superposition of all four possible 2-bit states.

Normalization Factor: Explain to students that the factor  1 2 is the result of multiplying the normalization factors of two individual qubits  ( 1 2 · 1 2 = 1 2 ) . For a system with n qubits, the general normalization factor is 1 2 n .

Task 2

Step II: We know that applying the function fa to the state obtained in Step I results in the following transformation:

1 2 [ (-1) fa (00) |00 + (-1) fa (01) |01 + (-1) fa (10) |10 + (-1) fa (11) |11 ] .

Note: The function fa introduces phase shifts in the first register without directly affecting the second register. This is a fundamental concept in quantum computing. If you would like to explore in detail how and why these phase shifts occur, go to the Quantum Function Evaluation worksheet.

Your tasks

a) Assume that a = 1 0 and determine the function values by filling in the following table:

Input x

 

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

 

00
    

 

01
    

 

10
    

 

11
    

 

b) Show that applying fa transforms the state from Step I as follows:

1 2 [ |00 + |01 - |10 - |11 ] .

Hint: The values in your table determine the signs of each basis state.

Applying fa (Step II)

a) Completing the Function Table (for a = 1 0 )

Input x

 

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

 

00

 

1·0 + 0·0 =0
 X0

 

01

 

1·0 + 0·1 =0
x 0

 

10

 

1·1 + 0·0 =1
X 1

 

11

 

1·1 + 0·1 =1
x 1

 

b) Showing the Transformation:

Since the function fa modifies the phase based on whether the function output is 0 or 1, we obtain:

1 2 ( |00 + |01 - |10 - |11 ) .

Task 3

a) Step III: Apply a Hadamard gate to each qubit in the state obtained in Step II:

1 2 [ |00 + |01 - |10 - |11 ] .

Show that this transformation exactly results in |10 .

Hint 1: Use the Hadamard transformations:

H |0 = 1 2 ( |0 + |1 ) , H |1 = 1 2 ( |0 - |1 ) .

Hint 2: For example, applying Hadamard gates to |0 and |1 separately:

H|0 H|1 = 1 2 ( |0 + |1 ) 1 2 ( |0 - |1 ) .

b) What do you obtain after measuring the first register?

  • Compare your result with a = 1 0 .
  • What does this tell you about the problem that the algorithm is designed to solve?

c) Scaling to n=3: Suppose you want to test the algorithm for n=3.

  • How many terms would you obtain in Task 3 a) before cancellation of equal terms?
  • Explain why your teacher asked you to test the algorithm only for n=2 and not for n=3.

Applying Hadamard gates to both qubits in:

1 2 ( |00 + |01 - |10 - |11 ) .

Using the Hadamard transformations:

H|0 = 1 2 ( |0 + |1 ) , H|1 = 1 2 ( |0 - |1 ) .

1. Apply HH to the superposition:

1 2 [ H|0 H|0 + H|0 H|1 - H|1 H|0 - H|1 H|1 ] .

2. Expanding each term:

H|0 H|0 = 12 ( |00 + |01 + |10 + |11 )

H|0 H|1 = 12 ( |00 - |01 + |10 - |11 )

- H|1 H|0 = - 12 ( |00 + |01 - |10 - |11 )

- H|1 H|1 = - 12 ( |00 - |01 - |10 + |11 )

3. Result: After summing these terms, all coefficients except |10 cancel out to zero. The final state is exactly |10.

We find that the final measurement gives |10, meaning the algorithm has extracted a = 1 0 .

Task 4

If you still have time and motivation, repeat Steps I, II, and III for the remaining binary strings: a=00 , a=01 , a=11 .

Testing for Other a-Values

If we repeat the algorithm for:

  • a=00 → Measurement result: |00
  • a=01 → Measurement result: |01
  • a=11 → Measurement result: |11

In each case, the algorithm directly reveals a after a single function call.

Scaling to n=3

  • Before cancellation, there would be 23 = 8 terms in the superposition.
  • The reason the teacher asks to test only for n=2 is:
    • n=3 would require manipulating and simplifying 8 terms.
    • The key ideas (Hadamard gates, phase shifts, and interference) are already evident with n=2.
    • The generalization to higher n follows the same pattern.

Implementing the Bernstein-Vazirani Algorithm in the IBM Quantum Composer

In this task, you will implement the Bernstein-Vazirani algorithm using the IBM Quantum Composer. The circuit in the image represents the implementation of the algorithm for n=2. Analyze it carefully and answer the following questions:

  • a) The first two Hadamard gates are applied to the first two qubits. What is their purpose? How does their effect relate to the superposition state you calculated in Task 1?
  • b) There is an X-gate applied at the beginning to the output qubit. Why is this necessary? Predict what would happen if the X-gate were missing and then test your prediction in the Quantum Composer.
  • c) The last two Hadamard gates are applied before measurement. What do they accomplish? How does this step relate to Task 3, where you extracted the hidden string?
  • d) After running the circuit, the measurement gives a two-bit binary string. What value of a1 a2 does the measurement reveal? Does this match your expectations based on the theoretical calculations in Tasks 1, 2, and 3?

 

Important Note on the Circuit Image in the optional task

Please note that the IBM Quantum Composer screenshot displays a circuit for a secret string 11 , as it contains two CNOT gates. But the theoretical calculations in Tasks 1–3 were based on 10 . Therefore, the measurement result in the image |11 will differ from your calculated result |10 .

Close search