Skip to content

Commit

Permalink
Fix py38 types (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
billti authored Oct 25, 2024
1 parent 468d811 commit 7ff323a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pip/qsharp/_qsharp.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,39 +95,48 @@ 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.")
if x + y + z > 1:
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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7ff323a

Please sign in to comment.