How to find correct angle values of Parametrized Quantum R gates using Qiskit

Beginner-friendly tutorial encouraging baby-steps in Quantum Computing using Qiskit

Dr. Pragya Katyayan
5 min readOct 1, 2020
Source: Wikipedia. Bloch sphere representing different basis (x, y, z), states (0, 1, ๐œ“) and relevant angle values (๐œƒ and ฯ•) as visualized for a single qubit.

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:

The circuit generated by above code snippet

To visualize the effect of the above circuit, we execute it on the statevector_simulator provided by Qiskit Aer.

The above code gives us a histogram showing both states |01โŸฉ and |10โŸฉ recorded with equal probabilities.

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).

Figure 1: Demo circuit displaying tentative arrangement of gates on both qubits and denoting intermediate states.

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:

Building the circuit with calculated angle values for all three gates

and on executing and plotting the results:

Executing the above circuit and plotting the values

Letโ€™s try to visualize the above states on a Q-sphere:

Plotting on Q-sphere for two qubits
Resultant states visualized on Q-sphere for multiple (two) qubits

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.

All the above code has been executed in Python using Qiskit library (v0.21.0) for Quantum Computing on Jupyter Notebook

Thank you for reading through! I hope it helps and encourages you to learn and experiment more.

--

--

Dr. Pragya Katyayan

PhD (CS) with specialization in Quantum Computing and Natural Language Processing.