<jupyter_start><jupyter_text>Copyright 2020 The Cirq Developers<jupyter_code>#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.<jupyter_output><empty_output><jupyter_text>Noise       View on QuantumLib        Run in Google Colab        View source on GitHub        Download notebook   For simulation, it is useful to have `Gate` objects that enact noisy quantum evolution.Cirq supports modeling noise via *operator sum* representations ofnoise (these evolutions are also known as quantum operations, quantumdynamical maps, or superoperators).  This formalism models evolution of thedensity matrix via:$$\rho \rightarrow \sum_k A_k \rho A_k^\dagger$$Where Ak are *Kraus* operators. These operators are notnecessarily unitary and must satisfy the trace-preserving property:$$\sum_k A_k^\dagger A_k = I$$As a noisy channel, Kraus operators are not unique. For more details of these operators, see [<NAME>'s notes](http://www.theory.caltech.edu/people/preskill/ph219/chap3_15.pdf).  Magic methodsA `Gate` can represent an operator sum representation by supporting the`channel` protocol.  Alternatively, for channels that represent probabilisticmixtures of unitaries, one can implement the `mixture` protocol.  `cirq.channel` and `def _channel_`To represent an operator sum evolution, a `Gate` should implement the`SupportsChannel` protocol. To do this, the `Gate` should implement the`_channel_(self) -> Sequence[np.ndarray]:` method. This method should return the sequence of `numpy` matrices corresponding to the Kraus operators. The basis in which this matrix is expressed is always implicit with respect to the object being called. For example, in `GateOperations`, these matrices must be ordered with respect to the list of qubits that the channel is applied to. The qubit-to-amplitude order mapping matches the ordering of `numpy.kron(A, B)`, where `A` is a qubit earlier in the list than the qubit `B`.If one has defined `_channel_`, then that `Gate` and any `GateOperation`that uses that gate can be used as an argument to `cirq.channel` and`cirq.channel` will return this sequence of matrices.Besides objects that support `_channel_`, `cirq.channel` will also fallback to other objects that can be interpreted as channels. For example, if a channel is a probabilistic mixture of unitary gates (see below), then `cirq.channel` will fall back to seeing if the object supports `_mixture_`. If `_mixture_` is not supported, then `cirq.channel` checks to see if `_unitary_` is supported.In addition to supporting `_channel_`, objects that are channels should also implement `_has_channel_(self) -> bool` to return `True`. This method is used to determine whether an object has a `_channel_` or not without having to do the potentially expensive creation of the matrices for the channel.  `cirq.mixture` and `def _mixture_`Some channels can be interpreted as probabilistically selecting betweendifferent unitary evolutions:$$\rho \rightarrow \sum_k p_k U_k \rho U_k^\dagger {\rm ~where~} \sum_k p_k =1 {\rm ~and~ U_k U_k^\dagger= I}$$In this case, it is possible to perform **Monte Carlo simulations** of these gates using a wave function based simulator (and not a density matrix based simulator). An example of this can be seen below:<jupyter_code>!pip install cirq --quiet
import cirq

q = cirq.NamedQubit('a')
circuit = cirq.Circuit(cirq.bit_flip(p=0.2)(q), cirq.measure(q))
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=100)
print(result.histogram(key='a'))<jupyter_output>Counter({0: 87, 1: 13})
{'fraction_non_alphanumeric': 0.07730549341287596, 'fraction_numerical': 0.006462838677603778, 'mean_word_length': 5.162327718223583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '566558', 'license': 'Apache-2.0', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1047, 'n_words': 519, 'path': 'docs/noise.ipynb', 'repo_name': 'BillGatesNephew/Cirq', 'size': '4030'}