1
$\begingroup$

I am using source code of split_dataset_to_data_and_labels as I am unable to install qiskit-aqua because of error. But in that source code, "keys" is not being identified. I am using qiskit's tutorials as I am learning, and code I wrote from it, is-:

feature_dim = 2
training_dataset_size = 20
testing_dataset_size = 10
shot = 10000
sample_Total, training_input, test_input, class_labels = ad_hoc_data(training_size=training_dataset_size,
                                                                    test_size=testing_dataset_size,
                                                                    gap=0.3,
                                                                    n=feature_dim,
                                                                    plot_data=True)
datapoints, class_to_label = split_dataset_to_data_and_labels(test_input)
print(class_to_label)

It is giving this error-:

AttributeError                            Traceback (most recent call last)
Input In [6], in <cell line: 10>()
      4 shot = 10000
      5 sample_Total, training_input, test_input, class_labels = ad_hoc_data(training_size=training_dataset_size,
      6                                                                     test_size=testing_dataset_size,
      7                                                                     gap=0.3,
      8                                                                     n=feature_dim,
      9                                                                     plot_data=True)
---> 10 datapoints, class_to_label = split_dataset_to_data_and_labels(test_input)
     11 print(class_to_label)

Input In [4], in split_dataset_to_data_and_labels(dataset, class_names)
     22 labels = []
     23 if class_names is None:
---> 24     sorted_classes_name = sorted(list(dataset.keys()))
     25     class_to_label = {k: idx for idx, k in enumerate(sorted_classes_name)}
     26 else:

AttributeError: 'numpy.ndarray' object has no attribute 'keys'

The link to source code, I am using is https://github.com/Qiskit/qiskit-aqua/blob/main/qiskit/aqua/utils/dataset_helper.py . I found at https://stackoverflow.com/questions/66591189/can-you-help-me-in-this-error-attributeerror-numpy-ndarray-object-has-no-att that flags can be used but it also gives error.

$\endgroup$

1 Answer 1

2
$\begingroup$

Qiskit Aqua (now deprecated) contains the function ad_hoc_data[1] which returns sample_total, training_input, test_input, class_labels. When using it, you need to call split_dataset_to_data_and_labels to split the retuned dataset to data and labels.

On the other hand, Qiskit Machine Learning contains a new version of ad_hoc_data[2] whcih returns training_features, training_labels, test_features, test_labels. There is no need to use split_dataset_to_data_and_labels with it.

It looks like you are using functions from old qiskit.ml with functions from new qiskit_machine_learning. You shouldn't. Try to follow the new Qiskit Machine Learning tutorials (here) to avoid these errors. The new version of ad_hoc_data is used in this tutorial.

$\endgroup$
1
  • $\begingroup$ Thank you for your help. $\endgroup$ Commented May 2, 2022 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.