# Implementing controlled $\frac{\pi}{2}$ y and z rotation in Qiskit

I'm trying to implement a controlled $$\frac{\pi}{2}$$ rotation along y axis and z axis (two individual gates) in Qiskit but I'm stuck. My thought was calculating the square root of the y and z pauli matrices (done), and implement using the CU3 gate. However, I wasn't able to find the correct parameters ($$\theta, \phi, \lambda$$) for the matrices I got. Is there a workaround for this? Thanks in advance.

Edit: I know if I remove the overall phase of $$\sqrt{Y}$$ then I can get the correct parameters, but I think since this is not an overall phase of all qubit so if I remove the phase I'll get a different gate?

The circuit for the controlled $$R_y(t)$$ that is true also for $$R_z$$ (just change the indexes). For $$R_x$$, instead of $$cx$$ we should use $$cz = I \otimes H \cdot cx \cdot I \otimes H$$: The corresponding code:

t = np.pi/2 # for the question's case
circuit = QuantumCircuit(2)
circuit.ry(t/2, 1)
circuit.cx(0, 1)
circuit.ry(-t/2, 1)
circuit.cx(0, 1)


Here are the expressions for $$cx$$ and $$R_y(t)$$:

$$\begin{equation*} cx = |0\rangle \langle 0| I + |1\rangle \langle 1 | X \qquad R_y(\theta) = \cos(\theta/2)I - i \sin(\theta/2)Y \end{equation*}$$

By taking into account that $$XYX = -Y$$ and thus $$XR_y(\theta)X = R_y(-\theta)$$:

$$cx \cdot I \otimes R_y(-t/2) \cdot cx = |0\rangle \langle0|R_y(-t/2) + |1\rangle \langle 1| R_y(t/2)$$

and if we multiply this with $$R_y(t/2)$$ from the right side we will obtain:

$$|0\rangle \langle0| I + |1\rangle \langle 1| R_y(t) = cR_y(t)$$

because $$R_y(\theta_1)R_y(\theta_2) = R_y(\theta_1 + \theta_2)$$.

• Smart! Thank you! – Frank Wang May 16 at 7:03
• @FrankWang you are welcome. Note that I had a mistake that I have corrected. The circuit for $R_x$ is slightly different. In that case, we should use $cz$-s ($ZXZ = -X$ and thus $Z \cdot R_x(t) \cdot Z = R_x(-t)$) – Davit Khachatryan May 16 at 7:20
• Please comment alongside the downvote. I want to know if I did a math mistake or is there another problem? – Davit Khachatryan May 18 at 20:29
• I didn’t downvote you. I have accepted your answer. – Frank Wang May 19 at 7:30
• @FrankWang I know ;), but somebody else did and I don't understand why. – Davit Khachatryan May 19 at 7:55

You can use gate $$CU3$$ directly on IBM Q and then leave the gate decomposition on transpiler.

There are relations between rotational gates and U3 gate:

$$Ry(\theta) = U3(\theta, 0, 0)$$

$$Rx(\theta) = U3(\theta, -\pi/2, \pi/2).$$

If you don't know the decomposition for this kind of thing you can just program it at a high level in Qiskit and then rely on the compiler to decompose it.

For this you can do two things. Either start with an RYGate(pi/2) then control it; or start with a YGate(), raise it to power 1/2, then control it.

(Note the answer from these two approaches will differ by a relative phase, as YGate and RYGate(pi) have a global phase difference in their matrix definitions in Qiskit).

For the first approach, here's the code:

import numpy as np
import qiskit as qk
from qiskit.circuit.library import RYGate

circ = qk.QuantumCircuit(2)

circ.append(RYGate(np.pi/2).control(), [0, 1])

print('before...')
print(circ.draw())

new_circ = qk.transpile(circ, basis_gates=['u3', 'cx'], optimization_level=3)

print('after...')
print(new_circ_1.draw(fold=100))

before...

q_0: ─────■──────
┌────┴─────┐
q_1: ┤ RY(pi/2) ├
└──────────┘
after...

q_0: ──────────────────■─────────────────────■──────────────────────
┌──────────────┐┌─┴─┐┌───────────────┐┌─┴─┐┌──────────────────┐
q_1: ┤ U3(0,0,pi/2) ├┤ X ├┤ U3(-pi/4,0,0) ├┤ X ├┤ U3(pi/4,-pi/2,0) ├
└──────────────┘└───┘└───────────────┘└───┘└──────────────────┘