Skip to content

Commit ad098c9

Browse files
Merge pull request #376 from sQUlearn/develop
Release 0.10.1
2 parents 6ab9621 + 1711c21 commit ad098c9

19 files changed

Lines changed: 965 additions & 112 deletions

examples/kernel/example_projected_kernel.ipynb

Lines changed: 927 additions & 41 deletions
Large diffs are not rendered by default.

src/squlearn/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .util import Executor
44
from . import observables, encoding_circuit, kernel, optimizers, qnn, util
55

6-
__version__ = "0.10.0"
6+
__version__ = "0.10.1"
77

88
__all__ = [
99
"Executor",

src/squlearn/encoding_circuit/circuit_library/highdim_encoding_circuit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HighDimEncodingCircuit(EncodingCircuitBase):
3636
cycling_type (str): Defines, how the indices are cycled.\n
3737
``saw``: restarts by 0, e.g. 0,1,2,3,0,1,2,3 (recommended);
3838
``hat``: goes up and then down, e.g. 0,1,2,3,2,1,0,1,2,3
39-
number_of_layers (int): Sets the number of layer repetitions. If not given, the number of
39+
num_layers (Union[None, int]): Sets the number of layer repetitions. If not given, the number of
4040
layers is determined automatically by the number of features and
4141
qubits. If the given number of layers is to low, a error is thrown.
4242
layer_type (str): Defines in which directions the features are assigned to the gates.

src/squlearn/encoding_circuit/circuit_library/random_encoding_circuit.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,7 @@ def set_params(self, **params):
402402
Args:
403403
params: Hyper-parameters and their values, e.g. ``num_qubits=2``.
404404
"""
405-
valid_params = self.get_params()
406-
for key, value in params.items():
407-
if key not in valid_params:
408-
raise ValueError(
409-
f"Invalid parameter {key!r}. "
410-
f"Valid parameters are {sorted(valid_params)!r}."
411-
)
412-
try:
413-
setattr(self, key, value)
414-
except:
415-
setattr(self, "_" + key, value)
405+
super().set_params(**params)
416406

417407
# Reset the random configuration
418408
self._is_config_available = False

src/squlearn/encoding_circuit/encoding_circuit_base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EncodingCircuitBase(ABC):
2222

2323
def __init__(self, num_qubits: int, num_features: int = None) -> None:
2424
self._num_qubits = num_qubits
25-
self._num_features = num_features
25+
self._num_features = num_features if num_features != 0 else None
2626

2727
if num_features is not None:
2828
warnings.warn(
@@ -149,14 +149,16 @@ def draw(
149149
):
150150
feature_vec = ParameterVector(feature_label, self.num_encoding_slots)
151151

152-
elif self.num_features is not None and num_features is None:
153-
feature_vec = ParameterVector(feature_label, self.num_features)
152+
elif num_features or self.num_features:
153+
feature_vec = ParameterVector(feature_label, num_features or self.num_features)
154154
else:
155155
feature_vec = [Parameter(feature_label)]
156156

157157
# ensure random configuration is available
158158
if hasattr(self, "_is_config_available") and not self._is_config_available:
159-
self._gen_random_config(num_features=num_features, seed=self.get_params()["seed"])
159+
self._gen_random_config(
160+
num_features=num_features or self.num_features or 0, seed=self.get_params()["seed"]
161+
)
160162

161163
# ensure that the LayeredEncodingCircuit is built before drawing
162164
if hasattr(self, "_build_layered_pqc"):
@@ -211,6 +213,8 @@ def set_params(self, **params) -> EncodingCircuitBase:
211213
f"Invalid parameter {key!r}. "
212214
f"Valid parameters are {sorted(valid_params)!r}."
213215
)
216+
if key == "num_features" and value == 0:
217+
value = None
214218
try:
215219
setattr(self, key, value)
216220
except:

src/squlearn/encoding_circuit/layered_encoding_circuit.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,23 +2158,8 @@ def get_params(self, deep: bool = True) -> dict:
21582158

21592159
return params
21602160

2161-
def set_params(self, **params) -> None:
2162-
if "encoding_circuit_str" in params:
2163-
self._encoding_circuit_str = params["encoding_circuit_str"]
2164-
2165-
valid_params = self.get_params()
2166-
for key, value in params.items():
2167-
if key not in valid_params:
2168-
raise ValueError(
2169-
f"Invalid parameter {key!r}. "
2170-
f"Valid parameters are {sorted(valid_params)!r}."
2171-
)
2172-
2173-
if "num_features" in params:
2174-
self._num_features = params["num_features"]
2175-
2176-
if "num_qubits" in params:
2177-
self._num_qubits = params["num_qubits"]
2161+
def set_params(self, **params):
2162+
super().set_params(**params)
21782163

21792164
dict_layered_pqc = {}
21802165
for key in params.keys():

src/squlearn/kernel/loss/ode_loss.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class ODELoss(KernelLossBase):
7777
eta=1.2,
7878
)
7979
80-
See Also
81-
--------
80+
See Also:
8281
squlearn.kernel.QKODE : Quantum Kernel for ODE solving.
8382
8483
References

src/squlearn/kernel/lowlevel_kernel/projected_quantum_kernel.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def evaluate_qnn(self, x: np.ndarray) -> np.ndarray:
488488
Returns:
489489
The evaluated output of the QNN as numpy array
490490
"""
491-
self.__check_num_params()
491+
self._initialize_kernel(num_features=extract_num_features(x))
492492

493493
# Copy parameters in QNN form
494494
if self._parameters is None and self.num_parameters == 0:
@@ -961,10 +961,8 @@ def __call__(
961961
param = parameters[: qnn.num_parameters]
962962
param_op = parameters[qnn.num_parameters :]
963963

964-
if len(param.shape) == 1 and len(param) == 1:
965-
param = float(param)
966-
if len(param_op.shape) == 1 and len(param_op) == 1:
967-
param_op = float(param_op)
964+
param = param.item() if param.size == 1 else param
965+
param_op = param_op.item() if param_op.size == 1 else param_op
968966

969967
x_result = qnn.evaluate(x, param, param_op, "f")["f"]
970968
if y is not None:

src/squlearn/kernel/qgpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class QGPC(GaussianProcessClassifier, SerializableModelMixin):
3333
(``num_qubits=``), or (if supported) the number of layers (``num_layers=``)
3434
of the underlying encoding circuit.
3535
36-
See Also
37-
--------
36+
See Also:
3837
squlearn.kernel.QSVC : Quantum Support Vector classification.
3938
4039
References

src/squlearn/kernel/qgpr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ class QGPR(BaseEstimator, RegressorMixin, SerializableModelMixin):
6060
(``num_qubits=``), or (if supported) the number of layers (``num_layers=``)
6161
of the underlying encoding circuit.
6262
63-
See Also
64-
--------
63+
See Also:
6564
squlearn.kernel.QKRR : Quantum Gaussian Process regression.
6665
squlearn.kernel.QSVR : Quantum Support Vector regression.
6766

0 commit comments

Comments
 (0)