Skip to content

Commit b3d60ce

Browse files
Integration of HQS automatic backend selection (#234)
* added files from hqs * black format * wip * first version of automatic selection is working * modified parallel primitves * wip * wip * wip * wip * wip * wip * cleaning * wip * Add more comments * bug fix * removed unimportant code * fixed test * wip * ip * doc strings fixed * added test for parallel execution * get everything working with fake backends * added more tests for automatic selection * add missng testjob * update off docu and dependencies * deleted tests * reset of regression_example * black * fixed testjobs * adjust testjobs * black * remove value checks in tests, since they are not reproduceable * black * remove result check from test_parallel_primtives.py * cleaning of test * black * update executor graphic * update of the doc * fix of primitive * removed print statement * final run of black * fix test * fix some stuff * Update src/squlearn/util/executor.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * Update src/squlearn/util/execution/backend_auto_selection.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * Update src/squlearn/util/execution/backend_auto_selection.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * Update src/squlearn/qnn/qnnr.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * Update src/squlearn/qnn/qnnc.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * Update src/squlearn/qnn/lowlevel_qnn_qiskit.py Co-authored-by: Moritz <44642314+MoritzWillmann@users.noreply.github.com> * fix in test * renamed AutoSelBack * fix pyproject.toml * fix pyproject.toml v2 * fix pyproject.toml v3 * update of testjob * black * fix for testjobs * fix test * rechange dependencies * remove IBMQuantum variable in the executor * fix executor docstring * black * add docstring for select_backend, remove comments * rename function * change ValueError to TypeError * fix if clauses * update parallel primitives * rename `parse_openqasm` * clean backend auto selection code * black * fix in Qiskit lowleven QNN and fix of IBMQuantum variable in the executor * black * remove metric * remove result comparison in parallel primitives * make `heuristic` function and add tests * black * black tests * rename remote and ibmquantum * clean backend auto selection * add hqaa modes --------- Co-authored-by: Moritz <moritz.willmann@ipa.fraunhofer.de>
1 parent 6b3aac7 commit b3d60ce

24 files changed

Lines changed: 2720 additions & 181 deletions

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img alt="sQUlearn" src="https://raw.githubusercontent.com/sQUlearn/squlearn/main/docs/_static/logo.png" />
33
</p>
44

5-
sQUlearn is a user-friendly, NISQ-ready Python library for quantum machine learning (QML), designed for seamless integration with classical machine learning tools like scikit-learn. The library's dual-layer architecture serves both QML researchers and practitioners, enabling efficient prototyping, experimentation, and pipelining. sQUlearn provides a comprehensive toolset that includes both quantum kernel methods and quantum neural networks, along with features like customizable data encoding strategies, automated execution handling, and specialized kernel regularization techniques. By focusing on NISQ-compatibility and end-to-end automation, sQUlearn aims to bridge the gap between current quantum computing capabilities and practical machine learning applications.
5+
sQUlearn is a user-friendly, NISQ-ready Python library for quantum machine learning (QML), designed for seamless integration with classical machine learning tools like scikit-learn. The library's dual-layer architecture serves both QML researchers and practitioners, enabling efficient prototyping, experimentation, and pipelining. sQUlearn provides a comprehensive tool set that includes both quantum kernel methods and quantum neural networks, along with features like customizable data encoding strategies, automated execution handling, and specialized kernel regularization techniques. By focusing on NISQ-compatibility and end-to-end automation, sQUlearn aims to bridge the gap between current quantum computing capabilities and practical machine learning applications.
66

77
sQUlearn offers scikit-learn compatible high-level interfaces for various kernel methods and QNNs. They build on top of the low-level interfaces of the QNN engine and the quantum kernel engine. The executor is used to run experiments on simulated and real backends of the Qiskit or PennyLane frameworks.
88

@@ -39,7 +39,7 @@ pip install .[examples]
3939
```
4040

4141
## Contribute to sQUlearn
42-
Thanks for considering to contribute to sQUlearn! Please read our [contribution guidelines](https://github.com/sQUlearn/squlearn/blob/main/.github/CONTRIBUTING.md) before you submit a pull request.
42+
Thanks for considering contributing to sQUlearn! Please read our [contribution guidelines](https://github.com/sQUlearn/squlearn/blob/main/.github/CONTRIBUTING.md) before you submit a pull request.
4343

4444
---
4545
## License
@@ -61,11 +61,6 @@ For general questions regarding sQUlearn, use the [GitHub Discussions](https://g
6161
---
6262
## Acknowledgements
6363

64-
This project was supported by the German Federal Ministry
65-
of Economic Affairs and Climate Action through the
66-
projects AutoQML (grant no. 01MQ22002A) and AQUAS
67-
(grant no. 01MQ22003D), as well as the German Federal
68-
Ministry of Education and Research through the project
69-
H2Giga Degrad-EL3 (grant no. 03HY110D).
64+
This project was supported by the German Federal Ministry of Economic Affairs and Climate Action through the projects AutoQML (grant no. 01MQ22002A) and AQUAS (grant no. 01MQ22003D), as well as the German Federal Ministry of Education and Research through the project H2Giga Degrad-EL3 (grant no. 03HY110D).
7065

71-
---
66+
---

docs/_static/util/executor.png

-55.3 KB
Loading

docs/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ def setup(app):
106106

107107
# intersphinx
108108
intersphinx_mapping = {
109-
"qiskit": ("https://docs.quantum.ibm.com/api/qiskit/", None),
109+
"pennylane": ("https://docs.pennylane.ai/en/stable/", None),
110+
"qiskit": ("https://docs.quantum.ibm.com/api/qiskit/1.1/", None),
110111
"qiskit-aer": ("https://qiskit.github.io/qiskit-aer/", None),
112+
"qiskit-ibm-runtime": ("https://docs.quantum.ibm.com/api/qiskit-ibm-runtime/0.27/", None),
111113
"scipy": ("https://docs.scipy.org/doc/scipy/", None),
112-
"sklearn": ("https://scikit-learn.org/stable/", None),
114+
"sklearn": ("https://scikit-learn.org/1.4/", None),
113115
}
114116

115117
suppress_warnings = ["myst.header", "config.cache"]

docs/modules/classes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ Execution Tools
145145
:template: class.rst
146146

147147
Executor
148+
util.executor.ExecutorEstimator
149+
util.executor.ExecutorSampler
148150

149151

150152
Core

docs/user_guide/executor.rst

Lines changed: 194 additions & 51 deletions
Large diffs are not rendered by default.

pyproject.toml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ classifiers = [
2525
]
2626
keywords = ["quantum", "machine learning", "qml"]
2727
dependencies = [
28+
"bayesian-optimization>=1.4.3",
2829
"dill>=0.3",
29-
"numpy>=1.17,<2.0",
30-
"qiskit>=0.43.0,<1.1.0",
30+
"mapomatic>=0.10.0",
31+
"networkx>=3.0",
32+
"numpy>=1.17",
33+
"pennylane>=0.34.0",
34+
"qiskit>=0.43.0,<1.2.0",
3135
"qiskit-aer>=0.12.0",
3236
"qiskit-algorithms>=0.3.0",
33-
"qiskit-ibm-runtime>=0.15.1",
37+
"qiskit-ibm-runtime>=0.15.1,<0.28.0",
3438
"qiskit-machine-learning>=0.6.1",
35-
"pennylane>=0.34.0",
3639
"scipy>=1.5",
3740
"scikit-learn>=1.0,<1.4.2",
38-
"bayesian-optimization>=1.4.3",
3941
"tqdm>=4.0",
4042
]
4143
requires-python = ">=3.9"
@@ -60,6 +62,7 @@ examples = [
6062
]
6163
docs = [
6264
"ipykernel",
65+
"jupyter-sphinx",
6366
"matplotlib>=3.5",
6467
"myst-parser",
6568
"nbsphinx",
@@ -69,7 +72,6 @@ docs = [
6972
"sphinxcontrib-spelling",
7073
"sphinx-rtd-theme<3.0.0",
7174
"sphinx-sitemap",
72-
"jupyter-sphinx",
7375
]
7476

7577
[project.urls]

src/squlearn/kernel/matrix/fidelity_kernel.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ def __init__(
116116
self._enc_circ = self._encoding_circuit.get_circuit(
117117
self._feature_vector, self._parameter_vector
118118
)
119+
120+
# Automatic select backend if not chosen
121+
if not self._executor.backend_chosen:
122+
self._enc_circ, _ = self._executor.select_backend(self._enc_circ)
123+
119124
if self._executor.is_statevector:
120125
if self._parameter_vector is None:
121126
self._quantum_kernel = FidelityStatevectorKernel(

src/squlearn/observables/observable_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def set_map(self, qubit_map: Union[list, dict], num_all_qubits: int):
4545
qubits are mapped to the output qubits.
4646
num_all_qubits (int): The total number of qubits in the system.
4747
"""
48+
4849
self._qubit_map = qubit_map
4950
self._num_all_qubits = num_all_qubits
5051
self._is_mapped = True

src/squlearn/qnn/base_qnn.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class BaseQNN(BaseEstimator, ABC):
4444
callback (Union[Callable, str, None], default=None): A callback for the optimization loop.
4545
Can be either a Callable, "pbar" (which uses a :class:`tqdm.tqdm` process bar) or None.
4646
If None, the optimizers (default) callback will be used.
47+
primitive : The Qiskit primitive that is utilized in the qnn, if a Qiskit backend
48+
is used in the executor (not supported for PennyLane backends)
49+
Default primitive is the one specified in the executor initialization,
50+
if nothing is specified, the estimator will used.
51+
Possible values are ``"estimator"`` or ``"sampler"``.
4752
"""
4853

4954
def __init__(
@@ -65,6 +70,7 @@ def __init__(
6570
caching: bool = True,
6671
pretrained: bool = False,
6772
callback: Union[Callable, str, None] = None,
73+
primitive: Union[str, None] = None,
6874
**kwargs,
6975
) -> None:
7076
super().__init__()
@@ -117,6 +123,7 @@ def __init__(
117123
self.caching = caching
118124
self.pretrained = pretrained
119125

126+
self.primitive = primitive
120127
self.executor = executor
121128

122129
self.shot_control = shot_control
@@ -324,7 +331,11 @@ def _fit(self, X, y, weights: np.ndarray = None) -> None:
324331

325332
def _initialize_lowlevel_qnn(self):
326333
self._qnn = LowLevelQNN(
327-
self.encoding_circuit, self.operator, self.executor, caching=self.caching
334+
self.encoding_circuit,
335+
self.operator,
336+
self.executor,
337+
caching=self.caching,
338+
primitive=self.primitive,
328339
)
329340

330341
def _validate_input(self, X, y, incremental, reset):

src/squlearn/qnn/lowlevel_qnn.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def __new__(
3434
):
3535

3636
if executor.quantum_framework == "pennylane":
37+
if "primitive" in kwargs:
38+
RuntimeError("Warning: Primitive argument is not supported for PennyLane")
39+
kwargs.pop("primitive")
3740
return LowLevelQNNPennyLane(
3841
parameterized_quantum_circuit, observable, executor, *args, **kwargs
3942
)

0 commit comments

Comments
 (0)