Skip to content

Commit

Permalink
Reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Bottrill committed May 7, 2024
1 parent 85fc4b5 commit 998d21d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
97 changes: 49 additions & 48 deletions pennylane/ops/qutrit/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class QutritDepolarizingChannel(Channel):
wires (Sequence[int] or int): the wire the channel acts on
id (str or None): String representing the operation (optional)
"""

num_params = 1
num_wires = 1
grad_method = "F"
Expand All @@ -144,47 +145,47 @@ def __init__(self, p, wires, id=None):
@staticmethod
def compute_kraus_matrices(p): # pylint:disable=arguments-differ
r"""Kraus matrices representing the depolarizing channel.
Args:
p (float): each gate is applied with probability :math:`\frac{p}{9}`
Returns:
list (array): list of Kraus matrices
**Example**
>>> np.round(qml.QutritDepolarizingChannel.compute_kraus_matrices(0.5), 3)
array([[[ 0.745+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , 0.745+0.j , 0. +0.j ],
[ 0. +0.j , 0. +0.j , 0.745+0.j ]],
[[ 0. +0.j , 0.236+0.j , 0. +0.j ],
[ 0. +0.j , 0. +0.j , 0.236+0.j ],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , 0.236+0.j ],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , 0.236+0.j , 0. +0.j ]],
[[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118+0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118-0.204j]],
[[ 0. +0.j , -0.118+0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118-0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , -0.118-0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118+0.204j, 0. +0.j ]],
[[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118-0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118+0.204j]],
[[ 0. +0.j , -0.118-0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118+0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , -0.118+0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118-0.204j, 0. +0.j ]]])
Args:
p (float): each gate is applied with probability :math:`\frac{p}{9}`
Returns:
list (array): list of Kraus matrices
**Example**
>>> np.round(qml.QutritDepolarizingChannel.compute_kraus_matrices(0.5), 3)
array([[[ 0.745+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , 0.745+0.j , 0. +0.j ],
[ 0. +0.j , 0. +0.j , 0.745+0.j ]],
[[ 0. +0.j , 0.236+0.j , 0. +0.j ],
[ 0. +0.j , 0. +0.j , 0.236+0.j ],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , 0.236+0.j ],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , 0.236+0.j , 0. +0.j ]],
[[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118+0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118-0.204j]],
[[ 0. +0.j , -0.118+0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118-0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , -0.118-0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118+0.204j, 0. +0.j ]],
[[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118-0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118+0.204j]],
[[ 0. +0.j , -0.118-0.204j, 0. +0.j ],
[ 0. +0.j , 0. +0.j , -0.118+0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ]],
[[ 0. +0.j , 0. +0.j , -0.118+0.204j],
[ 0.236+0.j , 0. +0.j , 0. +0.j ],
[ 0. +0.j , -0.118-0.204j, 0. +0.j ]]])
"""
if not math.is_abstract(p) and not 0.0 <= p <= 1.0:
raise ValueError("p must be in the interval [0,1]")
Expand All @@ -202,17 +203,17 @@ def compute_kraus_matrices(p): # pylint:disable=arguments-differ
depolarizing_mats = [
[[z, one, z], [z, z, one], [one, z, z]],
[[z, z, one], [one, z, z], [z, one, z]],

[[one, z, z], [z, w, z], [z, z, w ** 2]],
[[one, z, z], [z, w, z], [z, z, w**2]],
[[z, w, 0j], [z, z, w**2], [one, z, z]],
[[z, z, w**2], [one, z, z], [z, w, z]],

[[one, z, z], [z, w ** 2, z], [z, z, w ** 4]],
[[z, w ** 2, z], [z, z, w ** 4], [one, z, z]],
[[z, z, w**4], [one, z, z], [z, w**2, z]]
[[one, z, z], [z, w**2, z], [z, z, w**4]],
[[z, w**2, z], [z, z, w**4], [one, z, z]],
[[z, z, w**4], [one, z, z], [z, w**2, z]],
]
normalization = math.sqrt(p / 9 + math.eps)
Ks = [normalization * math.array(m, like=interface) for m in depolarizing_mats]
identity = math.sqrt(1 - (8 * p / 9) + math.eps) * math.array(math.eye(QUDIT_DIM, dtype=complex), like=interface)
identity = math.sqrt(1 - (8 * p / 9) + math.eps) * math.array(
math.eye(QUDIT_DIM, dtype=complex), like=interface
)

return [identity] + Ks
19 changes: 12 additions & 7 deletions tests/ops/qutrit/test_qutrit_channel_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def get_expected_kraus_matrices(p):
w = np.exp(2j * np.pi / 3)

Z0 = np.eye(QUDIT_DIM)
Z1 = np.diag([1, w, w ** 2])
Z2 = np.diag([1, w ** 2, w ** 4])
Z1 = np.diag([1, w, w**2])
Z2 = np.diag([1, w**2, w**4])

X0 = np.eye(QUDIT_DIM)
X1 = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
Expand Down Expand Up @@ -75,12 +75,16 @@ def test_grad_depolarizing(self, angle):

@qml.qnode(dev)
def circuit(p):
qml.TRX(angle, wires=0, subspace=(0,1))
qml.TRX(angle, wires=0, subspace=(1,2))
qml.TRX(angle, wires=0, subspace=(0, 1))
qml.TRX(angle, wires=0, subspace=(1, 2))
qml.QutritDepolarizingChannel(p, wires=0)
return qml.expval(qml.GellMann(0, 3) + qml.GellMann(0, 8))

expected_errorless = (np.sqrt(3)-3)*(1-np.cos(2*angle))/24 -2/np.sqrt(3)*np.sin(angle/2)**4+(np.sqrt(1/3)+1)*np.cos(angle/2)**2
expected_errorless = (
(np.sqrt(3) - 3) * (1 - np.cos(2 * angle)) / 24
- 2 / np.sqrt(3) * np.sin(angle / 2) ** 4
+ (np.sqrt(1 / 3) + 1) * np.cos(angle / 2) ** 2
)
assert np.allclose(circuit(prob), prob * expected_errorless)

gradient = np.squeeze(qml.grad(circuit)(prob))
Expand All @@ -98,8 +102,8 @@ def expected_jac_fn(p):
w = np.exp(2j * np.pi / 3)

Z0 = np.eye(QUDIT_DIM)
Z1 = np.diag([1, w, w ** 2])
Z2 = np.diag([1, w ** 2, w ** 4])
Z1 = np.diag([1, w, w**2])
Z2 = np.diag([1, w**2, w**4])

X0 = np.eye(QUDIT_DIM)
X1 = np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]])
Expand Down Expand Up @@ -154,6 +158,7 @@ def test_kraus_jac_torch(self):
def test_kraus_jac_tf(self):
"""Tests Jacobian of Kraus matrices using tensorflow."""
import tensorflow as tf

p = tf.Variable(0.43)
with tf.GradientTape() as real_tape:
real_out = self.kraus_fn_real(p)
Expand Down

0 comments on commit 998d21d

Please sign in to comment.