Skip to content

toshi-koike-akino/vqar

Repository files navigation

VQAR: Variational Quantum Autoregressive

This project is to demonstrate variational quantum autoregressive (VQAR) model as a quantum machine learning (QML) method to forecast data trends (such as foreign exchange and COVID-19 cases), submitted under an open hackathon in QHack 2022.

qu

Methodology

VQAR is a variational quancum circuit (VQC)-based autoregressive (AR) model, also related to nonlinear autoregressive exogenous (NARX) to predict future trend.

A schematic is illustrated below. A time-series vector input is time-delayed to feed into VQC, and the output of VQC is a prediction of future data. The input data is optionally pre-scaled as an angle value to embed into VQC gates, while the quantum measurement is also optionally post-scaled. The VQC uses variational parameters to be trained such that the prediction loss is minimized. We may use a VQC ansatz, e.g., based on 2-design and data reuploading. An option with skip connection will model residual nonlinear VAR. vqc

Prerequisite

Use the package manager pip for python=3.9. We build VQAR model using Pennylane.

pip install pennylane=0.21.0
pip install matplotlib=3.5.1
pip install pandas=1.4.1
pip install scikit-learn=1.0.2
pip install tqdm=4.62.3
pip install argparse

Other versions should work.

Demo 1: Quantum Forex Forecasting

The first demo is to predict a financial trend, focusing on foreign exchange (a.k.a. forex or FX).

fx

Forex Dataset

We use a submodule exchange-rates licensed under the PDDL.

To update the data, run as

pip install dataflows
git submodule update --remote exchange-rates
python exchange-rates/exchange_rates_flow.py

There are 'annual', 'monthly', and 'daily' datasets from 1971 to 2018 for some countries:

  • exchange-rates/data/annual.csv
  • exchange-rates/data/monthly.csv
  • exchange-rates/data/daily.csv

A wrapper to load the data is forex_loader.py. E.g., select 'monthly' data for Australia and Canada for dates from 1980 to 2011/05:

python forex_loader.py --data monthly --country Australia Canada --dates 1980 2011/05

annaul

Usage

For example, to predict Denmark and Sweden currencies from USD, you may run quforex.py as

python quforex.py --data annual --country Denmark Sweden --memory 2 --epoch 100

To sweep memory, layer, and reuploading parameters of VQAR, you may run quforex_sweep.py as

python quforex_sweep.py --target memory --sweep 1 2 3 4 5 6 --reup 3

It will save results in plots/annual_sweep-memory_6_1_2.png.

Performance

Example performance for Canada/US rate, you may run

python quforex_sweep.py --target memory --sweep 1 2 3 4 5 6 7 --reup 3 --layer 1 --data annual --country Canada

The prediction performance in mean-square error (MSE) improves with the VQAR memory:

mse

Optimized VQC may look like:

 0: ──RY(2.06)────╭C──RY(1.62)─────RY(2.06)──────────────────────────╭C──RY(-0.322)───RY(2.06)──────────────────────────╭C──RY(0.901)────────────────────┤ ⟨Z1: ──RY(0.0649)──╰Z──RY(-0.612)──╭C─────────RY(1.74)────RY(0.0649)──╰Z──RY(-0.384)──╭C─────────RY(0.0422)──RY(0.0649)──╰Z──RY(-0.684)──╭C──RY(-0.692)───┤ ⟨Z2: ──RY(-1.61)───╭C──RY(-0.528)──╰Z─────────RY(-0.761)──RY(-1.61)───╭C──RY(1.13)────╰Z─────────RY(0.583)───RY(-1.61)───╭C──RY(-0.123)──╰Z──RY(-0.397)───┤ ⟨Z3: ──RY(0.939)───╰Z──RY(-1.07)───╭C─────────RY(0.319)───RY(0.939)───╰Z──RY(-1.1)────╭C─────────RY(-1.1)────RY(0.939)───╰Z──RY(-0.936)──╭C──RY(-0.687)───┤ ⟨Z4: ──RY(0.196)───╭C──RY(0.865)───╰Z─────────RY(-0.249)──RY(0.196)───╭C──RY(-0.172)──╰Z─────────RY(1.14)────RY(0.196)───╭C──RY(-0.268)──╰Z──RY(-0.845)───┤ ⟨Z5: ──RY(0.264)───╰Z──RY(-2.3)────╭C─────────RY(1.46)────RY(0.264)───╰Z──RY(-0.878)──╭C─────────RY(0.902)───RY(0.264)───╰Z──RY(0.53)────╭C──RY(-0.671)───┤ ⟨Z6: ──RY(-0.641)──────────────────╰Z─────────RY(-2.06)───RY(-0.641)──────────────────╰Z─────────RY(0.502)───RY(-0.641)──────────────────╰Z──RY(-0.0127)──┤ ⟨Z

