New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CV Expectation value NumberState for Classification #130

Closed
chMoussa opened this Issue Nov 15, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@chMoussa
Copy link

chMoussa commented Nov 15, 2018

Hello,

I want to write a CV classifier. To get probabilities in the context of binary classification,
we would need two expectation values. For instance, get the Fock probability of [0,1] and [1,0] outcomes and normalize like :
p0 = qml.expval.cv.NumberState(np.array([1,0]),wires=[0,1]) p1 = qml.expval.cv.NumberState(np.array([0,1]),wires=[0,1]) return p1 / (p0+p1 + 1e-10)

However, I am not able to do so because :

  • QuantumFunctionError: Each wire in the quantum circuit can only be measured once.
  • TypeError: unsupported operand type(s) for +: 'NumberState' and 'NumberState'

How can we do so currently?

@josh146

This comment has been minimized.

Copy link
Member

josh146 commented Nov 16, 2018

Hi @chMoussa. While QNodes can contain quantum functions that are constructed similarly to Python functions, there are some important restrictions:

  1. Quantum functions must only contain quantum operations, one operation per line, in the order in which they are to be applied,
  2. Quantum functions must return either a single or a tuple of expectation values, with one expectation value per wire,
  3. Quantum functions must not contain any classical processing.

In the example you have posted, this breaks the above restrictions in a few ways:

  • Across the two expectation values, wires 0 and wires 1 are measured twice. This is not allowed, as it does not map to physical hardware devices.

  • return p1 / (p0+p1 + 1e-10) is also invalid, as it involves classical processing within the QNode.

One solution is to use a combination of two QNodes, one for each expectation value you wish to measure, alongside a classical node for post-processing:

@qml.qnode(dev)
def p0(x):
    # quantum operations
    return qml.expval.cv.NumberState(np.array([1, 0]),wires=[0, 1])

@qml.qnode(dev)
def p1(x):
    # quantum operations
    return qml.expval.cv.NumberState(np.array([0, 1]),wires=[0, 1])

def postprocessing(x):
    return p1(x)/(p0(x) + p1(x) + 1e-10)
@chMoussa

This comment has been minimized.

Copy link
Author

chMoussa commented Nov 16, 2018

Hi @josh146 I understand better now. Thanks.

@chMoussa chMoussa closed this Nov 16, 2018

@josh146

This comment has been minimized.

Copy link
Member

josh146 commented Nov 16, 2018

Hi @chMoussa, just letting you know we now have a PennyLane discussion forum: https://discuss.pennylane.ai.

Feel free to post your PennyLane usage questions there, and we will aim to answer as soon as possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment