def test_register_noise(): class _FooNoise(Noise): def __init__(self): super().__init__(qubit_count=1, ascii_symbols=["foo"]) Noise.register_noise(_FooNoise) assert Noise._FooNoise().name == _FooNoise().name
def test_equality(): noise_1 = Noise(qubit_count=1, ascii_symbols=["foo"]) noise_2 = Noise(qubit_count=1, ascii_symbols=["foo"]) other_noise = Noise.AmplitudeDamping(gamma=0.5) non_noise = "non noise" assert noise_1 == noise_2 assert noise_1 is not noise_2 assert noise_1 != other_noise assert noise_1 != non_noise
def kraus( targets: QubitSetInput, matrices: Iterable[np.array], display_name: str = "KR" ) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: targets (Qubit, int, or iterable of Qubit / int): Target qubit(s) matrices (Iterable[np.array]): Matrices that define a general noise channel. Returns: Iterable[Instruction]: `Iterable` of Kraus instructions. Examples: >>> K0 = np.eye(4) * sqrt(0.9) >>> K1 = np.kron([[1., 0.],[0., 1.]], [[0., 1.],[1., 0.]]) * sqrt(0.1) >>> circ = Circuit().kraus(0, matrices=[K0, K1]) """ if 2 ** len(targets) != matrices[0].shape[0]: raise ValueError( "Dimensions of the supplied Kraus matrices are incompatible with the targets" ) return Instruction( Noise.Kraus(matrices=matrices, display_name=display_name), target=targets )
def test_noise_equality(noise, equal_noise, unequal_noise, param_noise): assert noise == noise assert noise is noise assert noise == equal_noise assert noise is not equal_noise assert noise != unequal_noise assert noise != param_noise assert noise != Noise(qubit_count=1, ascii_symbols=["foo"])
def from_dict(cls, noise_model_item: dict) -> NoiseModelInstruction: """ Converts a dictionary representing an object of this class into an instance of this class. Args: noise_model_item (dict): A dictionary representation of an object of this class. Returns: NoiseModelInstruction: An object of this class that corresponds to the passed in dictionary. """ return NoiseModelInstruction( noise=Noise.from_dict(noise_model_item["noise"]), criteria=Criteria.from_dict(noise_model_item["criteria"]), )
def phase_damping(target: QubitSetInput, gamma: float) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubit(s) gamma (float): Probability of phase damping. Returns: Iterable[Instruction]: `Iterable` of PhaseDamping instructions. Examples: >>> circ = Circuit().phase_damping(0, gamma=0.1) """ return [ Instruction(Noise.PhaseDamping(gamma=gamma), target=qubit) for qubit in QubitSet(target) ]
def bit_flip(target: QubitSetInput, probability: float) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubit(s) probability (float): Probability of bit flipping. Returns: Iterable[Instruction]: `Iterable` of BitFlip instructions. Examples: >>> circ = Circuit().bit_flip(0, probability=0.1) """ return [ Instruction(Noise.BitFlip(probability=probability), target=qubit) for qubit in QubitSet(target) ]
def amplitude_damping(target: QubitSetInput, gamma: float) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubit(s). gamma (float): decaying rate of the amplitude damping channel. Returns: Iterable[Instruction]: `Iterable` of AmplitudeDamping instructions. Examples: >>> circ = Circuit().amplitude_damping(0, gamma=0.1) """ return [ Instruction(Noise.AmplitudeDamping(gamma=gamma), target=qubit) for qubit in QubitSet(target) ]
def two_qubit_dephasing( target1: QubitInput, target2: QubitInput, probability: float ) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubits probability (float): Probability of two-qubit dephasing. Returns: Iterable[Instruction]: `Iterable` of Dephasing instructions. Examples: >>> circ = Circuit().two_qubit_dephasing(0, 1, probability=0.1) """ return [ Instruction(Noise.TwoQubitDephasing(probability=probability), target=[target1, target2]) ]
def depolarizing(target: QubitSetInput, probability: float) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (QubitSetInput): Target qubit(s) probability (float): Probability of depolarizing. Returns: Iterable[Instruction]: `Iterable` of Depolarizing instructions. Examples: >>> circ = Circuit().depolarizing(0, probability=0.1) """ return [ Instruction(Noise.Depolarizing(probability=probability), target=qubit) for qubit in QubitSet(target) ]
def pauli_channel( target: QubitSetInput, probX: float, probY: float, probZ: float ) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubit(s) probability List[float]: Probabilities for the Pauli X, Y and Z noise happening in the Kraus channel. Returns: Iterable[Instruction]: `Iterable` of PauliChannel instructions. Examples: >>> circ = Circuit().pauli_channel(0,probX=0.1,probY=0.2,probZ=0.3) """ return [ Instruction(Noise.PauliChannel(probX=probX, probY=probY, probZ=probZ), target=qubit) for qubit in QubitSet(target) ]
def two_qubit_pauli_channel( target1: QubitInput, target2: QubitInput, probabilities: Dict[str, float]) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target1 (QubitInput): Target qubit 1. target2 (QubitInput): Target qubit 2. probabilities (Dict[str, float]): Probability of two-qubit Pauli channel. Returns: Iterable[Instruction]: `Iterable` of Depolarizing instructions. Examples: >>> circ = Circuit().two_qubit_pauli_channel(0, 1, {"XX": 0.1}) """ return [ Instruction( Noise.TwoQubitPauliChannel(probabilities=probabilities), target=[target1, target2], ) ]
def generalized_amplitude_damping( target: QubitSetInput, gamma: float, probability: float ) -> Iterable[Instruction]: """Registers this function into the circuit class. Args: target (Qubit, int, or iterable of Qubit / int): Target qubit(s). p(float): Probability of the system being excited by the environment. gamma (float): The damping rate of the amplitude damping channel. Returns: Iterable[Instruction]: `Iterable` of GeneralizedAmplitudeDamping instructions. Examples: >>> circ = Circuit().generalized_amplitude_damping(0, probability = 0.9, gamma=0.1) """ return [ Instruction( Noise.GeneralizedAmplitudeDamping(gamma=gamma, probability=probability), target=qubit, ) for qubit in QubitSet(target) ]
@pytest.mark.xfail(raises=NotImplementedError) def test_to_matrix_not_implemented_by_default(base_noise): base_noise.to_matrix(None) @pytest.mark.xfail(raises=NotImplementedError) def test_invalid_deserializatoin(): Noise.from_dict({}) @pytest.mark.parametrize( "noise, expected_string, expected_repr", [ (Noise(1, ["foo"]), "Noise('qubit_count': 1)", "Noise('qubit_count': 1)"), ( SingleProbabilisticNoise(0.1, 1, ["foo"]), "SingleProbabilisticNoise(0.1)", "SingleProbabilisticNoise('probability': 0.1, 'qubit_count': 1)", ), ( DampingNoise(0.1, 1, ["foo"]), "DampingNoise(0.1)", "DampingNoise('gamma': 0.1, 'qubit_count': 1)", ), ( GeneralizedAmplitudeDampingNoise(0.1, 0.2, 1, ["foo"]), "GeneralizedAmplitudeDampingNoise(0.1, 0.2)", "GeneralizedAmplitudeDampingNoise('gamma': 0.1, 'probability': 0.2, 'qubit_count': 1)", ),
def test_noise_base_not_equal_to_different_type(): assert Noise(qubit_count=1, ascii_symbols=["foo"]) != "foo"
def noise_1qubit(): return Noise.BitFlip(probability=0.1)
target (Qubit, int, or iterable of Qubit / int): Target qubit(s) probability (float): Probability of bit flipping. Returns: Iterable[Instruction]: `Iterable` of BitFlip instructions. Examples: >>> circ = Circuit().bit_flip(0, probability=0.1) """ return [ Instruction(Noise.BitFlip(probability=probability), target=qubit) for qubit in QubitSet(target) ] Noise.register_noise(BitFlip) class PhaseFlip(SingleProbabilisticNoise): """Phase flip noise channel which transforms a density matrix :math:`\\rho` according to: .. math:: \\rho \\Rightarrow (1-p) \\rho + p X \\rho X^{\\dagger} where .. math:: I = \\left( \\begin{matrix} 1 & 0 \\\\ 0 & 1 \\end{matrix} \\right)
def test_invalid_deserializatoin(): Noise.from_dict({})
def test_invalid_data_ascii_symbols(qubit_count, ascii_symbols): Noise(qubit_count, ascii_symbols)
def noise(): return Noise(qubit_count=1, ascii_symbols=["foo"])
def noise_1qubit_2(): return Noise.Depolarizing(probability=0.1)
def noise_2qubit(): E0 = np.sqrt(0.8) * np.eye(4) E1 = np.sqrt(0.2) * np.kron(np.array([[0, 1], [1, 0]]), np.array([[0, 1], [1, 0]])) return Noise.Kraus(matrices=[E0, E1])