PennyLaneAI / pennylane

PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
https://pennylane.ai
Apache License 2.0
2.35k stars 604 forks source link

Add support of more noise channels present to pennylane #971

Closed hrushikesh890 closed 2 years ago

hrushikesh890 commented 3 years ago

Issue description

Description of the issue - Currently pennylane supports only following noise channels

AmplitudeDamping GeneralizedAmplitudeDamping PhaseDamping DepolarizingChannel QubitChannel.

While using these channels variety of other noise channels can be created, however for the user it would be more convenient if there is direct support for Thermal relaxation error, pauli error, readout error and reset error in pennylane. Such noisy simulations are supported in qiskit and will be valuable to have it in pennylane

co9olguy commented 3 years ago

Thanks for the feature request @hrushikesh890! We will add it to our to-do list.

If you're so-inclined, adding new noise channels is actually a relatively straightforward task that might be a good task for a new contributor. Let us know if you might be interested in contributing to the library in this way :smile:

hrushikesh890 commented 3 years ago

@co9olguy Hi, I am very much willing to contribute to pennylane. Me and @yulunwang (Teammate of mine in our research group) are looking to contribute to pennylane as we use it a lot in our research. If possible we would like to try our hands at this task.

co9olguy commented 3 years ago

Awesome :tada:, glad to hear that @hrushikesh890!

If you want a particular contribution to start with, as I said, this particular issue is actually a really good one, and would be a valued addition. Feel free to also browse other issues for the good first issue tag.

Note that the PennyLane dev team will be taking a bit of a holiday to end the year, so if you want/need our feedback or advice, you might have to wait until we're back in the office in early Jan :smiley:

ixfoduap commented 3 years ago

Hi @hrushikesh890,

This is awesome! I'm the PennyLane developer that has been championing noisy simulations and quantum channels. Adding new noisy channels is relatively straightforward once you know its Kraus operators. Testing is a bit trickier, but you can just follow what has been done in previous cases. Finally, if all the Kraus operators are themselves valid channels, e.g., they are unitaries up to rescaling, then we know how construct analytical gradients.

Please let me know of any way that I can help!

Best,

Juan Miguel

alejomonbar commented 3 years ago

is someone working on this or can I try it? @josh146 @ixfoduap @antalszava

antalszava commented 3 years ago

Hi @alejomonbar, feel free to go for it! :slightly_smiling_face: #1529 is a duplicate and is meant to focus its description on Pauli errors, but any of the error types mentioned here are great candidates to be added. :+1:

alejomonbar commented 3 years ago

Thank you for your reply @antalszava. What about the Thermal relaxation error?

Jaybsoni commented 3 years ago

Hey @alejomonbar, you can for sure implement thermal relaxation error! Just a reminder to open a work in progress ([WIP]) PR and link it in this issue so we know you are working on it. This way if you get stuck or have any questions you can link the specific lines of code and we can comment there directly !

All the best for Hacktoberfest, happy hacking !

alejomonbar commented 3 years ago

Thank you @Jaybsoni, Yes I will do it soon. I just want to be sure that it's a good PR.

meenu-kri commented 2 years ago

Regarding adding readout error to pennylane, I am wondering what would be the best way to add it. As far as I understand, simulating the readout error in measurement is like applying a qml.BitFlip channel (with a nonzero probability) to the quantum state just before reading out the diagonal entries of the density matrix corresponding to the quantum state for the required probabilities. If expectation value of any observable needs to be calculated, then the qml.BitFlip channel (for the readout error) should be applied after applying qml.obs.diagonalizing_gates(corresponding to the observable) to the quantum state.

Application of qml.BitFlip channel can be simulated on the qml.devices.default_mixed and not on the qml.devices.default_qubit. Ideally, it would have been good to have readout error as an attribute to qml.measurements. Nonetheless, since it can be simulated on the qml.devices.default_mixed only, it makes sense to add it as an attribute to the qml.devices.default_mixed currently. Any thoughts? @josh146 @antalszava @ixfoduap

antalszava commented 2 years ago

Hi @meenu-kri,

Nonetheless, since it can be simulated on the qml.devices.default_mixed only, it makes sense to add it as an attribute to the qml.devices.default_mixed currently.

Agreed! :slightly_smiling_face:

Posting steps from our chat just now:

  1. Updating the DefaultMixed.__init__ method:

Could be done, for example, by introducing a new readout_prob argument (or one with comparable name) and using it to store the readout error probability as an attribute:

    def __init__(
        self, wires, *, r_dtype=np.float64, c_dtype=np.complex128, shots=None, analytic=None, readout_prob=None
    ):
        if isinstance(wires, int) and wires > 23:
            raise ValueError(
                "This device does not currently support computations on more than 23 wires"
            )

        # call QubitDevice init
        super().__init__(wires, shots, r_dtype=r_dtype, c_dtype=c_dtype, analytic=analytic)
        self._debugger = None

        # Create the initial state.
        self._state = self._create_basis_state(0)
        self._pre_rotated_state = self._state

        # TODO: check that if readout_prob is not None, then it is float and between 0 and 1
        self.readout_err = readout_prob

Note that if the user doesn't explicitly pass in readout_prob on the device creation, then no readout error is added to the simulation.

  1. Inserting the following snippet into the DefaultMixed.apply method:
        if self.readout_err:
            bitflips = [qml.BitFlip(self.readout_err,wires=k) for k in self.num_wires]
            for bf in bitflips:
                self._apply_operation(bf)
meenu-kri commented 2 years ago

Hi @antalszava,

Thanks for the response 🙂. It's quite helpful.

I made the changes we talked about in default_mixed.py. I have also written tests in test_default_mixed.py. Link to the PR https://github.com/PennyLaneAI/pennylane/pull/2786 that I have created with these changes.

antalszava commented 2 years ago

With #2786 merged, we have added all 4 originally mentioned noise channel types mentioned (Thermal relaxation error, pauli error, readout error and reset error).