QuantumFunctionError

I created a quantum node with this circuit:
def single_input_circuit(params, x):
qml.Displacement(x[0], 0, wires=0)
qml.RY(params[0], wires=0)
return qml.expval.PauliZ(0)

but I got this error
QuantumFunctionError: Continuous and discrete operations are not allowed in the same quantum circuit.
I don’t understand why??

Hi @EMY91,

In PennyLane, we support two main models of quantum computation:



There are several important things to note here:

  • You cannot mix continuous and discrete operations in the same QNode.
    In your case, you are using qml.Displacement (a continuous operation) as well as qml.RY (a discrete operation) and qml.expval.PauliZ (a discrete expectation)

  • If the QNode device is a continuous device, you must use only continuous operations. Likewise, if your QNode device is a discrete device, you must use only discrete operations.
    For example, default.qubit can use qml.RY, but not qml.Displacement.

I hope that helps, let me know if you have any other questions.

1 Like

I think this a weakness of Pennylane. i encountered the same problem in Pennylane:

@qml.qnode(dev)
def circuit(x1, x2):

qml.Hadamard(wires=0)


qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[1])

qml.RX(2*pi, wires=[0]) #second gate
qml.RX(pi, wires=[2])

qml.RX(2*pi, wires=[0]) #third gate
qml.RX(pi, wires=[3])

qml.templates.MottonenStatePreparation(x1, wires=[1, 2, 3])

qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[3])

qml.RX(2*pi, wires=[0]) #second gate
qml.RX(pi, wires=[2])

qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[1])

qml.X(wires=0)


qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[1])

qml.RX(2*pi, wires=[0]) #second gate
qml.RX(pi, wires=[2])

qml.RX(2*pi, wires=[0]) #third gate
qml.RX(pi, wires=[3])

qml.templates.MottonenStatePreparation(x2, wires=[1, 2, 3])

qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[3])

qml.RX(2*pi, wires=[0]) #second gate
qml.RX(pi, wires=[2])

qml.RX(2*pi, wires=[0]) #first gate
qml.RX(pi, wires=[1])

qml.X(wires=0)

qml.Hadamard(wires=0)

return qml.expval(qml.PauliZ(0))

Hi @sassan_moradi,

Yes, this circuit wouldn’t be possible to run due to using the continuous variable operation qml.X with other qubit operations. I hope my reply in the other thread can help you out. Otherwise, feel free to keep asking us questions.

i solved the problem. Thanks.

1 Like

That’s great! Let us know if you have any other issues or questions!

@qml.qnode(dev)
def circuit1(x1, x2):
    
    qml.Hadamard(wires=0)
    
    qml.U3(2*pi, 0, 0, wires=[0])  #first gate
    qml.U3(pi, -pi/2, pi/2, wires=[1])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #second gate
    qml.U3(pi, -pi/2, pi/2, wires=[2])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #third gate
    qml.U3(pi, -pi/2, pi/2, wires=[3])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #fourth
    #qml.U3(pi, -pi/2, pi/2, wires=[4])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #fifth gate
    #qml.U3(pi, -pi/2, pi/2, wires=[5])
    

    qml.templates.MottonenStatePreparation(x1, wires=[1, 2, 3])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #first gate
    #qml.U3(pi, -pi/2, pi/2, wires=[5])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #second gate
    #qml.U3(pi, -pi/2, pi/2, wires=[4])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #third gate
    qml.U3(pi, -pi/2, pi/2, wires=[3])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #fourth gate
    qml.U3(pi, -pi/2, pi/2, wires=[2])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #fifth gate
    qml.U3(pi, -pi/2, pi/2, wires=[1])
    
    
    
    qml.U3(pi, 0, pi, wires=0)
    
    
    qml.U3(2*pi, 0, 0, wires=[0])  #first gate
    qml.U3(pi, -pi/2, pi/2, wires=[1])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #second gate
    qml.U3(pi, -pi/2, pi/2, wires=[2])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #third gate
    qml.U3(pi, -pi/2, pi/2, wires=[3])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #fourth
    #qml.U3(pi, -pi/2, pi/2, wires=[4])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #fifth gate
    #qml.U3(pi, -pi/2, pi/2, wires=[5])
    
   
    qml.templates.MottonenStatePreparation(x2, wires=[1, 2, 3])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #first gate
    #qml.U3(pi, -pi/2, pi/2, wires=[5])
    
    #qml.U3(2*pi, 0, 0, wires=[0])  #second gate
    #qml.U3(pi, -pi/2, pi/2, wires=[4])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #third gate
    qml.U3(pi, -pi/2, pi/2, wires=[3])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #fourth gate
    qml.U3(pi, -pi/2, pi/2, wires=[2])
    
    qml.U3(2*pi, 0, 0, wires=[0])  #fifth gate 
    qml.U3(pi, -pi/2, pi/2, wires=[1])
    
    qml.U3(pi, 0, pi, wires=0)
    
    qml.Hadamard(wires=0)

    return qml.expval(qml.PauliZ(0))

i had to use u3 gate.

Thanks for sharing your code @sassan_moradi. I’m glad you solved it!