6

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?

Martin Vesely
  • 15,244
  • 4
  • 32
  • 75

2 Answers2

8

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

1

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;
}

Custom Gate

Add Gate

Add Code