question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QuantumKernel doesn't work with RawFeatureVector.

See original GitHub issue

Information

  • Qiskit Machine Learning version: IBM Quantum Lab (Jupyter Notebook using the Qiskit v0.29.0 launcher)
  • Python version: IBM Quantum Lab (Jupyter Notebook using the Qiskit v0.29.0 launcher)
  • Operating system: IBM Quantum Lab (Jupyter Notebook using the Qiskit v0.29.0 launcher)

The behavior has been firstly reproduced also locally:

  • Qiskit Machine Learning version: 0.2.0
  • Python version: Python 3.8.8
  • Operating system: Red Hat Enterprise Linux Server 7.7 (Maipo)

What is the current behavior?

QiskitError: ‘Cannot define a ParameterizedInitialize with unbound parameters’ occurs when trying to train a QSVM classifier on the breast_cancer data set, using RawFeatureVector as a feature map for the kernel. Same error has occurred with other data sets as well: adhoc data set, random data generated using numpy in the interval [0,1).

 Traceback (most recent call last):
  File "<ipython-input-2-5b15e6dd39e3>", line 24, in <module>
    main()
  File "<ipython-input-2-5b15e6dd39e3>", line 18, in main
    adhoc_svc.fit(train_features, train_labels)
  File "/opt/conda/lib/python3.8/site-packages/sklearn/svm/_base.py", line 226, in fit
    fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
  File "/opt/conda/lib/python3.8/site-packages/sklearn/svm/_base.py", line 266, in _dense_fit
    X = self._compute_kernel(X)
  File "/opt/conda/lib/python3.8/site-packages/sklearn/svm/_base.py", line 396, in _compute_kernel
    kernel = self.kernel(X, self.__Xfit)
  File "/opt/conda/lib/python3.8/site-packages/qiskit_machine_learning/kernels/quantum_kernel.py", line 306, in evaluate
    parameterized_circuit = self.construct_circuit(
  File "/opt/conda/lib/python3.8/site-packages/qiskit_machine_learning/kernels/quantum_kernel.py", line 149, in construct_circuit
    qc.append(psi_y_dag.to_instruction().inverse(), qc.qubits)
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/instruction.py", line 389, in inverse
    inverse_gate.definition._data = [
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/instruction.py", line 390, in <listcomp>
    (inst.inverse(), qargs, cargs) for inst, qargs, cargs in reversed(self._definition)
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/instruction.py", line 364, in inverse
    if self.definition is None:
  File "/opt/conda/lib/python3.8/site-packages/qiskit/circuit/instruction.py", line 221, in definition
    self._define()
  File "/opt/conda/lib/python3.8/site-packages/qiskit_machine_learning/circuit/library/raw_feature_vector.py", line 170, in _define
    raise QiskitError("Cannot define a ParameterizedInitialize with unbound parameters")
QiskitError: 'Cannot define a ParameterizedInitialize with unbound parameters'

Steps to reproduce the problem

import matplotlib.pyplot as plt
import numpy as np

from sklearn.svm import SVC

from qiskit import Aer
from qiskit.circuit.library import ZZFeatureMap
from qiskit.utils import QuantumInstance, algorithm_globals
from qiskit_machine_learning.algorithms import QSVC
from qiskit_machine_learning.kernels import QuantumKernel
from qiskit_machine_learning.datasets import breast_cancer
from qiskit_machine_learning.circuit.library import RawFeatureVector

seed = 12345
algorithm_globals.random_seed = seed

def main():
    n_features = 2
    train_features, train_labels, test_features, test_labels = breast_cancer(
    training_size=20,test_size=5, n=n_features,one_hot = False
)

    feature_map = RawFeatureVector(n_features)
    backend = QuantumInstance(Aer.get_backend('qasm_simulator'), shots=1024,
                                    seed_simulator=seed, seed_transpiler=seed)

    kernel = QuantumKernel(feature_map=feature_map, quantum_instance=backend)

    qsvm = SVC(kernel=kernel.evaluate) #pass custom kernel as callable function
    qsvm.fit(train_features, train_labels)

    score = qsvm.score(test_features, test_labels)

    print(f'Callable kernel classification test score: {adhoc_score}')
    
if __name__ == '__main__':
    main()

What is the expected behavior?

The model is expected to train.

Suggested solutions

No clue. I hope these might be helpful: the code runs properly if ZZFeatureMap is used for the construction of the kernel. In the code tests I checked that in the QuantumKernel test the ZZFeatureMap is used, whereas in the RawFeatureVector test the RawFeatureVector circuit is used with a VQC.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vbeliscommented, Sep 16, 2021

@stefan-woerner thank you for the message and explanation. It is clear that an amplitude encoding circuit as a feature map, i.e., RawFeatureVector, reproduces the quadratic kernel. My goal was to extend the RawFeatureVector circuit by attaching additional circuits achieving different feature maps for the QuantumKernel, some of which could also have trainable parameters. If I have understood correctly from this thread, this won’t be possible with the current version of the RawFeatureVector and QuantumKernel classes. However, this was possible in the corresponding older versions of these classes (before the deprecation of Aqua, e.g., 0.23.2).

0reactions
vbeliscommented, Sep 28, 2021

@adekusar-drl thank you for the valuable information provided in this thread. Since I do not have currently any questions or remarks I will close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RawFeatureVector
In ML, this circuit can be used to load the training data into qubit amplitudes. It does not apply an kernel transformation (therefore,...
Read more >
Applications of the Quantum Kernel Method on a ... - UWSpace
In this work I present an analysis of QKM algorithms used to encode and classify real data ... 5 Computing quantum kernels on...
Read more >
Advantage of density matrix over vector to form quantum ...
For generality, we consider a feature map generically via ϕ:X→H where H is some Hilbert space imbued with some inner product. Then, fixing...
Read more >
Quantum Kernel Method in the Presence of Noise
Abstract: Kernel method in machine learning consists of encoding input data into a vector in a Hilbert space called the feature space and ......
Read more >
Introduction to Quantum Kernels and Support Vector Machines
All notes are available for download over on the site under "Suggested Links": ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found