qadence 1.6.2__py3-none-any.whl → 1.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qadence/__init__.py +2 -2
- qadence/backends/gpsr.py +15 -3
- qadence/backends/pyqtorch/backend.py +28 -43
- qadence/backends/pyqtorch/convert_ops.py +44 -227
- qadence/backends/utils.py +45 -4
- qadence/blocks/matrix.py +2 -2
- qadence/blocks/primitive.py +1 -1
- qadence/constructors/__init__.py +2 -0
- qadence/constructors/hamiltonians.py +38 -1
- qadence/draw/utils.py +1 -1
- qadence/engines/torch/differentiable_expectation.py +1 -1
- qadence/ml_tools/__init__.py +11 -3
- qadence/ml_tools/config.py +277 -2
- qadence/ml_tools/constructors.py +774 -0
- qadence/ml_tools/models.py +338 -250
- qadence/ml_tools/saveload.py +6 -6
- qadence/ml_tools/train_grad.py +3 -7
- qadence/ml_tools/utils.py +2 -8
- qadence/{models/quantum_model.py → model.py} +14 -8
- qadence/overlap.py +1 -1
- qadence/serialization.py +6 -6
- qadence/types.py +42 -0
- {qadence-1.6.2.dist-info → qadence-1.7.0.dist-info}/METADATA +14 -3
- {qadence-1.6.2.dist-info → qadence-1.7.0.dist-info}/RECORD +26 -29
- qadence/backends/adjoint.py +0 -163
- qadence/finitediff.py +0 -47
- qadence/models/__init__.py +0 -7
- qadence/models/qnn.py +0 -265
- {qadence-1.6.2.dist-info → qadence-1.7.0.dist-info}/WHEEL +0 -0
- {qadence-1.6.2.dist-info → qadence-1.7.0.dist-info}/licenses/LICENSE +0 -0
qadence/draw/utils.py
CHANGED
@@ -23,7 +23,7 @@ from qadence.blocks import (
|
|
23
23
|
)
|
24
24
|
from qadence.blocks.analog import ConstantAnalogRotation, InteractionBlock
|
25
25
|
from qadence.circuit import QuantumCircuit
|
26
|
-
from qadence.
|
26
|
+
from qadence.model import QuantumModel
|
27
27
|
from qadence.operations import RX, RY, RZ, SWAP, HamEvo, I
|
28
28
|
from qadence.transpile.block import fill_identities
|
29
29
|
from qadence.utils import format_parameter
|
@@ -6,12 +6,12 @@ from functools import partial
|
|
6
6
|
from typing import Any, Callable, Sequence
|
7
7
|
|
8
8
|
import torch
|
9
|
+
from pyqtorch.adjoint import AdjointExpectation
|
9
10
|
from torch import Tensor
|
10
11
|
from torch.autograd import Function
|
11
12
|
|
12
13
|
from qadence.backend import Backend as QuantumBackend
|
13
14
|
from qadence.backend import ConvertedCircuit, ConvertedObservable
|
14
|
-
from qadence.backends.adjoint import AdjointExpectation
|
15
15
|
from qadence.backends.utils import infer_batchsize, is_pyq_shape, param_dict, pyqify, validate_state
|
16
16
|
from qadence.blocks.abstract import AbstractBlock
|
17
17
|
from qadence.blocks.utils import uuid_to_eigen
|
qadence/ml_tools/__init__.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
|
3
|
-
from .config import TrainConfig
|
3
|
+
from .config import AnsatzConfig, FeatureMapConfig, TrainConfig
|
4
|
+
from .constructors import create_ansatz, create_fm_blocks, observable_from_config
|
4
5
|
from .data import DictDataLoader, InfiniteTensorDataset, to_dataloader
|
6
|
+
from .models import QNN
|
5
7
|
from .optimize_step import optimize_step as default_optimize_step
|
6
8
|
from .parameters import get_parameters, num_parameters, set_parameters
|
7
9
|
from .printing import print_metrics, write_tensorboard
|
@@ -12,10 +14,16 @@ from .train_no_grad import train as train_gradient_free
|
|
12
14
|
|
13
15
|
# Modules to be automatically added to the qadence namespace
|
14
16
|
__all__ = [
|
15
|
-
"
|
17
|
+
"AnsatzConfig",
|
18
|
+
"create_ansatz",
|
19
|
+
"create_fm_blocks",
|
16
20
|
"DictDataLoader",
|
21
|
+
"FeatureMapConfig",
|
22
|
+
"load_checkpoint",
|
23
|
+
"observable_from_config",
|
24
|
+
"QNN",
|
25
|
+
"TrainConfig",
|
17
26
|
"train_with_grad",
|
18
27
|
"train_gradient_free",
|
19
|
-
"load_checkpoint",
|
20
28
|
"write_checkpoint",
|
21
29
|
]
|
qadence/ml_tools/config.py
CHANGED
@@ -2,9 +2,20 @@ from __future__ import annotations
|
|
2
2
|
|
3
3
|
import datetime
|
4
4
|
import os
|
5
|
-
from dataclasses import dataclass
|
5
|
+
from dataclasses import dataclass, field, fields
|
6
|
+
from logging import getLogger
|
6
7
|
from pathlib import Path
|
7
|
-
from typing import Callable, Optional
|
8
|
+
from typing import Callable, Optional, Type
|
9
|
+
|
10
|
+
from sympy import Basic
|
11
|
+
|
12
|
+
from qadence.blocks.analog import AnalogBlock
|
13
|
+
from qadence.blocks.primitive import ParametricBlock
|
14
|
+
from qadence.operations import RX, AnalogRX
|
15
|
+
from qadence.parameters import Parameter
|
16
|
+
from qadence.types import AnsatzType, BasisSet, MultivariateStrategy, ReuploadScaling, Strategy
|
17
|
+
|
18
|
+
logger = getLogger(__file__)
|
8
19
|
|
9
20
|
|
10
21
|
@dataclass
|
@@ -70,3 +81,267 @@ class TrainConfig:
|
|
70
81
|
self.trainstop_criterion = lambda x: x <= self.max_iter
|
71
82
|
if self.validation_criterion is None:
|
72
83
|
self.validation_criterion = lambda *x: False
|
84
|
+
|
85
|
+
|
86
|
+
@dataclass
|
87
|
+
class FeatureMapConfig:
|
88
|
+
num_features: int = 1
|
89
|
+
"""Number of feature parameters to be encoded."""
|
90
|
+
|
91
|
+
basis_set: BasisSet | dict[str, BasisSet] = BasisSet.FOURIER
|
92
|
+
"""
|
93
|
+
Basis set for feature encoding.
|
94
|
+
|
95
|
+
Takes qadence.BasisSet.
|
96
|
+
Give a single BasisSet to use the same for all features.
|
97
|
+
Give a dict of (str, BasisSet) where the key is the name of the variable and the
|
98
|
+
value is the BasisSet to use for encoding that feature.
|
99
|
+
BasisSet.FOURIER for Fourier encoding.
|
100
|
+
BasisSet.CHEBYSHEV for Chebyshev encoding.
|
101
|
+
"""
|
102
|
+
|
103
|
+
reupload_scaling: ReuploadScaling | dict[str, ReuploadScaling] = ReuploadScaling.CONSTANT
|
104
|
+
"""
|
105
|
+
Scaling for encoding the same feature on different qubits.
|
106
|
+
|
107
|
+
Scaling used to encode the same feature on different qubits in the
|
108
|
+
same layer of the feature maps. Takes qadence.ReuploadScaling.
|
109
|
+
Give a single ReuploadScaling to use the same for all features.
|
110
|
+
Give a dict of (str, ReuploadScaling) where the key is the name of the variable and the
|
111
|
+
value is the ReuploadScaling to use for encoding that feature.
|
112
|
+
ReuploadScaling.CONSTANT for constant scaling.
|
113
|
+
ReuploadScaling.TOWER for linearly increasing scaling.
|
114
|
+
ReuploadScaling.EXP for exponentially increasing scaling.
|
115
|
+
"""
|
116
|
+
|
117
|
+
feature_range: tuple[float, float] | dict[str, tuple[float, float]] | None = None
|
118
|
+
"""
|
119
|
+
Range of data that the input data is assumed to come from.
|
120
|
+
|
121
|
+
Give a single tuple to use the same range for all features.
|
122
|
+
Give a dict of (str, tuple) where the key is the name of the variable and the
|
123
|
+
value is the feature range to use for that feature.
|
124
|
+
"""
|
125
|
+
|
126
|
+
target_range: tuple[float, float] | dict[str, tuple[float, float]] | None = None
|
127
|
+
"""
|
128
|
+
Range of data the data encoder assumes as natural range.
|
129
|
+
|
130
|
+
Give a single tuple to use the same range for all features.
|
131
|
+
Give a dict of (str, tuple) where the key is the name of the variable and the
|
132
|
+
value is the target range to use for that feature.
|
133
|
+
"""
|
134
|
+
|
135
|
+
multivariate_strategy: MultivariateStrategy = MultivariateStrategy.PARALLEL
|
136
|
+
"""
|
137
|
+
The encoding strategy in case of multi-variate function.
|
138
|
+
|
139
|
+
Takes qadence.MultivariateStrategy.
|
140
|
+
If PARALLEL, the features are encoded in one block of rotation gates
|
141
|
+
with each feature given an equal number of qubits.
|
142
|
+
If SERIES, the features are encoded sequentially, with an ansatz block
|
143
|
+
between. PARALLEL is allowed only for DIGITAL `feature_map_strategy`.
|
144
|
+
"""
|
145
|
+
|
146
|
+
feature_map_strategy: Strategy = Strategy.DIGITAL
|
147
|
+
"""
|
148
|
+
Strategy for feature map.
|
149
|
+
|
150
|
+
Accepts DIGITAL, ANALOG or RYDBERG. Defaults to DIGITAL.
|
151
|
+
If the strategy is incompatible with the `operation` chosen, then `operation`
|
152
|
+
gets preference and the given strategy is ignored.
|
153
|
+
"""
|
154
|
+
|
155
|
+
param_prefix: str | None = None
|
156
|
+
"""
|
157
|
+
String prefix to create trainable parameters in Feature Map.
|
158
|
+
|
159
|
+
A string prefix to create trainable parameters multiplying the feature parameter
|
160
|
+
inside the feature-encoding function. Note that currently this does not take into
|
161
|
+
account the domain of the feature-encoding function.
|
162
|
+
Defaults to `None` and thus, the feature map is not trainable.
|
163
|
+
Note that this is separate from the name of the parameter.
|
164
|
+
The user can provide a single prefix for all features, and they will be appended
|
165
|
+
by appropriate feature name automatically.
|
166
|
+
"""
|
167
|
+
|
168
|
+
num_repeats: int | dict[str, int] = 0
|
169
|
+
"""
|
170
|
+
Number of feature map layers repeated in the data reuploadig step.
|
171
|
+
|
172
|
+
If all are to be repeated the same number of times, then can give a single
|
173
|
+
`int`. For different number of repeatitions for each feature, provide a dict
|
174
|
+
of (str, int) where the key is the name of the variable and the value is the
|
175
|
+
number of repeatitions for that feature.
|
176
|
+
This amounts to the number of additional reuploads. So if `num_repeats` is N,
|
177
|
+
the data gets uploaded N+1 times. Defaults to no repeatition.
|
178
|
+
"""
|
179
|
+
|
180
|
+
operation: Callable[[Parameter | Basic], AnalogBlock] | Type[RX] | None = None
|
181
|
+
"""
|
182
|
+
Type of operation.
|
183
|
+
|
184
|
+
Choose among the analog or digital rotations or a custom
|
185
|
+
callable function returning an AnalogBlock instance. If the type of operation is
|
186
|
+
incompatible with the `strategy` chosen, then `operation` gets preference and
|
187
|
+
the given strategy is ignored.
|
188
|
+
"""
|
189
|
+
|
190
|
+
inputs: list[Basic | str] | None = None
|
191
|
+
"""
|
192
|
+
List that indicates the order of variables of the tensors that are passed.
|
193
|
+
|
194
|
+
Optional if a single feature is being encoded, required otherwise. Given input tensors
|
195
|
+
`xs = torch.rand(batch_size, input_size:=2)` a QNN with `inputs=["t", "x"]` will
|
196
|
+
assign `t, x = xs[:,0], xs[:,1]`.
|
197
|
+
"""
|
198
|
+
|
199
|
+
def __post_init__(self) -> None:
|
200
|
+
if self.multivariate_strategy == MultivariateStrategy.PARALLEL and self.num_features > 1:
|
201
|
+
assert (
|
202
|
+
self.feature_map_strategy == Strategy.DIGITAL
|
203
|
+
), "For `parallel` encoding of multiple features, the `feature_map_strategy` must be \
|
204
|
+
of `digital` type."
|
205
|
+
|
206
|
+
if self.operation is None:
|
207
|
+
if self.feature_map_strategy == Strategy.DIGITAL:
|
208
|
+
self.operation = RX
|
209
|
+
elif self.feature_map_strategy == Strategy.ANALOG:
|
210
|
+
self.operation = AnalogRX # type: ignore[assignment]
|
211
|
+
|
212
|
+
else:
|
213
|
+
if self.feature_map_strategy == Strategy.DIGITAL:
|
214
|
+
if isinstance(self.operation, AnalogBlock):
|
215
|
+
logger.warning(
|
216
|
+
"The `operation` is of type `AnalogBlock` but the `feature_map_strategy` is\
|
217
|
+
`digital`. The `feature_map_strategy` will be modified and given operation\
|
218
|
+
will be used."
|
219
|
+
)
|
220
|
+
|
221
|
+
self.feature_map_strategy = Strategy.ANALOG
|
222
|
+
|
223
|
+
elif self.feature_map_strategy == Strategy.ANALOG:
|
224
|
+
if isinstance(self.operation, ParametricBlock):
|
225
|
+
logger.warning(
|
226
|
+
"The `operation` is a digital gate but the `feature_map_strategy` is\
|
227
|
+
`analog`. The `feature_map_strategy` will be modified and given operation\
|
228
|
+
will be used."
|
229
|
+
)
|
230
|
+
|
231
|
+
self.feature_map_strategy = Strategy.DIGITAL
|
232
|
+
|
233
|
+
if self.inputs is not None:
|
234
|
+
assert (
|
235
|
+
len(self.inputs) == self.num_features
|
236
|
+
), "Inputs list must be of same size as the number of features"
|
237
|
+
else:
|
238
|
+
if self.num_features == 1:
|
239
|
+
self.inputs = ["x"]
|
240
|
+
else:
|
241
|
+
raise ValueError(
|
242
|
+
"""
|
243
|
+
Your QNN has more than one input. Please provide a list of inputs in the order
|
244
|
+
of your tensor domain. For example, if you want to pass
|
245
|
+
`xs = torch.rand(batch_size, input_size:=3)` to you QNN, where
|
246
|
+
```
|
247
|
+
t = x[:,0]
|
248
|
+
x = x[:,1]
|
249
|
+
y = x[:,2]
|
250
|
+
```
|
251
|
+
you have to specify
|
252
|
+
```
|
253
|
+
inputs=["t", "x", "y"]
|
254
|
+
```
|
255
|
+
You can also pass a list of sympy symbols.
|
256
|
+
"""
|
257
|
+
)
|
258
|
+
|
259
|
+
property_list = [
|
260
|
+
"basis_set",
|
261
|
+
"reupload_scaling",
|
262
|
+
"feature_range",
|
263
|
+
"target_range",
|
264
|
+
"num_repeats",
|
265
|
+
]
|
266
|
+
|
267
|
+
for target_field in fields(self):
|
268
|
+
if target_field.name in property_list:
|
269
|
+
prop = getattr(self, target_field.name)
|
270
|
+
if isinstance(prop, dict):
|
271
|
+
assert set(prop.keys()) == set(
|
272
|
+
self.inputs
|
273
|
+
), f"The keys in {target_field.name} must be the same as the inputs provided. \
|
274
|
+
Alternatively, provide a single value of {target_field.name} to use the same\
|
275
|
+
{target_field.name} for all features."
|
276
|
+
else:
|
277
|
+
prop = {key: prop for key in self.inputs}
|
278
|
+
setattr(self, target_field.name, prop)
|
279
|
+
|
280
|
+
|
281
|
+
@dataclass
|
282
|
+
class AnsatzConfig:
|
283
|
+
depth: int = 1
|
284
|
+
"""Number of layers of the ansatz."""
|
285
|
+
|
286
|
+
ansatz_type: AnsatzType = AnsatzType.HEA
|
287
|
+
"""What type of ansatz.
|
288
|
+
|
289
|
+
HEA for Hardware Efficient Ansatz.
|
290
|
+
IIA for Identity intialized Ansatz.
|
291
|
+
"""
|
292
|
+
|
293
|
+
ansatz_strategy: Strategy = Strategy.DIGITAL
|
294
|
+
"""Ansatz strategy.
|
295
|
+
|
296
|
+
DIGITAL for fully digital ansatz. Required if `ansatz_type` is `iia`.
|
297
|
+
SDAQC for analog entangling block.
|
298
|
+
RYDBERG for fully rydberg hea ansatz.
|
299
|
+
"""
|
300
|
+
|
301
|
+
strategy_args: dict = field(default_factory=dict)
|
302
|
+
"""
|
303
|
+
A dictionary containing keyword arguments to the function creating the ansatz.
|
304
|
+
|
305
|
+
Details about each below.
|
306
|
+
|
307
|
+
For DIGITAL strategy, accepts the following:
|
308
|
+
periodic (bool): if the qubits should be linked periodically.
|
309
|
+
periodic=False is not supported in emu-c.
|
310
|
+
operations (list): list of operations to cycle through in the
|
311
|
+
digital single-qubit rotations of each layer.
|
312
|
+
Defaults to [RX, RY, RX] for hea and [RX, RY] for iia.
|
313
|
+
entangler (AbstractBlock): 2-qubit entangling operation.
|
314
|
+
Supports CNOT, CZ, CRX, CRY, CRZ, CPHASE. Controlld rotations
|
315
|
+
will have variational parameters on the rotation angles.
|
316
|
+
Defaults to CNOT
|
317
|
+
|
318
|
+
For SDAQC strategy, accepts the following:
|
319
|
+
operations (list): list of operations to cycle through in the
|
320
|
+
digital single-qubit rotations of each layer.
|
321
|
+
Defaults to [RX, RY, RX] for hea and [RX, RY] for iia.
|
322
|
+
entangler (AbstractBlock): Hamiltonian generator for the
|
323
|
+
analog entangling layer. Time parameter is considered variational.
|
324
|
+
Defaults to NN interaction.
|
325
|
+
|
326
|
+
For RYDBERG strategy, accepts the following:
|
327
|
+
addressable_detuning: whether to turn on the trainable semi-local addressing pattern
|
328
|
+
on the detuning (n_i terms in the Hamiltonian).
|
329
|
+
Defaults to True.
|
330
|
+
addressable_drive: whether to turn on the trainable semi-local addressing pattern
|
331
|
+
on the drive (sigma_i^x terms in the Hamiltonian).
|
332
|
+
Defaults to False.
|
333
|
+
tunable_phase: whether to have a tunable phase to get both sigma^x and sigma^y rotations
|
334
|
+
in the drive term. If False, only a sigma^x term will be included in the drive part
|
335
|
+
of the Hamiltonian generator.
|
336
|
+
Defaults to False.
|
337
|
+
"""
|
338
|
+
# The default for a dataclass can not be a mutable object without using this default_factory.
|
339
|
+
|
340
|
+
param_prefix: str = "theta"
|
341
|
+
"""The base bame of the variational parameter."""
|
342
|
+
|
343
|
+
def __post_init__(self) -> None:
|
344
|
+
if self.ansatz_type == AnsatzType.IIA:
|
345
|
+
assert (
|
346
|
+
self.ansatz_strategy != Strategy.RYDBERG
|
347
|
+
), "Rydberg strategy not allowed for Identity-initialized ansatz."
|