How can I create the Pennylane equivalent of:

from qiskit import QuantumCircuit

circ = QuantumCircuit(2, 2)
circ.rxx(theta=0.3, qubit1=0, qubit2=1)
  • 4
    $\begingroup$ rjh324 and BẢO BẠCH GIA both give great answers for adding custom PennyLane operations. In addition, this gate has been added to the latest development version of PennyLane, available as qml.IsingXX(). $\endgroup$
    – Josh Izaac
    May 25, 2021 at 17:42
  • $\begingroup$ Fantastic! Thank you $\endgroup$ May 25, 2021 at 18:16

2 Answers 2


Here's a simple example if you're looking for a quick hack:

import pennylane as qml
import numpy as np

def RXX(theta):
    rxx = np.array([
        [np.cos(theta/2), 0, 0, -1j*np.sin(theta/2)],
        [0, np.cos(theta/2), -1j*np.sin(theta/2), 0],
        [0, -1j*np.sin(theta/2), np.cos(theta/2), 0],
        [-1j*np.sin(theta/2), 0, 0, np.cos(theta/2)]
    return rxx

dev = qml.device('default.qubit', wires=2)
def circuit(theta):
    qml.QubitUnitary(RXX(theta), wires=[0, 1])
    return qml.expval(qml.PauliZ(0))

Alternatively, you can create a new RXX class as they do in this custom gate tutorial:

import pennylane as qml
from pennylane.operation import Operation
from pennylane import numpy as np

class RXX(Operation):
    num_params = 1
    num_wires = 2
    par_domain = "R"

    grad_method = "A"
    grad_recipe = None # This is the default but we write it down explicitly here.

    generator = [(qml.PauliX(0) @ qml.PauliX(1)).matrix, -0.5]

    def decomposition(theta, wires):
        return [qml.PauliRot(theta, 'XX', wires=wires)]

    def _matrix(*params):
        theta = params[0]
        c = np.cos(0.5 * theta)
        s = np.sin(0.5 * theta)
        return np.array(
                [c, 0, 0, -s],
                [0, c, -s, 0],
                [0, -s, c, 0],
                [-s, 0, 0, c]

    def adjoint(self):
        return RXX(-self.data[0], wires=self.wires)

In this tutorial in PennyLane, they guide you to create a custom gate (Rxx gate)


After creating it you can simply use these code to add it:

dev = qml.device('default.qubit', wires=3)

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.