Skip to content

LayerdEndodingCircuit in QSVC with PQK not working in cross_val_score #347

@JanSchnabel

Description

@JanSchnabel

Describe the bug
I am initializing an empty LayerdEncodingCircuit object and pass it to a PQK, which I pass to a QSVC. Then I want to set the encoding circuit using set_params(encoding_circuit_str=fm_str) and put the resulting QML model into scikit-learns cross_val_score. This gives me the error

TypeError: LayeredEncodingCircuit.init() missing 1 required positional argument: 'num_qubits'

To Reproduce
I am using the develop branch of sQUlearn. This is a minimal example to reproduce the error

from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score

from squlearn import Executor
from squlearn.encoding_circuit.layered_encoding_circuit import LayeredEncodingCircuit
from squlearn.kernel import ProjectedQuantumKernel, QSVC


x,y = make_classification(n_samples=100, n_features=8)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)

num_qubits = 8

lfm = LayeredEncodingCircuit(num_qubits=num_qubits)
quantum_kernel = ProjectedQuantumKernel(
    encoding_circuit=lfm,
    executor=Executor(),
    parameter_seed=0,
    gamma=0.5
)

svc_kwargs = {"C": 100}
qsvc_model = QSVC(
    quantum_kernel=quantum_kernel,
    **svc_kwargs
)

fm_str = 'Rx(p;=0*p+np.pi,{p})-Ry(x;=np.arctan(x),{x})-'
qsvc_model.set_params(encoding_circuit_str=fm_str)

cv_score = cross_val_score(qsvc_model, x_train, y_train, cv=5, scoring="roc_auc")

Expected behavior
Before merging the "removing num_features" branch into develop it worked.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions