Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom boolean functions for supporting noise models #5674

Merged
merged 74 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
202c365
init
obliviateandsurrender Apr 26, 2024
4c03e36
init
obliviateandsurrender May 4, 2024
5ff822f
basic structure
obliviateandsurrender May 9, 2024
d49a88b
more versatile support
obliviateandsurrender May 9, 2024
2b8b65d
fix formatting
obliviateandsurrender May 9, 2024
72d25d0
Merge branch 'master' into noise-models-1
obliviateandsurrender May 9, 2024
1892a42
fix `pylint`?
obliviateandsurrender May 9, 2024
3e301a2
more fixes
obliviateandsurrender May 9, 2024
4b6c6b4
format tweaks
obliviateandsurrender May 9, 2024
1317189
add `partial_wires`
obliviateandsurrender May 10, 2024
5332c61
add tests
obliviateandsurrender May 10, 2024
edcb7a4
add repr test
obliviateandsurrender May 10, 2024
59d09de
happy `isort`
obliviateandsurrender May 10, 2024
edb464d
happy `isort`
obliviateandsurrender May 10, 2024
f7465d2
add test
obliviateandsurrender May 10, 2024
09936a6
happy `black`
obliviateandsurrender May 10, 2024
ec1f1c2
happy `isort`
obliviateandsurrender May 10, 2024
d4bfbaf
improve coverage
obliviateandsurrender May 10, 2024
07587a7
improve coverage and simplify bits
obliviateandsurrender May 10, 2024
3d5fe94
Merge branch 'master' into noise-models-1
obliviateandsurrender May 11, 2024
f30501f
happy ci for a while
obliviateandsurrender May 11, 2024
064c6a0
fix test
obliviateandsurrender May 12, 2024
087a007
add docstrings
obliviateandsurrender May 13, 2024
65e158f
add docstrings
obliviateandsurrender May 13, 2024
d806fa3
show in docs
obliviateandsurrender May 13, 2024
72a47e2
tweak
obliviateandsurrender May 13, 2024
75ee072
doc tweak
obliviateandsurrender May 13, 2024
7d92269
doc tweak
obliviateandsurrender May 13, 2024
299a030
Apply suggestions from code review
obliviateandsurrender May 13, 2024
e5d481e
Merge branch 'noise-models-1' of https://github.com/PennyLaneAI/penny…
obliviateandsurrender May 13, 2024
4f27312
add support for arithmetic depth
obliviateandsurrender May 14, 2024
2797a53
make lc support robust
obliviateandsurrender May 14, 2024
7fc2dd2
fix coverage
obliviateandsurrender May 14, 2024
d1192b4
simplify doubts
obliviateandsurrender May 14, 2024
823daad
restructure
obliviateandsurrender May 16, 2024
8a2760d
Merge branch 'master' into noise-models-1
obliviateandsurrender May 16, 2024
6cdfda8
add `changelog`
obliviateandsurrender May 16, 2024
0adc86f
add dunder to bitwise
obliviateandsurrender May 17, 2024
f84811e
Merge branch 'master' into noise-models-1
obliviateandsurrender May 17, 2024
ca5adb8
improve coverage
obliviateandsurrender May 17, 2024
4da09b0
Merge branch 'noise-models-1' of https://github.com/PennyLaneAI/penny…
obliviateandsurrender May 17, 2024
19c6fff
add doc page
obliviateandsurrender May 17, 2024
9dcd7d0
add docs
obliviateandsurrender May 17, 2024
e2e7a31
Merge branch 'master' into noise-models-1
Jaybsoni May 22, 2024
6caae2a
apply suggestions
obliviateandsurrender May 23, 2024
caf9def
Merge branch 'noise-models-1' of https://github.com/PennyLaneAI/penny…
obliviateandsurrender May 23, 2024
dd45dcd
Merge branch 'master' into noise-models-1
obliviateandsurrender May 23, 2024
7e6c332
improve LC comparison
obliviateandsurrender May 26, 2024
d3c5fc6
improve docs
obliviateandsurrender May 27, 2024
0979d6d
add exp check
obliviateandsurrender May 27, 2024
6a565e9
tweak fixes
obliviateandsurrender May 27, 2024
4464bf1
tweak docs
obliviateandsurrender May 28, 2024
8fb77a2
tweak docs
obliviateandsurrender May 28, 2024
eb54e04
docs tweaks
obliviateandsurrender May 29, 2024
2f40fa3
improve docs
obliviateandsurrender May 29, 2024
8922964
little tweaks
obliviateandsurrender May 29, 2024
b08b836
Merge branch 'master' into noise-models-1
Jaybsoni May 29, 2024
c7ca37b
add suggestions
obliviateandsurrender May 29, 2024
4a9e787
Merge branch 'noise-models-1' of https://github.com/PennyLaneAI/penny…
obliviateandsurrender May 29, 2024
85b63d1
add suggestions
obliviateandsurrender May 29, 2024
fe011f3
improve docs
obliviateandsurrender May 30, 2024
55ab757
Merge branch 'master' into noise-models-1
obliviateandsurrender May 30, 2024
72a28fe
tweak docs
obliviateandsurrender May 30, 2024
8e5cddf
Merge branch 'master' into noise-models-1
obliviateandsurrender May 31, 2024
636e2d0
Merge branch 'master' into noise-models-1
obliviateandsurrender May 31, 2024
420b845
Merge branch 'master' into noise-models-1
obliviateandsurrender Jun 4, 2024
1eb5d12
Merge branch 'master' of https://github.com/PennyLaneAI/pennylane int…
obliviateandsurrender Jun 6, 2024
c793e39
adapt for controlled
obliviateandsurrender Jun 6, 2024
7088b9c
minor tweak
obliviateandsurrender Jun 7, 2024
accbeda
Merge branch 'master' into noise-models-1
obliviateandsurrender Jun 10, 2024
da1ffb3
add suggestions to docs
obliviateandsurrender Jun 11, 2024
84e3738
minor fixups
obliviateandsurrender Jun 11, 2024
e34f9ad
hopeful final suggestion
obliviateandsurrender Jun 11, 2024
c93e483
Merge branch 'master' into noise-models-1
obliviateandsurrender Jun 11, 2024
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
14 changes: 9 additions & 5 deletions pennylane/boolean_fn.py
trbromley marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import functools


