Two QNodes execution error

Hi PennyLane team!

I am trying to execute two QNodes at the same time but something goes wrong:

import pennylane as qml

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

@qml.qnode(dev)
def circuit1(x, y):
    qml.Hadamard(0)
    qml.RX(x, wires=0)
    qml.RY(y, wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0))

@qml.qnode(dev)
def circuit2(x, y):
    qml.RX(x, wires=0)
    qml.RY(y, wires=0)
    return qml.expval(qml.PauliY(0)), qml.probs(wires=[0,1])

qnodes = qml.QNodeCollection([circuit1, circuit2])

qnodes(0.5643, -0.45)
~/pennylane/pennylane/collections/qnode_collection.py in convert_results(results, interface)
    269             from autograd import numpy as np
    270 
--> 271             return np.stack(results)
    272 
    273         return results

~/autograd/autograd/numpy/numpy_wrapper.py in stack(arrays, axis)
     92     shapes = set(arr.shape for arr in arrays)
     93     if len(shapes) != 1:
---> 94         raise ValueError('all input arrays must have the same shape')
     95 
     96     result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape

Please help me, what is the issue here?

1 Like

Hi @quantum_alejandro!

I was able to replicate the issue on my end. I noticed that each node, if run and called by itself, works perfectly fine. The issue arose when I tried to evaluate both using qml.QNodeCollection

Referring to the documentation, linked here, QNodeCollection

[r]epresents a sequence of independent QNodes that all share the same signature. When the collection is evaluated, all QNodes are simultaneously evaluated with the same parameters.

and

the results from each QNode have been flattened and concatenated into a single one-dimensional list.

Now, circuit1(0.5643, -0.45) returns 0.0, while circuit1(0.5643, -0.45) returns [-0.53482447 0.23258776 0. 0.76741224 0. ]

Note here that the outputs are different in form, and flattening and concatenating this to a single one-dimensional list returns an error.

This is why when running

qnodes = qml.QNodeCollection([circuit1, circuit2])

qnodes(0.5643, -0.45)

we get the error: ValueError: all input arrays must have the same shape.

Now, I also want to note that by default, the qnodes within QNodeCollection are executed sequentially.

For this reason, as a way of getting the code to run, we can try running them individually:

import pennylane as qml

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

@qml.qnode(dev)
def circuit1(x, y):
    qml.Hadamard(0)
    qml.RX(x, wires=0)
    qml.RY(y, wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0))

@qml.qnode(dev)
def circuit2(x, y):
    qml.RX(x, wires=0)
    qml.RY(y, wires=0)
    return qml.expval(qml.PauliY(0)), qml.probs(wires=[0,1])

result1 = circuit1(0.5643, -0.45)
result2 = circuit2(0.5643, -0.45)

where your results for circuit1 and circuit2 are stored in result1 and result2, respectively.

This should work.

For a fix using QNodeCollection, we need to ensure that the output of both circuits have the appropriate form, which is more of a Python formatting thing.

I hope this helps!

1 Like

Dear @anishrverma,

Thank you for the details! That’s very helpful, will be using this from now on then.

Thanks again!

1 Like

Dear @quantum_alejandro,

You’re very welcome!

Thank you for your questions and participating on the Xanadu discussion forum. I wish you the best in your experience with Pennylane, and hope you continue to participate in the forums, perhaps helping others as well!

1 Like