View on GitHub
Open this notebook in GitHub to run it yourself
- Defining Quantum Oracles using arithmetics in Classiq
- Phase Kickback and Phase encoding
- A first example: The Deutsch-Jozsa Algorithm
- Unstructured search: Grover’s Algorithm
- Classiq documentation
- The Classiq GitHub repository
- The community Slack of Classiq - you can ask any question you have over here
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 . How is it possible to construct and design an oracle for a quantum algorithm? In general, an oracle is represented by a unitary operator . This operator acts on a quantum state to evaluate a binary function . For example, in the context of Grover’s and Deutsch-Jozsa algorithm, the oracle takes the action . The represents the XOR operation:- equals to if ;
- equals to if .
The quantum oracles are developed in order to entangle the and 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 that follows the arithmetic expression:
Quantum oracles and arithmetics: A simple example
with and . We first define a quantum function that implements the arithmetic operation described above:-
The
^=expression represents an in-place XOR operation between thezqubit on the left-hand side and the right-hand side expression, assigning the result to the qubitz. A short explanation of this concept can be found here. -
Therefore,
z ^= 2*x + y == 4means that we are doing an XOR operation that follows the rule2*x + y == 4, assigning the result toz(in-place).
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 on quantum states is by using the oracle with digital encoding by performing: The phase kickback takes the oracle and performs the action The circuit that applies the Phase Kickback to a quantum Oracle is of the following form:
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 , 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 evaluations of the oracle.
Deutsch-Jozsa Algorithm Exercise:
In this exercise, we will use the Deutsch-Jozsa algorithm to check if the following function is balanced.
How do we build the oracle for the function displayed in the table?
How do we build the oracle for the function displayed in the table?
The function assumes its value as only when the integer value of is even.This is equivalent to the condition that the LSB must be 0 to have a phase flip.
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 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 , which evaluates to 1 if is the desired element and 0 otherwise. Grover’s algorithm performs about 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 queries to search a database of items, Grover’s algorithm achieves this in queries, demonstrating the advantage of quantum parallelism and the effects of quantum interference.
Grover’s Algorithm Exercise:
In this exercise, we will use the Grover algorithm to solve the following equation: For this exercise, begin by defining the oracle . First, create aQStruct that contains the two QNum variables, x and y:
grover_search function that automatically implements the Grover operator iterations:
Output:
Output:
Output:
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: