Пример #1
0
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
Пример #2
0
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
Пример #3
0
    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
        )
Пример #4
0
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"])
Пример #5
0
    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"]),
        )
Пример #6
0
    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)
        ]
Пример #7
0
    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)
        ]
Пример #8
0
    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)
        ]
Пример #9
0
    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])
        ]
Пример #10
0
    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)
        ]
Пример #11
0
    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)
        ]
Пример #12
0
    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],
            )
        ]
Пример #13
0
    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)
        ]
Пример #14
0

@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)",
        ),
Пример #15
0
def test_noise_base_not_equal_to_different_type():
    assert Noise(qubit_count=1, ascii_symbols=["foo"]) != "foo"
Пример #16
0
def noise_1qubit():
    return Noise.BitFlip(probability=0.1)
Пример #17
0
            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)
Пример #18
0
def test_invalid_deserializatoin():
    Noise.from_dict({})
Пример #19
0
def test_invalid_data_ascii_symbols(qubit_count, ascii_symbols):
    Noise(qubit_count, ascii_symbols)
Пример #20
0
def noise():
    return Noise(qubit_count=1, ascii_symbols=["foo"])
Пример #21
0
def noise_1qubit_2():
    return Noise.Depolarizing(probability=0.1)
Пример #22
0
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])