Skip to content

Commit cb48775

Browse files
Replace algorithm result eigvecs by eingenstate (qiskit-community/qiskit-aqua#997)
* Replace algorithm result eigvecs by eingenstate * remove return type from docstring
1 parent 666fff4 commit cb48775

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

qiskit/optimization/applications/ising/vehicle_routing.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,34 @@
1818
checking its objective function value).
1919
"""
2020

21+
from typing import Tuple
2122
import numpy as np
2223
from qiskit.quantum_info import Pauli
2324

25+
from qiskit.aqua.algorithms import MinimumEigensolverResult
2426
from qiskit.aqua.operators import WeightedPauliOperator
2527

28+
# pylint: disable=invalid-name
2629

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]:
2834
"""Constructs auxiliary matrices from a vehicle routing instance,
2935
which represent the encoding into a binary quadratic program.
3036
This is used in the construction of the qubit ops and computation
3137
of the solution cost.
3238
3339
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.
3743
3844
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.
4348
"""
44-
# pylint: disable=invalid-name
4549
# N = (n - 1) * n
4650
A = np.max(instance) * 100 # A parameter of cost function
4751

@@ -83,22 +87,21 @@ def get_vehiclerouting_matrices(instance, n, K): # pylint: disable=invalid-name
8387
# c is the constant offset
8488
c = 2 * A * (n - 1) + 2 * A * (K ** 2)
8589

86-
return (Q, g, c)
90+
return Q, g, c
8791

8892

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:
9094
"""Computes the cost of a solution to an instance of a vehicle routing problem.
9195
9296
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.
97101
98102
Returns:
99-
float: objective function value.
103+
objective function value.
100104
"""
101-
# pylint: disable=invalid-name
102105
(Q, g, c) = get_vehiclerouting_matrices(instance, n, K)
103106

104107
def fun(x):
@@ -108,18 +111,17 @@ def fun(x):
108111
return cost
109112

110113

111-
def get_operator(instance, n, K): # pylint: disable=invalid-name
114+
def get_operator(instance: np.ndarray, n: int, K: int) -> WeightedPauliOperator:
112115
"""Converts an instance of a vehicle routing problem into a list of Paulis.
113116
114117
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.
118121
119122
Returns:
120-
WeightedPauliOperator: operator for the Hamiltonian.
123+
operator for the Hamiltonian.
121124
"""
122-
# pylint: disable=invalid-name
123125
N = (n - 1) * n
124126
(Q, g__, c) = get_vehiclerouting_matrices(instance, n, K)
125127

@@ -154,25 +156,27 @@ def get_operator(instance, n, K): # pylint: disable=invalid-name
154156
return WeightedPauliOperator(paulis=pauli_list)
155157

156158

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:
158163
"""Tries to obtain a feasible solution (in vector form) of an instance
159164
of vehicle routing from the results dictionary.
160165
161166
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.
166171
167172
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.
169174
170175
#TODO: support statevector simulation, results should be a statevector or counts format, not
171176
a result from algorithm run
172177
"""
173-
# pylint: disable=invalid-name
174178
del instance, K # unused
175-
v = result['eigvecs'][0]
179+
v = result.eigenstate
176180
N = (n - 1) * n
177181

178182
index_value = [x for x in range(len(v)) if v[x] == max(v)][0]

0 commit comments

Comments
 (0)