Qml.ctrl problem

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

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

I share part of my code:

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!

Thanks @Guillermo_Alonso!

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

qml.adjoint(function1(x,w))

should be

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 :slight_smile:

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

Cool! Thanks @josh :muscle:

I’ve made a PR here to fix this bug: 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:

pip install git+https://github.com/PennyLaneAI/pennylane.git@pennylane-release
1 Like