Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Welcome to the Classiq Workshop for Quantum Oracles! In this notebook, you will cover hands-on examples and exercises of the following topics:
  • Defining Quantum Oracles using arithmetics in Classiq
  • Phase Kickback and Phase encoding
  • A first example: The Deutsch-Jozsa Algorithm
  • Unstructured search: Grover’s Algorithm
**For each exercise, complete the code in the #TODO sections correctly. You can find the complete solutions at the end of this notebook.** Additional resources you should use: Good luck!

Quantum Arithmetics: The Oracle

In quantum computing, an oracle is a method used to encode information about a function without revealing its explicit form. An oracle is also known as a black box and plays a crucial role in many quantum algorithms, such as the Deutsch-Jozsa algorithm and Grover’s search algorithm. The oracle can be thought of as a tool that, when given a specific input, produces an output according to an unknown function f(x)f(x). How is it possible to construct and design an oracle for a quantum algorithm? In general, an oracle is represented by a unitary operator UfU_f. This operator acts on a quantum state to evaluate a binary function f(x)f(x). For example, in the context of Grover’s and Deutsch-Jozsa algorithm, the oracle UfU_f takes the action Ufxy=xyf(x)U_f|x\rangle |y\rangle = |x\rangle |y\oplus f(x)\rangle. The \oplus represents the XOR operation:
  • xyx \oplus y equals to 00 if x=yx=y;
  • xyx \oplus y equals to 11 if xyx\neq y.
Oracle_fig The quantum oracles are developed in order to entangle the xx and yy qubits according to a set of rules in a particular way we want to. Classiq provides a distinctive and efficient approach to working with oracles, which are defined through arithmetic expressions. Starting with a simple example, we create an oracle for a binary function f(x,y)f(x,y) that follows the arithmetic expression:

Quantum Oracles and Arithmetics: A Simple Example

{f(x,y)=1, if (2x+y=4)f(x,y)=0, else  \begin{cases} f(x,y) = 1,\text{ if }(2\cdot x+y =4)\\ f(x,y) = 0,\text{ else } \end{cases} with x{0,1}x\in\{0,1\} and y{0,1,2,3}y\in\{0,1,2,3\}. We first define a quantum function that implements the arithmetic operation described above:
  • The ^= expression represents an in-place XOR operation between the z qubit on the left-hand side and the right-hand side expression, assigning the result to the qubit z. A short explanation of this concept can be found here.
  • Therefore, z ^= 2*x + y == 4 means that we are doing an XOR operation that follows the rule 2*x + y == 4, assigning the result to z (in-place).
Now, let’s see how this looks when evaluating this oracle over all possible values of x and y:
Output:
Output:

Quantum Oracles and Arithmetics: Phase Kickback

Every quantum algorithm can be decomposed into three key steps: 1) Encoding the data, 2) Manipulating the data, and 3) Extracting the result. In the current class, we are studying the first step, where the data is loaded into the quantum computer. For the second step, the phase kickback is a powerful technique in data manipulation, facilitating the extraction of desired results and allowing more freedom in data encoding techniques. Phase kickback deals with kicking the result of a function to the phase of a quantum state so it can be smartly manipulated with constructive and destructive interferences. The standard way to apply a classical, binary, function f:{0,1}n{0,1}f: \{0, 1\}^n \to \{0, 1\} on quantum states is by using the oracle with digital encoding by performing: Ofxny=xnyf(x).O_f |x\rangle_n |y\rangle = |x\rangle_n |y\oplus f(x)\rangle. The phase kickback takes the oracle OfO_f and performs the action x(1)f(x)x.|x\rangle \to (-1)^{f(x)}|x\rangle. The circuit that applies the Phase Kickback to a quantum Oracle OO is of the following form: Oracle_fig

Exercise: Phase Kickback

Apply the phase Kickback to the oracle given in the first example and execute it using the statevector simulator.
Output:
Output:
Output:

Quantum Oracles and Arithmetics: The Deutsch-Jozsa Algorithm

Deutch-Jozsa algorithm is a seminal quantum algorithm, well-known for its exponential speed-up over classical algorithms to identify if a binary function is either constant or balanced. Given a binary function ff, assumed to be either constant or balanced, the Deutsch-Jozsa algorithm requires only one evaluation to assert this, while a classical algorithm would require up to 2n1+12^{n-1} +1 evaluations of the oracle. Oracle_fig

Deutsch-Jozsa Algorithm Exercise:

In this exercise, we will use the Deutsch-Jozsa algorithm to check if the following function is balanced. Oracle_table
The function f(x)f(x) assumes its value as 11 only when the integer value of x| x \rangle is even.This is equivalent to the condition that the LSB must be 0 to have a phase flip.
We can thus set the rule for the oracle of f(x)f(x): Everytime the integer value of the qubit x| x \rangle is divisible by 22, ff will output 11. In other words, the oracle for this function should flip the phases of the even integers. In this case we can cleverly construct such an oracle, but it is not always an easy task to build it. Once you have found the arithmetic expression for the oracle, it is possible to construct this algorithm with only a few lines of code; the synthesis engine handles the hard work (and can optimize for circuit depth or width): When implementing the Deutsch-Jozsa algorithm below, use the new phase and control statements elegant implementation method:
Output:
Output:

Quantum Oracles and Arithmetics: The Grover Algorithm

Grover’s algorithm is a quantum search algorithm, well-known for its ability to search an unsorted database or solve the “unstructured search problem” quadratically faster than any classical counterpart. Given an unsorted list of NN elements and a search condition, Grover’s algorithm’s task is to find the input that satisfies the condition. To achieve this, the algorithm uses an oracle associated to a function f(x)f(x), which evaluates to 1 if xx is the desired element and 0 otherwise. Grover’s algorithm performs about N\sqrt N iterations, each one applying a Grover operator that flips the phase of the marked state and then amplifies its amplitude. Repeating this process gradually boosts the marked state’s amplitude until it becomes highly probable upon measurement. While a classical computer would require O(N)O(N) queries to search a database of NN items, Grover’s algorithm achieves this in O(N)O(\sqrt N) queries, demonstrating the advantage of quantum parallelism and the effects of quantum interference. Oracle_fig

Grover’s Algorithm Exercise:

In this exercise, we will use the Grover algorithm to solve the following equation: xy=2 x - y = 2 For this exercise, begin by defining the oracle O{O}. First, create a QStruct that contains the two QNum variables, x and y:
Next, incorporate the quantum oracle into the grover_search function that automatically implements the Grover operator iterations:
Output:
Output:
Output:
Try to also write the Grover algorithm with the new phase and control method (not making use of the built in phase_oracle function)!

Solutions

Phase Kickback:

Elegant Method (Phase and Control)

Output:
Output:
Output:

Deutsch-Jozsa (Only New Elegant Method):

Output:
Output:
Output:

Grover’s Algorithm:

Regular Method:

Output:
Output:
Output: