qiskit-community / qiskit-aqua

Quantum Algorithms & Applications (**DEPRECATED** since April 2021 - see readme for more info)
https://qiskit.org/aqua
Apache License 2.0
571 stars 377 forks source link

QSVM fails under Python 3.8.5 #1226

Closed woodsp-ibm closed 4 years ago

woodsp-ibm commented 4 years ago

Hello, I am trying this modification using the simple updated tutorial: https://github.com/Qiskit/qiskit-tutorials/blob/master/legacy_tutorials/aqua/machine_learning/qsvm_classification.ipynb

I notice that everything works fine using Python 3.7.7 BUT using the newest python 3.8.5 only the jupyter version of this code is working. If I run it as a code, the qsvm.run instance create n subprocesses of the same instances going into a endless loop...the code recall itself....

The above was a comment in PR #1190 from @grossiM The other PR is related to QSVM but different so I broke this out here.

@grossiM Can I ask for some other information that can be of help to us. Such as what version are you running when you see this and what platform are you using

grossiM commented 4 years ago

Hello Steve, thanks for splitting up this problem. I attach my python code and the output file that I am running on a MacBook Pro (15-inch, 2019): 2,3 GHz 8-Core Intel Core i9 ProductName: Mac OS X ProductVersion: 10.15.5 BuildVersion: 19F101 executed into Anaconda(conda 4.8.3) env generated with: Python 3.8.5 and the latest qiskit ('qiskit-terra': '0.15.1', 'qiskit-aer': '0.6.1', 'qiskit-ignis': '0.4.0', 'qiskit-ibmq-provider': '0.8.0', 'qiskit-aqua': '0.7.5', 'qiskit': '0.20.0') with the modification described https://github.com/Qiskit/qiskit-aqua/pull/1190/commits/51789a6657c20e4c8ea5747870f4fa262fc44694

output.log THE CODE IS HERE:

`import matplotlib.pyplot as plt import numpy as np from qiskit import BasicAer from qiskit.ml.datasets import * from qiskit.circuit.library import ZZFeatureMap from qiskit.aqua.utils import split_dataset_to_data_and_labels, map_label_to_class_name from qiskit.aqua import QuantumInstance from qiskit.aqua.algorithms import QSVM import qiskit print(qiskit.__qiskit_version__) import logging from qiskit.aqua import set_qiskit_aqua_logging set_qiskit_aqua_logging(logging.DEBUG)

feature_dim=2 # we support feature_dim 2 or 3 sample_Total, training_input, test_input, class_labels = ad_hoc_data( training_size=20, test_size=10, n=feature_dim, gap=0.3, plot_data=False ) extra_test_data = sample_ad_hoc_data(sample_Total, 10, n=feature_dim) datapoints, class_to_label = split_dataset_to_data_and_labels(extra_test_data)

seed = 10598 feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement='linear') qsvm = QSVM(feature_map, training_input, test_input, datapoints[0])

backend = BasicAer.get_backend('qasm_simulator') quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed) print('A'10) result = qsvm.run(quantum_instance) print('B'10)

manoelmarques commented 4 years ago

Everybody should always include if __name__ == "__main__": in their python scripts.

manoelmarques commented 4 years ago

This version works fine:

""" test """

import matplotlib.pyplot as plt
import numpy as np
from qiskit import BasicAer
from qiskit.ml.datasets import *
from qiskit.circuit.library import ZZFeatureMap
from qiskit.aqua.utils import split_dataset_to_data_and_labels, map_label_to_class_name
from qiskit.aqua import QuantumInstance
from qiskit.aqua.algorithms import QSVM
import qiskit
import logging
from qiskit.aqua import set_qiskit_aqua_logging

if __name__ == "__main__":
    # print(qiskit.qiskit_version)
    # set_qiskit_aqua_logging(logging.DEBUG)
    feature_dim = 2  # we support feature_dim 2 or 3
    sample_Total, training_input, test_input, class_labels = ad_hoc_data(training_size=20,
                                                                         test_size=10,
                                                                         n=feature_dim,
                                                                         gap=0.3,
                                                                         plot_data=False)
    extra_test_data = sample_ad_hoc_data(sample_Total, 10, n=feature_dim)
    datapoints, class_to_label = split_dataset_to_data_and_labels(extra_test_data)

    seed = 10598
    feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement='linear')
    qsvm = QSVM(feature_map, training_input, test_input, datapoints[0])

    backend = BasicAer.get_backend('qasm_simulator')
    quantum_instance = QuantumInstance(backend, shots=1024, seed_simulator=seed, seed_transpiler=seed)
    print('A'*10)
    result = qsvm.run(quantum_instance)
    print('B'*10)
manoelmarques commented 4 years ago

If one runs everything at global level, the multiprocessing module may spawn new processes that will import your whole module (file) hence the strange multiple print outputs. Adding __name__ == "__main__": should be added to any python script to avoid those side effects.

grossiM commented 4 years ago

Hello, thank you very much for the input. Yes, it's working fine now. In previous release I was working without the if name == "main": and it was working, from now on I will include it.

woodsp-ibm commented 4 years ago

As your issue is solved I will close this. I will note that multiprocessing changed in python in 3.8 from what it was in 3.7 hence that is explanation around the behavior change.