Returning NumberState expectation values for classification

From GitHub issue #130 by @chMoussa:

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?

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)