Path: blob/main/quantum-applications-to-finance/solutions/02_quantum_walks_solutions.ipynb
1154 views
Quantum Walks for Finance Part 2: Learnable Quantum Walk in Financial Simulation
Overview
Efficiently loading classical data into quantum states is a fundamental challenge in quantum computing, underpinning a wide range of applications from quantum machine learning to financial modeling. For instance, encoding a probability distribution such as the historical closing prices of a stock over a fixed time period can be particularly demanding. In this notebook, you will adapt the Discrete Time Quantum Walk into a variational multi-Split Step Quantum Walk, capable of modeling the trading patterns of multiple investors for a specific stock. You will then optimize the walk’s parameters to effectively load the given probability distribution into a quantum state.
What you'll do:
Experiment with the Split Step Quantum Walk (SSQW), which is a generalization of the Discrete Time Quantum Walk (DTQW) introduced in Lab 1.
Add variational gates for the coin operators to create the multi-Split Step Quantum Walk (mSSQW)
Use a classical optimizer to identify optimal parameters in the mSSQW that will generate a good approximation to the targeted log-normal probability distribution.
The notebook is organized as follows:
Section 1: Introduction to the context and problem
Section 2: Split Step Quantum Walk
Section 3: Multi-Split Step Quantum Walk
Pre-requisites: Familiarity with discrete time quantum walks, such as the material in Quantum Walks for Finance Part 1.
🎥 You can watch a recording of the presentation of a version of this notebook from a GTC DC tutorial in October 2025.
Let's begin by installing and importing the necessary packages:
We'll reuse some code from Quantum Walks for Finance Part 1 which we've copied in the code block below. This block contains a helper function for graphing the results of the SSQW and the functions to prevent one-step transitions between the states and .
Make sure to execute the cell below.
Below we've modified some of the code from Quantum Walks for Finance Part 1 that we will repurpose here. In particular, we'll replace the INC and DEC operations for shifting the walker's position with the kernels inc and dec that are defined with gate operators as opposed to unitary operators. The advantage of this approach is that we'll be able to create nested kernels that will simplify the exposition. The code below works for a 4-qubit walk. At the end of this lab you'll be tasked with adapting all the code to work for any number of qubits.
We also use the helper function below to plot histograms of the results of the quantum walks.
Introduction to the Context and Problem
The Context
A significant challenge in quantum computing is the efficient encoding of classical data into quantum states that can be processed by quantum hardware or simulated on classical computers. This is particularly crucial for many financial applications, where the first step of a quantum algorithm often involves efficiently loading a probability distribution. For instance, to enable lenders to price loans more accurately based on each borrower's unique risk profile, it is essential to estimate individual loan risk distributions (such as default and prepayment) while accounting for uncertainties in modeling and macroeconomic factors. Efficiently implementing this process on a quantum computer requires the ability to load a log-normal or other complex distribution into a quantum state (Breeden and Leonova).
Financial markets exhibit complex probability distributions and multi-agent interactions that classical models struggle to capture efficiently. Quantum walks can be used to generate probability distributions of market data. The quantum walk approach offers:
Flexible modeling of price movements
Better representation of extreme events compared to classical methods
Ability to capture asymmetric return distributions (Backer et al).
The Problem
This tutorial explores the multi-split-step quantum walks (mSSQW) technique, as detailed by Chang et al., to load a specific probability distribution into a quantum state. Although this technique is applicable to various distributions, our focus here is on the log-normal distribution, which can model the spot price of a financial asset at maturity. In brief, the logarithm of the asset price follows:
Where:
→ Asset price at time T.
→ Initial asset price.
→ Time to maturity (future time period).
→ Risk-free interest rate.
→ Volatility (measure of price fluctuation).
Below, you will find a graph illustrating the sampling of a continuous log-normal distribution targeted in this tutorial.
Split Step Quantum Walk
The aim of this tutorial is to load the targeted distribution, graphed above, into a quantum state using quantum walks. Before getting into the details of the mSSQW, let's first define the split-step quantum walk (SSQW).
SSQW is an extension of the DTQW introduced in Part 1 of this series. For the SSQW, each time step is split into two actions determined by the outcome of two coin flips, hence the name "split step." As was the case in the DTQW, instead of classical coins that can be in the discrete states heads or tails, we use single-qubit quantum state to represent the state of the coin.
In contrast to the DTQW, the SSQW involves flipping the coin twice for each time step. The first flip determines whether the state of the walker shifts right, and the second flip determines whether the state of the walker shifts left. That is, we will apply a quantum coinflip operation on The outcome of this flip will determine whether the walker's state shifts to the right or remains unchanged. Next, during this time step, we apply another, possibly different, quantum operation on the same coin. This outcome determines whether the walker's state shifts to the left or remains unchanged. The circuit diagram for one time step of the SSQW algorithm is given below:

