# Generic 2-qubit gate in qiskit

I need to implement a 2-qubit gate of the following form in qiskit (I've barely started using it, so I'm happy to try a different package if that's worth it)

$$A(\vec c)=\prod_{j=1}^3[I\otimes I\cos(c_j/2)-i\sigma_j\otimes\sigma_j\sin(c_j/2)]$$

(this is taken from this paper https://arxiv.org/abs/1306.2811).

Perhaps qiskit is the wrong tool, but I've been trying to find something in the documentation, but I find it very difficult to parse. There is UnitaryGate https://qiskit.org/documentation/stubs/qiskit.extensions.UnitaryGate.html and Gate https://qiskit.org/documentation/stubs/qiskit.circuit.Gate.html#qiskit.circuit.Gate, but apart from saying that the function needs parameters, there isn't any more documentation.

There is also the UnitaryGate.power(x) which could be useful if I knew how to define say a function that returns a generic XX gate.

Yet another option could be to have a function that would take a matrix, such as

g = twoQubitGateFrom4-by-4Matrix( -- some 4x4 matrix that is the desired unitary -- )


Such that later I can apply it some where, e.g.,

myQuantumCircuit.g(3,5)


or even parameterized?

myQuantumCircuit.g(3,5,vecAlpha)


The iso() function allows you to add a gate, defined by means of a unitary, to your quantum circuit:

https://qiskit.org/documentation/stubs/qiskit.circuit.QuantumCircuit.iso.html

• Thanks, this is great! Just to check, if I, say, want a single qubit X, then I would do qc.iso([[0,1],[1,0]],1,[])? Also, in principle there are several ways to write the basis, but presumably it's {|0..00>,|0..01>,|0..10>, ...}? Thanks! – Daniel May 31 at 21:52
• Yes, that is the way to use iso(). The basis is the computational one, by default. – Michele Amoretti Jun 1 at 5:31

For completeness, the operator $$A$$ you're describing can be implemented more efficiently using the rotations $$R_{XX}, R_{YY}$$ and $$R_{ZZ}$$: $$A(\vec c)=\prod_{j=1}^3[I\otimes I\cos(c_j/2)-i\sigma_j\otimes\sigma_j\sin(c_j/2)] = R_{XX}(c_1)R_{YY}(c_2)R_{ZZ}(c_2)$$ as $$R_{XX}(\theta) = e^{-i\theta/2 X \otimes X} = \cos\left(\frac{\theta}{2}\right) I \otimes I - i\sin\left(\frac{\theta}{2}\right) X \otimes X$$ and analogously for $$YY$$ and $$ZZ$$. The isometry and initialize functions are very generic and work for any input. Therefore the gate decompositions might not be optimal, especially if the number of qubits gets large.

In Qiskit that would simply be

from qiskit import QuantumCircuit

c = [0.2, 0.3, 0.4]
A = QuantumCircuit(2)
A.rzz(c[2])  # remember: R_ZZ is applied first to the state!
A.ryy(c[1])
A.rxx(c[0])