Skip to content

Commit 590ab51

Browse files
author
Zhuoyang Ye
committed
Merge branch 'dev' into density-measure
2 parents 264dbe3 + c43c4bf commit 590ab51

26 files changed

+264
-129
lines changed

.github/workflows/functional_tests.yaml

+33-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"]
17+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
1818

1919
steps:
2020
- uses: actions/checkout@v3
@@ -25,8 +25,8 @@ jobs:
2525
- name: Install dependencies
2626
run: |
2727
python -m pip install --upgrade pip
28-
python -m pip install flake8 pytest qiskit-aer qiskit-ibmq-provider
2928
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
29+
python -m pip install flake8 pytest qiskit-aer qiskit_ibm_runtime
3030
- name: Lint with flake8
3131
run: |
3232
# stop the build if there are Python syntax errors or undefined names
@@ -36,3 +36,34 @@ jobs:
3636
- name: Test with pytest
3737
run: |
3838
python -m pytest -m "not skip"
39+
- name: Install TorchQuantum
40+
run: |
41+
pip install --editable .
42+
- name: Test Examples
43+
run: |
44+
python3 examples/qubit_rotation/qubit_rotation.py --epochs 1
45+
python3 examples/vqe/vqe.py --epochs 1 --steps_per_epoch 1
46+
python3 examples/train_unitary_prep/train_unitary_prep.py --epochs 1
47+
python3 examples/train_state_prep/train_state_prep.py --epochs 1
48+
python3 examples/superdense_coding/superdense_coding_torchquantum.py
49+
python3 examples/regression/run_regression.py --epochs 1
50+
python3 examples/param_shift_onchip_training/param_shift.py
51+
python3 examples/mnist/mnist_2qubit_4class.py --epochs 1
52+
python3 examples/hadamard_grad/circ.py
53+
python3 examples/encoder_examples/encoder_8x2ry.py
54+
python3 examples/converter_tq_qiskit/convert.py
55+
python3 examples/amplitude_encoding_mnist/mnist_new.py --epochs 1
56+
python3 examples/amplitude_encoding_mnist/mnist_example.py --epochs 1
57+
python3 examples/PauliSumOp/pauli_sum_op.py
58+
python3 examples/regression/new_run_regression.py --epochs 1
59+
python3 examples/quanvolution/quanvolution_trainable_quantum_layer.py --epochs 1
60+
python3 examples/grover/grover_example_sudoku.py
61+
python3 examples/param_shift_onchip_training/param_shift.py
62+
python3 examples/quanvolution/quanvolution.py --epochs 1
63+
python3 examples/quantum_lstm/qlstm.py --epochs 1
64+
python3 examples/qaoa/max_cut_backprop.py --steps 1
65+
python3 examples/optimal_control/optimal_control.py --epochs 1
66+
python3 examples/optimal_control/optimal_control_gaussian.py --epochs 1
67+
python3 examples/optimal_control/optimal_control_multi_qubit.py --epochs 1
68+
python3 examples/save_load_example/save_load.py
69+
python3 examples/mnist/mnist.py --epochs 1

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
<a href="https://discord.gg/VTHZAB5E">
2020
<img alt="Chat @ Discord" src="https://img.shields.io/badge/contact-me-blue?logo=discord&logoColor=white">
2121
</a>
22-
<a href="https://qmlsys.hanruiwang.me">
22+
<!-- <a href="https://qmlsys.hanruiwang.me">
2323
<img alt="Forum" src="https://img.shields.io/discourse/status?server=https%3A%2F%2Fqmlsys.hanruiwang.me%2F">
24-
</a>
24+
</a> -->
2525
<a href="https://qmlsys.mit.edu">
2626
<img alt="Website" src="https://img.shields.io/website?up_message=qmlsys&url=https%3A%2F%2Fqmlsys.mit.edu">
2727
</a>
@@ -58,10 +58,10 @@ Researchers on quantum algorithm design, parameterized quantum circuit training,
5858
Dynamic computation graph, automatic gradient computation, fast GPU support, batch model tersorized processing.
5959

