Skip to content

Commit

Permalink
change from default noise tuple to None
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles MOUSSA committed Dec 16, 2024
1 parent 37b2a9a commit 0936e16
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 26 deletions.
5 changes: 3 additions & 2 deletions horqrux/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def apply_operator_with_noise(
if not is_density
else apply_operator_dm(state, operator, target, control)
)
if len(noise) == 0:
if noise is None:
return state_gate
else:
kraus_ops = jnp.stack(tuple(reduce(add, tuple(n.kraus for n in noise))))
Expand Down Expand Up @@ -243,7 +243,8 @@ def apply_gate(
operator, target, control = merge_operators(operator, target, control)
noise = [g.noise for g in gate]

has_noise = len(reduce(add, noise)) > 0
# faster way to check has_noise
has_noise = noise != [None] * len(noise)
if has_noise and not is_density:
state = density_mat(state)
is_density = True
Expand Down
7 changes: 3 additions & 4 deletions horqrux/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from jax.tree_util import register_pytree_node_class

from .utils import (
ErrorProbabilities,
StrEnum,
)
from .utils_noise import (
Expand Down Expand Up @@ -46,14 +45,14 @@ class NoiseType(StrEnum):
@dataclass
class NoiseInstance:
type: NoiseType
error_probability: ErrorProbabilities
error_probability: tuple[float, ...] | float

def __iter__(self) -> Iterable:
return iter((self.kraus, self.error_probability))

def tree_flatten(
self,
) -> tuple[tuple, tuple[NoiseType, ErrorProbabilities]]:
) -> tuple[tuple, tuple[NoiseType, tuple[float, ...] | float]]:
children = ()
aux_data = (self.type, self.error_probability)
return (children, aux_data)
Expand All @@ -71,4 +70,4 @@ def __repr__(self) -> str:
return self.type + f"(p={self.error_probability})"


NoiseProtocol = tuple[NoiseInstance, ...]
NoiseProtocol = tuple[NoiseInstance, ...] | None
12 changes: 6 additions & 6 deletions horqrux/parametric.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass, field
from dataclasses import dataclass
from typing import Any, Iterable

import jax.numpy as jnp
Expand Down Expand Up @@ -31,7 +31,7 @@ class Parametric(Primitive):
generator_name: str
target: QubitSupport
control: QubitSupport
noise: NoiseProtocol = field(default_factory=tuple)
noise: NoiseProtocol = None
param: str | float = ""

def __post_init__(self) -> None:
Expand Down Expand Up @@ -84,7 +84,7 @@ def RX(
param: float | str,
target: TargetQubits,
control: ControlQubits = (None,),
noise: NoiseProtocol = tuple(),
noise: NoiseProtocol = None,
) -> Parametric:
"""RX gate.
Expand All @@ -104,7 +104,7 @@ def RY(
param: float | str,
target: TargetQubits,
control: ControlQubits = (None,),
noise: NoiseProtocol = tuple(),
noise: NoiseProtocol = None,
) -> Parametric:
"""RY gate.
Expand All @@ -124,7 +124,7 @@ def RZ(
param: float | str,
target: TargetQubits,
control: ControlQubits = (None,),
noise: NoiseProtocol = tuple(),
noise: NoiseProtocol = None,
) -> Parametric:
"""RZ gate.
Expand Down Expand Up @@ -161,7 +161,7 @@ def PHASE(
param: float,
target: TargetQubits,
control: ControlQubits = (None,),
noise: NoiseProtocol = tuple(),
noise: NoiseProtocol = None,
) -> Parametric:
"""Phase gate.
Expand Down
20 changes: 10 additions & 10 deletions horqrux/primitive.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass, field
from dataclasses import dataclass
from typing import Any, Iterable, Union

import numpy as np
Expand Down Expand Up @@ -28,7 +28,7 @@ class Primitive:
generator_name: str
target: QubitSupport
control: QubitSupport
noise: NoiseProtocol = field(default_factory=tuple)
noise: NoiseProtocol | None = None

@staticmethod
def parse_idx(
Expand Down Expand Up @@ -85,7 +85,7 @@ def __repr__(self) -> str:


def I(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""Identity / I gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -104,7 +104,7 @@ def I(


def X(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""X gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -127,7 +127,7 @@ def X(


def Y(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""Y gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -147,7 +147,7 @@ def Y(


def Z(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""Z gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -167,7 +167,7 @@ def Z(


def H(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""H/ Hadamard gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -186,7 +186,7 @@ def H(


def S(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""S gate or constant phase gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -205,7 +205,7 @@ def S(


def T(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""T gate. This function returns an instance of 'Primitive' and does *not* apply the gate.
By providing tuple of ints to 'control', it turns into a controlled gate.
Expand All @@ -227,7 +227,7 @@ def T(


def SWAP(
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = tuple()
target: TargetQubits, control: ControlQubits = (None,), noise: NoiseProtocol = None
) -> Primitive:
"""SWAP gate. By providing a control, it turns into a controlled gate (Fredkin gate),
use None for no control qubits.
Expand Down
2 changes: 0 additions & 2 deletions horqrux/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
QubitSupport = tuple[Any, ...]
ControlQubits = tuple[Union[None, tuple[int, ...]], ...]
TargetQubits = tuple[tuple[int, ...], ...]
ErrorProbabilities = Union[tuple[float, ...], float]

ATOL = 1e-014


Expand Down
4 changes: 2 additions & 2 deletions horqrux/utils_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def PauliChannel(error_probability: tuple[float, ...]) -> tuple[Array, ...]:
+ pz Z \\rho Z^{\\dagger}
Args:
error_probability (ErrorProbabilities): tuple containing probabilities
error_probability (tuple[float, ...] | float): tuple containing probabilities
of X, Y, and Z errors.
Raises:
Expand Down Expand Up @@ -224,7 +224,7 @@ def GeneralizedAmplitudeDamping(error_probability: tuple[float, ...]) -> tuple[A
K3 = sqrt(1-p) * [[0, 0], [sqrt(rate), 0]]
Args:
error_probability (ErrorProbabilities): The first float must be the probability
error_probability (tuple[float, ...] | float): The first float must be the probability
of amplitude damping error, and the second float is the damping rate, indicating
the probability of generalized amplitude damping.
Expand Down

0 comments on commit 0936e16

Please sign in to comment.