Skip to content

Commit

Permalink
Changed amplitude damping gamma_x to gamma_xy for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel-Bottrill committed Jun 3, 2024
1 parent 899e425 commit 493993a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
57 changes: 28 additions & 29 deletions pennylane/ops/qutrit/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,39 +234,39 @@ class QutritAmplitudeDamping(Channel):
.. math::
K_0 = \begin{bmatrix}
1 & 0 & 0\\
0 & \sqrt{1-\gamma_1} & 0 \\
0 & 0 & \sqrt{1-(\gamma_2+\gamma_3)}
0 & \sqrt{1-\gamma_{01}} & 0 \\
0 & 0 & \sqrt{1-(\gamma_{02}+\gamma_{12})}
\end{bmatrix}
.. math::
K_1 = \begin{bmatrix}
0 & \sqrt{\gamma_1} & 0 \\
0 & \sqrt{\gamma_{01}} & 0 \\
0 & 0 & 0 \\
0 & 0 & 0
\end{bmatrix}, \quad
K_2 = \begin{bmatrix}
0 & 0 & \sqrt{\gamma_2} \\
0 & 0 & \sqrt{\gamma_{02}} \\
0 & 0 & 0 \\
0 & 0 & 0
\end{bmatrix}, \quad
K_3 = \begin{bmatrix}
0 & 0 & 0 \\
0 & 0 & \sqrt{\gamma_3} \\
0 & 0 & \sqrt{\gamma_{12}} \\
0 & 0 & 0
\end{bmatrix}
where :math:`\gamma_1, \gamma_2, \gamma_3 \in [0, 1]` are the amplitude damping
where :math:`\gamma_{01}, \gamma_{02}, \gamma_{12} \in [0, 1]` are the amplitude damping
probabilities for subspaces (0,1), (0,2), and (1,2) respectively.
.. note::
When :math:`\gamma_3=0` then Kraus operators :math:`\{K_0, K_1, K_2\}` are adapted from
When :math:`\gamma_{12}=0` then Kraus operators :math:`\{K_0, K_1, K_2\}` are adapted from
[`1 <https://doi.org/10.48550/arXiv.1905.10481>`_] (Eq. 8).
The Kraus operator :math:`K_3` represents the :math:`|2 \rangle \rightarrow |1 \rangle` transition which is more
likely on some devices [`2 <https://arxiv.org/abs/2003.03307>`_] (Sec II.A).
To maintain normalization :math:`\gamma_2 + \gamma_3 \leq 1`.
To maintain normalization :math:`\gamma_{02} + \gamma_{12} \leq 1`.
**Details:**
Expand All @@ -275,9 +275,9 @@ class QutritAmplitudeDamping(Channel):
* Number of parameters: 3
Args:
gamma_1 (float): :math:`|1 \rangle \rightarrow |0 \rangle` amplitude damping probability.
gamma_2 (float): :math:`|2 \rangle \rightarrow |0 \rangle` amplitude damping probability.
gamma_3 (float): :math:`|2 \rangle \rightarrow |1 \rangle` amplitude damping probability.
gamma_01 (float): :math:`|1 \rangle \rightarrow |0 \rangle` amplitude damping probability.
gamma_02 (float): :math:`|2 \rangle \rightarrow |0 \rangle` amplitude damping probability.
gamma_12 (float): :math:`|2 \rangle \rightarrow |1 \rangle` amplitude damping probability.
wires (Sequence[int] or int): the wire the channel acts on
id (str or None): String representing the operation (optional)
"""
Expand All @@ -286,25 +286,25 @@ class QutritAmplitudeDamping(Channel):
num_wires = 1
grad_method = "F"

def __init__(self, gamma_1, gamma_2, gamma_3, wires, id=None):
def __init__(self, gamma_01, gamma_02, gamma_12, wires, id=None):
# Verify input
for gamma in (gamma_1, gamma_2, gamma_3):
for gamma in (gamma_01, gamma_02, gamma_12):
if not math.is_abstract(gamma):
if not 0.0 <= gamma <= 1.0:
raise ValueError("Each probability must be in the interval [0,1]")
if not (math.is_abstract(gamma_2) or math.is_abstract(gamma_3)):
if not 0.0 <= gamma_2 + gamma_3 <= 1.0:
raise ValueError(r"\gamma_2+\gamma_3 must be in the interval [0,1]")
super().__init__(gamma_1, gamma_2, gamma_3, wires=wires, id=id)
if not (math.is_abstract(gamma_02) or math.is_abstract(gamma_12)):
if not 0.0 <= gamma_02 + gamma_12 <= 1.0:
raise ValueError(r"\gamma_{02}+\gamma_{12} must be in the interval [0,1]")
super().__init__(gamma_01, gamma_02, gamma_12, wires=wires, id=id)

@staticmethod
def compute_kraus_matrices(gamma_1, gamma_2, gamma_3): # pylint:disable=arguments-differ
def compute_kraus_matrices(gamma_01, gamma_02, gamma_12): # pylint:disable=arguments-differ
r"""Kraus matrices representing the ``QutritAmplitudeDamping`` channel.
Args:
gamma_1 (float): :math:`|1\rangle \rightarrow |0\rangle` amplitude damping probability.
gamma_2 (float): :math:`|2\rangle \rightarrow |0\rangle` amplitude damping probability.
gamma_3 (float): :math:`|2\rangle \rightarrow |1\rangle` amplitude damping probability.
gamma_01 (float): :math:`|1\rangle \rightarrow |0\rangle` amplitude damping probability.
gamma_02 (float): :math:`|2\rangle \rightarrow |0\rangle` amplitude damping probability.
gamma_12 (float): :math:`|2\rangle \rightarrow |1\rangle` amplitude damping probability.
Returns:
list(array): list of Kraus matrices
Expand All @@ -328,21 +328,20 @@ def compute_kraus_matrices(gamma_1, gamma_2, gamma_3): # pylint:disable=argumen
]
"""
K0 = math.diag(
[1, math.sqrt(1 - gamma_1 + math.eps), math.sqrt(1 - gamma_2 - gamma_3 + math.eps)]
[1, math.sqrt(1 - gamma_01 + math.eps), math.sqrt(1 - gamma_02 - gamma_12 + math.eps)]
)
K1 = math.sqrt(gamma_1 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]]), gamma_1), gamma_1
K1 = math.sqrt(gamma_01 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]]), gamma_01), gamma_01
)
K2 = math.sqrt(gamma_2 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]]), gamma_2), gamma_2
K2 = math.sqrt(gamma_02 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]]), gamma_02), gamma_02
)
K3 = math.sqrt(gamma_3 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]), gamma_3), gamma_3
K3 = math.sqrt(gamma_12 + math.eps) * math.convert_like(
math.cast_like(math.array([[0, 0, 0], [0, 0, 1], [0, 0, 0]]), gamma_12), gamma_12
)
return [K0, K1, K2, K3]



class TritFlip(Channel):
r"""
Single-qutrit parameter flipping error channel, similar to (GellMann :math:`\{\lambda_1, \lambda_4, \lambda_6\}`).
Expand Down
65 changes: 33 additions & 32 deletions tests/ops/qutrit/test_qutrit_channel_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class TestQutritAmplitudeDamping:
"""Tests for the qutrit quantum channel QutritAmplitudeDamping"""

