Systematic Preparation of Arbitrary Probability Distribution with a Quantum Computer

Yuma Nakamura
Qiskit
Published in
9 min readJul 27, 2021

Hello, Qiskit Developers! I’m Yuma Nakamura, a Data Scientist at IBM in Japan. I’ve been exploring quantum computing for a while now, and like many of you, I’m interested in transitioning to a role as a formal quantum data scientist.

For my Qiskit Advocate Mentorship Program project, I investigated quantum strategies for enhancing the Monte Carlo techniques used in Finance. You can learn more about our project here. This study taught me some useful lessons about quantum finance application, and I want to share those with you in this blog post.

In finance, data scientists often find themselves attempting to figure out how to handle a probabilistic process — for example, the pricing of an option — where the value of some financial market product depends on the price of an underlying asset. A quantum computer is able to assist in this kind of task because it can output results with a certain probability. That is to say, quantum computers can assign a probability to each possible quantum state (also called basis states).

At a high level, these probability distributions enable us to connect finance with quantum computing. However, to make them useful for finance applications, it is important that we first understand how to prepare and utilize a particular probability distribution produced by a quantum circuit.

This raises three important questions, each of which has been explored in recent research papers.

(1) How do you systematically prepare a probability distribution?
(2) How do you efficiently prepare a probability distribution?
(3) How do you make practical use of this probability distribution in finance applications like option pricing?

Each one of these questions is important enough to warrant its own article. For this blog post, I will focus on answering the first one.

Using quantum computers to systematically prepare a probability distribution

If you’ve read this far, then chances are you already know that circuits in classical computing are innately deterministic — meaning that if you run the same computation on a classical circuit more than once, you’ll always get the same output.

Quantum computers, of course, are a little different. The nature of quantum mechanics is such that the measurement outcome from a quantum circuit is usually randomly determined based on a probability distribution. Thus, it is often necessary to repeat measurements multiple times before we can obtain a distribution over the possible measurement outcomes.

For example, let’s say we have a 2-qubit system where both qubits are in superposition. We know that we can get probabilities of the occurrence of each possible quantum state: |00⟩, |01⟩, |10⟩ and |11⟩. Taken altogether, these probabilities can be interpreted as a probability distribution over the possible basis states. By performing quantum operations in a certain way, we can create a quantum state that expresses a particular distribution, e.g., a normal distribution, log-normal distribution, and so on.

This may sound simple enough (or maybe not!), but it leaves us with a crucial question. How can we create quantum states such that, when we sample from them, we get the distribution we want? For example, how would we create a quantum state that produces a log-normal distribution?

In 2002, famed computer scientist Lov Grover published a paper entitled “Creating superpositions that correspond to efficiently integrable probability distributions,” which describes an algorithm that allows for the preparation of probability distributions. In this blog post, I will explain how to encode a simplified illustration of that algorithm in Qiskit. (Note: This one is different from “Grover’s algorithm,” the famous database search algorithm published in 1996.)

Creating a quantum state that produces a log-normal distribution

Let’s start by taking the log-normal distribution used in the Qiskit Finance Tutorial as our example.

The Qiskit Finance Tutorial represents the probability distribution 𝑓(𝑥) of the stock price 𝑥 at some point in the future (T days later). The log-normal distribution is represented by the following equation:

where 𝜇 and 𝜎 are the mean and standard deviation of this probability distribution. In the case of the distribution of stock prices:

The constants used to represent the mean 𝜇 and standard deviation 𝜎 are set in the Qiskit Finance Tutorial as follows

In this case, a common model used to understand pricing is called the Black-Scholes model which yields 𝜇 and 𝜎 as follows:

mu: 0.6898595093270685
sigma: 0.13241694217637887

Our goal is to reproduce a discretized version of this probability distribution using qubits. Here, we try to reproduce it with three qubits, as shown in the figure below. The horizontal axis denotes the price of the underlying asset, which is discretized and assigned to basis states 000, 001, …, 111.

Since the quantum distribution is discretized, we truncate the distribution to the desired range and set the number of bins in the histogram to be 2 for n qubits. Now, let the probability of the measurement represented by each bin be designated as P₀₀₀, P₀₀₁,…, P₁₁₁.

This figure is quite easy to make using the module qiskit.circuit.library.LogNormalDistribution. As an alternative, you can use the module np.random.lognormal(mean=mu, sigma=sigma, size=N) by sampling and post-processing to align the range and number of bins.

Now we will explain how to create the probability distribution in the three-qubit representation shown in the above figure. The quantum state we want to create is:

To create this state, we will create a single-qubit representation |Ѱ₁⟩ and a two-qubit representation |Ѱ₂⟩.

Here, a single-qubit representation is a representation of a probability distribution aggregated into two bins, while a two-qubit representation is a representation aggregated into four bins. The probabilities Pᵢ and Pᵢⱼ are related to the desired three-qubit representation as follows:

Pᵢ : Probability that the observed value of the first of three qubits is i

Pᵢⱼ : Probability that the observed value of the first and second of three qubits is ij

