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

Adds bit and phase flip channels #954

Merged
merged 10 commits into from
Dec 10, 2020
Merged
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
19 changes: 18 additions & 1 deletion .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@

Currently, the only the `diff_method="backprop"` is supported, with plans to add reverse mode support in the future.

* Two new error channels, `BitFlip` and `PhaseFlip` have been added.
[#954](https://github.com/PennyLaneAI/pennylane/pull/954)

They can be used in the same manner as existing error channels:

```python
dev = qml.device("default.mixed", wires=2)

@qml.qnode(dev)
def circuit():
qml.RX(0.3, wires=0)
qml.RY(0.5, wires=1)
qml.BitFlip(0.01, wires=0)
qml.PhaseFlip(0.01, wires=1)
return qml.expval(qml.PauliZ(0))
```

<h3>Improvements</h3>

* A new test series, pennylane/devices/tests/test_compare_default_qubit.py, has been added, allowing to test if
Expand All @@ -49,7 +66,7 @@

This release contains contributions from (in alphabetical order):

Josh Izaac, Alejandro Montanez
Olivia Di Matteo, Josh Izaac, Alejandro Montanez

# Release 0.13.0 (current release)

Expand Down
2 changes: 2 additions & 0 deletions doc/introduction/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Noisy channels
~pennylane.GeneralizedAmplitudeDamping
~pennylane.PhaseDamping
~pennylane.DepolarizingChannel
~pennylane.BitFlip
~pennylane.PhaseFlip
~pennylane.QubitChannel

:html:`</div>`
Expand Down
2 changes: 2 additions & 0 deletions pennylane/devices/default_mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class DefaultMixed(QubitDevice):
"GeneralizedAmplitudeDamping",
"PhaseDamping",
"DepolarizingChannel",
"BitFlip",
"PhaseFlip",
"QubitChannel",
}

Expand Down
86 changes: 86 additions & 0 deletions pennylane/ops/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,90 @@ def _kraus_matrices(cls, *params):
return [K0, K1, K2, K3]


class BitFlip(Channel):
r"""BitFlip(p, wires)
Single-qubit bit flip (Pauli :math:`X`) error channel.

This channel is modelled by the following Kraus matrices:

.. math::
K_0 = \sqrt{1-p} \begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix}

.. math::
K_1 = \sqrt{p}\begin{bmatrix}
0 & 1 \\
1 & 0
\end{bmatrix}

where :math:`p \in [0, 1]` is the probability of a bit flip (Pauli :math:`X` error).

**Details:**

* Number of wires: 1
* Number of parameters: 1

Args:
p (float): The probability that a bit flip error occurs.
wires (Sequence[int] or int): the wire the channel acts on
"""
num_params = 1
num_wires = 1
par_domain = "R"
grad_method = "F"

@classmethod
def _kraus_matrices(cls, *params):
p = params[0]
K0 = np.sqrt(1 - p) * np.eye(2)
K1 = np.sqrt(p) * np.array([[0, 1], [1, 0]])
return [K0, K1]


class PhaseFlip(Channel):
r"""PhaseFlip(p, wires)
Single-qubit bit flip (Pauli :math:`Z`) error channel.

This channel is modelled by the following Kraus matrices:

.. math::
K_0 = \sqrt{1-p} \begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix}

.. math::
K_1 = \sqrt{p}\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix}

where :math:`p \in [0, 1]` is the probability of a phase flip (Pauli :math:`Z`) error.

**Details:**

* Number of wires: 1
* Number of parameters: 1

Args:
p (float): The probability that a phase flip error occurs.
wires (Sequence[int] or int): the wire the channel acts on
"""
num_params = 1
num_wires = 1
par_domain = "R"
grad_method = "F"

@classmethod
def _kraus_matrices(cls, *params):
p = params[0]
K0 = np.sqrt(1 - p) * np.eye(2)
K1 = np.sqrt(p) * np.array([[1, 0], [0, -1]])
return [K0, K1]


class QubitChannel(Channel):
r"""QubitChannel(K_list, wires)
Apply an arbitrary fixed quantum channel.
Expand Down Expand Up @@ -282,6 +366,8 @@ def _kraus_matrices(cls, *params):
"GeneralizedAmplitudeDamping",
"PhaseDamping",
"DepolarizingChannel",
"BitFlip",
"PhaseFlip",
"QubitChannel",
}

Expand Down
33 changes: 33 additions & 0 deletions tests/ops/test_channel_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@

X = np.array([[0, 1], [1, 0]])
Y = np.array([[0, -1j], [1j, 0]])
Z = np.array([[1, 0], [0, -1]])

ch_list = [
channel.AmplitudeDamping,
channel.GeneralizedAmplitudeDamping,
channel.PhaseDamping,
channel.BitFlip,
channel.PhaseFlip,
channel.DepolarizingChannel,
]

Expand Down Expand Up @@ -120,6 +123,36 @@ def test_gamma_arbitrary(self, tol):
assert np.allclose(op(0.1, wires=0).kraus_matrices, expected, atol=tol, rtol=0)


class TestBitFlip:
"""Tests for the quantum channel BitFlipChannel"""

@pytest.mark.parametrize("p", [0, 0.1, 0.5, 1])
def test_p_arbitrary(self, p, tol):
"""Test that various values of p give correct Kraus matrices"""
op = channel.BitFlip

expected_K0 = np.sqrt(1 - p) * np.eye(2)
assert np.allclose(op(p, wires=0).kraus_matrices[0], expected_K0, atol=tol, rtol=0)

expected_K1 = np.sqrt(p) * X
assert np.allclose(op(p, wires=0).kraus_matrices[1], expected_K1, atol=tol, rtol=0)


class TestPhaseFlip:
"""Test that various values of p give correct Kraus matrices"""

@pytest.mark.parametrize("p", [0, 0.1, 0.5, 1])
def test_p_arbitrary(self, p, tol):
"""Test p=0.1 gives correct Kraus matrices"""
op = channel.PhaseFlip

expected_K0 = np.sqrt(1 - p) * np.eye(2)
assert np.allclose(op(p, wires=0).kraus_matrices[0], expected_K0, atol=tol, rtol=0)

expected_K1 = np.sqrt(p) * Z
assert np.allclose(op(p, wires=0).kraus_matrices[1], expected_K1, atol=tol, rtol=0)


class TestDepolarizingChannel:
"""Tests for the quantum channel DepolarizingChannel"""

Expand Down