Additionally, since our goal is to model financial data, we must avoid scenarios where the walker with one coin flip moves directly between positions and . Such transitions might suggest an investor with positive sentiment about the stock’s value encoded as would sell it at a very low price near the value encoded as . To prevent this, we use an auxiliary qubit endpoint_qubit as we did in Part 1. One step of the SSQW with an additional endpoint qubit is depicted as follows:

Exercise 1:
Your next exercises is to code one step of the SSQW below using two different coin operations and and disallowing movement of the walker between position and .
Once you've completed the exercise above, feel free to experiment with different combinations of the following:
initial states of the coin
initial states of the walker
coin operations (e.g.,
h,t,z, etc.)num_time_steps
For instance, you might select initial states and coin operators to verify that regardless of the coin, a walker beginning in state will not move to in one time step:

Multi-Split Step Quantum Walk
As you might have noticed in the previous section, by varying the initial state of the walker and coin, and experimenting with different coin flip operations, we can generate many different distributions. Another variation to consider is to change the coin operations, and , throughout the walk. For example, perhaps we start with two bit flip operations to change the state of the coin in time step one and then switch to two Hadamard operations on the coin in time step 2. Go ahead and edit the code block above to observe the newly generated probability distribution.
The challenge, and the focus of the remainder of this tutorial, is to identify initial states and coins that will generate a targeted probability distribution. To keep things simple, we'll fix the initial states of the coin and the walker. Using parameterized coin operations, we can employ a classical optimizer to iteratively evolve the system to generate the desired distribution.
What we've just described is the multi-Split-Step Quantum Walk (multi-SSQW) framework, which we have depicted below for a one time step. To adapt this for multiple time steps, the multi-SSQW quantum circuit is replaced with one made up of num_time_steps copies of the block sequence of the investor's split steps. In a financial application, the different coin operations at each time step can represent distinct investors' decision-making processes reflecting positive or negative sentiments about the value of the stock. The investor's actions can affect a stock price that is being modeled by the probability distribution (Chang et al.).

We've encoded the multi-SSQW circuit below using the universal three-parameter gate, u3, for the coin flip operators.
The code block below defines functions for the classical optimization in the multi-SSQW hybrid workflow. We've selected the COYBLA classical optimizer and the mean square error (MSE) cost function for this example, but other optimizers and cost functions could easily be substituted.
Now, it's time to execute the multi-SSQW and plot the results!
Challenge:
Use the multi-split step quantum walk to model other distributions such as stock values over a period of time
(you can access historical stock market data using packages such as
yfinance).
You might notice that the success of this approach is very sensitive to the number of steps and the number of walkers.
How might you edit the code above to treat these constants as additional parameters that can be optimized?
For an even bigger challenge, rewrite the code above to work for an arbitrary number of qubits using the variable
num_qubits.
Future Directions: Apply what you have learned here to adapt the code to create a Quantum Walk-Based Adaptive Distribution Generator that will allow you to not only model financial data, but also model 2D data such as pixelized images of handwritten digits.