# Implementing gate with two parameters using Qiskit in Python

I am trying to implement the HHL algorithm (for solving $$Ax=b$$). I am assuming $$A$$ to be unitary and Hermitian so that I can find the Hamiltonian simulation for it easily. For any $$A$$ to be Hermitian and unitary, it has to be of form, $$A = \begin{pmatrix} x & \pm\sqrt{1-x^2}\\ \pm\sqrt{1-x^2} & x\end{pmatrix}$$ I reduced $$e^{i\alpha A}$$ to following (by using formula $$e^{i\theta A} = (\cos\theta)I + i(\sin\theta)A$$ where $$A^2=I$$), but I don't know how to implement it on Qiskit. $$e^{i\alpha A} = \begin{pmatrix} \cos\alpha+i\sin\alpha\cos\frac{\theta}{2} & i\sin\alpha \sin\frac{\theta}{2} \\ i\sin\alpha \sin\frac{\theta}{2} & \cos\alpha+i\sin\alpha\cos\frac{\theta}{2} \end{pmatrix} .$$ where $$\theta = 2\cos^{-1}{x}$$. How to construct this gate?

(Remark: I have corrected a typo in the matrix exponentiation).

Please notice that the matrix has the form:

$$e^{i\alpha A} = \begin{pmatrix} a & b \\ b & a \end{pmatrix}$$ with $$|a|^2 + |b|^2 = 1$$ Now, please notice that this matrix can be expanded as: $$\begin{pmatrix} a & b \\ b & a \end{pmatrix} = \begin{pmatrix} e^{i \arg(a)} & 0 \\ 0 & e^{i \arg(a)} \end{pmatrix} \begin{pmatrix} |a | & i|b| \\ i|b |& |a| \end{pmatrix}$$ Defining $$|a| = \cos \phi$$ $$|b| = \sin \phi$$ Thus we obtain: $$e^{i\alpha A} = e^{i \arg(a) I_2} e^{i\phi \sigma_x}$$ In short, the result constitutes of an overall phase multiplication and an $$R_x$$ gate.

Expressed in the question's original variables: $$\arg(a) = \arctan (\tan \alpha \cos \frac{\theta}{2})$$ and $$\phi = \arcsin (\sin \alpha \sin \frac{\theta}{2})$$

There are plenty of methods that can be used to implement a given unitary matrix.

## Only for 1-qubit gates ($$2\times 2$$ matrix):

1. The algorithm described in https://arxiv.org/abs/1212.6253 seems to be the most efficient at the moment. It is restricted to 1-qubit quantum gates ($$2\times 2$$ unitary matrices) and only decompose in the Clifford + T basis.
## For n-qubit gates ($$2^n \times 2^n$$ unitary matrices):
As you specifically mentioned Qiskit, you will probably be interested by this answer that show how to use Qiskit to initialise the $$|0^{\otimes n}\rangle$$ state to an arbitrary state.
If you give to this procedure the desired output state $$e^{i\alpha A}|0\rangle = \begin{pmatrix} \cos\alpha+i\sin\alpha\cos\frac{\theta}{2} & i\sin\alpha \sin\frac{\theta}{2} \\ i\sin\alpha \sin\frac{\theta}{2} & \cos\alpha+i\sin\alpha\cos\frac{\theta}{2} \end{pmatrix} \begin{pmatrix}1 \\ 0\end{pmatrix} = \begin{pmatrix} \cos\alpha+i\sin\alpha\cos\frac{\theta}{2} \\ i\sin\alpha \sin\frac{\theta}{2}\end{pmatrix}$$ then the quantum circuit generated by the procedure will implement the unitary $$e^{i\alpha A}$$.
The issue with this method is that you will need to recompute the unitary for every different value of $$\alpha$$ you encounter.