# Pytorch Interface using multiple inputs - best practice?

**URL:** <https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232>\
**Category:** PennyLane Help\
**Created:** [September 23, 2019, 11:04pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232 "2019-09-23T23:04:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnada](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cnada](https://discuss.pennylane.ai/u/cnada)\
**Post date:** [September 23, 2019, 11:04pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/1 "2019-09-23T23:04:17Z")

</div>

Hello,

I was wondering how to use the Pytorch interface the best way in order to apply a QNode on multiple inputs of a dataset X,Y. I was thinking something like :

```auto
@qml.qnode(dev,interface="torch")
def circuit(parameters, x):
    # do some quantum operations
    # return an expectation value giving probability
    # output in a binary supervised learning setup
    return qml.expval(qml.PauliZ(0))

def apply_loss(labels, predictions):
    # define loss
    return loss

def cost(var, X, Y):
      predictions = ?
      # define cost

```

To define the cost, would we have to loop over all examples in X and Y to get their predictions? I guess since batching is not available yet right? Yet how to do so if we define X,Y to be tensors in order to apply our optimizer on the variable var ?

Thanks in advance for help 🙂

---

<div class="post-metadata">

**Author:** ![AroosaIjaz](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@AroosaIjaz](https://discuss.pennylane.ai/u/AroosaIjaz)\
**Post date:** [September 24, 2019, 5:27pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/2 "2019-09-24T17:27:22Z")

</div>

Dear @cnada,

Can you provide a bit more info about what you’re thinking so we can give the best answer? Are you trying to break a dataset into batches and feed those into the `circuit` function? Or are you trying to build a cost function involving multiple elements from the dataset?

---

<div class="post-metadata">

**Author:** ![cnada](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cnada](https://discuss.pennylane.ai/u/cnada)\
**Post date:** [September 24, 2019, 8:38pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/3 "2019-09-24T20:38:41Z")

</div>

Dear @AroosaIjaz,

Sure. I am trying to define a cost function with the elements (x\_i \in X), i=1..n from the input dataset X with n elements. Let us say the function is the binary cross-entropy. The quantum circuit would take as input each element, encode it as parameters of some gates, then apply other gates with the attribute parameters which I optimize onto.

I would also be interested in doing my optimization in batches. But I think this is done by inputing a batch as X and do optimiser.step() feeding it the batch right?

Sorry if I am not clear.

---

<div class="post-metadata">

**Author:** ![AroosaIjaz](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@AroosaIjaz](https://discuss.pennylane.ai/u/AroosaIjaz)\
**Post date:** [September 24, 2019, 8:55pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/4 "2019-09-24T20:55:34Z")

</div>

Dear @cnada,

In this case, you can simply use Numpy functions to randomly choose a batch from your dataset. The following tutorial does something similar:

[https://pennylane.readthedocs.io/en/latest/tutorials/pennylane\_run\_variational\_classifier.html#variational-classifier](https://pennylane.readthedocs.io/en/latest/tutorials/pennylane_run_variational_classifier.html#variational-classifier)

```
batch_size = 5
batch_index = np.random.randint(0, len(X), (batch_size,))
X_batch = X[batch_index]
Y_batch = Y[batch_index]

```

The cost function can then accept batches as input and the optimizer can update the `var` variable:

```
var = opt.step( cost(var, X_batch, Y_batch)`

```

Hope this helps. Don’t hesitate to ask any follow-up questions!

---

<div class="post-metadata">

**Author:** ![cnada](https://avatars.discourse-cdn.com/v4/letter/c/f05b48/32.png) [@cnada](https://discuss.pennylane.ai/u/cnada)\
**Post date:** [September 24, 2019, 10:21pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/5 "2019-09-24T22:21:58Z")

</div>

@AroosaIjaz,

Sure for that part but how to then apply the cost on the batch input instances? Do I need to loop over each of them to get the output of the quantum node for the loss? I am also asking for the best way to do so with the pytorch interface.

Thanks.

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [September 26, 2019, 2:20pm UTC](https://discuss.pennylane.ai/t/pytorch-interface-using-multiple-inputs-best-practice/232/6 "2019-09-26T14:20:46Z")

</div>

Hi @cnada,

Since none of the underlying simulators/hardware devices support batching, unfortunately you’ll still have to provide a batch of inputs manually (e.g., with a `for` loop).

A more intensive option (taken by fellow user @rooler) is to manually create a simulator which automatically handles batching 😃 (see [here](http://discuss.pennylane.ai/t/batching-inputs-to-quantum-circuit/139)).

We do have on our roadmap to provide batching support in PennyLane. This would require either that a backend provider exposes this feature, or that we build our own support in PennyLane’s own simulators. But for the moment, you’ll have to use the old-fashioned option mentioned above.
