qiskit-community / qiskit-machine-learning

Quantum Machine Learning
https://qiskit-community.github.io/qiskit-machine-learning/
Apache License 2.0
678 stars 326 forks source link

Error VQC training when using binary number list(0,1) as feature #731

Closed Herutriana44 closed 5 months ago

Herutriana44 commented 10 months ago

System Requirements

Error

when i using binary number list(0,1) as feature, i got this error

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File [c:\Users\herutriana44\AppData\Local\Programs\Python\Python311\Lib\site-packages\qiskit_machine_learning\neural_networks\sampler_qnn.py:360](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:360), in SamplerQNN._forward(self, input_data, weights)
    [359](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:359) try:
--> [360](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:360)     results = job.result()
    [361](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:361) except Exception as exc:

File [c:\Users\herutriana44\AppData\Local\Programs\Python\Python311\Lib\site-packages\qiskit\primitives\primitive_job.py:55](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit/primitives/primitive_job.py:55), in PrimitiveJob.result(self)
     [54](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit/primitives/primitive_job.py:54) self._check_submitted()
---> [55](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit/primitives/primitive_job.py:55) return self._future.result()

File [c:\Users\herutriana44\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\_base.py:456](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:456), in Future.result(self, timeout)
    [455](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:455) elif self._state == FINISHED:
--> [456](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:456)     return self.__get_result()
    [457](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:457) else:

File [c:\Users\herutriana44\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\_base.py:401](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:401), in Future.__get_result(self)
    [400](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:400) try:
--> [401](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:401)     raise self._exception
    [402](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:402) finally:
    [403](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/_base.py:403)     # Break a reference cycle with the exception in self._exception

File [c:\Users\herutriana44\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py:58](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/thread.py:58), in _WorkItem.run(self)
     [57](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/thread.py:57) try:
---> [58](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/concurrent/futures/thread.py:58)     result = self.fn(*self.args, **self.kwargs)
...
--> [362](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:362)     raise QiskitMachineLearningError("Sampler job failed.") from exc
    [363](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:363) result = self._postprocess(num_samples, results)
    [365](file:///C:/Users/herutriana44/AppData/Local/Programs/Python/Python311/Lib/site-packages/qiskit_machine_learning/neural_networks/sampler_qnn.py:365) return result

QiskitMachineLearningError: 'Sampler job failed.'

but, when i was change feature from binary list(every list, has 30 values in integer) to integer list(has 2 values in integer). in bottom, my dataset, my plan i use dna binary as feature(i've changed to list binary) but i got error. but if i use 0 and 1 as feature(this columns are count 0 or 1 in string dna binary), i don't get this error.

i        dna binary 0   1   label
0   0100001000010100010000010010001100100010001001...   43  17  alpha
1   0100001100100010010001000001001000010010000101...   44  16  beta
2   0100001000010010001000010001000100010100010000...   45  15  beta
3   0100001000010010001000010001000100010100010000...   44  16  beta
4   0010001101000001001101000010001100100010001001...   42  18  beta

in bottom, vqc code for dna binary

feature_map = ZZFeatureMap(feature_dimension=len(X[0]), reps=3, entanglement="linear") # len(X[0]) = 30
ansatz = TwoLocal(feature_map.num_qubits, ["ry", "rz"], "cz", reps=3)
vqc = VQC(
                feature_map=feature_map,
                ansatz=ansatz,
                optimizer=COBYLA(maxiter=100),
            )

in bottom, vqc code for 0 and 1 column

feature_map = ZZFeatureMap(feature_dimension=2, reps=3, entanglement="linear") # len(X[0]) = 30
ansatz = TwoLocal(feature_map.num_qubits, ["ry", "rz"], "cz", reps=3)
vqc = VQC(
                feature_map=feature_map,
                ansatz=ansatz,
                optimizer=COBYLA(maxiter=100),
            )

any idea for this error?

oscar-wallis commented 5 months ago

Hi @Herutriana44, could you send me the full code and data you use for this? TheQiskitMachineLearningError: 'Sampler job failed.' error is generic and doesn't tell me much about your error.

Possible Issues

  1. Memory Limitations My immediate thought of what your error is gonna be a memory issue, as in you are trying to simulate 30 qubits and your device hasn't got enough RAM.
  2. Backend Limitations You could also be being limited by your backend which isn't mentioned in the code, different backends have hard limits on how many qubits they can simulate. Often at or below 30 qubits.
  3. Length of Binary dna. Another problem I could see is you said the 1 and 0 columns work fine and each one of them sums the number of 1s and 0s in the binary number respectively. If those numbers are correct your dna binary strings are actually 50 characters long, 50 features is definitely out of reach for regular computer hardware to simulate.
  4. Binary string dtype conversion The binary strings need to be converted to a list of integers where each 0 or 1 is split from the others for the input to make sense to the network, Ie bin_string = "0110"- a string of numbers needs to convert to -> bin_array = np.array([0, 1, 1,0 ], dtype=int) - an array of 0s and 1s. If you input the str, byte (binary) or the binary numbers not split up (as in bin_int = 0110 which is just the number 110) then the network won't understand the input data, though I think this would likely throw a different error.

Those are my immediate intuitions but I can't confirm anything without testing your code myself.