# Probability() usage

**URL:** <https://discuss.pennylane.ai/t/probability-usage/284>\
**Category:** PennyLane Help\
**Created:** [November 30, 2019, 12:16pm UTC](https://discuss.pennylane.ai/t/probability-usage/284 "2019-11-30T12:16:48Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [November 30, 2019, 12:16pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/1 "2019-11-30T12:16:48Z")

</div>

According to the documentation, there is a class probability() which will return probabilities of each computational basis state.  
Can anybody enligthen me with an example? I cant get it working after some attemps

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [November 30, 2019, 3:47pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/3 "2019-11-30T15:47:30Z")

</div>

Hi @Miguel_Fernandez!

There is currently an [open PR on our GitHub repo](https://github.com/XanaduAI/PennyLane/pull/432), adding a new `prob()` measurement function. You can install this branch using `pip`:

```nohighlight
pip install git+https://github.com/XanaduAI/PennyLane.git@prob_fn

```

Once this branch is installed, you can import this new experimental measurement function and use it in a QNode like so:

```python
import numpy as np
import pennylane as qml
from pennylane.beta import prob

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(wires=0)
    qml.RY(x, wires=0)
    qml.RX(x, wires=1)
    qml.CNOT(wires=[0, 1])
    return prob(wires=[0])

```

**Note:**

- `prob()` must accept a `wires` argument, which specifies for which wires the marginal probability is returned.

- This feature is experimental, and might change before the next release!

- Currently, it only supports the `default.qubit` device, and the PennyLane-qiskit plugin under some modification, but additional device support will be coming soon.

---

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [December 1, 2019, 5:40pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/4 "2019-12-01T17:40:22Z")

</div>

Thanks Josh, that made the trick!

---

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [December 19, 2019, 6:07pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/5 "2019-12-19T18:07:19Z")

</div>

I am running prob() in a known algorithm, and the results I get are consistent with basis vectors ordering {00,10,01,11}, while I am used to {00,01,10,11}. Is this a desired feature, a bug, or its just by happenstance that I’ve seen this second ordering?

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 19, 2019, 6:58pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/6 "2019-12-19T18:58:29Z")

</div>

Thanks for the update @Miguel_Fernandez — let us look into this and double check the ordering!

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 20, 2019, 5:46pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/9 "2019-12-20T17:46:21Z")

</div>

Hi @Miguel_Fernandez, I’ve tried the following minimal example,

```python
dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev)
def circuit():
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])
    return prob(wires=[0, 1])

# expected probability, using [00, 01, 10, 11]
# ordering, is [0.5, 0.5, 0, 0]

res = circuit()
expected = np.array([0.5, 0.5, 0, 0])
assert np.allclose(res, expected, atol=tol, rtol=0)

```

and it seems to agree with `[00, 01, 10, 11]` ordering:

```auto
>>> import numpy as np
>>> H = np.array([[1, 1], [1, -1]])/np.sqrt(2)
>>> CNOT = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
>>> state = CNOT @ np.kron(np.eye(2), H) @ np.array([1, 0, 0, 0])
>>> expected = np.abs(state)**2
>>> expected
array([0.5, 0.5, 0. , 0.])

```

Could you share a small example that shows `{00,10,01,11}`? It might be a edge-case related bug in the code that we are not taking into account.

---

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [December 21, 2019, 6:15pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/10 "2019-12-21T18:15:44Z")

</div>

Thanks for looking into this. When these discrepancies arise, its normally my fault. I am double-debugging my code, but I cant catch it yet. Will let you know in any sense

---

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [December 22, 2019, 6:07pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/11 "2019-12-22T18:07:17Z")

</div>

