# Realizing Haar random unitary matrix on IBM Q

I am thinking about if it is possible to achieve Haar random single qubit unitary matrix on some real quantum computers like IBM Q. I am reading a paper https://arxiv.org/abs/2203.04338. In this paper, they mention that they use Haar uniform single qubit rotation. But I don't know how this is achieved and how large the error should be.

If you want to do it with qiskit, you can use the random_unitary function right away:

from qiskit.quantum_info import random_unitary
from qiskit import QuantumCircuit
from qiskit.extensions import UnitaryGate

haar_random_gate = UnitaryGate(random_unitary(2))
qc = QuantumCircuit(1)
qc.append(haar_random_gate, [0])


You can then execute this circuit normally.

If you want to do it by hand, which can be useful if you want to use another framework which doesn't have such a method, according to this link, an Haar-random matrix is a matrix that can be written as: $$U_H(\theta,\varphi,\omega)=\begin{pmatrix}\mathrm{e}^{-\mathrm{i}\frac{\varphi+\omega}{2}}\cos\left(\frac{\theta}{2}\right)&-\mathrm{e}^{\mathrm{i}\frac{\varphi-\omega}{2}}\sin\left(\frac{\theta}{2}\right)\\\mathrm{e}^{-\mathrm{i}\frac{\varphi-\omega}{2}}\sin\left(\frac{\theta}{2}\right)&\mathrm{e}^{\mathrm{i}\frac{\varphi+\omega}{2}}\cos\left(\frac{\theta}{2}\right)\end{pmatrix}$$ with $$\varphi$$ and $$\omega$$ being uniformly sampled from $$[0\,;\,2\pi]$$ and $$\theta$$ being sampled from $$[0\,;\pi]$$ with density function $$f(\theta)=\frac12\sin(\theta)$$.

You thus simply have to create such a matrix with the correct distribution (you can for instance take the code from the Pennylane link above) to generate such a matrix.