About Bell states

Hi, I am a newbie in this QC area. If I want to produce |βxy>, how could I change the default input(x=0, y=0)
|βxyi =(|0, y> + (−1)^x |1, 1-y>)/√2


in general a Bell state can be obtained by applying a Hadamard gate to one qubit and then use this qubit as the control of a CNOT gate whose target is the other qubit you want to generate. For example, if you start with |0,0\rangle and apply these operations, you get

|\Psi_+\rangle = CNOT_{1,2} H_1 |0,0\rangle = (|0,0\rangle + |1,1\rangle)/\sqrt{2}.

If you want to generate all the other Bell states, the only thing you have to do now is to choose different default inputs such as (x=0, y=1). This one you can obtain it by doing a flip operation (apply X operator) over the second qubit.

If you need any other help about the implementation, or any other question, I’ll be glad to help you :slightly_smiling_face:

Thank you! I have solved this problem this morning. Would you mind explaining more about qml.sample and qml.prob (I want to know the mathematics expressions since I am studying QC these days and trying to practice more.)


With qml.sample basically what you are doing is a measurement in the computational basis, i.e. of the Z operator. So assume that you have defined your device in such a way that it does a single shot (in qml.device write shots = 1) and your state is the Bell state I wrote before. Then, according to that probability, it will give you either a list [1,1] corresponding to state |00\rangle or [-1,-1] corresponding to state |11\rangle. Of course, this is true if you tell the circuit that you want measures over both qubits.

On the other hand, qml.probs gives you the exact probability distribution obtained at the output, independently on the number of shots that you are doing. For this simple case with two qubits, you can get four different outcomes which are \{|0,0\rangle, |0,1\rangle, |1,0\rangle, |1,1\rangle\}. Thus, for the entangled state I wrote before, you’ll get a list of the form [0.5, 0, 0, 0.5] which is telling you that your final state has the form
|\psi\rangle = \sqrt{0.5} |0,0\rangle + \sqrt{0.5} |1,1\rangle.

The position in the list of each probability given by qml.probs tells you which is the state with the associated probability. According to Python’s way of numbering elements in a list, we have the 0.5 results in elements 0 and 3, and the binary representation of these elements is 00 and 11 respectively. Likewise, we have probabilities 0 for elements 1 and 2 in the list, and the binary representation of these two numbers is 01 and 10 respectively.

Hope this helps yous!

Hey 3Db,

Just adding to Javier’s excellent explanation.

As opposed to the standard expval measurement, which is just the expectation value \langle \psi | M | \psi \rangle of an observable M with respect to a quantum state | \psi \rangle, qml.sample and qml.probs are indeed somewhat different beasts. Let me try to explain:

  • qml.probs returns an array of probabilities of measuring the computational basis states. Mathematically speaking, the i'th entry of the array is given by the absolute square of the inner product between the i'th computational basis state and the current quantum state of the system, p_i = |\langle i | \psi \rangle |^2. On hardware, this function is implemented by measuring the PauliZ observable of all qubits several times and estimating the probability from the frequency of how often a specific result occured. For example, to estimate the probability of a single qubit state |\psi \rangle to be in | 0 \rangle or |1 \rangle, we can measure the PauliZ observable 5 times and get the results [-1, 1, 1, 1, -1], which means we found the qubit in states 1, 0, 0, 0, 1. From here we estimate p_0 =|\langle 0 | \psi \rangle |^2 = 3/5 and p_1 =|\langle 1 | \psi \rangle |^2 = 2/5.

  • qml.sample returns samples of measurement results, which are the eigenvalues of the observable. As @Javier said, if the observable is a PauliZ operator, the eigenvalues are -1 and 1. If you specified multiple observables as in qml.sample(qml.PauliZ(wires=0), qml.PauliZ(wires=1)) you will get multiple arrays of such samples, one for each observable. This measurement basically means “run the circuit several times and return the (stochastic) measurement result”. With hardware, expectations and probabilities (as shown above) are actually computed by post-processing measurement samples - so this is in a sense the most basic or “realistic” measurement. Mathematically speaking, the samples are drawn from a probability distribution p_{\lambda_i} = |\langle \xi_i | \psi \rangle|^2, where |\xi_i \rangle is the i'th basis state of the observable’s eigenbasis. (For PauliZ the eigenbasis is of course the computational basis \{ | i \rangle \} ).

Hi Javier! Thank you so much for your explanation! I have understood! Thank you!

Hi Maria, Thank you soooo much! I have figured out! Hope it can be added to the API book. Very clear! Thank you again!

Good idea, I will update the docstrings of the measurement functions.

Brilliant! Thank you~