From 9d430e68f45b36ea7170f67f32bc853e2050ee38 Mon Sep 17 00:00:00 2001 From: Bill Ticehurst Date: Fri, 25 Oct 2024 16:49:20 -0700 Subject: [PATCH] Fix py38 types --- pip/qsharp/_qsharp.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pip/qsharp/_qsharp.py b/pip/qsharp/_qsharp.py index 539e99f28f..87fe7c3b03 100644 --- a/pip/qsharp/_qsharp.py +++ b/pip/qsharp/_qsharp.py @@ -95,11 +95,13 @@ def _repr_mimebundle_( ) -> Dict[str, Dict[str, str]]: return {"application/x.qsharp-config": self._config} + class PauliNoise(Tuple[float, float, float]): """ The Pauli noise to use in simulation represented as probabilities of Pauli-X, Pauli-Y, and Pauli-Z errors """ + def __new__(cls, x: float, y: float, z: float): if x < 0 or y < 0 or z < 0: raise ValueError("Pauli noise probabilities must be non-negative.") @@ -107,27 +109,34 @@ def __new__(cls, x: float, y: float, z: float): raise ValueError("The sum of Pauli noise probabilities must be at most 1.") return super().__new__(cls, (x, y, z)) + class DepolarizingNoise(PauliNoise): """ The depolarizing noise to use in simulation. """ + def __new__(cls, p: float): return super().__new__(cls, p / 3, p / 3, p / 3) + class BitFlipNoise(PauliNoise): """ The bit flip noise to use in simulation. """ + def __new__(cls, p: float): return super().__new__(cls, p, 0, 0) + class PhaseFlipNoise(PauliNoise): """ The phase flip noise to use in simulation. """ + def __new__(cls, p: float): return super().__new__(cls, 0, 0, p) + def init( *, target_profile: TargetProfile = TargetProfile.Unrestricted, @@ -247,9 +256,15 @@ def run( *, on_result: Optional[Callable[[ShotResult], None]] = None, save_events: bool = False, - noise: Optional [ - Tuple[float, float, float] | PauliNoise | BitFlipNoise | PhaseFlipNoise | DepolarizingNoise - ] = None + noise: Optional[ + Union[ + Tuple[float, float, float], + PauliNoise, + BitFlipNoise, + PhaseFlipNoise, + DepolarizingNoise, + ] + ] = None, ) -> List[Any]: """ Runs the given Q# expression for the given number of shots.