3
$\begingroup$

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?

$\endgroup$

3 Answers 3

2
$\begingroup$

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$:

enter image description here

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

$\endgroup$
5
  • 1
    $\begingroup$ Smart! Thank you! $\endgroup$
    – Frank Wang
    May 16, 2020 at 7:03
  • 1
    $\begingroup$ @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)$) $\endgroup$ May 16, 2020 at 7:20
  • $\begingroup$ Please comment alongside the downvote. I want to know if I did a math mistake or is there another problem? $\endgroup$ May 18, 2020 at 20:29
  • $\begingroup$ I didn’t downvote you. I have accepted your answer. $\endgroup$
    – Frank Wang
    May 19, 2020 at 7:30
  • $\begingroup$ @FrankWang I know ;), but somebody else did and I don't understand why. $\endgroup$ May 19, 2020 at 7:55
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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) ├
     └──────────────┘└───┘└───────────────┘└───┘└──────────────────┘
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.