Hello,
I have a use-case wherein I need to process inputs of large shapes (e.g. [64, 128, 51]) through a VQC circuit. To do this, I’ve been making use of torch.vmap to help me speed things up.
I noticed something a little curious about the qml.RZ operation. In the example below, I attempt to process a tensor with shape [2, 2, 2] through a single-gate Z rotation circuit and return expectations on each entry in the tensor.
import pennylane as qml
import torch
dev = qml.device("default.qubit", wires=1)
@qml.qnode(dev, interface="torch")
def ansatz(x):
qml.RZ(x, wires=0)
return qml.expval(qml.PauliZ(0))
x = torch.tensor([[[0.1, 0.2], [0.3, 0.4]],[[0.1, 0.2], [0.3, 0.4]]])
res = torch.vmap(
lambda x_i: torch.vmap(
lambda x_j: ansatz(x_j), in_dims=0)(x_i),
in_dims=0
)(x)
print(res)
I get a huge error message, which culminates in “RuntimeError: Cannot access data pointer of Tensor that doesn’t have storage”, as follows:
Traceback (most recent call last):
File "c:\Users\ouyzh\Documents\Quantum Activations\repo\quantum-activations\test.py", line 13, in <module>
res = torch.vmap(
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\apis.py", line 202, in wrapped
return vmap_impl(
^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\vmap.py", line 334, in vmap_impl
return _flat_vmap(
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\vmap.py", line 484, in _flat_vmap
batched_outputs = func(*batched_inputs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\ouyzh\Documents\Quantum Activations\repo\quantum-activations\test.py", line 14, in <lambda>
lambda x_i: torch.vmap(
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\apis.py", line 202, in wrapped
return vmap_impl(
^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\vmap.py", line 334, in vmap_impl
return _flat_vmap(
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\torch\_functorch\vmap.py", line 484, in _flat_vmap
batched_outputs = func(*batched_inputs, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\Users\ouyzh\Documents\Quantum Activations\repo\quantum-activations\test.py", line 15, in <lambda>
lambda x_j: ansatz(x_j), in_dims=0)(x_i),
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\workflow\qnode.py", line 895, in __call__
return self._impl_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\workflow\qnode.py", line 868, in _impl_call
res = execute(
^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\workflow\execution.py", line 238, in execute
results = run(tapes, device, config, inner_transform)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\workflow\run.py", line 298, in run
results = inner_execute(tapes)
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\workflow\run.py", line 263, in inner_execute
results = device.execute(transformed_tapes, execution_config=execution_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\modifiers\simulator_tracking.py", line 28, in execute
results = untracked_execute(self, circuits, execution_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\modifiers\single_tape_support.py", line 30, in execute
results = batch_execute(self, circuits, execution_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\logging\decorators.py", line 61, in wrapper_entry
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\default_qubit.py", line 823, in execute
return tuple(
^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\default_qubit.py", line 824, in <genexpr>
_simulate_wrapper(
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\default_qubit.py", line 1189, in _simulate_wrapper
return simulate(circuit, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\logging\decorators.py", line 61, in wrapper_entry
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\qubit\simulate.py", line 370, in simulate
state, is_state_batched = get_final_state(
^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\logging\decorators.py", line 61, in wrapper_entry
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\qubit\simulate.py", line 201, in get_final_state
state = apply_operation(
^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\functools.py", line 909, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\qubit\apply_operation.py", line 237, in apply_operation
return _apply_operation_default(op, state, is_state_batched, debugger)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\qubit\apply_operation.py", line 263, in _apply_operation_default
return apply_operation_einsum(op, state, is_state_batched=is_state_batched)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\devices\qubit\apply_operation.py", line 84, in apply_operation_einsum
mat = op.matrix() + 0j
^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\operation.py", line 831, in matrix
canonical_matrix = self.compute_matrix(*self.parameters, **self.hyperparameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\ops\qubit\parametric_ops_single_qubit.py", line 465, in compute_matrix
return diags[:, :, np.newaxis] * qml.math.cast_like(qml.math.eye(2, like=diags), diags)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\math\utils.py", line 291, in cast_like
dtype = ar.to_numpy(tensor2).dtype.type
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\autoray\autoray.py", line 1132, in to_numpy
return do("to_numpy", x)
^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\autoray\autoray.py", line 81, in do
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages\pennylane\math\single_dispatch.py", line 641, in _to_numpy_torch
return x.detach().cpu().numpy()
^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Cannot access data pointer of Tensor that doesn't have storage
However, these errors complete disappear if I simply change the gate to an X rotation:
qml.RX(x, wires=0)
I successfully get the expectation values on each entry in the tensor:
tensor([[[0.9950, 0.9801],
[0.9553, 0.9211]],
[[0.9950, 0.9801],
[0.9553, 0.9211]]], dtype=torch.float64)
This is also not an issue for qml.RY.
I’m just wondering if this is expected behaviour or not, and if so, I’m also wondering why this might be the case. Here’s the output of qml.about():
Name: pennylane
Version: 0.43.1
Summary: 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.
Home-page:
Author:
Author-email:
License-Expression: Apache-2.0
Location: C:\Users\ouyzh\AppData\Local\Programs\Python\Python311\Lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, diastatic-malt, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, tomlkit, typing_extensions
Required-by: pennylane_lightning
Platform info: Windows-10-10.0.26200-SP0
Python version: 3.11.9
Numpy version: 2.3.4
Scipy version: 1.16.2
JAX version: 0.8.1
Installed devices:
- default.clifford (pennylane-0.43.1)
- default.gaussian (pennylane-0.43.1)
- default.mixed (pennylane-0.43.1)
- default.qubit (pennylane-0.43.1)
- default.qutrit (pennylane-0.43.1)
- default.qutrit.mixed (pennylane-0.43.1)
- default.tensor (pennylane-0.43.1)
- null.qubit (pennylane-0.43.1)
- reference.qubit (pennylane-0.43.1)
- lightning.qubit (pennylane_lightning-0.43.0)
Thanks so much for your help!