1515from abc import ABC , abstractmethod
1616from dataclasses import dataclass
1717from enum import Enum
18- from typing import List , Union , Any , Optional , Dict , Type , Tuple , cast
18+ from typing import Any , Dict , List , Optional , Tuple , Type , Union , cast
1919from warnings import warn
2020
2121import numpy as np
22+ from qiskit .opflow import DictStateFn , StateFn
23+ from qiskit .quantum_info import Statevector
24+ from qiskit .result import QuasiDistribution
2225
23- from qiskit . opflow import StateFn , DictStateFn
26+ from .. converters . quadratic_program_to_qubo import QuadraticProgramConverter , QuadraticProgramToQubo
2427from ..exceptions import QiskitOptimizationError
25- from ..converters .quadratic_program_to_qubo import QuadraticProgramToQubo , QuadraticProgramConverter
2628from ..problems .quadratic_program import QuadraticProgram , Variable
2729
2830
@@ -518,7 +520,7 @@ def _interpret_samples(
518520
519521 @staticmethod
520522 def _eigenvector_to_solutions (
521- eigenvector : Union [dict , np .ndarray , StateFn ],
523+ eigenvector : Union [QuasiDistribution , Statevector , dict , np .ndarray , StateFn ],
522524 qubo : QuadraticProgram ,
523525 min_probability : float = 1e-6 ,
524526 ) -> List [SolutionSample ]:
@@ -566,7 +568,25 @@ def generate_solution(bitstr, qubo, probability):
566568 )
567569
568570 solutions = []
569- if isinstance (eigenvector , dict ):
571+ if isinstance (eigenvector , QuasiDistribution ):
572+ probabilities = eigenvector .binary_probabilities ()
573+ # iterate over all samples
574+ for bitstr , sampling_probability in probabilities .items ():
575+ # add the bitstring, if the sampling probability exceeds the threshold
576+ if sampling_probability >= min_probability :
577+ solutions .append (generate_solution (bitstr , qubo , sampling_probability ))
578+
579+ elif isinstance (eigenvector , Statevector ):
580+ probabilities = eigenvector .probabilities ()
581+ num_qubits = eigenvector .num_qubits
582+ # iterate over all states and their sampling probabilities
583+ for i , sampling_probability in enumerate (probabilities ):
584+ # add the i-th state if the sampling probability exceeds the threshold
585+ if sampling_probability >= min_probability :
586+ bitstr = f"{ i :b} " .rjust (num_qubits , "0" )
587+ solutions .append (generate_solution (bitstr , qubo , sampling_probability ))
588+
589+ elif isinstance (eigenvector , dict ):
570590 # When eigenvector is a dict, square the values since the values are normalized.
571591 # See https://github.com/Qiskit/qiskit-terra/pull/5496 for more details.
572592 probabilities = {bitstr : val ** 2 for (bitstr , val ) in eigenvector .items ()}
@@ -579,7 +599,6 @@ def generate_solution(bitstr, qubo, probability):
579599 elif isinstance (eigenvector , np .ndarray ):
580600 num_qubits = int (np .log2 (eigenvector .size ))
581601 probabilities = np .abs (eigenvector * eigenvector .conj ())
582-
583602 # iterate over all states and their sampling probabilities
584603 for i , sampling_probability in enumerate (probabilities ):
585604 # add the i-th state if the sampling probability exceeds the threshold
@@ -588,6 +607,8 @@ def generate_solution(bitstr, qubo, probability):
588607 solutions .append (generate_solution (bitstr , qubo , sampling_probability ))
589608
590609 else :
591- raise TypeError ("Unsupported format of eigenvector. Provide a dict or numpy.ndarray." )
592-
610+ raise TypeError (
611+ f"Eigenvector should be QuasiDistribution, Statevector, dict or numpy.ndarray. "
612+ f"But, it was { type (eigenvector )} ."
613+ )
593614 return solutions
0 commit comments