View on GitHub
Open this notebook in GitHub to run it yourself
The Deutsch–Jozsa algorithm [1], [2], named after David Deutsch and Richard Jozsa, is one of the first fundamental quantum algorithms showing exponential speedup over its classical counterpart. While it has no practical applicative use, it serves as a toy model for quantum computing, demonstrating how the concepts of superposition and interference enable quantum algorithms to outperform classical ones. The algorithm treats the following problem:We define the Deutsch-Jozsa algorithm, which has a quantum part and a classical postprocess part. Then, we run the algorithm on two different examples, one with a simple and another that is more complex. A mathematical explanation of the algorithm is provided at the end of this notebook.Complexity: The quantum approach requires a single query. If we require a deterministic answer to the problem, classically, we must inquire of the oracle times in the worst case. Therefore, the quantum algorithm exhibits a clear exponential speedup. However, without requiring deterministic determination—namely, allowing application of the classical probabilistic algorithm to get the result up to some error—then the exponential speedup is lost: taking classical evaluations of the function determines whether the function is constant or balanced, with a probability of . The exponential speedup is in the oracle complexity setting. It only refers to deterministic classical machines.
- Input: A black box Boolean function that acts on the integers in the range .
- Promise: The function is either constant or balanced (for half of the values it is 1 and for the other half it is 0).
- Output: Whether the function is constant or balanced.
Keywords: Foundational quantum algorithms, Phase oracle, Function evaluation, Oracle problem, Oracle/Query complexity.

How to Build the Algorithm with Classiq
We define adeutsch_jozsa quantum function whose arguments are a quantum function for the black box , and a quantum variable on which it acts, .
The Deutsch-Jozsa algorithm is composed of three quantum blocks (see Figure 1): a Hadamard transform, an arithmetic oracle for the black box function, and another Hadamard transform.
The Quantum Part
The Classical Postprocess
The classical part of the algorithm reads: The probability of measuring the state is 1 if the function is constant and 0 if it is balanced. We define a classical function that gets the execution results from running the quantum part and returns whether the function is constant or balanced:Example: Simple Arithmetic Oracle
We start with a simple example on qubits, and . Classically, in the worst case, the function should be evaluated times. However, with the Deutsch-Jozsa algorithm, this function is evaluated only once. We build a predicate for this specific use case:deutsch_jozsa function:
Output:
Output:
Example: Complex Arithmetic Oracle
Generalizing to more complex scenarios makes no difference for modeling. Let us take a complicated function, working with : a function that first takes the maximum between the input bitwise-xor with 4 and the input bitwise-and with 3, then checks whether the result is greater or equal to- Can you tell whether the function is balanced or constant?
Output:

Output: