forked from qiskit-community/qiskit-machine-learning
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable sparse support in TorchConnector and other minor updates (qisk…
…it-community#571) * minor updates * update imports * updates * working version * some test optimizations * unpack * some tests * more tests * fix black, lint, mypy * add reno * fix copyright, spell, black * fix copyright, fix initial_weights * no sparse support on 3.7 * more skips * more skips * add quotes * Update qiskit_machine_learning/kernels/quantum_kernel.py Co-authored-by: Steve Wood <[email protected]> * rollback spelling changes * update reno * code review * update tests * fix copyright * update reno * Update releasenotes/notes/add-sparse-torch-connector-a3b9e3d50b405a01.yaml Co-authored-by: Steve Wood <[email protected]> * update reno more --------- Co-authored-by: Steve Wood <[email protected]>
- Loading branch information
1 parent
9596dc4
commit 2c771cc
Showing
5 changed files
with
435 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
releasenotes/notes/add-sparse-torch-connector-a3b9e3d50b405a01.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
features: | ||
- | | ||
The PyTorch connector :class:`~qiskit_machine_learning.connector.TorchConnector` now fully | ||
supports sparse output in both forward and backward passes. To enable sparse support, first of | ||
all, the underlying quantum neural network must be sparse. In this case, if the `sparse` | ||
property of the connector itself is not set, then the connector inherits sparsity from the | ||
networks. If the connector is set to be sparse, but the network is not, an exception will be | ||
raised. Also you may set the connector to be dense if the network is sparse. | ||
This snippet illustrates how to create a sparse instance of the connector. | ||
.. code-block:: python | ||
import torch | ||
from qiskit import QuantumCircuit | ||
from qiskit.circuit.library import ZFeatureMap, RealAmplitudes | ||
from qiskit_machine_learning.connectors import TorchConnector | ||
from qiskit_machine_learning.neural_networks import SamplerQNN | ||
num_qubits = 2 | ||
fmap = ZFeatureMap(num_qubits, reps=1) | ||
ansatz = RealAmplitudes(num_qubits, reps=1) | ||
qc = QuantumCircuit(num_qubits) | ||
qc.compose(fmap, inplace=True) | ||
qc.compose(ansatz, inplace=True) | ||
qnn = SamplerQNN( | ||
circuit=qc, | ||
input_params=fmap.parameters, | ||
weight_params=ansatz.parameters, | ||
sparse=True, | ||
) | ||
connector = TorchConnector(qnn) | ||
output = connector(torch.tensor([[1., 2.]])) | ||
print(output) | ||
loss = torch.sparse.sum(output) | ||
loss.backward() | ||
grad = connector.weight.grad | ||
print(grad) | ||
In hybrid setup, where a PyTorch-based neural network has classical and quantum layers, sparse | ||
operations should not be mixed with dense ones, otherwise exceptions may be thrown by PyTorch. | ||
Sparse support works on python 3.8+. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.