# pylint: disable=unnecessary-lambda
class BooleanFn:
r"""Wrapper for simple callables with boolean output that can be
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
manipulated and combined with bit-wise operators.
Expand Down Expand Up @@ -82,13 +83,16 @@
functools.update_wrapper(self, fn)

def __and__(self, other):
return BooleanFn(lambda obj: self.fn(obj) and other.fn(obj))
return BooleanFn(lambda *args, **kwargs: self(*args, **kwargs) and other(*args, **kwargs))
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved

def __or__(self, other):
return BooleanFn(lambda obj: self.fn(obj) or other.fn(obj))
return BooleanFn(lambda *args, **kwargs: self(*args, **kwargs) or other(*args, **kwargs))

def __invert__(self):
return BooleanFn(lambda obj: not self.fn(obj))
return BooleanFn(lambda *args, **kwargs: not self(*args, **kwargs))

def __call__(self, obj):
return self.fn(obj)
def __call__(self, *args, **kwargs):
return self.fn(*args, **kwargs)

def __repr__(self):
return f"BooleanFn({self.fn.__name__})"

Check warning on line 98 in pennylane/boolean_fn.py

View check run for this annotation

Codecov / codecov/patch

pennylane/boolean_fn.py#L98

Added line #L98 was not covered by tests
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions pennylane/noise/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""

Check warning on line 14 in pennylane/noise/__init__.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/__init__.py#L14

Added line #L14 was not covered by tests
This module contains the functionality to work with noise in PennyLane.
"""

from conditionals import NoiseConditional, AndConditional, OrConditional
from conditionals import wire_in, wire_eq, op_in, op_eq

Check warning on line 19 in pennylane/noise/__init__.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/__init__.py#L18-L19

