def init_params(self, params, algo_input): algo_params = params.get(QuantumAlgorithm.SECTION_KEY_ALGORITHM) override_spsa_params = algo_params.get('override_SPSA_params') # Set up optimizer opt_params = params.get(QuantumAlgorithm.SECTION_KEY_OPTIMIZER) optimizer = get_optimizer_instance(opt_params['name']) # If SPSA then override SPSA params as reqd to our predetermined values if opt_params['name'] == 'SPSA' and override_spsa_params: opt_params['c0'] = 4.0 opt_params['c1'] = 0.1 opt_params['c2'] = 0.602 opt_params['c3'] = 0.101 opt_params['c4'] = 0.0 opt_params['skip_calibration'] = True optimizer.init_params(opt_params) # Set up variational form fea_map_params = params.get(QuantumAlgorithm.SECTION_KEY_FEATURE_MAP) num_qubits = get_feature_dimension(algo_input.training_dataset) fea_map_params['num_qubits'] = num_qubits feature_map = get_feature_map_instance(fea_map_params['name']) feature_map.init_params(fea_map_params) # Set up variational form var_form_params = params.get(QuantumAlgorithm.SECTION_KEY_VAR_FORM) var_form_params['num_qubits'] = num_qubits var_form = get_variational_form_instance(var_form_params['name']) var_form.init_params(var_form_params) self.init_args(algo_input.training_dataset, algo_input.test_dataset, algo_input.datapoints, optimizer, feature_map, var_form)
def init_params(self, params, algo_input): # Set up optimizer opt_params = params.get(QuantumAlgorithm.SECTION_KEY_OPTIMIZER) optimizer = get_optimizer_instance(opt_params['name']) # hard-coded params if SPSA is used. if opt_params['name'] == 'SPSA' and opt_params['parameters'] is None: opt_params['parameters'] = np.asarray( [4.0, 0.1, 0.602, 0.101, 0.0]) optimizer.init_params(opt_params) # Set up variational form fea_map_params = params.get(QuantumAlgorithm.SECTION_KEY_FEATURE_MAP) num_qubits = self._auto_detect_qubitnum(algo_input.training_dataset) fea_map_params['num_qubits'] = num_qubits feature_map = get_feature_map_instance(fea_map_params['name']) feature_map.init_params(fea_map_params) # Set up variational form var_form_params = params.get(QuantumAlgorithm.SECTION_KEY_VAR_FORM) var_form_params['num_qubits'] = num_qubits var_form = get_variational_form_instance(var_form_params['name']) var_form.init_params(var_form_params) self.init_args(algo_input.training_dataset, algo_input.test_dataset, algo_input.datapoints, optimizer, feature_map, var_form)
def init_params(self, params, algo_input): if algo_input.training_dataset is None: raise AlgorithmError("Training dataset is required.") fea_map_params = params.get(QuantumAlgorithm.SECTION_KEY_FEATURE_MAP) feature_map = get_feature_map_instance(fea_map_params['name']) num_qubits = get_feature_dimension(algo_input.training_dataset) fea_map_params['num_qubits'] = num_qubits feature_map.init_params(fea_map_params) is_multiclass = get_num_classes(algo_input.training_dataset) > 2 if is_multiclass: multicls_ext_params = params.get( QuantumAlgorithm.SECTION_KEY_MULTICLASS_EXTENSION) multiclass_extension = get_multiclass_extension_instance( multicls_ext_params['name']) # we need to set this explicitly for quantum version multicls_ext_params['params'] = [feature_map, self] multicls_ext_params['estimator_cls'] = QKernalSVM_Estimator multiclass_extension.init_params(multicls_ext_params) logger.info("Multiclass classifcation algo:" + multicls_ext_params['name']) else: logger.warning( "Only two classes in the dataset, use binary classifer" " and ignore all options of multiclass_extension") multiclass_extension = None self.init_args(algo_input.training_dataset, algo_input.test_dataset, algo_input.datapoints, feature_map, multiclass_extension)
def test_svm_qkernel_binary_directly(self): svm = get_algorithm_instance("QSVM.Kernel") svm.random_seed = self.random_seed svm.setup_quantum_backend(backend='local_qasm_simulator_py', shots=self.shots) num_qubits = 2 feature_map = get_feature_map_instance('SecondOrderExpansion') feature_map.init_args(num_qubits=num_qubits, depth=2, entangler_map={0: [1]}) svm.init_args(self.training_data, self.testing_data, None, feature_map, None) result = svm.run() np.testing.assert_array_almost_equal(result['kernel_matrix_training'], self.ref_kernel_matrix_training, decimal=4) np.testing.assert_array_almost_equal(result['kernel_matrix_testing'], self.ref_kernel_matrix_testing, decimal=4) self.assertEqual(len(result['svm']['support_vectors']), 4) np.testing.assert_array_almost_equal(result['svm']['support_vectors'], self.ref_support_vectors, decimal=4) np.testing.assert_array_almost_equal(result['svm']['alphas'], self.ref_alpha, decimal=4) np.testing.assert_array_almost_equal(result['svm']['bias'], self.ref_bias, decimal=4) self.assertEqual(result['testing_accuracy'], 0.5)
def test_svm_variational_directly(self): np.random.seed(self.random_seed) svm = get_algorithm_instance("QSVM.Variational") svm.random_seed = self.random_seed svm.setup_quantum_backend(backend='local_qasm_simulator', shots=1024) optimizer = get_optimizer_instance('SPSA') optimizer.init_args(max_trials=10, c0=4.0, skip_calibration=True) optimizer.set_options(save_steps=1) num_qubits = 2 feature_map = get_feature_map_instance('SecondOrderExpansion') feature_map.init_args(num_qubits=num_qubits, depth=2) var_form = get_variational_form_instance('RYRZ') var_form.init_args(num_qubits=num_qubits, depth=3) svm.init_args(self.training_data, self.testing_data, None, optimizer, feature_map, var_form) result = svm.run() np.testing.assert_array_almost_equal(result['opt_params'], self.ref_opt_params, decimal=4) np.testing.assert_array_almost_equal(result['training_loss'], self.ref_train_loss, decimal=8) self.assertEqual(result['testing_accuracy'], 0.5)
def init_params(self, params, algo_input): fea_map_params = params.get(QuantumAlgorithm.SECTION_KEY_FEATURE_MAP) feature_map = get_feature_map_instance(fea_map_params['name']) num_qubits = get_feature_dimension(algo_input.training_dataset) fea_map_params['num_qubits'] = num_qubits feature_map.init_params(fea_map_params) multiclass_extension = None multiclass_extension_params = params.get(QuantumAlgorithm.SECTION_KEY_MULTICLASS_EXTENSION) if multiclass_extension_params is not None: multiclass_extension = get_multiclass_extension_instance(multiclass_extension_params['name']) multiclass_extension_params['params'] = [feature_map, self] multiclass_extension_params['estimator_cls'] = QSVM_Kernel_Estimator multiclass_extension.init_params(multiclass_extension_params) logger.info("Multiclass dataset with extension: {}".format(multiclass_extension_params['name'])) self.init_args(algo_input.training_dataset, algo_input.test_dataset, algo_input.datapoints, feature_map, multiclass_extension)
sample_Total, training_input, test_input, class_labels = datasets.userDefinedData( file='bank1000.csv', location='', training_size=training_dataset_size, test_size=testing_dataset_size, class_labels=[0, 1], n=n, PLOT_DATA=False) datapoints, class_to_label = split_dataset_to_data_and_labels(test_input) svm = get_algorithm_instance("QSVM.Kernel") svm.random_seed = 10598 svm.setup_quantum_backend(backend='statevector_simulator') feature_map = get_feature_map_instance('SecondOrderExpansion') feature_map.init_args(num_qubits=2, depth=2, entanglement='linear') svm.init_args(training_input, test_input, datapoints[0], feature_map) print("Running algo") result = svm.run() print("kernel matrix during the training:") kernel_matrix = result['kernel_matrix_training'] img = plt.imshow(np.asmatrix(kernel_matrix), interpolation='nearest', origin='upper', cmap='bone_r') plt.show()