The Bernstein-Vazirani Algorithm - Mathematical approach
Overview
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)
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 phases | Activity | Material |
|---|---|---|---|
| 0–5 | Introduction | In 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–20 | Work Phase I | In 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–45 | Intermediate Reflection and Work Phse II | In 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-40 | Work Phase III | In 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–90 | Closure | Discussion 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 bits, you required 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 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 bits.
The problem
Imagine we have a mystery box represented by a function . This box is special because when you give a binary number with bits, it returns either or . The function is defined using a fixed secret binary number , where each is either or .
To compute , the function follows these steps :
- Take a -bit input , where each is or .
- Multiply each input bit by the corresponding bit in a:
- Check the result of the sum:
- If the sum is even, the function outputs .
- If the sum is odd, the function outputs .
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 for the case and complete the following table by computing .
| Input x |
| Odd | Even | Output |
|---|---|---|---|---|
|
| x | 0 | |
|
| x | 1 | |
| ||||
| ||||
| ||||
| ||||
| ||||
|
Computing for and
| Input x |
| Odd | Even | Output |
|---|---|---|---|---|
|
| x | 0 | |
|
| x | 1 | |
|
| X | 0 | |
|
| X | 1 | |
|
| X | 1 | |
|
| X | 0 | |
|
| X | 1 | |
|
| x | 0 |
Understanding the challenge
Our goal is to discover the secret binary string used by the function . To achieve this, we can ask the function questions by giving it different binary strings 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 . 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
Minimum number of queries needed: 3 (one for each bit).
Justification: Each bit is only affected by in the function , meaning we can reveal by choosing queries carefully:
- Query reveals .
- Query reveals .
- Query reveals .
Task 3
Now, consider the case where the binary string has bits: .
Determine the number of questions needed in this general case. Justify your answer.
Number of queries required in the classical case:
Justification: The best strategy is to query one bit at a time:
- reveals
- reveals
- reveals , 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 qubis in state in the first register. Apply a Hadamard gate to all of them. This creates a superposition of all possible states on the qubits:
Step II: Applying the Function and Introducing Phases
Apply the function to the obtained state in Step I, using the second register. This operation adds a phase factor of either or to each term of the superposition:
Step III: Applying Hadamard Gates
Apply a Hadamard gate to each qubit in the first register:
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 with 100% probability. Unlike the classical case, where multiple queries are required, the quantum algorithm finds in one query. After measurement, we obtain .
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?
Similarities & Differences
| Aspect | Deutsch Algorithm | Bernstein-Vazirani Algorithm |
|---|---|---|
| Problem Solved | Determines if is constant or balanced | Finds the hidden bit-string |
| Number of Qubits | 2 qubits (1 input + 1 auxiliary) | qubits ( input + 1 auxiliary) |
| Quantum Speedup | Reduces queries from 2 to 1 | Reduces queries from to 1 |
| Key Difference | Solves a binary classification problem | Extracts full information about |
Worksheet 2
Testing the algorithm for the case
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 . Here is the circuit for this case:
Task 1
Step I: Show that after applying a Hadamard gate to each in the first register, the first register transforms into:
Hint: The Hadamard operator can be expressed as
We apply a Hadamard gate to each qubit in the first register, which transforms the initial state as follows:
Explanation:
- The Hadamard gate applied to creates an equal superposition of and .
- 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 is the result of multiplying the normalization factors of two individual qubits For a system with qubits, the general normalization factor is .
Task 2
Step II: We know that applying the function to the state obtained in Step I results in the following transformation:
Note: The function 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 and determine the function values by filling in the following table:
| Input x |
| Odd | Even | Output |
|---|---|---|---|---|
| ||||
| ||||
| ||||
|
b) Show that applying transforms the state from Step I as follows:
Hint: The values in your table determine the signs of each basis state.
Applying (Step II)
a) Completing the Function Table (for )
| Input x |
| Odd | Even | Output |
|---|---|---|---|---|
|
| X | 0 | |
|
| x | 0 | |
|
| X | 1 | |
|
| x | 1 |
b) Showing the Transformation:
Since the function modifies the phase based on whether the function output is or , we obtain:
Task 3
a) Step III: Apply a Hadamard gate to each qubit in the state obtained in Step II:
Show that this transformation exactly results in .
Hint 1: Use the Hadamard transformations:
Hint 2: For example, applying Hadamard gates to and separately:
b) What do you obtain after measuring the first register?
- Compare your result with .
- What does this tell you about the problem that the algorithm is designed to solve?
c) Scaling to : Suppose you want to test the algorithm for .
- 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 and not for .
Applying Hadamard gates to both qubits in:
Using the Hadamard transformations:
1. Apply to the superposition:
2. Expanding each term:
3. Result: After summing these terms, all coefficients except cancel out to zero. The final state is exactly .
We find that the final measurement gives , meaning the algorithm has extracted .
Task 4
If you still have time and motivation, repeat Steps I, II, and III for the remaining binary strings: , , .
Testing for Other -Values
If we repeat the algorithm for:
- → Measurement result:
- → Measurement result:
- → Measurement result:
In each case, the algorithm directly reveals after a single function call.
Scaling to
- Before cancellation, there would be terms in the superposition.
- The reason the teacher asks to test only for is:
- would require manipulating and simplifying terms.
- The key ideas (Hadamard gates, phase shifts, and interference) are already evident with .
- The generalization to higher 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 . 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 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 , as it contains two CNOT gates. But the theoretical calculations in Tasks 1–3 were based on . Therefore, the measurement result in the image will differ from your calculated result .
Share this page