0
$\begingroup$

This link https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.QuantumKernel.construct_circuit.html#qiskit_machine_learning.kernels.QuantumKernel.construct_circuit tells us that the circuit can be built automatically, but an error was found after using it, how can I solve it?

from qiskit import BasicAer
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
seed = 12345
X_data = np.array([[1,2,7,12],[3,7,7,11],[12,18,7,90]])
Y_data = np.array([[1,12,7,22],[3,71,7,111],[12,118,7,90]])
y_train = np.array([12,5,9])
print(X_data.shape[0],X_data.shape[1])
qbits = X_data.shape[0]
ansatz0 = ZZFeatureMap(feature_dimension=qbits, insert_barriers = True, name = 'Quantum Kernel',reps=1, parameter_prefix='x')
ansatz1 = ZZFeatureMap(feature_dimension=qbits, insert_barriers = True, name = 'Quantum Kernel',reps=1, parameter_prefix='y').inverse()
qc = ansatz0.compose(ansatz1)
qc.draw('mpl')
QI = QuantumInstance(BasicAer.get_backend("qasm_simulator"), shots=1024, seed_simulator=seed, seed_transpiler=seed)
qk = QuantumKernel(feature_map=ansatz0, quantum_instance=QI)
qc = qk.construct_circuit(x=X_data, y=Y_data, is_statevector_sim = False)
qc.draw('mpl')

OUT:

3 4
---------------------------------------------------------------------------
CircuitError                              Traceback (most recent call last)
Input In [323], in <cell line: 18>()
     16 QI = QuantumInstance(BasicAer.get_backend("qasm_simulator"), shots=1024, seed_simulator=seed, seed_transpiler=seed)
     17 qk = QuantumKernel(feature_map=ansatz0, quantum_instance=QI)
---> 18 qc = qk.construct_circuit(x=X_data, y=Y_data, is_statevector_sim = False)
     19 qc.draw('mpl')

File d:\miniconda3\lib\site-packages\qiskit_machine_learning\kernels\quantum_kernel.py:317, in QuantumKernel.construct_circuit(self, x, y, measurement, is_statevector_sim)
    314 qc = QuantumCircuit(q, c)
    316 x_dict = dict(zip(self._feature_map.parameters, x))
--> 317 psi_x = self._feature_map.assign_parameters(x_dict)
    318 qc.append(psi_x.to_instruction(), qc.qubits)
    320 if not is_statevector_sim:

File d:\miniconda3\lib\site-packages\qiskit\circuit\library\n_local\n_local.py:810, in NLocal.assign_parameters(self, parameters, inplace)
    807 if not self._is_built:
    808     self._build()
--> 810 return super().assign_parameters(parameters, inplace=inplace)

File d:\miniconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py:2663, in QuantumCircuit.assign_parameters(self, parameters, inplace)
   2661     # replace the parameters with a new Parameter ("substitute") or numeric value ("bind")
   2662     for parameter, value in unrolled_param_dict.items():
-> 2663         bound_circuit._assign_parameter(parameter, value)
   2664 else:
   2665     if len(parameters) != self.num_parameters:

File d:\miniconda3\lib\site-packages\qiskit\circuit\quantumcircuit.py:2749, in QuantumCircuit._assign_parameter(self, parameter, value)
   2747 # Normal ParameterExpression.
   2748 if isinstance(assignee, ParameterExpression):
-> 2749     new_param = assignee.assign(parameter, value)
   2750     # if fully bound, validate
   2751     if len(new_param.parameters) == 0:

File d:\miniconda3\lib\site-packages\qiskit\circuit\parameterexpression.py:91, in ParameterExpression.assign(self, parameter, value)
     89 if isinstance(value, ParameterExpression):
     90     return self.subs({parameter: value})
---> 91 return self.bind({parameter: value})

File d:\miniconda3\lib\site-packages\qiskit\circuit\parameterexpression.py:113, in ParameterExpression.bind(self, parameter_values)
     94 """Binds the provided set of parameters to their corresponding values.
     95 
     96 Args:
   (...)
    109     parameter_values.
    110 """
    112 self._raise_if_passed_unknown_parameters(parameter_values.keys())
--> 113 self._raise_if_passed_nan(parameter_values)
    115 symbol_values = {}
    116 for parameter, value in parameter_values.items():

File d:\miniconda3\lib\site-packages\qiskit\circuit\parameterexpression.py:207, in ParameterExpression._raise_if_passed_nan(self, parameter_values)
    203 nan_parameter_values = {
    204     p: v for p, v in parameter_values.items() if not isinstance(v, numbers.Number)
    205 }
    206 if nan_parameter_values:
--> 207     raise CircuitError(
    208         f"Expression cannot bind non-numeric values ({nan_parameter_values})"
    209     )

CircuitError: 'Expression cannot bind non-numeric values ({ParameterVectorElement(x[0]): array([ 1,  2,  7, 12])})'
$\endgroup$

1 Answer 1

0
$\begingroup$

See QuantumKernel API Ref where construct_circuit takes a ParameterVector for x and y. You can see an instance of how it's used, when in the source it calls that as part of calculating the kernel

$\endgroup$
2
  • $\begingroup$ Thanks for your suggestion, I'm looking for examples to achieve what I want, but it seems that there are too few demos like this $\endgroup$ Commented Sep 19, 2022 at 23:53
  • $\begingroup$ There is a tutorial for QuantumKernel here qiskit.org/documentation/machine-learning/tutorials/… The construct circuit method was exposed since it has been the case that people often want to see the circuit(s) that get used under the covers - in normal use you would not have to call this, the tutorial for instance does not. $\endgroup$
    – Steve Wood
    Commented Sep 20, 2022 at 13:31

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .