def setUp(self): super().setUp() np.random.seed(50) pauli_dict = { 'paulis': [{"coeff": {"imag": 0.0, "real": -1.052373245772859}, "label": "II"}, {"coeff": {"imag": 0.0, "real": 0.39793742484318045}, "label": "IZ"}, {"coeff": {"imag": 0.0, "real": -0.39793742484318045}, "label": "ZI"}, {"coeff": {"imag": 0.0, "real": -0.01128010425623538}, "label": "ZZ"}, {"coeff": {"imag": 0.0, "real": 0.18093119978423156}, "label": "XX"} ] } qubit_op = Operator.load_from_dict(pauli_dict) self.algo_input = EnergyInput(qubit_op) backends = ['statevector_simulator', 'qasm_simulator'] res = {} for backend in backends: params_no_caching = { 'algorithm': {'name': 'VQE', 'operator_mode': 'matrix' if backend == 'statevector_simulator' else 'paulis'}, 'problem': {'name': 'energy', 'random_seed': 50, 'circuit_caching': False, 'skip_qobj_deepcopy': False, 'skip_qobj_validation': False, 'circuit_cache_file': None, }, 'backend': {'provider': 'qiskit.BasicAer', 'name': backend, 'shots': 1000}, } qiskit_aqua = QiskitAqua(params_no_caching, self.algo_input) res[backend] = qiskit_aqua.run() self.reference_vqe_result = res
def test_vqe_caching_via_run_algorithm(self, backend, caching, skip_qobj_deepcopy): skip_validation = True params_caching = { 'algorithm': {'name': 'VQE', 'operator_mode': 'matrix' if backend == 'statevector_simulator' else 'paulis'}, 'problem': {'name': 'energy', 'random_seed': 50, 'circuit_caching': caching, 'skip_qobj_deepcopy': skip_qobj_deepcopy, 'skip_qobj_validation': skip_validation, 'circuit_cache_file': None, }, 'backend': {'provider': 'qiskit.BasicAer', 'name': backend, 'shots': 1000}, } qiskit_aqua = QiskitAqua(params_caching, self.algo_input) result_caching = qiskit_aqua.run() self.assertAlmostEqual(result_caching['energy'], self.reference_vqe_result[backend]['energy']) np.testing.assert_array_almost_equal(self.reference_vqe_result[backend]['eigvals'], result_caching['eigvals'], 5) np.testing.assert_array_almost_equal(self.reference_vqe_result[backend]['opt_params'], result_caching['opt_params'], 5) if qiskit_aqua.quantum_instance.has_circuit_caching: self.assertEqual(qiskit_aqua.quantum_instance._circuit_cache.misses, 0) self.assertIn('eval_count', result_caching) self.assertIn('eval_time', result_caching)
def _build_refrence_result(self, backends): res = {} os.environ.pop('QISKIT_AQUA_CIRCUIT_CACHE', None) for backend in backends: params_no_caching = { 'algorithm': { 'name': 'VQE' }, 'problem': { 'name': 'energy', 'random_seed': 50, 'circuit_caching': False, 'skip_qobj_deepcopy': False, 'skip_qobj_validation': False, 'circuit_cache_file': None, }, 'backend': { 'provider': 'qiskit.BasicAer', 'name': backend }, } if backend != 'statevector_simulator': params_no_caching['backend']['shots'] = 1000 params_no_caching['optimizer'] = { 'name': 'SPSA', 'max_trials': 15 } qiskit_aqua = QiskitAqua(params_no_caching, self.algo_input) res[backend] = qiskit_aqua.run() os.environ['QISKIT_AQUA_CIRCUIT_CACHE'] = '1' self.reference_vqe_result = res
def _build_refrence_result(self, backends): res = {} for backend in backends: params_no_caching = { 'algorithm': { 'name': 'VQE', 'operator_mode': 'matrix' if backend == 'statevector_simulator' else 'paulis' }, 'problem': { 'name': 'energy', 'random_seed': 50, 'circuit_caching': False, 'skip_qobj_deepcopy': False, 'skip_qobj_validation': False, 'circuit_cache_file': None, }, 'backend': { 'provider': 'qiskit.BasicAer', 'name': backend }, } if backend != 'statevector_simulator': params_no_caching['backend']['shots'] = 1000 params_no_caching['optimizer'] = { 'name': 'SPSA', 'max_trials': 15 } qiskit_aqua = QiskitAqua(params_no_caching, self.algo_input) res[backend] = qiskit_aqua.run() self.reference_vqe_result = res
def test_vqe_caching_via_run_algorithm(self, backend, caching, skip_qobj_deepcopy): self._build_refrence_result(backends=[backend]) skip_validation = True params_caching = { 'algorithm': {'name': 'VQE'}, 'problem': {'name': 'energy', 'random_seed': 50, 'circuit_optimization_level': self.optimization_level, 'circuit_caching': caching, 'skip_qobj_deepcopy': skip_qobj_deepcopy, 'skip_qobj_validation': skip_validation, 'circuit_cache_file': None, }, 'backend': {'provider': 'qiskit.BasicAer', 'name': backend}, } if backend != 'statevector_simulator': params_caching['backend']['shots'] = 1000 params_caching['optimizer'] = {'name': 'SPSA', 'max_trials': 15} qiskit_aqua = QiskitAqua(params_caching, self.algo_input) result_caching = qiskit_aqua.run() self.assertAlmostEqual(result_caching['energy'], self.reference_vqe_result[backend]['energy']) np.testing.assert_array_almost_equal(self.reference_vqe_result[backend]['eigvals'], result_caching['eigvals'], 5) np.testing.assert_array_almost_equal(self.reference_vqe_result[backend]['opt_params'], result_caching['opt_params'], 5) if qiskit_aqua.quantum_instance.has_circuit_caching: self.assertEqual(qiskit_aqua.quantum_instance._circuit_cache.misses, 0) self.assertIn('eval_count', result_caching) self.assertIn('eval_time', result_caching)
def __init__(self): self.move = 0 self.data_file = 'data.csv' self.data_path = 'PlayerLogic' self.feature_dim = 9 # dimension of each data point sample_Total, training_input, test_input, class_labels = VQCQPlayer.userDefinedData( self.data_path, self.data_file, ['0', '1', '2', '3', '4', '5', '6', '7', '8'], training_size=6000, test_size=500, n=self.feature_dim, PLOT_DATA=False) temp = [test_input[k] for k in test_input] total_array = np.concatenate(temp) aqua_dict = { 'problem': { 'name': 'classification' }, 'algorithm': { 'name': 'SVM' }, 'multiclass_extension': { 'name': 'AllPairs' } } algo_input = ClassificationInput(training_input, test_input, total_array) from qiskit.aqua import QiskitAqua aqua_obj = QiskitAqua(aqua_dict, algo_input) self.algo_obj = aqua_obj.quantum_algorithm logger.info("Training the SVM....") aqua_obj.run() logger.info("Trained!")
def run_algorithm_from_json(params, output_file): """ Runs the Aqua Chemistry experiment from Qiskit Aqua json dictionary Args: params (dictionary): Qiskit Aqua json dictionary output_file (filename): Output file name to save results """ qiskit_aqua = QiskitAqua(params) ret = qiskit_aqua.run() if not isinstance(ret, dict): raise QiskitChemistryError('Algorithm run result should be a dictionary {}'.format(ret)) print('Output:') pprint(ret, indent=4) if output_file is not None: with open(output_file, 'w') as out: pprint(ret, stream=out, indent=4)
'algorithm': { 'name': 'SVM' }, 'multiclass_extension': { 'name': 'AllPairs' } } algo_input = SVMInput(training_input, test_input, total_array) from qiskit.aqua import QiskitAqua aqua_obj = QiskitAqua(aqua_dict, algo_input) algo_obj = aqua_obj.quantum_algorithm result = aqua_obj.run() #run_algorithm(aqua_dict, algo_input) for k, v in result.items(): print("'{}' : {}".format(k, v)) # 6 to_predict = singleDataItem('', 'data.csv', [1, 2, 0, 0, 0, 0, 0, 1, 0], n=3) print(algo_obj.predict(to_predict)) # 2 to_predict = singleDataItem('', 'data.csv', [1, 0, 1, 0, 0, 0, 0, 2, 0], n=3) print(algo_obj.predict(to_predict)) # 1 to_predict = singleDataItem('', 'data.csv', [0, 0, 0, 0, 0, 1, 0, 0, 0], n=3) print(algo_obj.predict(to_predict))