Last week we started with an easy challenge. This week it will be a little more difficult. A little. "How much more difficult?" you ask? Why don't we use a quantum circuit to calculate how hard it will be?
But before we continue with the next challenge, let's first recap the previous one and look at the solutions. I asked you to create a quantum system that reproduces a Bernoulli distribution with a given 𝑝. 𝑝 can be any real value between (including) 0 and 1. See the GitHub Discussion for more details.
A Bernoulli distribution describes a Boolean variable. It can have one out of two values. For instance 0 or 1. Therefore, it fits perfectly to a single qubit.
The tricky part of the challenge is how to make the qubit represent a specific distribution.
There are different ways to do this. First, you can specify the qubit state amplitudes during the initialization. But, you need to keep in mind that you specify amplitudes, not probabilities. If you start from the probabilities, you need to calculate their square roots to get the amplitudes. Generally, for a qubit 𝜓=𝛼|0⟩+𝛽|1⟩ we must assure that
|𝛼|^2+|𝛽|^2=1.
The following code snippet depicts that solution.
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
p = 0.3
qc = QuantumCircuit(1)
initial_state = [sqrt(1-p), sqrt(p)]
qc.initialize(initial_state, 0)
results = execute(qc,
Aer.get_backend('statevector_simulator')
).result().get_counts()
plot_histogram(results)
The second way uses the angle 𝜃 between the basis state |0⟩ and your qubit state vector |𝜓⟩. It controls the resulting amplitudes and thus the probabilities, too. The following snippet calculates 𝜃 for a given probability given the qubit you want to apply it on is in state |0⟩. You can use the 𝑅𝑌 gate to rotate the qubit state vector accordingly. Here's the respective code.
def prob_to_angle(prob):
return 2*asin(sqrt(prob))
p = 0.3
qc = QuantumCircuit(1)
qc.ry(prob_to_angle(p),0)
results = execute(qc,
Aer.get_backend('statevector_simulator')
).result().get_counts()
plot_histogram(results)
So, what do we do with a variable that is Bernoulli-distributed? How about this?
Let's say our variable represents whether a student can solve a challenge. 1 means yes. 0 means no. It can be any valid Bernoulli distribution. But we don't know it, yet. But, we know that if the student read my book Hands-On Quantum Machine Learning With Python, her chance of solving the challenge is 90%. Actually, I believe it is 100%. But that would seem a little too bold to say, wouldn’t it?
If she didn't read the book, her chance to solve the challenge is 50%.
The question is: "What is the absolute (marginal) probability of students solving the challenge if 60% of the students read the book?"
Please post your answer to the GitHub Discussion here. There, you'll also find some hints—just in case you didn't read the book. ... well, how this changes the probability looks like another interesting question to ask ;-)