Adding two quantum gates

I want to know the possibility of implementing addition of two quantum gates. For example: i want to add (CZ(n) + Identity(n)): addition of n-controlled Z with identity gate. I would be very appreciated, if you share your idea with me.

Hi @sassan_moradi!

As of version v0.25 in PennyLane it’s now super easy to add operators! You can find more info about this in the release blog.

As an example, you can do

4 + qml.PauliZ(0)

And the result will be
PauliZ(wires=[0]) + 4*(Identity(wires=[0]))

Please let me know if this is what you were looking for!

1 Like

This was very helpful. Many thanks.


While the new Controlled class is currently more hidden and will be exposed in the next release, you can use it to create arbitrary controlled gates.

from pennylane.ops.op_math import Controlled
op = Controlled(qml.PauliZ(n), control_wires=list(range(n)) )

This object will be supported by default.qubit and lightning.qubit.

You can also create a different version via:

qml.ctrl(qml.PauliZ(n), control=list(range(n)) )

but it will not be natively supported by devices.

1 Like

Many thanks for you useful answer. I used qml.MultiControlledX and two Hadamards gate. I will use this as well.