|
11 | 11 | # that they have been altered from the originals. |
12 | 12 |
|
13 | 13 | """An abstract class for optimization application classes.""" |
14 | | -from typing import Union, Dict |
15 | | -from collections import OrderedDict |
16 | 14 | from abc import ABC, abstractmethod |
| 15 | +from collections import OrderedDict |
| 16 | +from typing import Dict, Union |
17 | 17 |
|
18 | 18 | import numpy as np |
19 | | - |
20 | 19 | from qiskit.opflow import StateFn |
| 20 | +from qiskit.quantum_info import Statevector |
| 21 | +from qiskit.result import QuasiDistribution |
| 22 | + |
21 | 23 | from qiskit_optimization.algorithms import OptimizationResult |
22 | 24 | from qiskit_optimization.problems.quadratic_program import QuadraticProgram |
23 | 25 |
|
@@ -59,29 +61,54 @@ def _result_to_x(self, result: Union[OptimizationResult, np.ndarray]) -> np.ndar |
59 | 61 | return x |
60 | 62 |
|
61 | 63 | @staticmethod |
62 | | - def sample_most_likely(state_vector: Union[np.ndarray, Dict]) -> np.ndarray: |
| 64 | + def sample_most_likely( |
| 65 | + state_vector: Union[QuasiDistribution, Statevector, np.ndarray, Dict] |
| 66 | + ) -> np.ndarray: |
63 | 67 | """Compute the most likely binary string from state vector. |
64 | 68 |
|
65 | 69 | Args: |
66 | | - state_vector: state vector or counts. |
| 70 | + state_vector: state vector or counts or quasi-probabilities. |
67 | 71 |
|
68 | 72 | Returns: |
69 | 73 | binary string as numpy.ndarray of ints. |
| 74 | +
|
| 75 | + Raises: |
| 76 | + ValueError: if state_vector is not QuasiDistribution, Statevector, |
| 77 | + np.ndarray, or dict. |
70 | 78 | """ |
71 | | - if isinstance(state_vector, (OrderedDict, dict)): |
| 79 | + if isinstance(state_vector, QuasiDistribution): |
| 80 | + probabilities = state_vector.binary_probabilities() |
| 81 | + binary_string = max(probabilities.items(), key=lambda kv: kv[1])[0] |
| 82 | + x = np.asarray([int(y) for y in reversed(list(binary_string))]) |
| 83 | + return x |
| 84 | + elif isinstance(state_vector, Statevector): |
| 85 | + probabilities = state_vector.probabilities() |
| 86 | + n = state_vector.num_qubits |
| 87 | + k = np.argmax(np.abs(probabilities)) |
| 88 | + x = np.zeros(n) |
| 89 | + for i in range(n): |
| 90 | + x[i] = k % 2 |
| 91 | + k >>= 1 |
| 92 | + return x |
| 93 | + elif isinstance(state_vector, (OrderedDict, dict)): |
72 | 94 | # get the binary string with the largest count |
73 | | - binary_string = sorted(state_vector.items(), key=lambda kv: kv[1])[-1][0] |
| 95 | + binary_string = max(state_vector.items(), key=lambda kv: kv[1])[0] |
74 | 96 | x = np.asarray([int(y) for y in reversed(list(binary_string))]) |
75 | 97 | return x |
76 | 98 | elif isinstance(state_vector, StateFn): |
77 | 99 | binary_string = list(state_vector.sample().keys())[0] |
78 | 100 | x = np.asarray([int(y) for y in reversed(list(binary_string))]) |
79 | 101 | return x |
80 | | - else: |
| 102 | + elif isinstance(state_vector, np.ndarray): |
81 | 103 | n = int(np.log2(state_vector.shape[0])) |
82 | 104 | k = np.argmax(np.abs(state_vector)) |
83 | 105 | x = np.zeros(n) |
84 | 106 | for i in range(n): |
85 | 107 | x[i] = k % 2 |
86 | 108 | k >>= 1 |
87 | 109 | return x |
| 110 | + else: |
| 111 | + raise ValueError( |
| 112 | + "state vector should be QuasiDistribution, Statevector, ndarray, or dict. " |
| 113 | + f"But it is {type(state_vector)}." |
| 114 | + ) |
0 commit comments