Building a coin-flipping experiment
If you've ever taken a course in probability and statistics, you might have seen the coin flip example. In this example, you are given an unbiased coin to flip multiple times and track the results of each flip (experiment) as either heads or tails. What this experiment illustrates is that with an unbiased coin and enough samples, you will see that the probability of either heads or tails start to converge to about 50%.
This means that, after running a sufficient number of experiments, the number of times the coin lands on heads becomes very closely equal to the number of times that it lands on tails.
Let's take a moment to make an important note regarding the previously stated analogy with respect to the reality of the preceding experiment. It has been proven that in many ways, any coin could be easily made biased so that when it is flipped, it can land on the same side each time.
That being said, I want to ensure that this is a basic example of an attempt to create a classical analogy of a quantum computing principle in order to get an understanding of the experiment we will be creating, and not to insinuate that this classical experiment equates to a quantum experiment. I will cover these differentiations as we create the next circuit that will simulate flipping a coin over 1,000 times. Let's give this a try:
- Open the Composer Editor and create a new blank circuit.
- Click and drag the Hadamard (H) gate onto the first qubit.
- Click and drag the measurement operation onto the first qubit after the H gate. This will indicate that you wish the value of this qubit to be measured, and assign its resulting value of either 1 or 0 to the corresponding classic bit; in this case, the bit at position 0, as shown in the following screenshot:
- Name your circuit as Coin flip and save it.
- Click Run Settings to expand the options.
- Select the ibmq_qasm_simulator as the backend device and select the run count to 1024. This will run the experiment 1,024 times.
- Click Run on ibmq_qasm_simulator.
- Once completed, click on the completed experiment in the Completed jobs list.
The results will now show two different states. Remember that the Computational basis states are represented along the x axis. The main difference you will now see is highlighted by the first classic bit of the experiment (the least significant bit on the far right of each state), which you can see is either a 0 or 1.
Another thing to note is the Probabilities (the y axis) of each of the two states. This will differ each time you run the experiment. For example, the results in the following screenshot will have a different result for the probability than your experiment:
That being said, one thing you will notice from the preceding screenshot is that the results will fall fairly close to 50% each time you run the experiment. Rerun the experiment a few more times and examine the results for yourself.
The reason for this is our use of the Hadamard gate. This special gate leverages one of the two main quantum computing principles, superposition, that provides quantum computers with the potential to solve complex computations. We will cover what and how superposition works in Chapter 4, Understanding Basic Quantum Computing Principles, and how the Hadamard gate performs this gate operation on the qubit in Chapter 6, Understanding Quantum Logic Gates.
The use of the Hadamard gate, as you can see, allows your circuit to execute itself by leveraging a linear combination of two states, 0 and 1. As mentioned earlier, this helps to leverage superposition.
The second quantum computing principle used by quantum computers is entanglement. This quantum mechanical phenomenon helps us to entangle two or more qubits together. By entangling two qubits, we are in essence linking the value of one qubit and synchronizing it with another qubit. By synchronizing it, we mean that if I measure (observe) the value of one of the entangled qubits, then we can be sure that the other qubit will have the same value, whether you measure it at the same time or sometime later. The next experiment will cover this in more detail.
Entangling two coins together
Let's extend our coin-flipping example to include superposition by adding another coin and entangling them together so that when we run our experiment, we can determine the value of one coin without having to measure the other.
In the same way as our previous experiment, each qubit will represent a coin. In order to do this, we will use a multi-qubit gate called a Control-Not (CNOT) gate (pronounced see-not). The CNOT gate connects two qubits, where one is the source and the other the target. We will cover these gates in detail in Chapter 6, Understanding Quantum Logic Gates, but for now, here is a brief introduction so you can understand what you will expect to see.
When the source qubit (the qubit that is connected to the source of the CNOT gate) has a value equal to 1, then this enables the target of the CNOT, which as we can tell by the name is a NOT gate. This gate performs the same operation as the X gate that we ran in our previous first experiment, where we flipped the value of the qubit. Therefore, if the target qubit was set to 0, then it would flip the target qubit to 1 and vice versa. Let's try entangling our coins (qubits) to see how this works:
- Open the Circuit Composer and create a new blank circuit.
- Click and drag a Hadamard (H) gate onto the first qubit.
- Click and drag the CNOT gate onto the first qubit (round white gate with crosshairs on blue background). This will drop the source onto the first qubit. When selecting the CNOT gate, the first qubit you drop it on will be set as the source. Visually, the source of the CNOT gate is a solid dot on the qubit to which the gate was dragged (see Figure 2.21).
By default, the target will set itself to the next qubit. In this case, it will drop to qubit 2. Visually, the target for a CNOT is a large dot with a cross in the middle, made to resemble a target.
- Click and drag a measurement operator onto each of the two first qubits as shown in the following screenshot:
- Title and save your experiment as Entangled coins.
- Click Run Settings on the circuit to launch the Run Settings dialog.
- Select the ibmq_qasm_simulator or any other device from the backend selection as the backend device and select the run count to 1024. This will run the experiment 1,024 times.
- Click Run on ibmq_qasm_simulator (or whichever device you selected in the previous step).
- Once completed click the Coin flip experiment from the Completed jobs list.
Now let's review the results and see what happens when we entangle two qubits:
As you can see in the preceding screenshot, the results still have two states, as they did in the previous experiment. However, one thing to observe here is the results of the two qubits. Note that the state of both qubits is either 000 or 011. Recall that the third bit (the most significant bit) was not operated on, so it remains in the initial state of 0.
What makes this experiment interesting is when we flipped one coin in the previous experiment, you saw that the results were 50% (0 or 1). However, now we are running the same experiment, but we are entangling another coin. In effect, this results in both coins becoming entangled together and thus their states will always be the same as each other. This means that if we flip both coins and we observe one of the coin values, then we know that the other entangled coin will be the same value.