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

Add validation for Kraus operators #2271

Closed
peterse opened this issue Oct 6, 2019 · 6 comments · Fixed by #5247
Closed

Add validation for Kraus operators #2271

peterse opened this issue Oct 6, 2019 · 6 comments · Fixed by #5247
Assignees
Labels
area/channels complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality skill-level/beginner No background required, self-contained issue triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@peterse
Copy link
Collaborator

peterse commented Oct 6, 2019

Many of the methods involving noise/channels mention that the matrices making up a mixture or channel must satisfy \sum_{k=0}^{r-1} A_i^\dagger A_i = I. This is never enforced, which allows the user to simulate unphysical systems and get invalid density matrices (see example below).

Validation is easy enough (assert np.sum([m.T @ m for m in object._channel_()) but I'm not sure where to put this @Strilanc ? Ideally this will be called exactly once, before a call to simulate.

Code Example:

from typing import Iterable

import cirq
import numpy as np

class UnnormalizedChannel(cirq.GeneralizedAmplitudeDampingChannel):
    """Overwrite the correct amplitude damping channel with a bad one."""
    def _channel_(self) -> Iterable[np.ndarray]:
        p0 = 1
        sqrt_g = np.sqrt(self._gamma)
        return (
            p0 * np.array([[1., 0.], [0., 1.]]),
            p0 * np.array([[0., sqrt_g], [0., 0.]]),
        )


def unnormalized_channel(gamma):
    return UnnormalizedChannel(1.0, gamma)


q0 = cirq.LineQubit(0)
bad_noise_inst = unnormalized_channel(0.3)
c = cirq.Circuit.from_ops(cirq.X(q0), bad_noise_inst.on(q0))
dm = cirq.DensityMatrixSimulator().simulate(c).final_density_matrix
kraus_sum = np.sum(m.T @ m for m in bad_noise_inst._channel_())
print(np.allclose(kraus_sum, np.eye(2)))
print(np.isclose(np.trace(dm), 1.0))

which prints

>>> False
>>> False
@dabacon
Copy link
Collaborator

dabacon commented Oct 16, 2019

One first suggestion is that we should add a testing method that checks this.

My opinion is just as we don't enforce unitary returns a unitary (because checking it each time would be costly), I think we just need to make it easier for people to test that their channel is OK.

@c-poole
Copy link
Collaborator

c-poole commented Oct 18, 2019

We have routines in cirq.linalg.predicates that check for a variety of potential qualities. I would suggest adding some additional methods like is_cp and is_cptp there for the users to apply on their constructions themselves. We could potentially add a flag in simulate and run which when true performs quantum computing validity checks on the circuit but I'm not convinced whether that would add much benefit if we had a more complete list checks for individual operations in linalg.

@peterse peterse self-assigned this Oct 30, 2019
@dabacon dabacon added this to Gates, Channels in Bug Smash and Organize May 6, 2020
@balopat
Copy link
Contributor

balopat commented Sep 16, 2020

+1 for using predicate and I like the is_cp, is_cptp checks.

@balopat balopat added complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality skill-level/beginner No background required, self-contained issue triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add labels Sep 16, 2020
@balopat
Copy link
Contributor

balopat commented Sep 16, 2020

@peterse are you still planning to work on this?

@peterse
Copy link
Collaborator Author

peterse commented Sep 26, 2020

oops, sorry for taking so long. I'm struggling to find time to contribute so I'm going to unassign myself for now

@peterse peterse removed their assignment Sep 26, 2020
CirqBot pushed a commit that referenced this issue Jul 29, 2021
As requested in #4194. Can be used for #2271.

This predicate is meant to be invoked when constructing a channel to verify that the provided Kraus operators actually describe a valid quantum channel. Recommendations for cleaner `is_cptp` behavior or additional test cases are welcome.
@verult
Copy link
Collaborator

verult commented Mar 28, 2022

Keeping this before-1.0 as new validation will tighten the API. cc @95-martin-orion

@dabacon dabacon self-assigned this May 13, 2022
CirqBot pushed a commit that referenced this issue May 19, 2022
Adds a helper that tests that channel kraus operator is cptp (\sum_i A_i^\dagger A_i = I) and that has_kraus is true.  Uses this on channels.

Adds a helper for testing that mixture's probabilities sum to 1.  Uses this on mixtures.

Fixes #2271
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
As requested in quantumlib#4194. Can be used for quantumlib#2271.

This predicate is meant to be invoked when constructing a channel to verify that the provided Kraus operators actually describe a valid quantum channel. Recommendations for cleaner `is_cptp` behavior or additional test cases are welcome.
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
Adds a helper that tests that channel kraus operator is cptp (\sum_i A_i^\dagger A_i = I) and that has_kraus is true.  Uses this on channels.

Adds a helper for testing that mixture's probabilities sum to 1.  Uses this on mixtures.

Fixes quantumlib#2271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/channels complexity/low introduces/modifies 1-2 concepts, should take 1-2 days max for an advanced contributor good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. kind/feature-request Describes new functionality skill-level/beginner No background required, self-contained issue triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
No open projects
Bug Smash and Organize
Gates, Channels, Gate Construction
5 participants