def test_gamma_zero(self, tol):
"""Test gamma_1=gamma_2=0 gives correct Kraus matrices"""
"""Test gamma_01=gamma_02=0 gives correct Kraus matrices"""
kraus_mats = qml.QutritAmplitudeDamping(0, 0, 0, wires=0).kraus_matrices()
assert np.allclose(kraus_mats[0], np.eye(3), atol=tol, rtol=0)
for kraus_mat in kraus_mats[1:]:
Expand Down Expand Up @@ -209,55 +209,56 @@ def test_gamma_arbitrary(self, gamma1, gamma2, gamma3, tol):
),
)
def test_gamma_invalid_parameter(self, gamma1, gamma2, gamma3):
"""Ensures that error is thrown when gamma_1, gamma_2, gamma_3, or (gamma_2 + gamma_3) are outside [0,1]"""
"""Ensures that error is thrown when
gamma_01, gamma_02, gamma_12, or (gamma_02 + gamma_12) are outside [0,1]"""
with pytest.raises(ValueError, match="must be in the interval"):
channel.QutritAmplitudeDamping(gamma1, gamma2, gamma3, wires=0).kraus_matrices()

@staticmethod
def expected_jac_fn(gamma_1, gamma_2, gamma_3):
def expected_jac_fn(gamma_01, gamma_02, gamma_12):
"""Gets the expected Jacobian of Kraus matrices"""
partial_1 = [math.zeros((3, 3)) for _ in range(4)]
partial_1[0][1, 1] = -1 / (2 * math.sqrt(1 - gamma_1))
partial_1[1][0, 1] = 1 / (2 * math.sqrt(gamma_1))
partial_1[0][1, 1] = -1 / (2 * math.sqrt(1 - gamma_01))
partial_1[1][0, 1] = 1 / (2 * math.sqrt(gamma_01))

partial_2 = [math.zeros((3, 3)) for _ in range(4)]
partial_2[0][2, 2] = -1 / (2 * math.sqrt(1 - gamma_2 - gamma_3))
partial_2[2][0, 2] = 1 / (2 * math.sqrt(gamma_2))
partial_2[0][2, 2] = -1 / (2 * math.sqrt(1 - gamma_02 - gamma_12))
partial_2[2][0, 2] = 1 / (2 * math.sqrt(gamma_02))

