PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
(Note: this is a specific case of ragged output creation with QubitDevice. It is meant to serve as an example that we have on file for our future considerations.)
A QNode returning multiple probability measurements with varying number of wires executes in some form (note that we're not applying the JAX interface, but using the backprop device):
~/xanadu/pennylane/pennylane/interfaces/batch/__init__.py in fn(tapes, **kwargs)
118 def fn(tapes, **kwargs): # pylint: disable=function-redefined
119 tapes = [expand_fn(tape) for tape in tapes]
--> 120 return original_fn(tapes, **kwargs)
121
122 @wraps(fn)
~/anaconda3/lib/python3.8/contextlib.py in inner(*args, **kwds)
73 def inner(*args, **kwds):
74 with self._recreate_cm():
---> 75 return func(*args, **kwds)
76 return inner
77
~/xanadu/pennylane/pennylane/_qubit_device.py in batch_execute(self, circuits)
280 self.reset()
281
--> 282 res = self.execute(circuit)
283 results.append(res)
284
~/xanadu/pennylane/pennylane/_qubit_device.py in execute(self, circuit, **kwargs)
232
233 if (circuit.all_sampled or not circuit.is_sampled) and not multiple_sampled_jobs:
--> 234 results = self._asarray(results)
235 else:
236 results = tuple(self._asarray(r) for r in results)
~/anaconda3/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py in array(object, dtype, copy, order, ndmin, device)
3614 elif isinstance(object, (list, tuple)):
3615 if object:
-> 3616 out = stack([asarray(elt, dtype=dtype) for elt in object])
3617 else:
3618 out = _np_array([], dtype=dtype)
~/anaconda3/lib/python3.8/site-packages/jax/_src/numpy/lax_numpy.py in stack(arrays, axis, out)
3375 for a in arrays:
3376 if shape(a) != shape0:
-> 3377 raise ValueError("All input arrays must have the same shape.")
3378 new_arrays.append(expand_dims(a, axis))
3379 return concatenate(new_arrays, axis=axis)
ValueError: All input arrays must have the same shape.
Additional information
Likely, solving this issue will tie in with reconsidering the output type of multi-measurement QNodes. Our consideration of changing to the use of tuples would likely address this issue.
The issue boils down to the self._asarray call in QubitDevice:
~/xanadu/pennylane/pennylane/_qubit_device.py in execute(self, circuit, **kwargs)
232
233 if (circuit.all_sampled or not circuit.is_sampled) and not multiple_sampled_jobs:
--> 234 results = self._asarray(results)
autograd handles the same case while emitting the following warning:
autograd/autograd/numpy/numpy_wrapper.py:77: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
return _np.array(args, *array_args, **array_kwargs)
This is not something JAX can handle, hence the error.
Expected behavior
(Note: this is a specific case of ragged output creation with
QubitDevice
. It is meant to serve as an example that we have on file for our future considerations.)A QNode returning multiple probability measurements with varying number of wires executes in some form (note that we're not applying the JAX interface, but using the backprop device):
Actual behavior
Additional information
Likely, solving this issue will tie in with reconsidering the output type of multi-measurement QNodes. Our consideration of changing to the use of tuples would likely address this issue.
The issue boils down to the
self._asarray
call inQubitDevice
:autograd
handles the same case while emitting the following warning:This is not something JAX can handle, hence the error.
Source code
No response
Tracebacks
No response
System information