View on GitHub
Open this notebook in GitHub to run it yourself
Setting the scene
Warm Up
First Example
Write a function that prepares the minus state , assuming it recives the qubitHINT
HINT
Use
H(x),X(x)Output:
-
There should always be a main (
def main(...)) function - the model that captures your algortihm is described there - The model is always generated out of the main function
- The model is sent to the synthesis engine (compiler) that return a quantum program which contains the quantum circuit
-
Every quantum variable should be declared, either as a parameter of a funciton e.g.
def prepare_minus(x: QBit)or within the function itself withx = QBit('x') -
Some quantum variables need to be initalized with the
allocatefunction.
- A variable is a parameter of a function with the declaration
Outputlikedef main(x: Output[QNum]) - A variable that was declared within a function like
a = QNum('a')
- For the
mainfunction, you will always useOutputfor all variables, as the function does not receive any input
functions.py file within the classiq package (or by just right clicking a function and presing Go To Defintion)
Uniform Superposition
Let’s continue warming up with creating a function that receives a quantum register and creates a uniform superposition for all qubits within this array. You should use the functionapply_to_all(gate_operand=, target=):
Output:
Function of a function
In our Grover example we will have 3 variablesa,b,c. We want to prepare all of them in an initial state of equal superposition.
Create a fucntion that receives these 3 quantum variables as quantum integers (QNum) and applies the create_inital_state function to each:
Oracle
- Reflection around bad states
Theoretical Background
Overall we can understand the Grover operator as composed of two reflection operators:- Around the superposition of ‘bad states’ (i.e. not the solutions)
- Around the initial guess state

Implementation
Now we will actually implement the black box, the oracle, that people are speaking about in quantum algorithms. The beauty of Classiq is that we just need to specify its functionality, and Classiq automatically implements it for us. For our purposes, we want to find all the states that obey so there are 3 quantum variables. In addition, we want to store our results somewhere, i.e. to indicate for each tupple of 3 numbers (a,b,c) (e.g. (1,2,2)) if the state is what we are looking for ( so the result is FALSE in this case). We will store the result in the variableres such that:
What we really want to implement here is graphically described as:

oracle_black_box function to an initial res qubit to the state (has anyone prepared a function prepare_minus(x) by any chance?)
You can work out the math to see that the following scheme implements what we want:

Diffuser
- Reflection around initial guess
Theoretical Background
The second part of the Grover operator is the diffuser, which can be viewed as the reflection operator around our initial guess.
Implementation
First we will implement the not equal zero function which takesaux and x as inputs and applies
aux qubit for the not_equal_zero funciton we need to insert the state after it has been initialized and apploied, and after the application of the function we need to return aux to its initial state and to free it up (We did this precedure before).
HINT
HINT
- Declare the auxilary qubit
-
Initalize it using
allocate - Prepare the state
-
Apply the
not_equal_zerofunciton -
Reverse the with the
invertoperation - Free the auxilary qubit
a,b,c.
The tricky part here is that the zero diffuser expects to receive only 1 quantunm variable x but we have three. So what should we do? We combine them to one quantum variable with the bind operation, treating them as one variable, and then splitting them back into 3 variables with the bind operation again.
Putting all together
That’s it! You’ve made it! Now is the time to harvest the fruits of the hard work and put everything together! Complete your grover operator by implementing the two functions that you’ve built, first theoracle_fucntion and then the inital_state_diffuser:
- Initalize
a,bandcwithin the scope of themainfunction using theallocateoperation - Create the initial states for
a,bandc - Apply your Grover operator