[docs]defPauliErrorTranspile(noise_model:NoiseModel)->BasePass:"""Generates compiler pass which adds coherent noise to a circuit. :param noise_model: Model describing the noise to be added. Should be a Pauli noise model. :return: Compiler pass adding random coherent Pauli noise. """defadd_gates(circuit:Circuit)->Circuit:"""Function adding random coherent Pauli errors to a circuit. :param circuit: Circuit to which errors are added. :raises Exception: Raised if the noise model is not a Pauli one. :return: Circuit with additional noise operations. """# Initialise circuit with the same registers as input.noisy_circuit=Circuit()forq_registerincircuit.q_registers:noisy_circuit.add_q_register(q_register)forc_registerincircuit.c_registers:noisy_circuit.add_c_register(c_register)# Add each command in the original circuit,# and a pauli error if appropriate.forcommandincircuit.get_commands():ifcommand.op.type==OpType.Barrier:noisy_circuit.add_barrier(command.args)else:noisy_circuit.add_gate(command.op,command.args)# If command has noise model defined, add a random errorifcommand.op.typeinnoise_model.noisy_gates:# Sample a random error, which may be Noneerror=noise_model.get_error_distribution(command.op.type).sample()iferrorisnotNone:forqubit,pauliinzip(command.args,error):ifpauliin[Pauli.X,OpType.X]:noisy_circuit.X(cast(Qubit,qubit),opgroup="noisy")elifpauliin[Pauli.Z,OpType.Z]:noisy_circuit.Z(cast(Qubit,qubit),opgroup="noisy")elifpauliin[Pauli.Y,OpType.Y]:noisy_circuit.Y(cast(Qubit,qubit),opgroup="noisy")elifpauliin[Pauli.I]:passelse:raiseException("Not a Pauli noise model."+f" Contains {pauli} error")returnnoisy_circuitreturnCustomPass(add_gates)