5

Here it is explained how to define a custom non-parametric gate in Qiskit. How do I define a new parametric gate?

For example, I want to have the $CCR_y$ gate defined as

enter image description here

in order to use it as

qc.ccry(theta, c1, c2, targ)

Is this possible?

glS
  • 27,510
  • 7
  • 37
  • 125
mavzolej
  • 2,271
  • 9
  • 18

1 Answers1

2

Although it might not be exactly what you are looking for, you can still use the method you would to create the non-parametric custom gate. You would just encapsulate it into a function that takes in the parameter and creates the custom gate:

def add_ccry(theta):
    qc = QuantumCircuit(3)
    qc.cry(theta/2, 1, 2)
    qc.cx(0, 1)
    qc.cry(-theta/2, 1, 2)
    qc.cx(0, 1)
    qc.cry(theta/2, 0, 2)
    gate = qc.to_gate()
    return gate

qc = QuantumCircuit(3, 3) qc.append(add_ccry(np.pi), [0, 1, 2]) # As an example. Theta will be numpy.pi