partial_3 = [math.zeros((3, 3)) for _ in range(4)]
partial_3[0][2, 2] = -1 / (2 * math.sqrt(1 - gamma_2 - gamma_3))
partial_3[3][1, 2] = 1 / (2 * math.sqrt(gamma_3))
partial_3[0][2, 2] = -1 / (2 * math.sqrt(1 - gamma_02 - gamma_12))
partial_3[3][1, 2] = 1 / (2 * math.sqrt(gamma_12))

return [partial_1, partial_2, partial_3]

@staticmethod
def kraus_fn(gamma_1, gamma_2, gamma_3):
def kraus_fn(gamma_01, gamma_02, gamma_12):
"""Gets the Kraus matrices of QutritAmplitudeDamping channel, used for differentiation."""
damping_channel = qml.QutritAmplitudeDamping(gamma_1, gamma_2, gamma_3, wires=0)
damping_channel = qml.QutritAmplitudeDamping(gamma_01, gamma_02, gamma_12, wires=0)
return math.stack(damping_channel.kraus_matrices())

@pytest.mark.autograd
def test_kraus_jac_autograd(self):
"""Tests Jacobian of Kraus matrices using autograd."""
gamma_1 = pnp.array(0.43, requires_grad=True)
gamma_2 = pnp.array(0.12, requires_grad=True)
gamma_3 = pnp.array(0.35, requires_grad=True)
gamma_01 = pnp.array(0.43, requires_grad=True)
gamma_02 = pnp.array(0.12, requires_grad=True)
gamma_12 = pnp.array(0.35, requires_grad=True)

jac = qml.jacobian(self.kraus_fn)(gamma_1, gamma_2, gamma_3)
assert math.allclose(jac, self.expected_jac_fn(gamma_1, gamma_2, gamma_3))
jac = qml.jacobian(self.kraus_fn)(gamma_01, gamma_02, gamma_12)
assert math.allclose(jac, self.expected_jac_fn(gamma_01, gamma_02, gamma_12))

@pytest.mark.torch
def test_kraus_jac_torch(self):
"""Tests Jacobian of Kraus matrices using PyTorch."""
import torch

gamma_1 = torch.tensor(0.43, requires_grad=True)
gamma_2 = torch.tensor(0.12, requires_grad=True)
gamma_3 = torch.tensor(0.35, requires_grad=True)
gamma_01 = torch.tensor(0.43, requires_grad=True)
gamma_02 = torch.tensor(0.12, requires_grad=True)
gamma_12 = torch.tensor(0.35, requires_grad=True)

jac = torch.autograd.functional.jacobian(self.kraus_fn, (gamma_1, gamma_2, gamma_3))
jac = torch.autograd.functional.jacobian(self.kraus_fn, (gamma_01, gamma_02, gamma_12))
expected = self.expected_jac_fn(
gamma_1.detach().numpy(), gamma_2.detach().numpy(), gamma_3.detach().numpy()
gamma_01.detach().numpy(), gamma_02.detach().numpy(), gamma_12.detach().numpy()
)

for res_partial, exp_partial in zip(jac, expected):
Expand All @@ -268,26 +269,26 @@ def test_kraus_jac_tf(self):
"""Tests Jacobian of Kraus matrices using TensorFlow."""
import tensorflow as tf

gamma_1 = tf.Variable(0.43)
gamma_2 = tf.Variable(0.12)
gamma_3 = tf.Variable(0.35)
gamma_01 = tf.Variable(0.43)
gamma_02 = tf.Variable(0.12)
gamma_12 = tf.Variable(0.35)

with tf.GradientTape() as tape:
out = self.kraus_fn(gamma_1, gamma_2, gamma_3)
jac = tape.jacobian(out, (gamma_1, gamma_2, gamma_3))
assert math.allclose(jac, self.expected_jac_fn(gamma_1, gamma_2, gamma_3))
out = self.kraus_fn(gamma_01, gamma_02, gamma_12)
jac = tape.jacobian(out, (gamma_01, gamma_02, gamma_12))
assert math.allclose(jac, self.expected_jac_fn(gamma_01, gamma_02, gamma_12))

@pytest.mark.jax
def test_kraus_jac_jax(self):
"""Tests Jacobian of Kraus matrices using JAX."""
import jax

gamma_1 = jax.numpy.array(0.43)
gamma_2 = jax.numpy.array(0.12)
gamma_3 = jax.numpy.array(0.35)
gamma_01 = jax.numpy.array(0.43)
gamma_02 = jax.numpy.array(0.12)
gamma_12 = jax.numpy.array(0.35)

jac = jax.jacobian(self.kraus_fn, argnums=[0, 1, 2])(gamma_1, gamma_2, gamma_3)
assert math.allclose(jac, self.expected_jac_fn(gamma_1, gamma_2, gamma_3))
jac = jax.jacobian(self.kraus_fn, argnums=[0, 1, 2])(gamma_01, gamma_02, gamma_12)
assert math.allclose(jac, self.expected_jac_fn(gamma_01, gamma_02, gamma_12))


class TestTritFlip:
Expand Down

0 comments on commit 493993a

Please sign in to comment.