# Controlled unitary from the HHL algorithm – practical implementation using Qiskit

I have a question about the implementation of the controlled unitary $$e^{iAt}$$, from the paper Demonstration of a Quantum Circuit Design Methodology for Multiple Regression, using the Qiskit framework.

In this paper, a $$4\times 4$$ Hermitian matrix $$A$$ is transformed into $$4\times 4$$ unitary and then decomposed by applying the GLOA algorithm. I'm not interested in this particular example but rather in general implementation of the arbitrary unitary by using Qiskit. However, it is convenient to use this case as an example. In this paper, we have a series of controlled $$4\times4$$ unitary operators implemented according to the phase estimation algorithm. Since the resulting circuit is given in the paper, I have successfully implemented the unitary but I have a difficult time to apply the controlled action of the given unitary in Qiskit framework.

In other words, how can I implement a controlled unitary for some arbitrary $$A$$? Should I use a given circuit from the paper and apply control to each gate in the circuit or there is another way?

• – glS
Apr 17, 2019 at 9:05

Disclaimer: I'm the one that wrote the code of the 4x4 HHL.

Controlling a quantum gate $$U$$ can be achieved by controlling each of the $$U_i$$ gates that are composing $$U$$.

For the specific example you are considering, the implementation is available online. Some remarks about the code:

1. I think the code is not up to date with the last version of Qiskit. I did not re-tested recently. The code should be compatible with Qiskit 0.6.1 as written in the requirements.txt file.
2. The implementation of the controlled unitary is available here.
3. The controls are not really explicit, but you can see that each gate used in the implementation of the unitary is controlled by the same qubit.
4. Everything in the above implementation has been hand-tuned. You will probably not be able to understand the generic algorithms to control an arbitrary gate with the code.

In the general case, controlling an arbitrary gate may be programmatically quite hard. For example, in order to control a $$Z$$ gate (and so to perform a controlled-$$Z$$), you will probably end up decomposing the controlled-$$Z$$ gate as 2 $$H$$ gates around a controlled-$$X$$. Another example is how do you implement a multi-controlled-$$X$$ gate. There are known algorithms, but they should be implemented beforehand.

End note: if you want to discuss about the implementation, feel free to contact me at the mail address at the end of the README.

• Thank you very much for the help and invitation to contact you about the algorithm. I sincerely appreciate your time and effort to clarify me few things that have bother me for a while. I had some preliminary ideas about the controlling actions in your code, but I wasn't sure about the implementation. All other papers on this topic that I have came across mostly deal with simple unitary operatora, while in yours research one practical example have been implemented (which I find very interesting and helpful). Regarding the GLO algorithm, you are absolutely right. In general, I understand the me Apr 17, 2019 at 13:15
• @ljubab Hi. Welcome to Quantum Computing SE! If this answer addressed your question sufficiently, don't forget to accept it by clicking on the checkmark. Apr 18, 2019 at 19:29