18
18
checking its objective function value).
19
19
"""
20
20
21
+ from typing import Tuple
21
22
import numpy as np
22
23
from qiskit .quantum_info import Pauli
23
24
25
+ from qiskit .aqua .algorithms import MinimumEigensolverResult
24
26
from qiskit .aqua .operators import WeightedPauliOperator
25
27
28
+ # pylint: disable=invalid-name
26
29
27
- def get_vehiclerouting_matrices (instance , n , K ): # pylint: disable=invalid-name
30
+
31
+ def get_vehiclerouting_matrices (instance : np .ndarray ,
32
+ n : int ,
33
+ K : int ) -> Tuple [np .ndarray , np .ndarray , float ]:
28
34
"""Constructs auxiliary matrices from a vehicle routing instance,
29
35
which represent the encoding into a binary quadratic program.
30
36
This is used in the construction of the qubit ops and computation
31
37
of the solution cost.
32
38
33
39
Args:
34
- instance (numpy.ndarray) : a customers-to-customers distance matrix.
35
- n (integer) : the number of customers.
36
- K (integer) : the number of vehicles available.
40
+ instance: a customers-to-customers distance matrix.
41
+ n: the number of customers.
42
+ K: the number of vehicles available.
37
43
38
44
Returns:
39
- tuple(numpy.ndarray, numpy.ndarray, float):
40
- a matrix defining the interactions between variables.
41
- a matrix defining the contribution from the individual variables.
42
- the constant offset.
45
+ a matrix defining the interactions between variables.
46
+ a matrix defining the contribution from the individual variables.
47
+ the constant offset.
43
48
"""
44
- # pylint: disable=invalid-name
45
49
# N = (n - 1) * n
46
50
A = np .max (instance ) * 100 # A parameter of cost function
47
51
@@ -83,22 +87,21 @@ def get_vehiclerouting_matrices(instance, n, K): # pylint: disable=invalid-name
83
87
# c is the constant offset
84
88
c = 2 * A * (n - 1 ) + 2 * A * (K ** 2 )
85
89
86
- return ( Q , g , c )
90
+ return Q , g , c
87
91
88
92
89
- def get_vehiclerouting_cost (instance , n , K , x_sol ): # pylint: disable=invalid-name
93
+ def get_vehiclerouting_cost (instance : np . ndarray , n : int , K : int , x_sol : np . ndarray ) -> float :
90
94
"""Computes the cost of a solution to an instance of a vehicle routing problem.
91
95
92
96
Args:
93
- instance (numpy.ndarray) : a customers-to-customers distance matrix.
94
- n (integer) : the number of customers.
95
- K (integer) : the number of vehicles available.
96
- x_sol (numpy.ndarray) : a solution, i.e., a path, in its binary representation.
97
+ instance: a customers-to-customers distance matrix.
98
+ n: the number of customers.
99
+ K: the number of vehicles available.
100
+ x_sol: a solution, i.e., a path, in its binary representation.
97
101
98
102
Returns:
99
- float: objective function value.
103
+ objective function value.
100
104
"""
101
- # pylint: disable=invalid-name
102
105
(Q , g , c ) = get_vehiclerouting_matrices (instance , n , K )
103
106
104
107
def fun (x ):
@@ -108,18 +111,17 @@ def fun(x):
108
111
return cost
109
112
110
113
111
- def get_operator (instance , n , K ): # pylint: disable=invalid-name
114
+ def get_operator (instance : np . ndarray , n : int , K : int ) -> WeightedPauliOperator :
112
115
"""Converts an instance of a vehicle routing problem into a list of Paulis.
113
116
114
117
Args:
115
- instance (numpy.ndarray) : a customers-to-customers distance matrix.
116
- n (integer) : the number of customers.
117
- K (integer) : the number of vehicles available.
118
+ instance: a customers-to-customers distance matrix.
119
+ n: the number of customers.
120
+ K: the number of vehicles available.
118
121
119
122
Returns:
120
- WeightedPauliOperator: operator for the Hamiltonian.
123
+ operator for the Hamiltonian.
121
124
"""
122
- # pylint: disable=invalid-name
123
125
N = (n - 1 ) * n
124
126
(Q , g__ , c ) = get_vehiclerouting_matrices (instance , n , K )
125
127
@@ -154,25 +156,27 @@ def get_operator(instance, n, K): # pylint: disable=invalid-name
154
156
return WeightedPauliOperator (paulis = pauli_list )
155
157
156
158
157
- def get_vehiclerouting_solution (instance , n , K , result ): # pylint: disable=invalid-name
159
+ def get_vehiclerouting_solution (instance : np .ndarray ,
160
+ n : int ,
161
+ K : int ,
162
+ result : MinimumEigensolverResult ) -> np .ndarray :
158
163
"""Tries to obtain a feasible solution (in vector form) of an instance
159
164
of vehicle routing from the results dictionary.
160
165
161
166
Args:
162
- instance (numpy.ndarray) : a customers-to-customers distance matrix.
163
- n (integer) : the number of customers.
164
- K (integer) : the number of vehicles available.
165
- result (dictionary) : a dictionary obtained by QAOA.run or VQE.run containing key 'eigvecs' .
167
+ instance: a customers-to-customers distance matrix.
168
+ n: the number of customers.
169
+ K: the number of vehicles available.
170
+ result: a result obtained by QAOA.run or VQE.run.
166
171
167
172
Returns:
168
- numpy.ndarray: a solution, i.e., a path, in its binary representation.
173
+ a solution, i.e., a path, in its binary representation.
169
174
170
175
#TODO: support statevector simulation, results should be a statevector or counts format, not
171
176
a result from algorithm run
172
177
"""
173
- # pylint: disable=invalid-name
174
178
del instance , K # unused
175
- v = result [ 'eigvecs' ][ 0 ]
179
+ v = result . eigenstate
176
180
N = (n - 1 ) * n
177
181
178
182
index_value = [x for x in range (len (v )) if v [x ] == max (v )][0 ]
0 commit comments