Exemple #1
0
    def setUp(self):
        if 'CPLEX.Ising' not in local_pluggables('algorithm'):
            self.skipTest(
                'CPLEX.Ising algorithm not found - CPLEX not installed?')

        np.random.seed(8123179)
        self.w = maxcut.random_graph(4, edge_prob=0.5, weight_range=10)
        self.qubit_op, self.offset = maxcut.get_maxcut_qubitops(self.w)
        self.algo_input = get_input_instance('EnergyInput')
        self.algo_input.qubit_op = self.qubit_op
Exemple #2
0
    def setUp(self):
        self.random_seed = 10598
        self.training_data = {'A': np.asarray([[2.95309709, 2.51327412], [3.14159265, 4.08407045]]),
                              'B': np.asarray([[4.08407045, 2.26194671], [4.46106157, 2.38761042]])}
        self.testing_data = {'A': np.asarray([[3.83274304, 2.45044227]]),
                             'B': np.asarray([[3.89557489, 0.31415927]])}

        self.ref_opt_params = np.asarray([2.93868096, 0.76735399, 4.21845289, 4.28731786,
                                          -4.64804051, -4.0103384, 3.62083309, -3.1466139,
                                          3.36741576, 0.07314989, -1.92529824, -1.31781337,
                                          2.2547051, 7.29971351, 3.74421673, -3.74280352])
        self.ref_train_loss = 0.4999339230552529

        self.svm_input = get_input_instance('SVMInput')
        self.svm_input.training_dataset = self.training_data
        self.svm_input.test_dataset = self.testing_data
Exemple #3
0
    def setUp(self):
        self.random_seed = 10598
        self.training_data = {'A': np.asarray([[2.95309709, 2.51327412], [3.14159265, 4.08407045]]),
                              'B': np.asarray([[4.08407045, 2.26194671], [4.46106157, 2.38761042]])}
        self.testing_data = {'A': np.asarray([[3.83274304, 2.45044227]]),
                             'B': np.asarray([[3.89557489, 0.31415927]])}

        self.ref_kernel_matrix_training = np.asarray([[1., 0.8388671875, 0.1142578125, 0.3564453125],
                                                      [0.8388671875, 1., 0.1044921875, 0.427734375],
                                                      [0.1142578125, 0.1044921875, 1., 0.66015625],
                                                      [0.3564453125, 0.427734375, 0.66015625, 1.]])

        self.ref_kernel_matrix_testing = np.asarray([[0.1357421875, 0.166015625, 0.4580078125, 0.140625],
                                                     [0.3076171875, 0.3623046875, 0.0166015625, 0.150390625]])

        self.ref_support_vectors = np.asarray([[2.95309709, 2.51327412], [3.14159265, 4.08407045],
                                               [4.08407045, 2.26194671], [4.46106157, 2.38761042]])

        self.svm_input = get_input_instance('SVMInput')
        self.svm_input.training_dataset = self.training_data
        self.svm_input.test_dataset = self.testing_data
Exemple #4
0
    def setUp(self):
        self.random_seed = 10598
        self.shots = 8192
        np.random.seed(self.random_seed)
        self.training_data = {
            'A': np.asarray([[2.95309709, 2.51327412],
                             [3.14159265, 4.08407045]]),
            'B': np.asarray([[4.08407045, 2.26194671],
                             [4.46106157, 2.38761042]])
        }
        self.testing_data = {
            'A': np.asarray([[3.83274304, 2.45044227]]),
            'B': np.asarray([[3.89557489, 0.31415927]])
        }

        self.ref_kernel_matrix_training = np.asarray(
            [[1., 0.84851074, 0.12390137, 0.36669922],
             [0.84851074, 1., 0.11950684, 0.45507812],
             [0.12390137, 0.11950684, 1., 0.67211914],
             [0.36669922, 0.45507812, 0.67211914, 1.]])

        self.ref_kernel_matrix_testing = np.asarray(
            [[0.14575195, 0.18237305, 0.47644043, 0.14587402],
             [0.33203125, 0.37573242, 0.0222168, 0.15698242]])

        self.ref_support_vectors = np.asarray([[2.95309709, 2.51327412],
                                               [3.14159265, 4.08407045],
                                               [4.08407045, 2.26194671],
                                               [4.46106157, 2.38761042]])
        self.ref_alpha = np.asarray(
            [0.39755359, 1.46035009, 0.03446283, 1.82344085])

        self.ref_bias = np.asarray([-0.03624927])

        self.svm_input = get_input_instance('SVMInput')
        self.svm_input.training_dataset = self.training_data
        self.svm_input.test_dataset = self.testing_data
        "coeff": {
            "imag": 0.0,
            "real": 0.1570
        },
        "label": "ZX"
    }, {
        "coeff": {
            "imag": 0.0,
            "real": 0.1115
        },
        "label": "ZZ"
    }]
}

