batch_input(tape: Union[pennylane.tape.tape.QuantumTape, pennylane.qnode.QNode], argnum: Union[Sequence[int], int] = 0)Tuple[Sequence[pennylane.tape.tape.QuantumTape], Callable][source]

Transform a QNode to support an initial batch dimension for gate inputs.

In a classical ML application one needs to batch the non-trainable inputs of the network. This function executes the same analogue for a quantum circuit: separate circuit executions are created for each input, which are then executed with the same trainable parameters.

The batch dimension is assumed to be the first rank of the non trainable tensor object. For a rank 1 feature space, the shape needs to be (Nt, x) where x indicates the dimension of the features and Nt being the number of examples within a batch. Based on arXiv:2202.10471.

  • tape (tape.QuantumTape or QNode) – Input quantum circuit to batch

  • argnum (Sequence[int] or int) – One or more index value on all gate parameters indicating the location of the non-trainable batched inputs within the input argument sequence of the circuit. By default first argument is assumed to be the only batched input.


list of tapes arranged according to unbatched inputs and a callable function to batch the results.

Return type

Sequence[Sequence[tape.QuantumTape], Callable]

See also



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

@qml.qnode(dev, diff_method="parameter-shift", interface="tf")
def circuit(inputs, weights):
    qml.AngleEmbedding(inputs, wires = range(2), rotation="Y")
    qml.RY(weights[0], wires=0)
    qml.RY(weights[1], wires=1)
    return qml.expval(qml.PauliZ(1))
>>> x = tf.random.uniform((10, 2), 0, 1)
>>> w = tf.random.uniform((2,), 0, 1)
>>> circuit(x, w)
<tf.Tensor: shape=(10,), dtype=float64, numpy=
array([0.46230079, 0.73971315, 0.95666004, 0.5355225 , 0.66180948,
        0.44519553, 0.93874261, 0.9483197 , 0.78737918, 0.90866411])>