# Control Unitary Gates

**URL:** https://discuss.pennylane.ai/t/control-unitary-gates/541
**Category:** PennyLane Help
**Created:** [August 30, 2020, 5:26am UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541 "2020-08-30T05:26:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Leeseok\_Kim](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/leeseok_kim/32/106_2.png) [@Leeseok\_Kim](https://discuss.pennylane.ai/u/Leeseok_Kim)
#### Post date: [August 30, 2020, 5:26am UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/1 "2020-08-30T05:26:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![theodor](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/theodor/32/178_2.png) [@theodor](https://discuss.pennylane.ai/u/theodor)
#### Post date: [August 31, 2020, 3:10pm UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/2 "2020-08-31T15:10:13Z")

</div>

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](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.QubitUnitary.html)). Then you could e.g. define a two qubit arbitrary controlled-unitary like this:

```python
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](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.CRot.html)) 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.

---

<div class="post-metadata">

### Author: ![Leeseok\_Kim](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/leeseok_kim/32/106_2.png) [@Leeseok\_Kim](https://discuss.pennylane.ai/u/Leeseok_Kim)
#### Post date: [September 6, 2020, 8:09am UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/3 "2020-09-06T08:09:43Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![theodor](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/theodor/32/178_2.png) [@theodor](https://discuss.pennylane.ai/u/theodor)
#### Post date: [September 8, 2020, 2:56pm UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/4 "2020-09-08T14:56:55Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zj-lucky](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/zj-lucky/32/788_2.png) [@zj-lucky](https://discuss.pennylane.ai/u/zj-lucky)
#### Post date: [December 22, 2020, 1:49pm UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/5 "2020-12-22T13:49:29Z")

</div>

Thanks, It is very help to see your reply, I would like to ask how to design single control-2-qubit gate(the 2 qubit gate is the universal gate which has 15 parameters),the 2-qubit universal gate has given in Pennylane, however, I want to add a control on it, could you help me? Thanks very much!

---

<div class="post-metadata">

### Author: ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)
#### Post date: [December 22, 2020, 8:57pm UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/6 "2020-12-22T20:57:58Z")

</div>

Hi @zj-lucky,

The answer here is similar to above. For any 2-qubit unitary, you can be able to repeat the construction above, except the matrix you supply will be 8x8 (3 qubits):

```auto
CU = np.array([[1, 0, 0, 0, 0, 0, 0, 0], 
               [0, 1, 0, 0, 0, 0, 0, 0],
               [0, 0, 1, 0, 0, 0, 0, 0],
               [0, 0, 0, 1, 0, 0, 0, 0],
               [0, 0, 0, 0, a, b, c, d], 
               [0, 0, 0, 0, e, f, g, h],
               [0, 0, 0, 0, i, j, k, l],
               [0, 0, 0, 0, m, n, o, p]])

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

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

print(cu_circuit(CU))

```

Depending on what you would like to do, this might be sufficient.

But if you want the parameters of this circuit to be _trainable_ (while still remaining unitary), that is a bit trickier. Even without a control, the general two-qubit gate requires additional effort to make trainable (e.g., decomposing it into single-qubit parametrized gates and two-qubit fixed gates, or using the [stochastic-parameter-shift rule](https://arxiv.org/abs/2005.10299)). That would be the nontrivial part. Once you had that, you could make a controlled-U gate by sandwiching control gates on either side, e.g., like you see [here](http://web.mit.edu/2.111/www/2010/ps6_2010Sol.pdf).

---

<div class="post-metadata">

### Author: ![zj-lucky](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/zj-lucky/32/788_2.png) [@zj-lucky](https://discuss.pennylane.ai/u/zj-lucky)
#### Post date: [December 23, 2020, 4:25am UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/7 "2020-12-23T04:25:31Z")

</div>

@nathan Thank you very much for your reply, but there is still no way to add control to U. How to edit the code to add C to U?  
 ![%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20201223122451](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/f23ff9cc0c61cff3425015e9cbfc9561feb3043b.png)

---

<div class="post-metadata">

### Author: ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)
#### Post date: [December 23, 2020, 7:39pm UTC](https://discuss.pennylane.ai/t/control-unitary-gates/541/10 "2020-12-23T19:39:36Z")

</div>

Hi @zj-lucky,

Thanks for sharing the code example, it helps me understand better what you’re aiming for (i.e., creating a controlled gate out of a PennyLane template).

Currently we do not have this feature natively available. If you’d like to put in an official feature request, the best place to do that is our [GitHub issues page](https://github.com/pennylaneai/pennylane/issues).

You could also do this manually, but it will require some work obtaining the decomposition. You can check out this [StackExchange question](https://quantumcomputing.stackexchange.com/questions/1344/given-a-decomposition-for-a-unitary-u-how-do-you-decompose-the-corresponding) which seems to provide the most fleshed-out recipe for the case of creating a two-qubit controlled unitary. You already have a suitable decomposition for the (uncontrolled) unitary above, so that post seems to be tackling the exact same question as you.
