Path: blob/main/notebooks/summer-school/2021/resources/lab-notebooks/lab-1.ipynb
3855 views
Part I: Introduction to Qiskit
Welcome to Qiskit! Before starting with the exercises, please run the cell below by pressing 'shift' + 'return'.
I.1: Basic Rotations on One Qubit and Measurements on the Bloch Sphere
Before getting into complicated circuits on many qubits, let us start by looking at a single qubit. Read this chapter: https://qiskit.org/textbook/ch-states/introduction.html to learn the basics about the Bloch sphere, Pauli operators, as well as the Hadamard gate and the and gates.
By default, states in qiskit start in , which corresponds to "arrow up" on the Bloch sphere. Play around with the gates , , , , and to get a feeling for the different rotations. To do so, insert combinations of the following code lines in the lines indicated in the program:
Try to reach the given state in the Bloch sphere in each of the following exercises. (Press Shift + Enter to run a code cell)
1.) Let us start easy by performing a bit flip. The goal is to reach the state .
2.) Next, we would like to create superposition. The goal is to reach the state .
3.) Let's combine those two. The goal is to reach the state .
Can you even come up with different ways?
4.) Finally, we move on to the complex numbers. The goal is to reach the state .
I.2: Quantum Circuits Using Multi-Qubit Gates
Great job! Now that you've understood the single-qubit gates, let us look at gates on multiple qubits. Check out this chapter if you would like to refresh the theory: https://qiskit.org/textbook/ch-gates/introduction.html. The basic gates on two and three qubits are given by
We start with an easy gate on two qubits, the controlled-NOT (also CNOT) gate . As it has no effect applied on two qubits in state , we apply a Hadamard gate before to bring the control qubit in superposition. This way, we can create entanglement. The resulting state is one of the so-called Bell states.
5.) Construct the Bell state .
Let us now also add a measurement to the above circuit so that we can execute it (using the simulator) and plot the histogram of the corresponding counts.
As you can see in the histogram, the only possible outputs are "01" and "10", so the states of the two qubits are always perfectly anti-correlated.
6.) Write a function that builds a quantum circuit on 3 qubits and creates the GHZ-like state, .
Hint: the following circuit constructs the GHZ state, :
Congratulations for finishing these introductory exercises! Hopefully, they got you more familiar with the Bloch sphere and basic quantum gates. Let us now apply this knowledge to the second part, where we construct our first quantum algorithm, the Deutsch-Jozsa algorithm.
Part II: Oracles and the Deutsch-Jozsa algorithm
Many quantum algoritms revolve around the notion of so called . An oracle is a function that can be considered as a 'black box'. We generally want to find out specific properties of this function. We do this by asking questions to the oracle (querying). The query complexity is then defined as the minimum number of queries in order to find these properties.
To get familiar with the use of oracles we will now consider the Deutsch-Josza problem. We will see that the quantum solution has a drastically lower query complexity than its classical counterpart.
II.1: Deutsch-Jozsa Problem
We are given a hidden Boolean function , which takes as input a string of bits, and returns either or , that is:
The property of the given Boolean function is that it is guaranteed to either be balanced or constant. A constant function returns all 's or all 's for any input, while a balanced function returns 's for exactly half of all inputs and 's for the other half. Our task is to determine whether the given function is balanced or constant.
The Deutsch-Jozsa algorithm was the first example of a quantum algorithm that performs better than the best classical algorithm. It showed that there can be advantages to using a quantum computer as a computational tool for a specific problem.
In the Deutsch-Josza problem you are given an unknown orcale. This is in Qiskit implemented by the function:
This function gives a certain oracle with 5 input qubits. The last qubit () will be the output. In order to get a feeling for the oracle, let us create a circuit to which we add the oracle such that we can pass it different input strings and then measure the output of . This corresponds to the classical way of determining whether the oracle is balanced or constant.
Now we simulate the results to find the outcome of this circuit. Try different input bit strings to see the corresponding outputs!
Do you already have an idea whether the oracle is balanced or constant? What is the minimum and maximum number of inputs you would need to check to know whether this 4 bit classical Deutsch-Josza oracle is balanced or constant?
II.2: Quantum Solution to the Deutsch-Josza Problem
Using a quantum computer, we can find out if the oracle is constant or balanced with 100% confidence after only one call to the function , provided we have the function implemented as a quantum oracle, which maps the state to , where is addition modulo . Below we will walk through the algorithm.
Prepare two quantum registers. The first is an -qubit register initialised to , and the second is a one-qubit register initialised to . Note, that with Qiskit states are described as , i.e. just like for binary numbers, the last bit corresponds to the state of the first qubit. Thus, we want to initialize the state
Applying the quantum bit oracle to any state would yield the state . As we have prepared the state , which corresponds to the state on the last qubit , in the state , the output of the oracle for any input bitstring is given by: Thus, we have created a phase oracle acting on the bit string .
Before applying the oracle, we need to create our input state on the first qubits though. For that we want an equal superposition state, so that the total state on all qubits is given by
Now we are ready to apply our oracle to the prepared superposition state . This gives the state
In the final part of our algorithm we disregard the outcome on our second register and we apply an n-fold Hadamard to our first register. Afterwards we measure the outcome on these qubits.
At this point the second single qubit register may be ignored. Applying a Hadamard gate to each qubit in the first register yields the state:
where is the sum of the bitwise product.
Let us now run the circuit including the measurement of the first register on the simulator:
As we learnt in the lecture, if the output is the zero bit string, we know that the oracle is constant. If it is any other bit string, we know that it is balanced. You may also check the other oracles by just changing the oracle number in the beginning where the oracle is defined!