# Create input variable that specifies we want the energy value
algo_input = get_input_instance("EnergyInput")

# Adds the Hamiltonian information to the input variable
algo_input.qubit_op = Operator.load_from_dict(pauli_dict)

# Defines the algorithm input in terms of the pauli dict and that we want the energy value
# Specifies the attributes of the algorithm that will be used to find the ground state energy
# algorithm: we will use the VQE
# optimiser: best choice for optimisation dependent on the simulated system
# variational_form: the ansatz or first guess at a solution
# depth: the complexity of the circuit used in the algorithm
# backend: the actual device the code will be run on
params = {
    "algorithm": {
        "name": "VQE"
    },
Exemple #6
0
        "label": "ZI"
    }, {
        "coeff": {
            "imag": 0.0,
            "real": -0.39793742484318045
        },
        "label": "ZZ"
    }, {
        "coeff": {
            "imag": 0.0,
            "real": 0.18093119978423156
        },
        "label": "XX"
    }]
}
algo_input = get_input_instance('EnergyInput')
algo_input.qubit_op = Operator.load_from_dict(pauli_dict)
params = {
    'algorithm': {
        'name': 'VQE'
    },
    'optimizer': {
        'name': 'SPSA'
    },
    'variational_form': {
        'name': 'RY',
        'depth': 5
    },
    'backend': {
        'name': 'local_qasm_simulator'
    }
Exemple #7
0
    def test_qsvm_kernel_multiclass_one_against_all(self):
        training_input = {
            'A':
            np.asarray([[0.6560706, 0.17605998], [0.25776033, 0.47628296],
                        [0.8690704, 0.70847635]]),
            'B':
            np.asarray([[0.38857596, -0.33775802], [0.49946978, -0.48727951],
                        [0.49156185, -0.3660534]]),
            'C':
            np.asarray([[-0.68088231, 0.46824423], [-0.56167659, 0.65270294],
                        [-0.82139073, 0.29941512]])
        }

        test_input = {
            'A':
            np.asarray([[0.57483139, 0.47120732], [0.48372348, 0.25438544],
                        [0.48142649, 0.15931707]]),
            'B':
            np.asarray([[-0.06048935, -0.48345293], [-0.01065613, -0.33910828],
                        [0.06183066, -0.53376975]]),
            'C':
            np.asarray([[-0.74561108, 0.27047295], [-0.69942965, 0.11885162],
                        [-0.66489165, 0.1181712]])
        }

        temp = [test_input[k] for k in test_input]
        total_array = np.concatenate(temp)

        params = {
            'problem': {
                'name': 'svm_classification',
                'random_seed': self.random_seed
            },
            'algorithm': {
                'name': 'QSVM.Kernel',
            },
            'backend': {
                'name': 'qasm_simulator',
                'shots': self.shots
            },
            'multiclass_extension': {
                'name': 'OneAgainstRest'
            },
            'feature_map': {
                'name': 'SecondOrderExpansion',
                'depth': 2,
                'entangler_map': {
                    0: [1]
                }
            }
        }

        algo_input = get_input_instance('SVMInput')
        algo_input.training_dataset = training_input
        algo_input.test_dataset = test_input
        algo_input.datapoints = total_array

        result = run_algorithm(params, algo_input)

        # Note: Result here is dependent on platform with the C++ simulator
        expected_accuracy = 0.555555555 if sys.platform.startswith(
            'linux') else 0.444444444
        expected_classes = ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'C'] if sys.platform.startswith('linux') else \
                           ['A', 'A', 'C', 'A', 'A', 'A', 'A', 'C', 'C']
        self.assertAlmostEqual(result['testing_accuracy'],
                               expected_accuracy,
                               places=4,
                               msg='Please ensure you are using c++ simulator')
        self.assertEqual(result['predicted_classes'], expected_classes)