Skip to content

Commit

Permalink
adding the ability to check if the Kraus operators are valid + tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcombes authored May 28, 2019
1 parent f8717ec commit 949b222
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
22 changes: 22 additions & 0 deletions forest/benchmarking/superoperator_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,28 @@ def apply_choi_matrix_2_state(choi: np.ndarray, state: np.ndarray) -> np.ndarray
# ==================================================================================================
# Check physicality of Channels
# ==================================================================================================
def kraus_operators_are_valid(kraus_ops: Sequence[np.ndarray],
rtol: float = 1e-05,
atol: float = 1e-08)-> bool:
"""
Checks if a set of Kraus operators are valid.
:param kraus_ops: A list of Kraus operators
:param rtol: The relative tolerance parameter in np.allclose
:param atol: The absolute tolerance parameter in np.allclose
:return: True if the Kraus operators are valid with the given tolerance; False otherwise.
"""
if isinstance(kraus_ops, np.ndarray): # handle input of single kraus op
if len(kraus_ops[0].shape) < 2: # first elem is not a matrix
kraus_ops = [kraus_ops]
rows, _ = np.asarray(kraus_ops[0]).shape
# Standard case of square Kraus operators is if rows==cols. For non-square Kraus ops it is
# required that sum_i M_i^\dagger M_i = np.eye(rows,rows).
id_iff_valid = sum(np.transpose(op).conjugate().dot(op) for op in kraus_ops)
# TODO: check if each POVM element (i.e. M_i^\dagger M_i) is PSD
return np.allclose(id_iff_valid, np.eye(rows), rtol=rtol, atol=atol)


def choi_is_hermitian_preserving(choi: np.ndarray, rtol: float = 1e-05,
atol: float = 1e-08) -> bool:
"""
Expand Down
4 changes: 4 additions & 0 deletions forest/benchmarking/tests/test_superoperator_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def test_apply_choi_matrix_2_state():
# ==================================================================================================
# Test physicality of Channels
# ==================================================================================================
def test_kraus_operators_are_valid():
assert kraus_operators_are_valid(amplitude_damping_kraus(np.random.rand()))
assert kraus_operators_are_valid(H)
assert not kraus_operators_are_valid(AdKrausOps[0])

def test_choi_is_hermitian_preserving():
D = 2
Expand Down

0 comments on commit 949b222

Please sign in to comment.