@qml.qnode(dev)
def measure_basis(initial_state):
# Initialize the qubit in the given state
qml.StatePrep(initial_state,wires=range(2))
return [qml.sample(qml.PauliX(0)), qml.sample(qml.PauliZ(1))]
A quick question!
Does this code generate samples by first performing a measurement in X basis on the first qubit? Then again, creating state and performing Z measurement?
OR
This performs an X measurement on the first qubit, then performs a Z measurement on the resulting collapsed state?
I believe the latter is true, but I just wanted to confirm.
Thank you!
Hi @mdaamir ,
It’s the latter. The measurements are queued up for execution in order, so they will be sent to the device in the order that you define in your code.
The following example should illustrate this point
import pennylane as qml
import numpy as np
dev = qml.device("default.qubit", shots=5)
@qml.qnode(dev)
def circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0,1])
return [qml.sample(qml.PauliZ(0)), qml.sample(qml.PauliZ(1)), qml.probs()]
circuit()
Out:
[array([-1., -1., 1., 1., 1.]),
array([-1., -1., 1., 1., 1.]),
array([0.6, 0. , 0. , 0.4])]
In the circuit above we’re creating a Bell state. As you can see the samples for qubits 0 and 1 correspond to each other at each point. This is exactly what we would expect since we have an approximately 50-50 chance of having both eigenvalues being -1 or 1, but 0% probability of having a mix of both for a single shot.
Let me know if this clarifies things. I hope this helps!
1 Like