 # Control Unitary Gates

Hi! How can I design a controlled-U gate?

Hi @Leeseok_Kim,

Depending on what type of controlled-U gate you wish to apply, there are several different possible solutions to this.

If you simply want to create an arbitrary controlled-unitary gate you would need to write down the matrix representation of it and then use `qml.QubitUnitary` to apply it (see here for details). Then you could e.g. define a two qubit arbitrary controlled-unitary like this:

``````CU  = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, a, b],
[0, 0, c, d]])

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

@qml.qnode(dev)
def my_cu(U):
...
qml.QubitUnitary(U, wires=[0, 1])
...
return qml.expval(qml.PauliZ(1))

print(my_cu(CU))
``````

where `a`, `b`, `c` and `d` would be the unitary that you wish to apply to the target qubit and `...` could be replaced by any other operations you wish to apply in the circuit. This can be expanded to include several control qubits and/or several target qubits, as well as changing which qubits are control/target. You would need to write down and define these specific unitaries though.

In the above case you could also use the `qml.CRot` gate (see here) since you only have 1 control qubit and 1 target qubit. If you want to have more complicated unitaries you would need to use the above `QubitUnitary` approach.

Thank you so much! What if I already set up the U gate through lists of quantum gates not in terms of matrix and try to make it to controlled_U that is controlled by some specific wire index?

Should I do something like decomposing the U gate into matrix form and change that matrix and then use QubitUnitary thing?

Hi @Leeseok_Kim,

Unfortunately, I think that extracting the matrix representation and then using `QubitUnitary` to define a new gate (adding the conditional qubit to it in-between) might be necessary.

It would definitely be nice to be able to add controls to already existing operations, but as of now it’s possible in PennyLane in a straight-forward manner.

1 Like