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

Handle confusion matrices in deferred measurements #5851

Merged
merged 27 commits into from
Oct 12, 2022

Conversation

daxfohl
Copy link
Contributor

@daxfohl daxfohl commented Sep 2, 2022

Handles measurement confusion matrices by substituting with a confusion channel.

These channels are derived as described in the _ConfusionChannel docstring, and evolve the diagonal of the density matrix in the same way that the confusion matrix operates on the classical state distribution.

I hadn't really thought of it before, but when writing the test I realized this is great for sampling circuits with classical controls. Way better performance than having to simulate that same number of iterations due to classical logic.

@daxfohl daxfohl requested review from a team, vtomole and cduck as code owners September 2, 2022 07:14
@daxfohl daxfohl requested a review from verult September 2, 2022 07:14
@CirqBot CirqBot added the size: M 50< lines changed <250 label Sep 2, 2022
@daxfohl daxfohl changed the title Handle confusion matrixes in deferred measurements Handle confusion matrices in deferred measurements Sep 21, 2022
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea behind this PR is sound. The step that might make one uneasy is the confusion-matrix channel since it assumes the input density matrix is diagonal. This step is correct, because measurement may be thought to entail the application of the completely dephasing channel which kills all off-diagonal elements.

However, I have some comments about the implementation.

@daxfohl
Copy link
Contributor Author

daxfohl commented Oct 1, 2022

Thanks for the review! I'll clean up these items.

Nit on the review: these channel don't require the DM to be diagonal a priori. They just ignore anything not in the diagonal.

@viathor
Copy link
Collaborator

viathor commented Oct 1, 2022

Re nit on the review: Well, if you call it a "channel" then you should insist that the input density matrix be diagonal. Otherwise it fails to be a channel. By definition, channels map positive semi-definite matrices to positive semi-definite matrices. This property is lost when you apply a confusion matrix to the diagonal entries while leaving the off-diagonal entries alone. For example, the result of applying the confusion matrix that maps both 0 and 1 to 0 to the diagonal of

$$ |+\rangle\langle+|=\frac12\begin{bmatrix} 1 & 1\\ 1 & 1 \end{bmatrix} $$

is

$$ \frac12\begin{bmatrix} 2 & 1\\ 1 & 0 \end{bmatrix} $$

which is not positive semi-definite (since its determinant is negative).