Demo 2: Quantum COVID-19 Forecasting

The second demo is to predict COVID-19 cases.

covid

COVID-19 Dataset

We use a submodule nytimes/covid-19-data licensed under the CC-BY NC. There are data from The New York Times, based on reports from state and local health agencies

To update the data, run as

git submodule update --remote covid-19-data

We use CSV files:

  • covid-19-data/us.csv: daily US-nationwide cases
  • covid-19-data/us-states.csv: daily US state-wise cases

Note that the data are cummurative sum. A wrapper to load the data is covid_loader.py, which may convert to daily/weekley diff (with --periods and --decimate):

python covid_loader.py --periods 7 --decimate 7 --states Massachusetts Maine 'New Hampshire' 'New York' Connecticut

states

Usage

You may run qucovid.py as

python qucovid.py --memory 2 --states Massachusetts

Performance

Example results when running qucovid_sweep.py as

python qucovid_sweep.py --verb --target memory --reup 1 --memory 2 --layer 1 --sweep 1 2 3 4 5 6 7 --states Connecticut --lr 1e-2

may look like as below. Training MSE improves with memory size, whereas testing MSE is not improved.

connecticut

Optimized VQC circuits:

 0: ──RY(-0.997)──╭C──RY(1.62)────────────────────┤ ⟨Z1: ──RY(-0.107)──╰Z──RY(-0.612)──╭C──RY(1.74)────┤ ⟨Z2: ──RY(1.45)────╭C──RY(-0.528)──╰Z──RY(-0.761)──┤ ⟨Z3: ──RY(-0.618)──╰Z──RY(-1.07)───╭C──RY(0.319)───┤ ⟨Z4: ──RY(-2.04)───╭C──RY(0.865)───╰Z──RY(-0.249)──┤ ⟨Z5: ──RY(-1.94)───╰Z──RY(-2.3)────╭C──RY(1.46)────┤ ⟨Z6: ──RY(-2.51)───────────────────╰Z──RY(-2.06)───┤ ⟨Z

Real QPU Validation

IBM QPU

It is straightforward to use a real quantum processing unit (QPU) for testing our VQAR. For example, we may use IBM Q Experience. You may specify the account token via Pennylane configulation file, and a scpecific backend of real QPU, such as 'ibmq_london'. To run our vqar.py on a real quantum computer, you just need to change the device as follows:

pip install pennylane-qiskit=0.21.0 # qiskit plugin
python vqar.py --dev qiskit.ibmq

AWS Braket

We can use different backends such as Amazon braket. For example, we may run as

pip install amazon-braket-pennylane-plugin=1.5.6 # AWS plugin
python vqar.py --dev braket.aws.qubit

Further examples and tutorials are found at amazon-braket-example and README.

Support

Note that we do not guarantee to achieve quantum advantage. We do not intend to maintain this project for future.

slide

License

MIT. Copyright (c) 2022 Toshi Koike-Akino. This project is provided 'as-is', without warranty of any kind. In no event shall the authors be liable for any claims, damages, or other such variants.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages