My few observations while I prepared for the Qiskit Certification exam (2)
As I said in my previous blog, these couple of blogs are about some of my observations while I prepared for Qiskit Certification exam last year. This blog is related to qsphere.
qsphere is used mainly to understand relative phases of each state in a quantum state that may or may not be in superposition, but it is of course more useful in case of multi-qubit systems where there is superposition. It also gives an idea of which state is most probably going to be the result of measurement since the size of the blobs for every state within superposition(we will see how a qsphere looks soon) is proportional to their amplitudes. Relative phases become extremely important in a lot of algorithms, for eg: there is a relative phase difference between |+> and |-> states. So if in an algorithm, an auxiliary qubit is set to |->, it can be useful for phase kickback and it can pass on a negative phase if it is a target qubit in a controlled X or controlled Z gates, to the appropriate state of interest. We will not go into details in this blog, but one can check out the chapters on Phase kickback and some algorithms like the Grover’s algorithm, Deutsch-Jozsa algorithm in the Qiskit textbook. Similarly, understanding the result of measurement through the statevector and visualization, can help us alter the circuit in a desired manner so that we get the appropriate state after measurement on an actual Quantum hardware.
Coming to qsphere, the Qiskit documentation states that it shows relative phases after taking out the phase from the state that has maximum probability (modulus square of amplitude) as a global phase and normalizing the amplitudes of other states accordingly. In order to make sure I understand qsphere well, I created a random state vector like below. (The best part about using random_statevector from qiskit.quantum_info is that it ensures you get a valid quantum state for which the length or dot product with itself is 1. If we had to randomly create a state vector ourselves, we would have to put efforts to ensure that, though that wouldn’t have been too difficult either)
So for each state |00>, |01>, |10> and |11>, we have random complex amplitudes as we can see above, which means each amplitude can be written in the polar coordinate form, as r(cosθ + isinθ) for some r and θ. So the phase with respect to a complex number is this angle θ. We can find this phase for any complex number with python’s cmath library. So below are the phases for each of states:
Now, we said that the phase from the state that has max probability or modulus square of amplitude, is taken out as the global phase. So let’s find the mod square of amplitude for each state:
We can see that the state |11> has the maximum probability and so, we will have to take out the phase from |11> as the global phase, which means that after removing this global phase, the relative phase w.r.t |11> would be 0. For the remaining 3 states, note that we have an Euler’s representation of complex numbers:
and things become a lot easier with exponential because now, removing a phase is same as:
We can take r2 to be 1 since we are only interested in removing the phase, which means only θ2 is to be taken out or subtracted from the phases of states |00>, |01> and |10> and θ2 in our case is the phase of the state |11>. Let’s do it:
Now, the angles are negative numbers and if you remember from high school Math, a negative angle means that it is taken in the clockwise direction from the angle 2π in the cartesian plane, which means to know what the relative phases for |00>, |01> and |10> are, in the standard anti-clockwise direction, we would have to just add the above to 2π:
Now let’s actually find the float values for π/2, π, 3π/2 and 2π.
Now, let’s compare the relative phases we got for |00>, |01> and |10> to the above. We know the relative phase w.r.t |11> is 0 since its phase is removed as global phase. Hence, our conclusion would be: phase of |00> is around 3π/2, phase of |01> is between π/2 and π (closer to π/2), phase of |10> is around 2π or 0, phase of |11> is 0. Now, let’s plot the qsphere and check if what we concluded is right:
Great! We were right. Now, in the exam, one doesn’t have access to a calculator or to Python, so one wouldn’t face questions were such calculations are actually required. The questions in the exam would be such that one can answer them by just looking at the state. Typically, one would have to calculate the state from a given circuit and choose the right option from the given choices of qsphere representations. Few simple things to conclude are that if one has only real amplitudes, the phases for all the states would be 0 or π (π, if it is negative). If an amplitude has only an imaginary part, the phase would be π/2 or 3π/2 (3π/2 if it is negative imaginary).
This whole idea of removing the phase as global phase from max probability state, gets confusing when more than 1 state are of same max probability. For eg, take the two cases below:
This is of course understandable. It would have been more intuitive if |00> was seen as having the 0 phase and |11> was seen with phase π since there was no need to take out -1 as global phase here. This is also not consistent always, for example, see below:
In this case, -1 was not taken out as a global phase. I am sure there must be some convention that’s followed. Someone on Qiskit slack workspace pointed out that -1 is never taken out for either of the two cases above on the IBM Quantum Composer and that there should be consistency. I agree to that, even though removing or not removing a global phase doesn’t make any difference. In the exam, I don’t remember facing confusing options like these and I hope nobody does.
Thank you for reading! :)