The first screenshot is a toy Quantum Phase Estimation in Qiskit. The second the same in PennyLane. The only difference being my implementation of the cu1 gate, its matrix definition also included

 ![Captura1](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/facbe2f567b81b35ff31814360bee14ee5272e20.jpeg)  
 ![Captura2](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/1X/b0be9881fe30516712f7151bd88576d7ea1e2912.jpeg)

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 23, 2019, 3:47am UTC](https://discuss.pennylane.ai/t/probability-usage/284/12 "2019-12-23T03:47:55Z")

</div>

Hi @Miguel_Fernandez, as odd as it looks, I believe PennyLane and Qiskit are returning the exact same result.

The disagreement results from Qiskit not implementing the same convention as commonly seen in quantum information papers and textbook; typically, the top qubit (labelled 0) in a circuit is the _leftmost_ integer within the ket; |q\_0 q\_1 \cdots q\_{n-1} q\_n\rangle.

For example, for a 2-qubit system originally in the ground state |00\rangle, applying an X gate to qubit 0 in this convention gives |10\rangle.

This can be observed in PennyLane:

```python
@qml.qnode(dev)
def func():
     qml.PauliX(wires=0)
     return qml.probs(wires=0)

>>> func()
array([0., 0., 1., 0.])

```

where we are using the ordering (|00\rangle, |01\rangle,|10\rangle,|11\rangle).

Qiskit instead [uses the convention](https://github.com/Qiskit/qiskit-terra/issues/1148) such that the top qubit (qubit 0) is now the _rightmost_ integer within the ket: |q\_{n-1}q\_{n}\cdots q\_1q\_0\rangle. In this convention, applying an X gate to qubit 0 gives |01\rangle.

Running the exact same program in Qiskit, we can see this reversal:

```python
>>> q = qiskit.QuantumRegister(2, 'q')
>>> c = qiskit.ClassicalRegister(2, 'c')
>>> qpe = qiskit.QuantumCircuit(q, c)
>>> qpe.x(q[0])
>>> backend = qiskit.BasicAer.get_backend('statevector_simulator')
>>> job = qiskit.execute(qpe, backend)
>>> result = job.result()
>>> result.get_statevector().real
array([0., 1., 0., 0.])

```

where we are again using the ordering (|00\rangle, |01\rangle,|10\rangle,|11\rangle).

To convert from the Qiskit convention to the more standard/textbook convention (as used by PennyLane), all that needs to be done is reverse the basis state or permute the statevector/probabilities 🙂

---

<div class="post-metadata">

**Author:** ![Miguel\_Fernandez](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/miguel_fernandez/32/88_2.png) [@Miguel\_Fernandez](https://discuss.pennylane.ai/u/Miguel_Fernandez)\
**Post date:** [December 26, 2019, 6:34pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/13 "2019-12-26T18:34:37Z")

</div>

Hi Josh, many thanks!  
Quite disturbing, I need to revise all my qiskit work until now

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 26, 2019, 8:12pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/14 "2019-12-26T20:12:47Z")

</div>

No worries Miguel! Note that I noticed a couple of typos in my comment, and I’ve just updated them to be correct.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [December 26, 2019, 8:21pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/15 "2019-12-26T20:21:52Z")

</div>

Note also that this is why the matrix representation of the CNOT gate in Qiskit is non-standard:

CNOT=\begin{bmatrix}1& 0 & 0& 0\\ 0 & 0& 0& 1\\0& 0 & 1& 0 \\ 0 & 1& 0 & 0\end{bmatrix}

Compare this to the standard CNOT representation using the convention used in PennyLane/textbooks:

CNOT=\begin{bmatrix}1& 0 & 0& 0\\ 0 & 1& 0& 0\\0& 0 & 0& 1\\ 0 & 0& 1 & 0\end{bmatrix}

---

<div class="post-metadata">

**Author:** ![RonMeiburg](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@RonMeiburg](https://discuss.pennylane.ai/u/RonMeiburg)\
**Post date:** [July 1, 2020, 12:46pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/16 "2020-07-01T12:46:31Z")

</div>

Hi Josh,

it appears that:  
! There is currently an [open PR on our GitHub repo](https://github.com/XanaduAI/PennyLane/pull/432), adding a new prob() measurement function. You can install this branch using pip: pip install git+https://github.com/XanaduAI/PennyLane.git@prob\_fn Once this branch is installed, you can import this new experimental measuremen…

no longer works, and prob() is also not part of the 0.10 release, at least not that I can find. What’s the status?

---

<div class="post-metadata">

**Author:** ![RonMeiburg](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@RonMeiburg](https://discuss.pennylane.ai/u/RonMeiburg)\
**Post date:** [July 1, 2020, 12:49pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/17 "2020-07-01T12:49:20Z")

</div>

Ignore that question. It is probs() and part of the main branch

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [July 1, 2020, 1:56pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/18 "2020-07-01T13:56:35Z")

</div>

Hi @RonMeiburg, yes, it got renamed to `qml.probs()` before the pull request was merged!

Here is a link to the documentation in the latest stable version of PennyLane: [https://pennylane.readthedocs.io/en/stable/code/api/pennylane.probs.html](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.probs.html)

---

<div class="post-metadata">

**Author:** ![RonMeiburg](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@RonMeiburg](https://discuss.pennylane.ai/u/RonMeiburg)\
**Post date:** [July 1, 2020, 7:17pm UTC](https://discuss.pennylane.ai/t/probability-usage/284/19 "2020-07-01T19:17:35Z")

</div>

Thanks, I found the docs
