sQUlearn / squlearn

scikit-learn interface for quantum algorithms
https://squlearn.github.io
Apache License 2.0
53 stars 18 forks source link

Sampler with unparameterized observable is crashing #239

Closed MHagelueken closed 3 months ago

MHagelueken commented 3 months ago

The following code example raises "IndexError: list index out of range" in the develop tree. When parameterized=False -> True or BackendSampler -> BackendEstimator or qnn.fit(np.array([[0.2,0.2],[0.2,0.2]]),np.array([1,1])) -> qnn.fit(np.array([[0.2,0.2]]),np.array([1])) (only one input value) the code runs through.

from squlearn.encoding_circuit import QCNNEncodingCircuit

param = ParameterVector("p", 3) param_x = ParameterVector("x", 2) qc = QuantumCircuit(4) qc.rz(param_x[0], 0) qc.rz(param_x[1], 3) qc.rz(-np.pi / 2, 1) qc.cx(1, 0) qc.rz(param[0], 0) qc.ry(param[1], 1) qc.cx(0, 1) qc.ry(param[2], 1) qc.cx(1, 0) qc.rz(np.pi / 2, 0)

qc = QiskitEncodingCircuit(qc) obs = CustomObservable(4,["IIIZ"],parameterized=False) executor = Executor(BackendSampler(Aer.get_backend("statevector_simulator"))) qnn = qnn = QNNRegressor( qc, obs, executor, SquaredLoss(), Adam(options={"maxiter": 1, "lr": 0.005}), ) qnn.fit(np.array([[0.2,0.2],[0.2,0.2]]),np.array([1,1])) qnn.predict(np.array([0.2,0.2]))

David-Kreplin commented 3 months ago

should be fixed by PR #240