“Can the student solve the problem?”
A 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%. 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?"
Before we solve this problem, we should get clear about what the problem description tells us. If we strip away my poor attempt to promote my book (apologies for that), we are left with a set of probabilities.
Specifically, we got two conditional probabilities:
The student solves the problem in 90% if she read the book.
She solves the problem in 50% if she didn’t.
And, we got one marginal probability:
60% of the students read the book
If you need a little refresh on these different types of probabilities and how to model them in a quantum circuit, you may want to read these two posts (Quantum Programming — For Non-Mathematicians and Quantum Computing In Practice).
Let’s now implement these probabilities. We start with a little preparation. We import the libraries, define a useful function prob_to_angle
, and create a quantum circuit with two qubits.
from qiskit import QuantumCircuit, Aer, execute
from qiskit.visualization import plot_histogram
from math import asin, sqrt
def prob_to_angle(prob):
return 2*asin(sqrt(prob))
qc = QuantumCircuit(2)
Next, we let the first qubit (at position 0) represent the marginal probability of 60% of the students have read the book. Therefore, we use the RY-gate with the result of calling the prob_to_angle
function with the intended probability as a parameter.
qc.ry(prob_to_angle(0.6),0)
We can directly continue with modeling the conditional probability if the student has read the book. This is the case if the first qubit is 1. We apply a rotation that represents 90% on the second qubit only if the first qubit is 1. This is what the controlled RY-gate does.
qc.cry(prob_to_angle(0.9),0,1)
To apply the conditional probability if the student did not read the book, we first have to flip the amplitudes of the first qubit by applying the X-gate on it. The subsequent X-gate reverts this effect.
qc.x(0)
qc.cry(prob_to_angle(0.5),0,1)
qc.x(0)
Let’s look at the complete circuit.
When we run this circuit, we see the following probability distribution.
results = execute(qc,
Aer.get_backend('statevector_simulator')
).result().get_counts()
plot_histogram(results)
The upper right-hand number represents the value of the first qubit. It represents whether the student read the book. The lower left-hand qubit represents whether the student solves the problem. To answer the question for the overall (marginal) probability of solving the problem, we have to sum all states where the second qubit is 1.
Alternatively, we could simply measure the second qubit. The following listing depicts the full source code.
qc = QuantumCircuit(2, 1)
qc.ry(prob_to_angle(0.6),0)
qc.cry(prob_to_angle(0.9),0,1)
qc.x(0)
qc.cry(prob_to_angle(0.5),0,1)
qc.x(0)
qc.measure(1,0)
results = execute(qc,
Aer.get_backend('qasm_simulator'), shots=1000
).result().get_counts()
plot_histogram(results)
The small difference between this result and the sum of the previous simulation is due to the empirical nature of the qasm_simulator
we used.