# Qml.ctrl problem

**URL:** <https://discuss.pennylane.ai/t/qml-ctrl-problem/1075>\
**Category:** PennyLane Help\
**Created:** [May 31, 2021, 4:57pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075 "2021-05-31T16:57:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [May 31, 2021, 4:57pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/1 "2021-05-31T16:57:39Z")

</div>

Hi! I’ve been working with qml.ctrl and I get this problem, could anyone help me?

```auto
DeviceError: Gate ControlledOperation not supported on device default.qubit.autograd

```

I share part of my code:

```python
import pennylane as qml
from pennylane import numpy as np

np.random.seed(23)   
n = 2
@qml.template
def function1(x,w):
    for i in range(n):
        qml.CZ(wires = [i, (i+1)%n])
        qml.RY(w[i], wires = i)
        
    for i in range(n):
        qml.RY(x[i], wires = i)
        qml.CZ(wires = [i, (i+1)%n])
        qml.RZ(x[i], wires = i)

def circuit(x,y,w):
    qml.adjoint(function1(x,w))
    function1(y,w)
    

dev = qml.device("default.qubit", wires = n + 1)
@qml.qnode(dev)
def function2(x,y,w):
    ops = qml.ctrl(circuit, control = n)
    qml.Hadamard(n)
    ops(x = x, y = y, w = w)
    qml.Hadamard(n)
    return qml.probs(wires = n)

def function3(x,y,w):
    probs = function2(x,y,w)
    print(probs)
    return probs[0] - probs[1]

function3([1]*n,[2]*n,[3]*n)

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [May 31, 2021, 5:03pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/2 "2021-05-31T17:03:41Z")

</div>

Thanks @Guillermo_Alonso!

Just a small comment, I think there is a small typo in your code:

```auto
qml.adjoint(function1(x,w))

```

should be

```auto
qml.adjoint(function1)(x,w)

```

After making that change, however, I think I have found the bug - `qml.CZ` doesn’t seem to support being made ‘controlled’! If you change the `qml.CZ` to `qml.CNOT`, then it works.

I think there are two things that need to be done here:

1. Update the error message so that it is more helpful. The error message in this case should have said `CZ operation does not support controls`.

2. Add support for the CZ operation to be controlled 🙂

I will open a bug over on the PennyLane GitHub to make sure this gets fixed.

---

<div class="post-metadata">

**Author:** ![Guillermo\_Alonso](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/guillermo_alonso/32/816_2.png) [@Guillermo\_Alonso](https://discuss.pennylane.ai/u/Guillermo_Alonso)\
**Post date:** [May 31, 2021, 5:08pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/3 "2021-05-31T17:08:26Z")

</div>

Cool! Thanks @josh💪

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [May 31, 2021, 5:47pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/4 "2021-05-31T17:47:44Z")

</div>

I’ve made a PR here to fix this bug: [https://github.com/PennyLaneAI/pennylane/pull/1376](https://github.com/PennyLaneAI/pennylane/pull/1376)

In the meantime @Guillermo_Alonso, you can install PennyLane directly from this PR so that your code will run:

```auto
pip install git+https://github.com/PennyLaneAI/pennylane.git@pennylane-release

```

---

<div class="post-metadata">

**Author:** ![Julio\_Rocha](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/julio_rocha/32/917_2.png) [@Julio\_Rocha](https://discuss.pennylane.ai/u/Julio_Rocha)\
**Post date:** [February 10, 2022, 6:31am UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/6 "2022-02-10T06:31:14Z")

</div>

Hi,

I am struggle with qml.ctrl. I am trying to build a controlled time evolution gate and I completely failed. This is the code line:

qml.ctrl(qml.ApproxTimeEvolution(H,t,itrotter), control=w)

H is the hamiltonian, t is time, itrotter is the Trotter step, and w the control tire. So, I tried something simpler, that I show in the following. So I realized that the qml.ctrl is not working properly, and I want to know what I am doing wrong.

```python
import pennylane as qml
from pennylane import numpy as np

Nqubits = 4
phi = np.pi/2
dev = qml.device('default.qubit', wires=Nqubits)

@qml.qnode(dev)
def circuit(N, theta, w1, w2, w3, w4):
   qml.PauliX(w1)
   qml.PauliX(w3)
   qml.ctrl(qml.RX(theta,wires=w1), control=w2)
   qml.CRX(theta, wires=(w4,w3))
   return [qml.expval(qml.PauliZ(i)) for i in range(N)]

circuit(Nqubits,phi,0,1,2,3)
drawer = qml.draw(circuit)
print(drawer(Nqubits,phi,0,1,2,3))

exp_value = circuit(Nqubits,phi,0,1,2,3)
print(exp_value)

```

And this is the result that I got:

0: ──X───RX(1.57)──┤ ⟨Z⟩  
1: ────────────────┤ ⟨Z⟩  
2: ──X──╭RX(1.57)──┤ ⟨Z⟩  
3: ─────╰C─────────┤ ⟨Z⟩

[-2.22044605e-16 1.00000000e+00 -1.00000000e+00 1.00000000e+00]

As you can see, the wires=0 was rotated even with the control wires=1 being equal to |0\>.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [February 10, 2022, 9:47am UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/7 "2022-02-10T09:47:51Z")

</div>

Hi @Julio_Rocha! `qml.ctrl` works similarly to `qml.adjoint`; in particular, it takes the operation or template to transform, and returns a _new_ operation. So it can be used like this:

```python
qml.ctrl(qml.RX, control=w2)(theta, wires=w1)

```

Note the order of the arguments! We first create a controlled RX gate, and _then_ we pass the parameters.

This can be seen more clearly by breaking it up into two steps:

```python
# create a controlled version of RX, where
# the control wire is w2
ctrl_rx_gate = qml.ctrl(qml.RX, control=w2)

# this new gate now takes the same parameters as RX
ctrl_rx_gate(theta, wires=w1)

```

---

<div class="post-metadata">

**Author:** ![Julio\_Rocha](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/julio_rocha/32/917_2.png) [@Julio\_Rocha](https://discuss.pennylane.ai/u/Julio_Rocha)\
**Post date:** [February 10, 2022, 3:04pm UTC](https://discuss.pennylane.ai/t/qml-ctrl-problem/1075/8 "2022-02-10T15:04:33Z")

</div>

Hi Josh,

it works perfectly! Thank you very much! 😃