6060
## News
61-
62-
- v0.1.7 Available!
61+
- v0.1.8 Available!
62+
- Check the [dev branch](https://github.com/mit-han-lab/torchquantum/tree/dev) for new latest features on quantum layers and quantum algorithms.
6363
- Join our [Slack](https://join.slack.com/t/torchquantum/shared_invite/zt-1ghuf283a-OtP4mCPJREd~367VX~TaQQ) for real time support!
64-
- Welcome to contribute! Please contact us or post in the [forum](https://qmlsys.hanruiwang.me) if you want to have new examples implemented by TorchQuantum or any other questions.
64+
- Welcome to contribute! Please contact us or post in the Github Issues if you want to have new examples implemented by TorchQuantum or any other questions.
6565
- Qmlsys website goes online: [qmlsys.mit.edu](https://qmlsys.mit.edu) and [torchquantum.org](https://torchquantum.org)
6666

6767
## Features
@@ -358,6 +358,7 @@ pre-commit install
358358
- [ICCAD'22] [Wang et al., "QuEst: Graph Transformer for Quantum Circuit Reliability Estimation"](https://arxiv.org/abs/2210.16724)
359359
- [ICML Workshop] [Yun et al., "Slimmable Quantum Federated Learning"](https://dynn-icml2022.github.io/spapers/paper_7.pdf)
360360
- [IEEE ICDCS] [Yun et al., "Quantum Multi-Agent Reinforcement Learning via Variational Quantum Circuit Design"](https://ieeexplore.ieee.org/document/9912289)
361+
- [QCE'23] [Zhan et al., "Quantum Sensor Network Algorithms for Transmitter Localization"](https://ieeexplore.ieee.org/abstract/document/10313806)
361362
<details>
362363
<summary>Manuscripts</summary>
363364

docs/requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
furo @ git+https://github.com/frogcjn/torchquantum-doc-furo-theme.git
2+
nbsphinx
3+
recommonmark
4+
5+
torchquantum>=0.1
6+
opt_einsum
7+
qiskit_ibm_runtime

docs/source/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import sys
3030
import os
3131
sys.path.insert(0, os.path.abspath('../..'))
32-
import furo
3332

3433
#import pathlib
3534
#sys.path.insert(0, pathlib.Path(__file__).parents[2].resolve().as_posix())
@@ -160,4 +159,4 @@
160159
display_gitlab = False
161160
show_source = True
162161
# -- Options for EPUB output
163-
epub_show_urls = 'footnote'
162+
epub_show_urls = 'footnote'

examples/grover/grover_example_sudoku.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"""
2929

3030
import torchquantum as tq
31-
from torchquantum.algorithms import Grover
31+
from torchquantum.algorithm import Grover
3232

3333

3434
# To simplify the process, we can compile this set of comparisons into a list of clauses for convenience.
@@ -90,4 +90,4 @@ def XOR(input0, input1, output):
9090
print("b = ", key[1])
9191
print("c = ", key[2])
9292
print("d = ", key[3])
93-
print("")
93+
print("")

examples/mnist/mnist.py

+34-32
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def main():
179179
"--static", action="store_true", help="compute with " "static mode"
180180
)
181181
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
182+
parser.add_argument("--qiskit-simulation", action="store_true", help="run on a real quantum computer")
182183
parser.add_argument(
183184
"--wires-per-block", type=int, default=2, help="wires per block int static mode"
184185
)
@@ -243,38 +244,39 @@ def main():
243244
# test
244245
valid_test(dataflow, "test", model, device, qiskit=False)
245246

246-
# run on Qiskit simulator and real Quantum Computers
247-
try:
248-
from qiskit import IBMQ
249-
from torchquantum.plugin import QiskitProcessor
250-
251-
# firstly perform simulate
252-
print(f"\nTest with Qiskit Simulator")
253-
processor_simulation = QiskitProcessor(use_real_qc=False)
254-
model.set_qiskit_processor(processor_simulation)
255-
valid_test(dataflow, "test", model, device, qiskit=True)
256-
257-
# then try to run on REAL QC
258-
backend_name = "ibmq_lima"
259-
print(f"\nTest on Real Quantum Computer {backend_name}")
260-
# Please specify your own hub group and project if you have the
261-
# IBMQ premium plan to access more machines.
262-
processor_real_qc = QiskitProcessor(
263-
use_real_qc=True,
264-
backend_name=backend_name,
265-
hub="ibm-q",
266-
group="open",
267-
project="main",
268-
)
269-
model.set_qiskit_processor(processor_real_qc)
270-
valid_test(dataflow, "test", model, device, qiskit=True)
271-
except ImportError:
272-
print(
273-
"Please install qiskit, create an IBM Q Experience Account and "
274-
"save the account token according to the instruction at "
275-
"'https://github.com/Qiskit/qiskit-ibmq-provider', "
276-
"then try again."
277-
)
247+
if args.qiskit_simulation:
248+
# run on Qiskit simulator and real Quantum Computers
249+
try:
250+
from qiskit import IBMQ
251+
from torchquantum.plugin import QiskitProcessor
252+
253+
# firstly perform simulate
254+
print(f"\nTest with Qiskit Simulator")
255+
processor_simulation = QiskitProcessor(use_real_qc=False)
256+
model.set_qiskit_processor(processor_simulation)
257+
valid_test(dataflow, "test", model, device, qiskit=True)
258+
259+
# then try to run on REAL QC
260+
backend_name = "ibmq_lima"
261+
print(f"\nTest on Real Quantum Computer {backend_name}")
262+
# Please specify your own hub group and project if you have the
263+
# IBMQ premium plan to access more machines.
264+
processor_real_qc = QiskitProcessor(
265+
use_real_qc=True,
266+
backend_name=backend_name,
267+
hub="ibm-q",
268+
group="open",
269+
project="main",
270+
)
271+
model.set_qiskit_processor(processor_real_qc)
272+
valid_test(dataflow, "test", model, device, qiskit=True)
273+
except ImportError:
274+
print(
275+
"Please install qiskit, create an IBM Q Experience Account and "
276+
"save the account token according to the instruction at "
277+
"'https://github.com/Qiskit/qiskit-ibmq-provider', "
278+
"then try again."
279+
)
278280

279281

280282
if __name__ == "__main__":

examples/optimal_control/optimal_control.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@
2626
import torch.optim as optim
2727

2828
import torchquantum as tq
29-
import pdb
29+
import argparse
3030
import numpy as np
3131

3232
if __name__ == "__main__":
33-
pdb.set_trace()
33+
parser = argparse.ArgumentParser()
34+
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
35+
parser.add_argument(
36+
"--epochs", type=int, default=1000, help="number of training epochs"
37+
)
38+
39+
args = parser.parse_args()
40+
41+
if args.pdb:
42+
import pdb
43+
pdb.set_trace()
44+
3445
# target_unitary = torch.tensor([[0, 1], [1, 0]], dtype=torch.complex64)
3546
theta = 0.6
3647
target_unitary = torch.tensor(
@@ -41,11 +52,11 @@
4152
dtype=torch.complex64,
4253
)
4354

44-
pulse = tq.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])
55+
pulse = tq.pulse.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])
4556

4657
optimizer = optim.Adam(params=pulse.parameters(), lr=5e-3)
4758

48-
for k in range(1000):
59+
for k in range(args.epochs):
4960
# loss = (abs(pulse.get_unitary() - target_unitary)**2).sum()
5061
loss = (
5162
1

examples/optimal_control/optimal_control_gaussian.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@
2626
import torch.optim as optim
2727

2828
import torchquantum as tq
29-
import pdb
29+
import argparse
3030
import numpy as np
3131

3232
if __name__ == "__main__":
33-
pdb.set_trace()
33+
parser = argparse.ArgumentParser()
34+
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
35+
parser.add_argument(
36+
"--epochs", type=int, default=1000, help="number of training epochs"
37+
)
38+
39+
args = parser.parse_args()
40+
41+
if args.pdb:
42+
import pdb
43+
pdb.set_trace()
44+
3445
# target_unitary = torch.tensor([[0, 1], [1, 0]], dtype=torch.complex64)
3546
theta = 1.1
3647
target_unitary = torch.tensor(
@@ -41,11 +52,11 @@
4152
dtype=torch.complex64,
4253
)
4354

44-
pulse = tq.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])
55+
pulse = tq.pulse.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])
4556

4657
optimizer = optim.Adam(params=pulse.parameters(), lr=5e-3)
4758

48-
for k in range(1000):
59+
for k in range(args.epochs):
4960
# loss = (abs(pulse.get_unitary() - target_unitary)**2).sum()
5061
loss = (
5162
1

examples/optimal_control/optimal_control_multi_qubit.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@
2626
import torch.optim as optim
2727

2828
import torchquantum as tq
29-
import pdb
29+
import argparse
3030
import numpy as np
3131

3232
if __name__ == "__main__":
33-
pdb.set_trace()
33+
parser = argparse.ArgumentParser()
34+
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
35+
parser.add_argument(
36+
"--epochs", type=int, default=1000, help="number of training epochs"
37+
)
38+
39+
args = parser.parse_args()
40+
41+
if args.pdb:
42+
import pdb
43+
pdb.set_trace()
44+
3445
# target_unitary = torch.tensor([[0, 1], [1, 0]], dtype=torch.complex64)
3546
theta = 0.6
3647
target_unitary = torch.tensor(
@@ -43,9 +54,9 @@
4354
dtype=torch.complex64,
4455
)
4556

46-
pulse_q0 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
47-
pulse_q1 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
48-
pulse_q01 = tq.QuantumPulseDirect(
57+
pulse_q0 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
58+
pulse_q1 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
59+
pulse_q01 = tq.pulse.QuantumPulseDirect(
4960
n_steps=10,
5061
hamil=[
5162
[1, 0, 0, 0],
@@ -62,7 +73,7 @@
6273
lr=5e-3,
6374
)
6475

65-
for k in range(1000):
76+
for k in range(args.epochs):
6677
u_0 = pulse_q0.get_unitary()
6778
u_1 = pulse_q1.get_unitary()
6879
u_01 = pulse_q01.get_unitary()

examples/qaoa/max_cut_backprop.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import random
2929
import numpy as np
30+
import argparse
3031

3132
from torchquantum.functional import mat_dict
3233

@@ -172,6 +173,12 @@ def backprop_optimize(model, n_steps=100, lr=0.1):
172173

173174

174175
def main():
176+
parser = argparse.ArgumentParser()
177+
parser.add_argument(
178+
"--steps", type=int, default=300, help="number of steps"
179+
)
180+
args = parser.parse_args()
181+
175182
# create a input_graph
176183
input_graph = [(0, 1), (0, 3), (1, 2), (2, 3)]
177184
n_wires = 4
@@ -184,7 +191,7 @@ def main():
184191
# print("The circuit is", circ.draw(output="mpl"))
185192
# circ.draw(output="mpl")
186193
# use backprop
187-
backprop_optimize(model, n_steps=300, lr=0.01)
194+
backprop_optimize(model, n_steps=args.steps, lr=0.01)
188195
# use parameter shift rule
189196
# param_shift_optimize(model, n_steps=500, step_size=100000)
190197

0 commit comments

Comments
 (0)