-
Notifications
You must be signed in to change notification settings - Fork 740
Description
Feature details
It seems that broadcasting is implemented for most embedding templates besides BasisEmbedding. I see PR #2810 and Issue #2976. Issue #2976 mentioned that hyperparameters would be required, but that it was not yet implemented. Was this ever followed up on?
Please let me know if you would like a usage example, but I feel that this is relatively understandable and is understandable why it would be desirable. It prevents anyone who wants to perform batch training from doing so when using BasisEmbedding in their circuit. I am not finding any documentation of how one might provide a hyperparameter to indicate batching in BasisEmbedding.
Implementation
Derived from your own example that you have referred people to before in other posts:
from typing import Iterable
import pennylane as qml
import torch
n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs: Iterable[Iterable[int | float]]):
qml.AngleEmbedding(inputs, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
x = torch.tensor([[0.0, 0.25, 0.5, 0.75], [1.0, 0.75, 0.5, 0.25]])
results = qnode(x)
This should, and does produce two results. However, if you swap out BasisEmedding for AngleEmbedding, it results in an error:
from typing import Iterable
import pennylane as qml
import torch
n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs: Iterable[Iterable[int | float]]):
qml.BasisEmbedding(inputs, wires=range(n_qubits))
return [qml.expval(qml.PauliZ(wires=i)) for i in range(n_qubits)]
x = torch.tensor([[0, 0, 1, 1], [1, 0, 1, 0]])
results = qnode(x)
Error:
def __init__(self, state, wires: WiresLike, id=None):
wires = Wires(wires)
if isinstance(state, list):
state = qml.math.stack(state)
tracing = qml.math.is_abstract(state)
if not qml.math.shape(state):
if not tracing and state >= 2 ** len(wires):
raise ValueError(
f"Integer state must be < {2 ** len(wires)} to have a feasible binary representation, got {state}"
)
bin = 2 ** math.arange(len(wires))[::-1]
state = qml.math.where((state & bin) > 0, 1, 0)
shape = qml.math.shape(state)
if len(shape) != 1:
> raise ValueError(f"State must be one-dimensional; got shape {shape}.")
E ValueError: State must be one-dimensional; got shape (2, 4)._
I would like it to be able to broadcast like all of the other embedding templates do. If it needs a hyperparameter, so be it.
How important would you say this feature is?
3: Very important! Blocking work.
Additional information
No response