|
| 1 | +.. _sec-models: |
| 2 | + |
| 3 | +Choosing a model |
| 4 | +================ |
| 5 | + |
| 6 | +The following sections provide more information on the various models. |
| 7 | + |
| 8 | +.. _sec-numpymodel: |
| 9 | + |
| 10 | +NumpyModel |
| 11 | +---------- |
| 12 | + |
| 13 | +A :py:class:`.NumpyModel` uses the unitary and density matrix simulators in DisCoPy, which convert quantum circuits into a tensor network. The resulting tensor network is efficiently contracted using ``opt_einsum``. |
| 14 | + |
| 15 | +Circuits containing only :py:class:`Bra <discopy.quantum.gates.Bra>`, :py:class:`Ket <discopy.quantum.gates.Ket>` and unitary gates are evaluated using DisCoPy's unitary simulator, while circuits containing :py:class:`Encode <discopy.quantum.circuit.Encode>`, :py:class:`Measure <discopy.quantum.circuit.Measure>` or :py:class:`Discard <discopy.quantum.circuit.Discard>` are evaluated using DisCoPy's density matrix simulator. |
| 16 | + |
| 17 | +.. note:: |
| 18 | + |
| 19 | + Note that the unitary simulator converts a circuit with ``n`` output qubits into a tensor of shape ``(2, ) * n``, while the density matrix simulator converts a circuit with ``n`` output qubits and ``m`` output bits into a tensor of shape ``(2, ) * (2 * n + m)``. |
| 20 | + |
| 21 | +In the common use case of using a :py:data:`~lambeq.text2diagram.stairs_reader` or a :py:class:`.TreeReader` with discarding for binary classification, the process involves measuring (:py:class:`Measure <discopy.quantum.circuit.Measure>`) one of the "open" qubits, and discarding (:py:class:`Discard <discopy.quantum.circuit.Discard>`) the rest of them. |
| 22 | + |
| 23 | +One advantage that the :py:class:`.NumpyModel` has over the :py:class:`.TketModel` is that it supports the just-in-time (jit) compilation provided by the library ``jax``. This speeds up the model's diagram evaluation by an order of magnitude. The :py:class:`.NumpyModel` with ``jit`` mode enabled can be instantiated with the following command: |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + from lambeq import NumpyModel |
| 28 | +
|
| 29 | + model = NumpyModel.from_diagrams(circuits, use_jit=True) |
| 30 | +
|
| 31 | +.. note:: |
| 32 | + Using the :py:class:`.NumpyModel` with ``jit`` mode enabled is not recommended for large models, as it requires a large amount of memory to store the pre-compiled functions for each circuit. |
| 33 | + |
| 34 | +To use the :py:class:`.NumpyModel` with ``jit`` mode, you need to install ``lambeq`` with the extra packages by running the following command: |
| 35 | + |
| 36 | +.. code-block:: bash |
| 37 | +
|
| 38 | + pip install lambeq[extras] |
| 39 | +
|
| 40 | +.. note:: |
| 41 | + |
| 42 | + To enable GPU support for ``jax``, follow the installation instructions on the `JAX GitHub repository <https://github.com/google/jax#installation>`_. |
| 43 | + |
| 44 | +:py:class:`.NumpyModel` should be used with the :py:class:`.QuantumTrainer`. |
| 45 | + |
| 46 | +.. rubric:: See also the following use cases: |
| 47 | + |
| 48 | +- :ref:`uc1` |
| 49 | + |
| 50 | +.. _sec-pytorchmodel: |
| 51 | + |
| 52 | +PytorchModel |
| 53 | +------------ |
| 54 | + |
| 55 | +:py:class:`.PytorchModel` is the right choice for classical experiments. Here, string diagrams are treated as tensor networks, where boxes represent tensors and edges define the specific tensor contractions. Tensor contractions are optimised by the python package ``opt_einsum``. |
| 56 | + |
| 57 | +To prepare the diagrams for the computation, we use a :py:class:`.TensorAnsatz` that converts a rigid diagram into a tensor diagram. Subclasses of :py:class:`.TensorAnsatz` include the :py:class:`.SpiderAnsatz` and the :py:class:`.MPSAnsatz`, which reduce the size of large tensors by spliting them into chains of many smaller boxes. To prepare a tensor diagram for a sentence, for example: |
| 58 | + |
| 59 | +.. code-block:: python |
| 60 | +
|
| 61 | + from lambeq import AtomicType, BobcatParser, TensorAnsatz |
| 62 | + from discopy import Dim |
| 63 | +
|
| 64 | + parser = BobcatParser() |
| 65 | + rigid_diagram = parser.sentence2diagram('This is a tensor network.') |
| 66 | +
|
| 67 | + ansatz = TensorAnsatz({AtomicType.NOUN: Dim(2), AtomicType.SENTENCE: Dim(4)}) |
| 68 | + tensor_diagram = ansatz(rigid_diagram) |
| 69 | +
|
| 70 | +After preparing a list of tensor diagrams, we can initialise the model through: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + from lambeq import PytorchModel |
| 75 | +
|
| 76 | + model = PytorchModel.from_diagrams(tensor_diagrams) |
| 77 | +
|
| 78 | +The :py:class:`.PytorchModel` is capable of combining tensor networks and neural network architectures. For example, it is possible to feed the output of a tensor diagram into a neural network, by subclassing and modifying the :py:meth:`~lambeq.PytorchModel.forward` method: |
| 79 | + |
| 80 | +.. code-block:: python |
| 81 | +
|
| 82 | + import torch |
| 83 | + from lambeq import PytorchModel |
| 84 | +
|
| 85 | + class MyCustomModel(PytorchModel): |
| 86 | + def __init__(self): |
| 87 | + super().__init__() |
| 88 | + self.net = torch.nn.Linear(2, 2) |
| 89 | +
|
| 90 | + def forward(self, input): |
| 91 | + """define a custom forward pass here""" |
| 92 | + preds = self.get_diagram_output(input) # performs tensor contraction |
| 93 | + return self.net(preds) |
| 94 | +
|
| 95 | +To simplify training, the :py:class:`.PytorchModel` can be used with the :py:class:`.PytorchTrainer`. A comprehensive tutorial can be found `here <tutorials/trainer_classical.ipynb>`_. |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + The loss function and the accuracy metric in the tutorial are defined for two-dimensional binary labels: ``[[1,0], [0,1], ...]``. If your data has a different structure, you must implement your custom loss function and evaluation metrics. |
| 100 | + |
| 101 | +.. rubric:: See also the following use cases: |
| 102 | + |
| 103 | +- :ref:`uc4` |
| 104 | + |
| 105 | +.. _sec-tketmodel: |
| 106 | + |
| 107 | +TketModel |
| 108 | +--------- |
| 109 | + |
| 110 | +:py:class:`.TketModel` uses ``pytket`` to retrieve shot-based results from a quantum computer, then uses the shot counts to build the resulting tensor. |
| 111 | + |
| 112 | +The ``AerBackend`` can be used with :py:class:`.TketModel` to perform a noisy, architecture-aware simulation of an IBM machine. Other backends supported by ``pytket`` can also be used. To run an experiment on a real quantum computer, for example: |
| 113 | + |
| 114 | +.. code-block:: python |
| 115 | +
|
| 116 | + from lambeq import TketModel |
| 117 | + from pytket.extensions.quantinuum import QuantinuumBackend |
| 118 | +
|
| 119 | + machine = 'H1-1E' |
| 120 | + backend = QuantinuumBackend(device_name=machine) |
| 121 | + backend.login() |
| 122 | +
|
| 123 | + backend_config = { |
| 124 | + 'backend': backend, |
| 125 | + 'compilation': backend.default_compilation_pass(2), |
| 126 | + 'shots': 2048 |
| 127 | + } |
| 128 | +
|
| 129 | + model = TketModel.from_diagrams(all_circuits, backend_config=backend_config) |
| 130 | +
|
| 131 | +.. note:: |
| 132 | + |
| 133 | + Note that you need user accounts and allocated resources to run experiments on real machines. However, `IBM Quantum <https://quantum-computing.ibm.com/>`_ provides some limited resources for free. |
| 134 | + |
| 135 | +For initial experiments we recommend using a :py:class:`.NumpyModel`, as it performs noiseless simulations and is orders of magnitude faster. |
| 136 | + |
| 137 | +:py:class:`.TketModel` should be used with the :py:class:`.QuantumTrainer`. |
| 138 | + |
| 139 | +.. rubric:: See also the following use cases: |
| 140 | + |
| 141 | +- :ref:`uc2` |
| 142 | +- :ref:`uc3` |
0 commit comments