How to find correct angle values of Parametrized Quantum R gates using Qiskit
Beginner-friendly tutorial encouraging baby-steps in Quantum Computing using Qiskit
Quantum Computing is an emerging field which brings in loads of new aspects with itโs unique way to process data. The concept uses rules of quantum mechanics to build computers that can process data differently than classical computers. The circuit approach of implementing Quantum Computing is very popular. While participating in the Qiskit India hackathon, I observed many participants who were beginners like me, struggled in finding the angle values for Parametrized Quantum Gates. Even I faced a bit of difficulty, but finally got through. So I am putting together a tutorial blog to jot down the steps of finding the angle values in case of parametrized gates. For this post, I will assume the reader is accustomed to the fundamental understanding of quantum computing, and is familiar with usage of gates to make circuits (if not, you can start here).
While I was beginning to play with quantum gates, I bumped into โparametrized gatesโ. These gates demand certain parameters (usually angle values) from the user, in order to work. Two of such gates are: Rฯ-gates (Rx, Ry and Rz) and general U gates (U1, U2 and U3). This particular tutorial is on Rฯ-gates only, and I plan to do the next one specially for U3 gates.
Here, I am taking a special case to explain how to calculate phase angles for Rฯ-gates. I hope this gives you an idea of how to work your way forward in other general cases. I got to do this while solving the QOSF mentorship screening task. I struggled at the beginning, and after a little consultation with a friend I figured it out. So, here I am, taking you through the problem step-by-step, in case you too feel stuck at the same point.
Objective: To implement a circuit that returns |01โฉ and |10โฉ with equal probabilities, using only CNOTs, RXs and RYs.
First things first, a quick recap of basics, we know that quantum bits (qubits) can individually exist in a superposition of both |0โฉ and |1โฉ at any given point. That is, for single qubit, possible states are 2ยน. In case of two qubits, the number of possible states becomes 2ยฒ = 4, (i.e. |00โฉ, |10โฉ, |01โฉ, |11โฉ). As per our objective we want |01โฉ and|10โฉ in equal probabilities. So, if we assume the other two states are not gonna appear on my histogram, then we want to reach a state: |๐โฉ=๐ผ|01โฉ+๐ฝ|10โฉ, where both states must occur with equal probabilities, i.e. ||๐ผยฒ||= ||๐ฝยฒ|| = 0.5. That gives us, ๐ผ=๐ฝ=1/โ2.
We need to generate a state |๐โฉ=1/โ2 (|01โฉ + |10โฉ), which is a bell state (๐+). It is usually generated with the help of a Hadamard gate, an X gate and a CNOT gate in the following way:
To visualize the effect of the above circuit, we execute it on the statevector_simulator
provided by Qiskit Aer.
With this approach in perspective, we get an overall idea about what can be done to achieve our objective. Since, we have a restriction on using only Rx, Ry and CNOT gates, we will try to achieve a Hadamard effect on q0 and X effect on q1 with just Ry and Rx gates. CNOT can be used as it is from the usual approach in this particular case (as shown in Figure 1).
We know the final state, |๐โฉ=๐ผ|01โฉ+๐ฝ|10โฉ, where: ๐ผ=๐ฝ=1/โ2. Mathematically, it is
Also, we can write |๐โโฉ as:
As |๐โโฉ = โฎ1/โ2|0โฉ+1/โ2|1โฉโฏโ|1โฉ, we understand that this state is completely separable, which means we can treat both qubits separately in our further calculations as shown below.
Letโs start working with q0 first. Since we are dealing with two gates Ry and Rx on q0, letโs say Ry has an angle ๐โ and Rx has an angle ๐โ. The unitary matrices for both Ry(๐โ) and Rx(๐โ) are as follows:
So, equating these values with the values of q0:
We get the following equations:
Solving the above for ๐โ and ๐โ, we get: ๐โ=๐/2; and ๐โ=๐.
Now, coming on to q1, we will apply ๐ ๐ฅ(๐โ) gate to get an effect of X gate on q1, i.e., ๐ ๐ฅ(๐โ)โ|0โฉ=|1โฉ:
Applying a global phase โiโ on RHS:
Now, solving the above for ๐โ we get: ๐โ=๐.
Since, we have all three angle values for our circuit, letโs try to build it using Qiskit and execute it to see if it gives us what we aimed for:
and on executing and plotting the results:
Letโs try to visualize the above states on a Q-sphere:
Voila! we have successfully achieved the desired state using CNOT and parametrized Rx and Ry gates. This is just a single case, that gives you an idea of how to deal with background mathematics, if you are a beginner. I encourage you to go ahead and try different combinations of gates to achieve some other state and practice along.
Note: all the above codes are performed using Python library Qiskit version 0.21.0. The Github repository having the code notebook can be found here and can be viewed here.
Thank you for reading through! I hope it helps and encourages you to learn and experiment more.