1414from __future__ import annotations
1515
1616import copy
17- from typing import Tuple , Dict , Set , List
17+ from typing import Dict , List , Set , Tuple
1818
19- from qiskit import QuantumCircuit , ClassicalRegister
20- from qiskit .quantum_info import Statevector
19+ from qiskit import ClassicalRegister , QuantumCircuit
2120from qiskit .circuit import Qubit
2221from qiskit .circuit .library import grover_operator
23- from qiskit .primitives import BaseSamplerV2 , StatevectorSampler # change: BaseSampler and Sampler are replaced by BaseSamplerV2 and StatevectorSampler
24- from qiskit .transpiler .passmanager import BasePassManager
22+ from qiskit .primitives import (
23+ BaseSamplerV2 ,
24+ StatevectorSampler ,
25+ )
26+ from qiskit .quantum_info import Statevector
2527from qiskit .result import QuasiDistribution
28+ from qiskit .transpiler .passmanager import BasePassManager
2629
27- from ...utils .deprecation import issue_deprecation_msg
2830
2931class QBayesian :
3032 r"""
@@ -66,7 +68,7 @@ def __init__(
6668 * ,
6769 limit : int = 10 ,
6870 threshold : float = 0.9 ,
69- sampler : BaseSamplerV2 | None = None , # change: BaseSampler is replaced by BaseSamplerV2
71+ sampler : BaseSamplerV2 | None = None ,
7072 pass_manager : BasePassManager | None = None ,
7173 ):
7274 """
@@ -95,15 +97,7 @@ def __init__(
9597 self ._limit = limit
9698 self ._threshold = threshold
9799 if sampler is None :
98- sampler = StatevectorSampler () # change: Sampler is replaced by StatevectorSampler
99-
100- if isinstance (sampler , BaseSamplerV2 ): # change: BaseSamplerV1 is replaced by BaseSamplerV2
101- issue_deprecation_msg (
102- msg = "V1 Primitives are deprecated" ,
103- version = "0.8.0" ,
104- remedy = "Use V2 primitives for continued compatibility and support." ,
105- period = "4 months" ,
106- )
100+ sampler = StatevectorSampler ()
107101
108102 self ._sampler = sampler
109103
@@ -166,34 +160,22 @@ def _run_circuit(self, circuit: QuantumCircuit) -> Dict[str, float]:
166160 """Run the quantum circuit with the sampler."""
167161 counts = {}
168162
169- if isinstance (self ._sampler , BaseSamplerV2 ): # change: BaseSampler is replaced by BaseSamplerV2
170- # Sample from circuit
171- job = self ._sampler .run (circuit )
172- result = job .result ()
163+ # Sample from circuit
164+ if self ._pass_manager is not None :
165+ circuit = self ._pass_manager .run (circuit )
166+ job = self ._sampler .run ([circuit ])
167+ result = job .result ()
173168
174- # Get the counts of quantum state results
175- counts = result . quasi_dists [ 0 ]. nearest_probability_distribution (). binary_probabilities ()
169+ bit_array = list ( result [ 0 ]. data . values ())[ 0 ]
170+ bitstring_counts = bit_array . get_counts ()
176171
177- elif isinstance (self ._sampler , BaseSamplerV2 ): # change: BaseSamplerV2 is replaced by BaseSamplerV2
178- # Sample from circuit
179- if self ._pass_manager is not None :
180- circuit = self ._pass_manager .run (circuit )
181- job = self ._sampler .run ([circuit ])
182- result = job .result ()
183-
184- bit_array = list (result [0 ].data .values ())[0 ]
185- bitstring_counts = bit_array .get_counts ()
186-
187- # Normalize the counts to probabilities
188- total_shots = sum (bitstring_counts .values ())
189- probabilities = {k : v / total_shots for k , v in bitstring_counts .items ()}
190- # Convert to quasi-probabilities
191- quasi_dist = QuasiDistribution (probabilities )
192- binary_prob = quasi_dist .nearest_probability_distribution ().binary_probabilities ()
193- counts = {k : v for k , v in binary_prob .items () if int (k ) < 2 ** self .num_virtual_qubits }
194-
195- # counts = QuasiDistribution(probabilities)
196- # counts = {k: v for k, v in counts.items()}
172+ # Normalize the counts to probabilities
173+ total_shots = sum (bitstring_counts .values ())
174+ probabilities = {k : v / total_shots for k , v in bitstring_counts .items ()}
175+ # Convert to quasi-probabilities
176+ quasi_dist = QuasiDistribution (probabilities )
177+ binary_prob = quasi_dist .nearest_probability_distribution ().binary_probabilities ()
178+ counts = {k : v for k , v in binary_prob .items () if int (k ) < 2 ** self .num_virtual_qubits }
197179
198180 return counts
199181
@@ -411,12 +393,12 @@ def limit(self, limit: int):
411393 self ._limit = limit
412394
413395 @property
414- def sampler (self ) -> BaseSamplerV2 : # change: BaseSampler is replaced by BaseSamplerV2
396+ def sampler (self ) -> BaseSamplerV2 :
415397 """Returns the sampler primitive used to compute the samples."""
416398 return self ._sampler
417399
418400 @sampler .setter
419- def sampler (self , sampler : BaseSamplerV2 ): # change: BaseSampler is replaced by BaseSamplerV2
401+ def sampler (self , sampler : BaseSamplerV2 ):
420402 """Set the sampler primitive used to compute the samples."""
421403 self ._sampler = sampler
422404
@@ -428,4 +410,4 @@ def threshold(self) -> float:
428410 @threshold .setter
429411 def threshold (self , threshold : float ):
430412 """Set the threshold to accept the evidence."""
431- self ._threshold = threshold
413+ self ._threshold = threshold
0 commit comments