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 603 forks source link

[unitaryhack] Add a projector observable #1059

Closed mariaschuld closed 3 years ago

mariaschuld commented 3 years ago

This issue has been tagged for contributions during unitaryHACK

For some applications it would be elegant and useful to have a PennyLane observable that implements the projector |i >< i| onto the i'th computational basis state. For example, for 3 qubits and i=4, the computational basis state is "[0, 1, 1]" and the observable could be implemented as


def circuit():
     ....
     return qml.expval(qml.Projector([0, 1, 1]))

Note that the projector acts by default on all qubits.

At the moment one can implement this observable in two work-arounds:

1) Via the qml.Hermitian observable


from pennylane import numpy as np
import pennylane as qml

dev = qml.device("default.qubit", wires=2)

projector = np.zeros((4, 4))
projector[2,2] = 1

@qml.qnode(dev)
def circuit1(params):
    qml.templates.BasicEntanglerLayers(params, wires=[0, 1])
    return qml.expval(qml.Hermitian(projector, wires=[0, 1]))

params = np.array([[2.31, 1.22]])
print(circuit1(params)) # 0.274634968802943

2) By making use of the fact that this expectation is the i'th probability in the probs return value:


@qml.qnode(dev)
def circuit2(params):
    qml.templates.BasicEntanglerLayers(params, wires=[0, 1])
    return qml.probs(wires=[0, 1])

params = np.array([[2.31, 1.22]])

print(circuit2(params)[2]) # 0.274634968802943

Creating this new observable would involve creating the class itself (inheriting from qml.operations.Observable), and supporting it by the different devices (for example, in default.qubit one can implement it by selecting the right amplitude in the state vector and taking its absolute square; in the qiskit plugin one would have to translate the observable into the correct qiskit operation, etc).

co9olguy commented 3 years ago

See #1052

josh146 commented 3 years ago

See #1052

Ah!

I feel like this is subtly different though, as here we want to compute an expectation of a projector given a state and its gradients, whereas in #1052 the goal is to perform a projection of the quantum state on the device (two slightly different pathways). This is more analagous to qml.FockStateProjector(), which already exists in PL.

Although the UI might converge, in either case.

wongwsvincent commented 3 years ago

Hello! I have just started working on this issue and created a PR#1356. So far I have a brief draft that works for default.qubit, and I will still need to include checks to ensure correct inputs and tests for the Projector observable. Do you have any tips on what tests I should include? I only have input tests in my mind. And can you point some directions on how to implement for other devices, like qiskit?

josh146 commented 3 years ago

Thanks @wongwsvincent! Let us know by leaving a comment when it is ready for review 🙂

Regarding supporting multiple devices: one thing that could potentially be done is add support for this new observable directly in the parent QubitDevice class, in a hardware compatible manner. This way:

wongwsvincent commented 3 years ago

Sorry, @josh146 . I am a bit lost with your suggestion about implementing this under the QubitDevice class.

If I understand you correctly, you are saying we could add a function in pennylane/_qubit_device.py as a function of the QubitDevice class, like the functions marginal_prob. But I thought we were going to implement the Projector class as an observable class like the Hermitian class. In the PR, I have prepared a version of the latter case, which only works for default.qubit and I believe that's not what you are suggesting.

Could you give me more pointers? Should I create a function under the QubitDevice class and call it "projected_prob", or is it something else that you meant?

josh146 commented 3 years ago

Hi @wongwsvincent, let's move the discussion over to your PR, it will provide me with better context as to the changes you have made. I'll give it a preliminary review 🙂