# Custom gates on IBM Q

I realized that QASM supports custom gates. However, when I tried to create the gate, transpiling error appeared both on simulator and real quantum processor. I suspect that IBM has not implemented this functionality fully yet.

Does anybody (maybe from IBM) know when IBM will provide users with possibility to use custom gates?

• What do you mean by "custom gates"? Commented Dec 13, 2019 at 14:30
• @MatthewStypulkoski: you can create piece of code begining like this gate name (parameters) {}, put some part of a quantum algorithm inside the brackets and then call this gate in code. Commented Dec 13, 2019 at 14:42
• You can already implement an arbitrary single-qubit gate using the U3 gate (qiskit.org/documentation/api/qiskit.extensions.U3Gate.html) Commented Dec 13, 2019 at 14:43
• @Arthur-1: Custom gate allows you to create multiple qubits gates. Please see my reaction on Matthew comment above. Commented Dec 13, 2019 at 14:46
• @MartinVesely so if I understand you correctly, something like the following example would be what you are looking for: a single, custom gate that contains multiple H gates. You would be able to put this custom gate into a new circuit, which would make it appear as only 1 gate, but would contain and run the encapsulated H gates? Commented Dec 13, 2019 at 15:10

This is possible by using Composite Gates in Qiskit. With composite gates, you can create a circuit of gates, turn that circuit into an Instruction, and attach it to a new circuit which will perform the gates that were within your old circuit. Here is an example:

from qiskit import QuantumCircuit

qc = QuantumCircuit(2, name='bell')
qc.h(0)
qc.cx(0, 1)
custom_gate = qc.to_instruction()

new_circ = QuantumCircuit(2)

# Append custom gate. The parameters are the Instruction you made, and the qubits you will use with it
new_circ.append(custom_gate, [0, 1])

print(new_circ)

'''
This is the output
┌───────┐
q_0: |0>┤0      ├
│  bell │
q_1: |0>┤1      ├
└───────┘
'''


The to_instruction() method from QuantumCircuit turns your circuit into an instruction which can then be appended to another circuit in the future. It appears as a single gate on the new circuit, which you can name to make it more organized. When the new circuit is executed, it will run this composite gate, which will make it run through the bell state code we set earlier.

For more information about composite gates, you can go to this tutorial and scroll down to the "Composite Gates" section

• Thank you for comprehensive and useful answer. However, I am afraid that I was mainly asking about QASM and composer and when the functionality will be available. Commented Dec 13, 2019 at 22:33

There are two answers to this. Either turn your gate into a rotation that can be expressed on the Bloch sphere by and Rx or Ry operation, or use the "Add" feature by the gates at the top. This will create a gate in the code on the right under QASM that you will have to modify.

gate r23(param) q, r {
cx q, r;
}


• Thanks. My question is a little bit outdated. Now this functionality works fine which was not the case two years ago. Still, thanks. Commented Feb 23, 2022 at 7:47