Combined measurement results and error on numpy

I want to make combined measurement on the bell state, actually I don’t know if I implement the measurement correctly.

import pennylane as qml
from pennylane import numpy as np

dev = qml.device('default.qubit', wires = ['wire1', 'wire2'], shots = 1000)

@qml.qnode(dev, interface='autograd')
def circuit(params):
    qml.RX(params[0], wires = 'wire1') # Rx is applied with the 1st parameter, on the first qubit
    qml.RY(params[1], wires = 'wire1') # Ry is applied with the 2nd parameter, on the first qubit
    qml.CNOT(wires = ['wire1', 'wire2']) # CNOT is applied on the first and second qubit
    #my_circuit(wires = (1,0))
    return qml.sample(qml.PauliZ('wire1')), qml.sample(qml.PauliZ('wire2'))

Then I want to see if it is maximally entangled. I follow the tutorial for finding if the two qubits are maximally entangled and type the below codes:

result = circuit(params) 
result.shape
np.all(result['wire1'] == result['wire2'])

It returns the error:

only integers, slices (:), ellipsis (), numpy.newaxis (None) and integer or boolean arrays are valid indices

Can I keep the naming of the wires as string ?
Also, can I measure .sample(), expval(), var(), probs(wires) at the same time?

Thank you!

Hey @alanspace,

The following should work without an error:

result = circuit(params) 
result.shape
np.all(result[0] == result[1])

The result object returned by the circuit()is a NumPy array where the first axis corresponds to the quantities added to the return ... line, i.e., so that result[0] is the samples from the first wire. The way you were trying would be more suited to if result is a dictionary, but that is a bit harder to support with respect to all the differentiation frameworks.

Also, can I measure .sample(), expval(), var(), probs(wires) at the same time?

You can combine different measurement types as long as you do not measure a wire more than once. If you want to measure multiple quantities on a single wire, you can use the QNodeCollection class.

Also, regarding the problem you’re considering of maximally entangled Bell states - showing that something is entangled typically requires measurements in multiple bases. The code you shared measures in just the PauliZ computational basis. In that basis, even the unentangled |00> state will have the same set of samples from both qubits, i.e., np.all(result['wire1'] == result['wire2']).

The code block below shows how you can use a QNodeCollection to investigate entanglement:

import pennylane as qml
from pennylane import numpy as np

dev = qml.device('default.qubit', wires = ['wire1', 'wire2'])

def circuit(param, **kwargs):
    qml.RY(param, wires="wire1")
    qml.CNOT(wires = ['wire1', 'wire2'])

obs = [qml.PauliX('wire1') @ qml.PauliX('wire2'),
       qml.PauliY('wire1') @ qml.PauliY('wire2'),
       qml.PauliZ('wire1') @ qml.PauliZ('wire2')]
qnodes = qml.map(circuit, obs, dev)

print(type(qnodes))
print(qnode(0.0))
print(qnode(np.pi / 2))

Here we switch from using samples (which are not differentiable in PennyLane) to expectation values. We return three numbers giving the expectation values of joint measurements of XX, YY, ZZ. The GHZ state is entangled and our measurements should show a correlation in all bases: [1, -1, 1].