Stock Price Prediction with Quantum Machine Learning in Python – DataDrivenInvestor
An overview of the challenges and opportunities Photo by Anton Maksimov 5642.su on Unsplash
Today, were diving into the intersection of quantum computing and machine learning, exploring quantum machine learning. Our main goal is to compare the performance of a quantum neural network for stock price time series forecasting with a simple single-layer MLP.
To facilitate this project, well be utilizing the Historical API endpoint offered by Financial Modeling Prep (FMP) for reliable and accurate data which is very critical. With that being said, lets dive into the article.
Lets start by importing the necessary libraries for our analysis. These libraries will provide the basic tools required to explore and implement our project.
Weve set up our environment by installing the Qiskit library for working with quantum computing networks, along with other essential libraries. To extract the data, well use the historical data API endpoint provided by Financial Modeling Prep.
FMPs historical data API offers a conveniently accessible endpoint, providing a diverse and extensive collection of historical stock data that proves invaluable at every step of our project. This resource enables us to access a wide range of financial information, enhancing the depth and accuracy of our analysis. Its user-friendly interface and comprehensive dataset contribute significantly to the success and efficiency of our research and implementation.
Now we are going to extract historical data as follows:
Replace YOUR API KEYwith your secret API key which you can obtain by creating an FMP account. The output is a JSON response which looks as follows:
In regular computers, we have tiny switches called digital gates. These switches control how information moves around. They work with basic units of data called bits, which can be either 0 or 1. The gates help computers do calculations and process stuff. Now, in quantum computers, we use something called qubits instead of bits. Qubits are special because they can be both 0 and 1 at the same time. Its like having a coin thats spinning and showing both heads and tails until you catch it, and then it picks one side.
When we say the wave function collapses, its just a fancy way of saying the qubit decides to be either 0 or 1 when we check it. We make these qubits using different things like light particles (photons), tiny particles that make up stuff (atoms), or even small electrical circuits (Josephson junctions). These are like the building blocks for our special qubits.
These quantum systems (particles or circuits) do some interesting things. They can be in different states at the same time (superposition), connect in a special way (entanglement), and even go through barriers they shouldnt (tunneling).
Whats cool is that quantum computers, with their qubits and special behaviors, use certain algorithms to solve some problems faster than regular computers. Its like having a new tool that might help us solve tough puzzles more efficiently in the future.
In traditional computing, we perform operations using basic logic gates like AND, NOT, and OR. These gates work with 0s and 1s, and their rules are based on a simple mathematical system called:
which essentially deals with counting modulo 2.
Now, imagine a quantum computer it also has gates, but these are like supercharged versions. Instead of dealing with simple bits, quantum gates work with quantum bits or qubits. The math behind these quantum gates involves complex numbers and Matrix operations.
Lets take the quantum NOT gate, called:
as an example. Apply it to a qubit initially in the state 0, and the operator flips it to 1 , and if you apply it again, it goes back to 0. Its a bit like flipping a coin.
Theres also the Hadamard gate (H) that does something really cool. Applying it to a qubit initially in the state 0 puts it in this special mix of 0 and 1 states at the same time to show mathematically H operates on |0 and converts it into the standard superposition of the basis states:
Its like having a coin spinning in the air, showing both heads and tails until it lands.
Now, lets talk about the Controlled-NOT (CNOT) gate. This one works on two qubits. If the first qubit is 1, it flips the second qubit from 0 to 1 or vice versa. Its like a quantum switch that depends on the state of the first qubit.
In the quantum world, things get more interesting. If you have two qubits in a special state, the CNOT gate uniquely rearranges their combinations, creating what we call entanglement. This entanglement is like a special connection between the qubits, making them behave in a coordinated manner.
So, in a nutshell, while regular computers use basic rules with 0s and 1s, quantum computers have these fascinating gates that play with probabilities, mix states, and create connections between qubits, opening up a world of possibilities for solving complex problems more efficiently.
In our project, we place special emphasis on a category of gates known as parameterized gates. These gates exhibit behavior that is contingent on specific input parameters, denoted by the symbol . Notably, we focus on rotation gates such as:
each characterized by a unitary matrix as described in the below figure:
Lets delve a bit deeper into these rotation gates. Consider:
envision it as a quantum gate resembling a rotating door that allows for the rotation of a qubit by a specific angle . The
and,
gates function similarly, introducing rotations around different axes.
The significance of these gates lies in their parameterized nature. By adjusting the input parameter , we essentially introduce a customizable element into our quantum algorithms. These gates serve as the foundational components for constructing the quantum neural network integral to our Project.
In essence, acts as a tuning parameter, akin to a knob, enabling us to finely adjust and tailor the behavior of our quantum algorithms within the framework of the quantum neural network. This flexibility becomes pivotal in optimizing and customizing the performance of our quantum algorithms for specific tasks.
Quantum algorithms can be thought of as a series of operations performed on a quantum state, represented by expressions like:
These algorithms are translated into quantum circuits, as illustrated in Figure below. In this depiction, the algorithm starts from the initial state |q_0 q_1 = |00 and concludes with a measurement resulting in either |00 or |11 with an equal probability of 0.5, recorded into classical bits (line c).
In a quantum circuit, each horizontal line corresponds to a single qubit, and gates are applied sequentially until measurement. Its important to note that loops are not allowed in a quantum program. A specific type of quantum circuit is the Variational Quantum Circuit (VQC). Notably, VQC incorporates parameterized gates like the aforementioned R_x(), R_y(), R_z().
In simpler terms, quantum algorithms are like step-by-step instructions for a quantum computer, and quantum circuits visually represent these steps. The Variational Quantum Circuit introduces a special kind of flexibility with parameterized gates, allowing for customization based on specific values, denoted by .
The primary objective of QML is to devise and deploy methods capable of running on quantum computers to address conventional supervised, unsupervised, and reinforcement learning tasks encountered in classical Machine Learning.
What makes QML distinct is its utilization of quantum operations, leveraging unique features like superposition, tunneling, entanglement, and quantum parallelism inherent to Quantum Computing (QC). In our study, we specifically concentrate on Quantum Neural Network (QNN) design. A QNN serves as the quantum counterpart of a classical neural network.
Breaking it down, each layer in a QNN is a Variational Quantum Circuit (VQC) comprising parameterized gates. These parameters act as the quantum equivalents of the weights in a classical neural network. Additionally, the QNN incorporates a mechanism to exchange information among existing qubits, resembling the connections between neurons in different layers of a classical network. Typically, this information exchange is achieved through entanglements, employing operators such as the CNOT gate.
Creating a Quantum Machine Learning (QML) model typically involves several steps, as illustrated in Figure above. First, we load and preprocess the dataset on a classical CPU. Next, we use a quantum embedding technique to encode this classical data into quantum states on a Quantum Processing Unit (QPU) or quantum hardware. Once the classic data is represented in quantum states, the core model, implemented in the ansatz, is executed, and the results are measured using classical bits. Finally, if needed, we post-process these results on the CPU to obtain the expected model output. In our study, we follow this overall process to investigate the application of a Quantum Neural Network for time series forecasting.
A Quantum Neural Network (QNN) typically consists of three main layers:
1. Input Layer: This layer transforms classical input data into a quantum state. It uses a parameterized variational circuit with rotation and controlled-rotation gates to prepare the desired quantum state for a given input. This step, known as quantum embedding, employs techniques like basis encoding, amplitude encoding, Hamiltonian encoding, or tensor product encoding.
2. Ansatz Layer: The heart of the QNN, this layer contains a Variational Quantum Circuit, repeated L times to simulate L network layers classically. Its responsible for processing and manipulating quantum information.
3. Output Layer: This layer performs measurements on qubits, providing the final expected outcome.
For the input layer, we use a tensor product encoding technique. It involves a simple X-rotation gate for each qubit, where the gate parameter is set by scaling the classic data to the range [-, ]. Although its a quick and straightforward encoding method (O(1) operations), it has limitations. The number of qubits needed scales linearly with the input classic data. To address this, we introduce learnable parameters for scaling and bias in the input data, enhancing the flexibility of the quantum embedding. In Figure 3, you can see an example of the input layer for a network with 3 qubits, where classic data features:
, input scale parameters:
, and bias parameters:
come into play.
Regarding the ansatz, its interesting to note that, unlike classical neural networks, there isnt a fixed set of quantum layer structures commonly found in the literature (such as fully connected or recurrent layers). The realm of possible gates for quantum information transfer between qubits is extensive, and the optimal organization of these gates for effective data transfer is an area that hasnt been thoroughly explored yet.
In our Project, we adopt the Real Amplitudes ansatz, a choice inspired by its success in various domains like policy estimation for quantum reinforcement learning and classification. This ansatz initiates with full rotation X/Y/Z parameterized gates, akin to the quantum version of connection weights. It is then followed by a series of CNOT gates arranged in a ring structure to facilitate qubit information transfer. Figure 4 provides a visual representation of how this ansatz is implemented, serving as the quantum equivalent of a network layer for a 3-qubit network.
To break it down, a quantum network layer in our work involves a set of parameters totaling 3 times the number of qubits (3*n), where n represents the number of qubits in the quantum network.
Now, lets talk about the output layer, which is a critical part of our quantum model. In quantum computing, when we want to extract information from our quantum state, we often perform measurements using a chosen observable. One such commonly used observable is represented by the _z operator over the computational basis. To understand this, think of it as a way to extract information from our quantum state.
The network output is determined by calculating the expectation of this observable over our quantum state. This is expressed as |_z|, where | denotes the complex conjugate of |. The result falls within the range of [-1, 1].
No need to stress over those complex mathematical equations our trusty library, Qiskit, has got it covered! Qiskit will handle all the intricate quantum calculations seamlessly, making the quantum computing process much more accessible for us. So, you can focus on exploring the quantum world without getting bogged down by the nitty-gritty math
Now, to make our network output less sensitive to biases and scales inherent in the dataset, we introduce a final scale parameter and bias to be learned. This step adds a layer of adaptability to our model, allowing it to fine-tune and adjust the output based on the specific characteristics of our data. The entire model architecture is visually represented in the figure below.
The training of our proposed Quantum Neural Network (QNN) happens on a regular CPU using classical algorithms like the Adam optimizer. The CPU handles the gradient computation through traditional propagation rules, while on the Quantum Processing Unit (QPU), we calculate the gradient using the parameter-shift rule. Its a bit like having a dual system where the CPU manages the main training, and the QPU comes into play for specific quantum computations.
Visualize the training process pipeline in Figure 6, where represents the scale/bias parameters in the input layer, corresponds to the parameters of the layers containing the ansatz, and are the scale/bias parameters for the network outputs. This orchestration ensures a cohesive training approach, leveraging both classical and quantum computing resources.
As a Quantum Neural Network (QNN) operates as a feedforward model, our initial step involves defining a time horizon, denoted as T. To adapt the time series data for the QNN, we transform it into a tabular format. Here, the target is the time series value at time t, denoted as x(t), while the inputs encompass the values x(t-1), x(t-2), , x(t-T). This restructuring facilitates the models understanding of the temporal relationships in the data, allowing it to make predictions based on past values.
First, we fetch the data using the historical Data API endpoint provided by Financial Modeling Prep as follows:
The output is a Pandas dataframe which looks something like this (before that, make sure to replace YOUR API KEY with your secret API key):
Follow this link:
Stock Price Prediction with Quantum Machine Learning in Python - DataDrivenInvestor
- The Best Quantum Computing Stocks to Buy Today - Yahoo Finance - April 12th, 2026 [April 12th, 2026]
- 3 Quantum Computing Stocks with Potential to Beat the Market 4/10/2026 - TipRanks - April 12th, 2026 [April 12th, 2026]
- Why Quantum Computing Is Becoming a Strategic Lever in Materials Innovation - Wipro - April 12th, 2026 [April 12th, 2026]
- What Are the Best Quantum Computing Stocks to Buy in 2026? - CoinCentral - April 12th, 2026 [April 12th, 2026]
- Adam Back Says Quantum Threat to Bitcoin Is Decades Away, Urges Gradual Migration to Post-Quantum Security - Bitcoin Magazine - April 10th, 2026 [April 10th, 2026]
- Infleqtion and NASA Deploy Upgraded Quantum Hardware to International Space Station - Quantum Computing Report - April 10th, 2026 [April 10th, 2026]
- Long-term undervaluation lifts Quantum Computing stock to a 4.87% daily increase - Traders Union - April 10th, 2026 [April 10th, 2026]
- Up 1,460% Since 2024, Is It Too Late to Buy This Quantum Computing Leader? - Yahoo Finance - April 8th, 2026 [April 8th, 2026]
- Up 1,460% Since 2024, Is It Too Late to Buy This Quantum Computing Leader? - The Motley Fool - April 8th, 2026 [April 8th, 2026]
- Future of Quantum Computing | High-Speed Processing, AI Synergy - openPR.com - April 8th, 2026 [April 8th, 2026]
- Fully Programmable Quantum Computing With Trapped-ions - Quantum Zeitgeist - April 8th, 2026 [April 8th, 2026]
- News Explorer Quantum Computing Poses Potential Threats to Bitcoin, But Current Research Shows Limitations - Decrypt - April 8th, 2026 [April 8th, 2026]
- Quantum Computing Market to Reach US$ 22.75 Billion by 2033 - openPR.com - April 8th, 2026 [April 8th, 2026]
- A Cryptography Engineers Perspective on Quantum Computing Timelines - OODAloop - April 8th, 2026 [April 8th, 2026]
- Why the mind-bending physics of quantum computing is terrifying for bitcoin and crypto - CoinDesk - April 7th, 2026 [April 7th, 2026]
- Q-Factor emerges with $24M in funding and the next big bet to achieve quantum computing advantage - SiliconANGLE - April 7th, 2026 [April 7th, 2026]
- Alex Pruden: Quantum computing threatens elliptic curve cryptography, advancements could lead to utility-scale systems by decade's end, and the urgent... - April 7th, 2026 [April 7th, 2026]
- Lloyds Bank uses quantum computing to detect money mules - The Times - April 7th, 2026 [April 7th, 2026]
- SpinQ Technology Raises Nearly 1 Billion CNY ($145.3M USD) to Scale Industrial Quantum Computing - Quantum Computing Report - April 5th, 2026 [April 5th, 2026]
- Is Rigetti Computing the Best Quantum Computing Stock to Buy Right Now? - fool.com - April 5th, 2026 [April 5th, 2026]
- SpinQ Technology Secures Nearly 1 Billion Chinese Yuan in Series C Funding to Scale Industrial Superconducting Quantum Computing - The Quantum Insider - April 5th, 2026 [April 5th, 2026]
- Q&A: AWS on new AI agents, quantum computing in healthcare - MobiHealthNews - April 5th, 2026 [April 5th, 2026]
- QuiX Quantum Demonstrates Below-Threshold Error Mitigation in Photonic Quantum Computing for First Time - HPCwire - April 5th, 2026 [April 5th, 2026]
- Google says there's a 10% chance quantum computing will crack bitcoin cryptography by 2032 - PC Gamer - April 5th, 2026 [April 5th, 2026]
- Solana begins testing defenses against 'quantum-computing threat' "Stronger security leads to slower performance" - bloomingbit - April 5th, 2026 [April 5th, 2026]
- 'No Need to Panic': Binance Founder Says Crypto Can Survive Quantum Computing Threat by Upgrading - Yahoo Finance - April 5th, 2026 [April 5th, 2026]
- Clocking Quantum Instability: A New Process Could Open Pathways to Reliable Quantum Computing - thedebrief.org - April 5th, 2026 [April 5th, 2026]
- University of Chicago using "self-driving" lab in quantum computing research - CBS News - April 5th, 2026 [April 5th, 2026]
- IBM Advances Quantum Computing Research: Will it Boost Prospects? - sg.finance.yahoo.com - April 5th, 2026 [April 5th, 2026]
- Safeguarding cryptocurrency by disclosing quantum vulnerabilities responsibly - research.google - April 1st, 2026 [April 1st, 2026]
- New Teaching Approach Could Open The Door to Quantum Computing - The Quantum Insider - April 1st, 2026 [April 1st, 2026]
- EPB Joins Southeastern Quantum Collaborative to Support Regional Infrastructure Integration - Quantum Computing Report - April 1st, 2026 [April 1st, 2026]
- Google warns quantum computing may break bitcoin earlier than thought, prepares 2029 transition - The Block - April 1st, 2026 [April 1st, 2026]
- Alice & Bob secures 3.4 million ARPA-E award to discover rare-earth-free magnets using quantum computing - BeBeez International - April 1st, 2026 [April 1st, 2026]
- 1 Unstoppable Quantum Computing Stock to Buy Before It Soars 200%, According to 1 Wall Street Analyst - finance.yahoo.com - April 1st, 2026 [April 1st, 2026]
- Is Ciscos Quantum Networking Push With Atom Computing Reshaping Its AI Infrastructure Story (CSCO)? - finance.yahoo.com - April 1st, 2026 [April 1st, 2026]
- Changpeng Zhao dismisses 'quantum computing fears' "No need to be scared" - bloomingbit - April 1st, 2026 [April 1st, 2026]
- UMass Amherst and UCSB Demonstrate Photonic Chip Technology for Miniaturized Quantum Systems - Quantum Computing Report - April 1st, 2026 [April 1st, 2026]
- "Even if the odds of quantum computing success are low, preparation is needed" - bloomingbit - April 1st, 2026 [April 1st, 2026]
- The Best Quantum Computing Stock to Buy With $1,000 Right Now - The Motley Fool - April 1st, 2026 [April 1st, 2026]
- Oxford University's Natalia Ares on why quantum computing beats zeros and ones - reuters.com - April 1st, 2026 [April 1st, 2026]
- Quantum leap: U of S becomes national hub for advanced computing - 620 CKRM - April 1st, 2026 [April 1st, 2026]
- The Historical Trend of Bitcoin Suggests a Potential Rebound, While Concerns Over Quantum Computing Threaten the Industry - NAI500 - April 1st, 2026 [April 1st, 2026]
- 1 Unstoppable Quantum Computing Stock to Buy Before It Soars 200%, According to 1 Wall Street Analyst - The Motley Fool - March 30th, 2026 [March 30th, 2026]
- More Innovation through Different Computation: Quantum Computing and the New Substrate of Science - The Foundation for American Innovation - March 30th, 2026 [March 30th, 2026]
- Prediction: These 2 Quantum Computing Stocks Will Soar Over the Next 5 Years - The Motley Fool - March 30th, 2026 [March 30th, 2026]
- Opinion | Quantum Computing Is Todays Manhattan Project - WSJ - March 30th, 2026 [March 30th, 2026]
- Rigetti Computing Intends to Invest $100 Million in UK to Accelerate Quantum Computing Development - thequantuminsider.com - March 30th, 2026 [March 30th, 2026]
- Quantum computing offers a path beyond AI's limits in drug discovery - - March 30th, 2026 [March 30th, 2026]
- Digital Currency Risks 2026: Regulation, Environment & Quantum Computing - News and Statistics - indexbox.io - March 30th, 2026 [March 30th, 2026]
- Quantum Computing Poses Urgent Cybersecurity ThreatsAnd Opportunities - broadbandbreakfast.com - March 28th, 2026 [March 28th, 2026]
- Quantum computing firm Xanadu starts trading on TSX - Castanet - March 28th, 2026 [March 28th, 2026]
- US-China tech war: Who will win the global quantum computing race? - uk.investing.com - March 28th, 2026 [March 28th, 2026]
- Google Issues New Warning About the Quantum Computing Security Apocalypse - Gizmodo - March 26th, 2026 [March 26th, 2026]
- Quantum Computing Is on Its Way to Transforming Science. Inside IBMs Latest Breakthrough. - Barron's - March 26th, 2026 [March 26th, 2026]
- Quantum computing cybersecurity risk - Are we ready? - I by IMD - imd.org - March 26th, 2026 [March 26th, 2026]
- Analyzing National Quantum-Computing Ecosystems in the Indo-Pacific - The National Bureau of Asian Research (NBR) - March 26th, 2026 [March 26th, 2026]
- When Will Quantum Computing Deliver Real Business Value? - AZoQuantum - March 26th, 2026 [March 26th, 2026]
- Energy and physical resource impacts of quantum computing merit greater attention - Nature - March 26th, 2026 [March 26th, 2026]
- Cisco and Atom Computing Partner on Quantum Networking for Scalable Computing - The Quantum Insider - March 26th, 2026 [March 26th, 2026]
- Fujitsu and The University of Osaka Implement Early-FTQC Framework for Chemical Calculations - Quantum Computing Report - March 26th, 2026 [March 26th, 2026]
- IBM Stock Quantum Computing Breakthrough Confirmed in Landmark Simulation - CoinCentral - March 26th, 2026 [March 26th, 2026]
- Atom Computing Announces Strategic Collaboration with Cisco to Advance Scalable, Networked, and Distributed Quantum Computing - PR Newswire - March 26th, 2026 [March 26th, 2026]
- City approves $500,000 in economic incentives for quantum computing business - The Business Journals - March 26th, 2026 [March 26th, 2026]
- ANELLO Photonics and Q-CTRL Partner to Deliver Quantum-Resilient Navigation for UAVs - Quantum Computing Report - March 26th, 2026 [March 26th, 2026]
- A Look At Quantum Computing Inc. (QUBT) Valuation After The OFC 2026 Quantum Security Demo With Ciena - simplywall.st - March 24th, 2026 [March 24th, 2026]
- Quantum-Computing Stocks Are Trading at 100 Times Revenue -- Here's Why Buffett-Style Investors Are Staying Cautious - The Motley Fool - March 18th, 2026 [March 18th, 2026]
- SEEQC Validates Scalable Quantum Computing Architecture with On-Chip Control Breakthrough - citybiz - March 18th, 2026 [March 18th, 2026]
- Pasqal Introduces New Integration with NVIDIA CUDA-Q to Enhance Its Hybrid Quantum Computing Environment for HPC - The Quantum Insider - March 18th, 2026 [March 18th, 2026]
- IBM Fellow and Quantum Pioneer Charles H. Bennett Receives A.M. Turing Award, Computing's Highest Honor - PR Newswire - March 18th, 2026 [March 18th, 2026]
- U.K. to Spend $2.5 Billion on Quantum Computing. What It Means for IonQ Stock. - Barron's - March 18th, 2026 [March 18th, 2026]
- Kvantify Collaborates with Atom Computing and Aarhus University on Quantum Drug Discovery Research - HPCwire - March 18th, 2026 [March 18th, 2026]
- UK Government Commits 2B to Quantum Computing Procurement and Industry Scale-Up - HPCwire - March 18th, 2026 [March 18th, 2026]
- The More I Look At Quantum Computing, The More I Like It: Upgrading To Buy (QUBT) - Seeking Alpha - March 18th, 2026 [March 18th, 2026]
- The future of the battlefield runs right through quantum computing: IONQ CEO - Fox Business - March 11th, 2026 [March 11th, 2026]
- Xanadu: Partnership With University Of Marylands ARLIS To Advance Quantum Computing Security - Pulse 2.0 - March 11th, 2026 [March 11th, 2026]
- Pioneering Quantum-Supercomputing Integration: U.S. Leadership in the Next Computing Era - CSIS | Center for Strategic and International Studies - March 11th, 2026 [March 11th, 2026]
- Xanadu Joins University of Maryland's ARLIS to Advance the Security of Quantum Computing - Yahoo Finance - March 11th, 2026 [March 11th, 2026]
- Xanadu and AMD Team Up Ahead of Trading Debut. Quantum Computing Meets Aerospace. - Barron's - March 11th, 2026 [March 11th, 2026]
- 2 Top Quantum Computing Stocks to Buy in March - The Motley Fool - March 11th, 2026 [March 11th, 2026]