I'm trying to implement the parametric $\text{iSWAP}$ gate, also known as $\text{XY}(\theta)$, in Qiskit.

$$ \text{XY}(\theta)= \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos{\theta/2} & i\sin{\theta/2} & 0 \\ 0 & i\sin{\theta/2} & \cos{\theta/2} & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}. $$

Furthermore, once I've implemented this gate I would like to use it in order to decompose quantum circuits in terms of single-qubit gates and $\text{XY}(\theta)$ as the only two-qubit gate.

What are the steps that I need to do in order to achieve this?



2 Answers 2


Update Qiskit 0.35 introduced a new gate XXPlusYYGate.

$$\newcommand{\th}{\frac{\theta}{2}}\\\begin{split}R_{XX+YY}(\theta, \beta)\ q_0, q_1 = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\th) & i\sin(\th)e^{i\beta} & 0 \\ 0 & i\sin(\th)e^{-i\beta} & \cos(\th) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}$$ So, you can now add parameterized $\text{XY}$ to your circuit as follows:

theta = Parameter('θ')
circ.append(XXPlusYYGate(theta, 0), [0, 1])

Original Answer

For the first part of your question, we have $$XY(\theta) = \exp(-i {\frac{\theta}{2}} (X{\otimes}X + Y{\otimes}Y))$$ And since $X{\otimes}X$ and $Y{\otimes}Y$ commute, we can write it as $$XY(\theta) = \exp(-i {\frac{\theta}{2}} X{\otimes}X) \exp(-i {\frac{\theta}{2}} Y{\otimes}Y)$$ Qiskit already has these two gates: $$R_{XX}(\theta) = \exp(-i {\frac{\theta}{2}} X{\otimes}X)$$ And, $$R_{YY}(\theta) = \exp(-i {\frac{\theta}{2}} Y{\otimes}Y)$$

Hence, the implementation of $XY(\theta)$ as a parameterized gate in Qiskit will be as simple as

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

theta = Parameter('θ')

circuit = QuantumCircuit(2)
circuit.rxx(theta, 0, 1)
circuit.ryy(theta, 0, 1)
param_iswap = circuit.to_gate()

Another Solution

If you want to use more basic gates than rxx and ryy, you can use Qiskit's Operator Flow:

H = 0.5 * ((X^X) + (Y^Y))
theta = Parameter('θ')
evolution_op = (theta * H).exp_i() # exp(-iθH)

trotterized_op = PauliTrotterEvolution(trotter_mode = Suzuki(order = 1, reps = 1)).convert(evolution_op)
circuit = trotterized_op.to_circuit()

The composition: enter image description here And as before

param_iswap = circuit.to_gate()

For the second part of your question, I think the best answer you can have is the one mentioned in the comments by @epelaaez, as it is recent and from a member of Qiskit's development team.


You can implement an arbitrary unitary matrix as a Unitary Gate using the UnitaryGate class that qiskit provides. This class can be instantiated with an np.ndarray containing the definition of your valid unitary gate and a name for that unitary. It would look something like -

from qiskit.extensions import UnitaryGate
def U_theta(theta):
    arr = np.array([[1,0,0,0],
                    [0,np.cos(theta/2), 1j*np.sin(theta/2), 0],
    u_gate = UnitaryGate(data = arr,label = 'iSwap'+str(theta))
    return u_gate

gate1 = U_theta(0.5) # would return a gate with theta = 0.5

This gate can be used in your quantum circuits and attached to the relevant qubits using the compose function.

Regarding transpilation, I am not sure if qiskit allows us to give a custom basis set for transpilation but you could always keep a pre-transpiled version of your $iSWAP$ gate and just attach it to your current circuit.

Hope it helps!

  • 1
    $\begingroup$ This answer might helpl with the custom transpilation. $\endgroup$
    – epelaez
    Commented Jun 29, 2021 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.