Added lines #L18 - L19 were not covered by tests
119 changes: 119 additions & 0 deletions pennylane/noise/conditionals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Copyright 2018-2024 Xanadu Quantum Technologies Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contains utility functions for building boolean conditionals for noise models"""

Check warning on line 14 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L14

Added line #L14 was not covered by tests

from inspect import isclass

Check warning on line 16 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L16

Added line #L16 was not covered by tests

import pennylane as qml
from pennylane.boolean_fn import BooleanFn

Check warning on line 19 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L18-L19

Added lines #L18 - L19 were not covered by tests

# pylint: disable = unnecessary-lambda, too-few-public-methods


class NoiseConditional(BooleanFn):

Check warning on line 24 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L24

Added line #L24 was not covered by tests
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
"""Defines a BooleanFn for implementing noise"""

def __init__(self, fn, repr):
super().__init__(fn)
self.repr = repr

Check warning on line 29 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L27-L29

Added lines #L27 - L29 were not covered by tests

def __and__(self, other):
return AndConditional(self, other)

Check warning on line 32 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L31-L32

Added lines #L31 - L32 were not covered by tests

def __or__(self, other):
return OrConditional(self, other)

Check warning on line 35 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L34-L35

Added lines #L34 - L35 were not covered by tests

def __repr__(self):
return self.repr

Check warning on line 38 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L37-L38

Added lines #L37 - L38 were not covered by tests


# pylint: disable = bad-super-call
class AndConditional(NoiseConditional):

Check warning on line 42 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L42

Added line #L42 was not covered by tests
"""Defines a BooleanFn for implementing AND combination of noise fns"""

def __init__(self, left, right):
self.l_op = left
self.r_op = right
self.func = super(NoiseConditional, left).__and__(right)
super(NoiseConditional, self).__init__(self.func)
self.repr = f"And({left.repr}, {right.repr})"

Check warning on line 50 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L45-L50

Added lines #L45 - L50 were not covered by tests

def __str__(self):
return f"And({self.l_op}, {self.r_op})"

Check warning on line 53 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L52-L53

Added lines #L52 - L53 were not covered by tests


# pylint: disable = bad-super-call
class OrConditional(NoiseConditional):

Check warning on line 57 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L57

Added line #L57 was not covered by tests
"""Defines a BooleanFn for implementing OR combination of noise fns"""

def __init__(self, left, right):
self.l_op = left
self.r_op = right
self.func = super(NoiseConditional, left).__or__(right)
super(NoiseConditional, self).__init__(self.func)
self.repr = f"Or({left.repr}, {right.repr})"

Check warning on line 65 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L60-L65

Added lines #L60 - L65 were not covered by tests

def __str__(self):
return f"Or({self.l_op}, {self.r_op})"

Check warning on line 68 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L67-L68

Added lines #L67 - L68 were not covered by tests


def _get_wires(val):

Check warning on line 71 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L71

Added line #L71 was not covered by tests
"""Obtain wires as a set from a Wire, Iterable or Operation"""
iters = val if isinstance(val, (list, tuple, set)) else getattr(val, "wires", [val])
try:
wires = [[w] if isinstance(w, (int, str)) else getattr(w, "wires").tolist() for w in iters]
except TypeError:
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f"Wires cannot be computed for {val}") from None
return set(w for wire in wires for w in wire)

Check warning on line 78 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L73-L78

Added lines #L73 - L78 were not covered by tests


def wire_in(wires):

Check warning on line 81 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L81

Added line #L81 was not covered by tests
"""BooleanFn for checking if a wire exist in a set of specified wires"""
return NoiseConditional(

Check warning on line 83 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L83

Added line #L83 was not covered by tests
lambda x: _get_wires(x).issubset(_get_wires(wires)), f"WiresIn({wires})"
)


def wire_eq(wires):

Check warning on line 88 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L88

Added line #L88 was not covered by tests
"""BooleanFn for checking if a wire is equal to the set of specified wires"""
return NoiseConditional(

Check warning on line 90 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L90

Added line #L90 was not covered by tests
lambda x: _get_wires(x).issubset(_get_wires(wires)), f"WiresEq({wires})"
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
)


def _get_ops(val):

Check warning on line 95 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L95

Added line #L95 was not covered by tests
"""Help deal with arithmetic ops"""
vals = val if isinstance(val, (list, tuple, set, qml.wires.Wires)) else [val]
obliviateandsurrender marked this conversation as resolved.
Show resolved Hide resolved
return tuple(

Check warning on line 98 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L97-L98

Added lines #L97 - L98 were not covered by tests
(
getattr(qml.ops, val)
if isinstance(val, str)
else (val if isclass(val) else getattr(val, "__class__"))
)
for val in vals
)


def op_eq(ops):

Check warning on line 108 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L108

Added line #L108 was not covered by tests
"""BooleanFn for checking if an op exist in a set of specified ops"""
op_cls = _get_ops(ops)
op_repr = tuple(getattr(op, "__name__") for op in op_cls)[0]
return NoiseConditional(lambda x: isinstance(x, op_cls), f"OpEq({op_repr})")

Check warning on line 112 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L110-L112

Added lines #L110 - L112 were not covered by tests


def op_in(ops):

Check warning on line 115 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L115

Added line #L115 was not covered by tests
"""BooleanFn for checking if an op is equal to the set of specified ops"""
op_cls = _get_ops(ops)
op_repr = tuple(getattr(op, "__name__") for op in op_cls)
return NoiseConditional(lambda x: isinstance(x, op_cls), f"OpIn{op_repr}")

Check warning on line 119 in pennylane/noise/conditionals.py

View check run for this annotation

Codecov / codecov/patch

pennylane/noise/conditionals.py#L117-L119

Added lines #L117 - L119 were not covered by tests
Loading