Repository: PennyLaneAI/pennylane

Issue #4460: Add broadcasting support for MottonenStatePreparation
Created: 2023-08-10T22:01:20Z
Feature details
As title says. This will probably not be trivial since the decompositions would be different for each state in a batch, and they may not be able to all fit on one tape (ie. not true broadcasting).
Implementation
No response
How important would you say this feature is?
2: Somewhat important. Needed this quarter.
Additional information
I think this should be added because we're pushing to allow StatePrepBase ops (eg. StatePrep) to be allowed mid-circuit, except that they are decomposed when found mid-circuit. This is usually fine because they have decompositions defined, like how StatePrep (or even AmplitudeEmbedding which decomposes to a StatePrep) decomposes to MottonenStatePreparation. Those operators claim to support batching, but MottonenStatePreparation cannot, so they actually only support batching at the beginning of a circuit. This can cause unexpected behaviour.
Eg.
>>> op = qml.MottonenStatePreparation(np.array([[1, 0], [0, 1]]), wires=[0])
>>> op.decomposition()
[]
If an AmplitudeEmbedding is placed mid-circuit with a batch dimension, it will silently have no effect.

Duplicate issue #4753: [BUG]MottonenStatePreparation.compute_decomposition
Created: 2023-10-30T05:57:10Z
Expected behavior
I want to use MottonenStatePreparation.compute_decomposition to get op_list for batched tensor data. It seems different from issue #4589
For example
inputs = torch.rand(256,2**n_qubits)
norms = torch.norm(inputs, dim=1)
data = inputs/torch.stack([norms for i in range(inputs.size(dim=1))], dim=1)
op_list = MottonenStatePreparation.compute_decomposition(data, wires=range(n_qubits))
Actual behavior

RuntimeError                              Traceback (most recent call last)
 in 
----> 1 op_list = MottonenStatePreparation.compute_decomposition(data, wires=range(n_qubits))
~/opt/miniconda3/lib/python3.9/site-packages/pennylane/templates/state_preparations/mottonen.py in compute_decomposition(state_vector, wires)
359         # Apply inverse y rotation cascade to prepare correct absolute values of amplitudes
360         for k in range(len(wires_reverse), 0, -1):
--> 361             alpha_y_k = _get_alpha_y(a, len(wires_reverse), k)
362             control = wires_reverse[k:]
363             target = wires_reverse[k - 1]
~/opt/miniconda3/lib/python3.9/site-packages/pennylane/templates/state_preparations/mottonen.py in _get_alpha_y(a, n, k)
206
207     with np.errstate(divide="ignore", invalid="ignore"):
--> 208         division = numerator / denominator
209
210     # Cast the numerator and denominator to ensure compatibility with interfaces
RuntimeError: The size of tensor a (8) must match the size of tensor b (16) at non-singleton dimension 1
Additional information
It is ok if my inputs are inputs = np.random.rand(256,2**n_qubits), but it has some problem when the inputs are torch.tensor.
Source code
No response
Tracebacks
No response
System information
Name: PennyLane
Version: 0.31.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/beam/opt/miniconda3/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           macOS-10.16-x86_64-i386-64bit
Python version:          3.9.1
Numpy version:           1.23.5
Scipy version:           1.10.0
Installed devices:
- default.gaussian (PennyLane-0.31.1)
- default.mixed (PennyLane-0.31.1)
- default.qubit (PennyLane-0.31.1)
- default.qubit.autograd (PennyLane-0.31.1)
- default.qubit.jax (PennyLane-0.31.1)
- default.qubit.tf (PennyLane-0.31.1)
- default.qubit.torch (PennyLane-0.31.1)
- default.qutrit (PennyLane-0.31.1)
- null.qubit (PennyLane-0.31.1)
- qiskit.aer (PennyLane-qiskit-0.31.0)
- qiskit.basicaer (PennyLane-qiskit-0.31.0)
- qiskit.ibmq (PennyLane-qiskit-0.31.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.31.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.31.0)
- qiskit.remote (PennyLane-qiskit-0.31.0)
- lightning.qubit (PennyLane-Lightning-0.31.0)
Existing GitHub issues

 I have searched existing GitHub issues to make sure the issue does not already exist.

Closing comment (2023-10-30T13:38:36Z): While this is different than #4589 , it may be a duplicate of #4460, as it seems caused by MottonenStatePrep not supporting a broadcast dimension.