# Given the following matrix representing a quantum gate, how do I implement it in Qiskit?

I have the following matrix and I need to build the corresponding quantum gate operator using Qiskit native gates (I guess phase and $$CNOT$$ is the universal set but I'm not sure):

$$\begin{pmatrix} \cosh\left(\frac{\theta}{2}\right)+i\sinh\left(\frac{\theta}{2}\right)&0\\ 0&\cosh\left(\frac{\theta}{2}\right)-i\sinh\left(\frac{\theta}{2}\right) \end{pmatrix}$$

How should I proceed in order to find a suitable sequence of gates implementing this operator?

• Hi Alberto, just to clarify... do you mean - How do I find a quantum circuit to implement this unitary (i.e a sequence of gates)? The title of your question seems to ask for a way to express the unitary as a tensor product of other matrices which I'm not sure makes sense for a single qubit unitary. Jan 19, 2023 at 18:28
• My bad, it's what you meant for this 2x2 case. Jan 19, 2023 at 19:26
• Do you mean to write sin and cos instead of sinh and cosh? The matrix you wrote is not unitary...
– hft
Jan 19, 2023 at 22:08
• If it is sin and cos as opposed to cosh and sinh then this unitary is just an Rz gate . That's if you apply the complex exponential formula and assuming the minus signs match up qiskit.org/documentation/stubs/… Jan 19, 2023 at 22:46
• This matrix is unitary only if $\theta=0$. Jan 20, 2023 at 16:14

First of all, your operator (let me call it $$U$$) is represented by a $$2 \times 2$$ matrix, so it simply acts as a rotation on a single qubit (no $$CNOT$$ or any other multiple-qubit gates are needed).

That said, $$U = U(\theta)$$, meaning that your operator is parameterized by an angle $$\theta$$ and, of course, the actual unitary matrix depends on the value assigned to $$\theta$$. Unfortunately, the latest Qiskit version (qiskit-terra==0.22.4) does not yet support the transformation of the parameterized unitary operator $$U(\theta)$$ into the corresponding parameterized quantum circuit (and, by the way, neither the other way around: take a look here). So, as far as I know, there is no way to get directly the general decomposition of $$U(\theta)$$ using Qiskit.

However, a workaround to the problem could be to implement a Python function U(theta) returning your operator as a numpy array:

import numpy as np

def U(theta):
return np.array([[np.cosh(theta / 2) + 1j*np.sinh(theta / 2), 0],
[0, np.cosh(theta / 2) - 1j*np.sinh(theta / 2)]])


Then, you call this function (passing a specific value of theta) and you create the corresponding quantum circuit by the QuantumCircuit.unitary method as follows:

from qiskit import QuantumCircuit

qc = QuantumCircuit(1)
matrix = U(theta=<theta_value>)
qc.unitary(matrix, 0)

• This code gives an error stating that the matrix is not unitary... because the matrix is not unitary... (Not your fault, I upvoted)
– hft
Jan 19, 2023 at 22:09