I suppose an alternative would be to use something like the Generalized Amplitude Damping Channel, but this seems like an overkill for your use-case (and it would make it harder to fix #5901, so let's not do this).

@daxfohl
Copy link
Contributor Author

daxfohl commented Oct 2, 2022

Consolidating a few threads since they kind of lead into each other:

  1. This channel does not assume off-diagonals are zero nor does it leave them alone: it forces them to zero. This channel includes the phase dampening as part of itself. A _ConfusionChannel([[1, 0], [1, 0]]) is exactly equivalent to a reset, and a _ConfusionChannel([[1, 0], [0, 1]]) is exactly equivalent to a phase_damp(1). So given this, does that make it a valid channel? I agree that if the off-diagonals were untouched then it would not be a valid channel.
  2. In fact, the input is typically not diagonal. There's no explicit dephase op that gets inserted during the deferred measurement transformation. Textbooks don't do this either. I can see an argument that there should be, but for most cases the user is not going to care about it and it will just slow down the simulation, block Clifford simulator, etc. Maybe that should be an optional parameter, however the point remains that the input won't always be diagonal.
  3. Given this, it means this channel can affect the entire DM even when applied to a single qubit, I imagine that reduces the potential perf improvement to be gained by _apply_channel_, and is probably more complex than what you were originally picturing? If there was a way to implement the latter smartly only to apply a confusion step to a couple diagonal values then that would be super fast. But there isn't. I imagine there's still opportunity for improvement beyond a generic einsum given the LHS is always onehot, but I still haven't figured out the faster equivalent.

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Below are replies to your larger comment. For clarity, I'll use a few ad hoc definitions:
CMC = Confusion Matrix Channel, acts on diagonal, assumes all other elements are zero (IOW, it's not a quantum channel, but a map acting on classical probability distributions encoded in the computational basis)
CDC = Completely Dephasing Channel, zeroes out off-diagonal elements (it's a quantum channel that outputs the classical probability distribution associated with the computational basis measurement on the input state).
CC = Confusion Channel, CDC followed by CMC.

  1. Yes, CC is a valid quantum channel (CMC is not).
  2. Yes, the input is typically not diagonal, but the output is (block) diagonal. In other words, your CC channel entails an implicit CDC. I agree that you don't have to dephase, but at present, you actually do dephase! :-) Either way is correct because phase-flip channels such as CDC don't affect measurement and commute with the operations that may appear on the ancilla (control of other gates and bit-flip channel due to inversion mask). A benefit of the fact that you apply the CDC is that it creates opportunities for performance optimization since CDC zeroes out a bunch of entries in the density matrix. As agreed, this optimization is out-of-scope for this PR. A downside of CDC is that your CC channel will indeed block the Clifford simulator.
  3. No. Your CC is guaranteed to output a (block) diagonal matrix since all Kraus operators have the form $\sqrt{p_{ij}}|j\rangle\langle i|$ where $p_{ij}$ is an appropriate entry from the confusion matrix. Therefore, every term in $\mathcal{E}(\rho)$ looks like $p_{ij}\cdot \langle i|\rho|i\rangle \cdot |j\rangle\langle j|$. This means that we could implement _ConfusionChannel very simply without ever computing Kraus operators. Instead, we'd compute $j$ th diagonal block as $\sum_i p_{ij} \cdot \langle i|\rho|i\rangle$. I'd expect this algorithm to be pretty fast since it avoids matrix multiplications entirely. Note that we should be able to extract the matrix $\langle i|\rho|i\rangle$ from $\rho$ with clever indexing. This is the optimization I have in mind for Use _apply_channel_ when appropriate #5901. In any case, it's fine to do it in a follow-up.

@daxfohl
Copy link
Contributor Author

daxfohl commented Oct 11, 2022

Is https://github.com/quantumlib/Cirq/pull/5905/files is the "clever indexing" you refer to in (3)?

That solution is generic and works for any one-hot multiplier, but I closed the PR because it's slower for the normal case. For confusion and reset channels specifically, we could improve further on that by (1) we already know each component is one-hot, so no need to check, (2) we already know the index so there's no need to unravel, and (3) the linked code still does each Kraus component in two steps: left multiply and right multiply; we should be able to do it in a single step. Does this sound correct?

Thanks for the thorough review!

Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. I meant to specialize the application of a specific channel type (here _ConfusionChannel and also others like reset) rather than modifying a general utility method like targeted_left_multiply. In other words, I meant to do for channels what we already do for many unitary gates such as X and SWAP (e.g. here). This avoids the need for a check (such as the one using np.flatnonzero in #5905) and hence has absolutely no impact on operations other than the one being optimized.

Matrix multiplication is very general, but also expensive. Many interesting quantum operations can be viewed in terms of a simple recipe for constructing the output (e.g. swap some input elements; change some signs; set off-diagonal elements to zero then apply confusion matrix to the diagonal etc) rather than as matrix algebra. This view inspired _apply_unitary_ and I think we should pursue it for channels, too.

@viathor viathor added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Oct 12, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Oct 12, 2022
@CirqBot CirqBot merged commit 6e64449 into quantumlib:master Oct 12, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Oct 12, 2022
@daxfohl daxfohl deleted the deferred-kraus2 branch October 13, 2022 22:40
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Handles measurement confusion matrices by substituting with a confusion channel.

These channels are derived as described in the `_ConfusionChannel` docstring, and evolve the diagonal of the density matrix in the same way that the confusion matrix operates on the classical state distribution.

I hadn't really thought of it before, but when writing the test I realized this is _great_ for sampling circuits with classical controls. Way better performance than having to simulate that same number of iterations due to classical logic.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Handles measurement confusion matrices by substituting with a confusion channel.

These channels are derived as described in the `_ConfusionChannel` docstring, and evolve the diagonal of the density matrix in the same way that the confusion matrix operates on the classical state distribution.

I hadn't really thought of it before, but when writing the test I realized this is _great_ for sampling circuits with classical controls. Way better performance than having to simulate that same number of iterations due to classical logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants