Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 39 additions & 11 deletions src/bloqade/noise/native/stmts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple

from kirin import ir, types, lowering
from kirin.decl import info, statement
from kirin.dialects import ilist
Expand All @@ -7,25 +9,43 @@
from ._dialect import dialect


@statement(dialect=dialect)
class PauliChannel(ir.Statement):

@statement
class NativeNoiseStmt(ir.Statement):
traits = frozenset({lowering.FromPythonCall()})

@property
def probabilities(self) -> Tuple[Tuple[float, ...], ...]: ...

def check(self):
for probs in self.probabilities:
self.check_probability(sum(probs))
for p in probs:
self.check_probability(p)

Check warning on line 23 in src/bloqade/noise/native/stmts.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/noise/native/stmts.py#L20-L23

Added lines #L20 - L23 were not covered by tests

def check_probability(self, p: float):
if not 0 <= p <= 1:
raise ValueError(

Check warning on line 27 in src/bloqade/noise/native/stmts.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/noise/native/stmts.py#L26-L27

Added lines #L26 - L27 were not covered by tests
f"Invalid noise probability encountered in {type(self).__name__}: {p}"
)


@statement(dialect=dialect)
class PauliChannel(NativeNoiseStmt):
px: float = info.attribute(types.Float)
py: float = info.attribute(types.Float)
pz: float = info.attribute(types.Float)
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType])

@property
def probabilities(self) -> Tuple[Tuple[float, ...], ...]:
return ((self.px, self.py, self.pz),)

Check warning on line 41 in src/bloqade/noise/native/stmts.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/noise/native/stmts.py#L41

Added line #L41 was not covered by tests


NumQubits = types.TypeVar("NumQubits")


@statement(dialect=dialect)
class CZPauliChannel(ir.Statement):

traits = frozenset({lowering.FromPythonCall()})

class CZPauliChannel(NativeNoiseStmt):
paired: bool = info.attribute(types.Bool)
px_ctrl: float = info.attribute(types.Float)
py_ctrl: float = info.attribute(types.Float)
Expand All @@ -36,11 +56,19 @@
ctrls: ir.SSAValue = info.argument(ilist.IListType[QubitType, NumQubits])
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType, NumQubits])

@property
def probabilities(self) -> Tuple[Tuple[float, ...], ...]:
return (

Check warning on line 61 in src/bloqade/noise/native/stmts.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/noise/native/stmts.py#L61

Added line #L61 was not covered by tests
(self.px_ctrl, self.py_ctrl, self.pz_ctrl),
(self.px_qarg, self.py_qarg, self.pz_qarg),
)

@statement(dialect=dialect)
class AtomLossChannel(ir.Statement):

traits = frozenset({lowering.FromPythonCall()})

@statement(dialect=dialect)
class AtomLossChannel(NativeNoiseStmt):
prob: float = info.attribute(types.Float)
qargs: ir.SSAValue = info.argument(ilist.IListType[QubitType])

@property
def probabilities(self) -> Tuple[Tuple[float, ...], ...]:
return ((self.prob,),)

Check warning on line 74 in src/bloqade/noise/native/stmts.py

View check run for this annotation

Codecov / codecov/patch

src/bloqade/noise/native/stmts.py#L74

Added line #L74 was not covered by tests
19 changes: 19 additions & 0 deletions test/pyqrack/runtime/noise/native/test_pauli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import Mock, call

import pytest
from kirin import ir

from bloqade import qasm2
Expand Down Expand Up @@ -41,6 +42,23 @@ def test_atom_loss():
sim_reg.assert_has_calls([call.y(0)])


@pytest.mark.xfail
def test_pauli_probs_check():
@simulation
def test_atom_loss():
q = qasm2.qreg(2)
native.pauli_channel(
[q[0]],
px=0.1,
py=0.4,
pz=1.3,
)
return q

with pytest.raises(ir.ValidationError):
test_atom_loss.verify()


def test_cz_pauli_channel_false():
@simulation
def test_atom_loss():
Expand Down Expand Up @@ -126,5 +144,6 @@ def test_atom_loss():

if __name__ == "__main__":
test_pauli_channel()
test_pauli_probs_check()
test_cz_pauli_channel_false()
test_cz_pauli_channel_true()