Closed jackaraz closed 2 years ago
Hi @jackaraz, thank you for creating this issue! It's not obvious whether or not this will mess up any internal calculations. The best way to know would be to create a Pull Request. This way when the CI checks are run we can see if something is wrong.
Please let me know if you have any trouble creating the PR.
This issue has been resolved in PR #2069
Feature details
Hi, I'm trying to implement a possible batched run for QML applications following our chat with @josh146 in here. I was testing my circuit with
@qml.batch_params
to see how this works. So by default it takes inputs (non-trainable) of(n_batch, n_qubit)
and weights (trainable)(n_weights, n_batch)
and when I set@qml.batch_params(all_operations=False)
it only accepts batched trainables. This is a bit counter-intuitive to me because in a classical ML application I would batch the inputs (non-trainable) and run them through my network with the same weights and then take the derivative according to the mean/sum loss etc. Batching the input would allow much faster training and might be easier to parallelize. Additionally, neitherpennylane/qnn/keras.py
nortorch.py
allows batched run; they both unbatch the input and then execute the circuit (There might be a reason for this that I'm not aware of, please let me know if this is intentional).Implementation
Below I attached a simple implementation of the batched input option. Note that this works for me but don't think it's general enough where if you change the order of trainable and non-trainable inputs this will probably cause problems.
However, this implementation won't work in a Keras layer simply because
tf.Tensor
objects does not haverequires_grad
attribute so I added an additional function just for TensorFlow;and within
class KerasLayer(Layer):
qnode can be initialized viaself.batched_qnode = batch_input_tf(qnode)
then the call function can be modified as follows;How important would you say this feature is?
3: Very important! Blocking work.
Additional information
Please note that these implementations are not generic due to the reasons mentioned above. Also please let me know if this implementation would mess up some internal calculations within PennyLane, I just started using it a couple of days ago so not very familiar with the entire construction of the module.
Thanks Jack