Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions source/pip/qsharp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@
# Licensed under the MIT License.

from . import telemetry_events
from ._native import (
CircuitGenerationMethod,
Pauli,
QSharpError,
Result,
TargetProfile,
estimate_custom,
)
from ._noise import BitFlipNoise, DepolarizingNoise, PauliNoise, PhaseFlipNoise
from ._qsharp import (
init,
eval,
run,
compile,
circuit,
compile,
dump_circuit,
dump_machine,
estimate,
eval,
init,
logical_counts,
set_quantum_seed,
run,
set_classical_seed,
dump_machine,
dump_circuit,
StateDump,
ShotResult,
PauliNoise,
DepolarizingNoise,
BitFlipNoise,
PhaseFlipNoise,
CircuitGenerationMethod,
set_quantum_seed,
)
from ._session import Session
from ._types import ShotResult, StateDump

telemetry_events.on_import()

from ._native import Result, Pauli, QSharpError, TargetProfile, estimate_custom

# IPython notebook specific features
try:
Expand Down Expand Up @@ -61,4 +64,5 @@
"BitFlipNoise",
"PhaseFlipNoise",
"CircuitGenerationMethod",
"Session",
]
2 changes: 1 addition & 1 deletion source/pip/qsharp/_device/_atom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .._device import Device, Zone, ZoneType
from ..._simulation import NoiseConfig, run_qir_clifford, run_qir_cpu, run_qir_gpu
from ..._native import try_create_gpu_adapter
from ..._qsharp import QirInputData
from ..._types import QirInputData
from ... import telemetry_events

from typing import List, Literal, Optional
Expand Down
2 changes: 1 addition & 1 deletion source/pip/qsharp/_device/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the MIT License.

from enum import Enum
from .._qsharp import QirInputData
from .._types import QirInputData


class ZoneType(Enum):
Expand Down
7 changes: 4 additions & 3 deletions source/pip/qsharp/_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from IPython.display import display, clear_output
from IPython.core.magic import register_cell_magic
from ._native import QSharpError
from ._qsharp import get_interpreter, qsharp_value_to_python_value
from ._qsharp import _get_default_session
from . import telemetry_events


Expand All @@ -35,8 +35,9 @@ def callback(output):
start_time = monotonic()

try:
results = qsharp_value_to_python_value(
get_interpreter().interpret(cell, callback)
session = _get_default_session()
results = session._qsharp_value_to_python_value(
session._interpreter.interpret(cell, callback)
)

durationMs = (monotonic() - start_time) * 1000
Expand Down
94 changes: 94 additions & 0 deletions source/pip/qsharp/_noise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Noise Models for Quantum Simulation"""

from typing import Tuple


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):
"""
Creates a new :class:`PauliNoise` instance with the given error probabilities.

:param x: Probability of a Pauli-X (bit flip) error. Must be non-negative.
:type x: float
:param y: Probability of a Pauli-Y error. Must be non-negative.
:type y: float
:param z: Probability of a Pauli-Z (phase flip) error. Must be non-negative.
:type z: float
:return: A new :class:`PauliNoise` tuple ``(x, y, z)``.
:rtype: PauliNoise
:raises ValueError: If any probability is negative or if ``x + y + z > 1``.
"""
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):
"""
Creates a new :class:`DepolarizingNoise` instance.

The depolarizing channel applies Pauli-X, Pauli-Y, or Pauli-Z errors each with
probability ``p / 3``.

:param p: Total depolarizing error probability. Must satisfy ``0 ≤ p ≤ 1``.
:type p: float
:return: A new :class:`DepolarizingNoise` with equal X, Y, and Z error probabilities.
:rtype: DepolarizingNoise
:raises ValueError: If ``p`` is negative or ``p > 1``.
"""
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):
"""
Creates a new :class:`BitFlipNoise` instance.

The bit flip channel applies a Pauli-X error with probability ``p``.

:param p: Probability of a bit flip (Pauli-X) error. Must satisfy ``0 ≤ p ≤ 1``.
:type p: float
:return: A new :class:`BitFlipNoise` with X error probability ``p``.
:rtype: BitFlipNoise
:raises ValueError: If ``p`` is negative or ``p > 1``.
"""
return super().__new__(cls, p, 0, 0)


class PhaseFlipNoise(PauliNoise):
"""
The phase flip noise to use in simulation.
"""

def __new__(cls, p: float):
"""
Creates a new :class:`PhaseFlipNoise` instance.

The phase flip channel applies a Pauli-Z error with probability ``p``.

:param p: Probability of a phase flip (Pauli-Z) error. Must satisfy ``0 ≤ p ≤ 1``.
:type p: float
:return: A new :class:`PhaseFlipNoise` with Z error probability ``p``.
:rtype: PhaseFlipNoise
:raises ValueError: If ``p`` is negative or ``p > 1``.
"""
return super().__new__(cls, 0, 0, p)
Loading
Loading