Specifically, the relationship between Pᵢ, Pᵢⱼ, and Pᵢⱼₖ is as follows:

Thereafter, we will create|Ѱ₂⟩ based on |Ѱ₁⟩, and then create|Ѱ₃⟩ based on|Ѱ₂⟩.

Sigle-qubit representation

Now let’s see how we would go about representing the previous distribution with just one qubit. (This is depicted in the left figure below.) To start, we aggregate the three-qubit bins and represent P₀ and P₁.

To reproduce the distribution of this figure in a quantum circuit, we use RY gates as shown in the above figure on the right, where RY gates correspond to the following matrix:

where 𝜃 is set to:

In this way,

This gets us then get the state we want.
The corresponding code will look like this:

Two-qubit representation

Now let’s expand from a single-qubit representation |Ѱ₁⟩ to a two-qubit representation |Ѱ₂⟩. To prepare this distribution, we must split the coefficient P₀ of |0⟩ and assign the split values to |00⟩ and |01⟩ with the ratio P₀₀:P₀₁. Then we split the coefficient P₁ of |1⟩ and assign those values to |10⟩ and |11⟩ with the ratio P₁₀:P₁₁. This can be expressed as an extension of the one-qubit representation using the controlled RY gate as shown below. The rotation angles for the first and second controlled RY gates are 𝜃₁ and 𝜃₂, respectively, as shown in the figure.

The first controlled RY(𝜃₁) sandwiched between the X gates generates the state cos(𝜃₁/2)|00⟩+sin(𝜃₁/2)|01⟩ when the first qubit is |0⟩. Similarly, the second controlled RY(𝜃₂) generates the state cos(𝜃₂)|10⟩+sin(𝜃₂)|11⟩ when the first qubit is |1⟩.

Let us calculate this quantum state.

That’s the state we wanted!! Keep in mind that the absolute value squared of these coefficients corresponds to the probabilities of the possible basis states. Clearly, this is exactly the probability distribution we want.

The code for constructing the circuit and plotting the histogram above is as follows:

Three-qubit representation

Finally, let’s expand the distribution from two qubits to three qubits. In this case, we set the angle of the controlled RY gates as follows:

Now let’s take a look at what the quantum state looks like after this operation has been performed. (For brevity’s sake, I’m omitting some of the more detailed calculations):

Now, we can reproduce the desired state as above. I’ll post the code here as well. Note that Qiskit does not have built-in functions for multi-controlled RY gates (CCRY), so implementation requires a few clever workarounds.

And there you have it! If you’ve reached this point in the article, then you’ve learned just about everything you need to know to get started with the systematic preparation of your desired probability distribution. You can also use the method outlined in this article so far to construct the GHZ state |000⟩+|111⟩ without being forced to rely on intuition or sudden inspiration.

Gate Cost

Of course, nothing is perfect, and there is at least one significant disadvantage to this particular method of probability distribution preparation — namely, gate cost.

So far, we’ve looked at examples using one, two, and even three qubits, all of which are pretty manageable. The problem is that the number of gates required increases exponentially as the number of qubits increases.

To see what this looks like in action, let’s use Qiskit’s Transpiler to decompose the above three circuits into 𝑈₃ and CX gates, and count the number of gate operations.

OrderedDict([(‘u3’, 1), (‘measure’, 1)])
OrderedDict([(‘u3’, 7), (‘cx’, 4), (‘measure’, 2)])
OrderedDict([(‘u3’, 95), (‘cx’, 52), (‘measure’, 3)])

Since this is pretty inefficient in terms of computational cost, we still need to rely on a bit of good old-fashioned intuition and to think of strategies for reducing the number of gates. There is at least one method, known as quantum GAN, that has been proposed as a solution to the problem of reproducing probability distributions. However, we’ll save that subject for another article.

Conclusion

Now we know how to reproduce an arbitrary probability distribution on a quantum computer. The target probability distribution is truncated within the desired range and discretized with a certain number of bins, which corresponds to the number of qubits that will be used.

From there, using controlled RY gates, we can represent the distribution in these bins on a quantum computer by splitting them into 2 bins, where 𝑛 is the number of qubits. Even though we have inevitably approximated the distribution by truncation and discretization, we can asymptotically reproduce the exact distribution by increasing the number of qubits.

Since arbitrary probability distributions can be represented in this way, this may be a useful technique for quantum computing contests such as the Qiskit Challenge! Of course, as we mentioned before, this algorithm scales exponentially, so you’ll want to keep that in mind when considering whether or not to use it.

Thank you for reading this post and thanks to Amira Abbas for mentoring me through the Qiskit Advocate Mentorship program. Thanks also to Stefan Woerner, Ryan Mandelbaum, and Robert Davis for their advice on this blog post. See you next time!

Learn more about Qiskit Finance here, and for more stories like this, subscribe to the Qiskit Medium!

References

  1. Lov Grover and Terry Rudolph, “Creating superpositions that correspond to efficiently integrable probability distributions”, arXiv:quantph/0208112 (2002).
  2. https://qiskit.org/documentation/tutorials/finance/03_european_call_option_pricing.html

--

--