Skip to content

Commit

Permalink
Merge branch 'gh_test' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
01110011011101010110010001101111 authored Dec 2, 2023
2 parents 2988f27 + 15bfb8c commit 34b974c
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 15 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/functional_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ jobs:
- name: Test with pytest
run: |
pytest -m "not skip"
- name: Install TorchQuantum
run: |
pip install --editable .
- name: Test Examples
run: |
python3 examples/qubit_rotation/qubit_rotation.py --epochs 1
python3 examples/vqe/vqe.py --epochs 1 --steps_per_epoch 1
python3 examples/train_unitary_prep/train_unitary_prep.py --epochs 1
python3 examples/train_state_prep/train_state_prep.py --epochs 1
python3 examples/superdense_coding/superdense_coding_torchquantum.py
python3 examples/regression/run_regression.py --epochs 1
python3 examples/param_shift_onchip_training/param_shift.py
python3 examples/mnist/mnist_2qubit_4class.py --epochs 1
python3 examples/hadamard_grad/circ.py
python3 examples/encoder_examples/encoder_8x2ry.py
python3 examples/converter_tq_qiskit/convert.py
python3 examples/amplitude_encoding_mnist/mnist_new.py --epochs 1
python3 examples/amplitude_encoding_mnist/mnist_example.py --epochs 1
python3 examples/PauliSumOp/pauli_sum_op.py
python3 examples/regression/new_run_regression.py --epochs 1
python3 examples/quanvolution/quanvolution_trainable_quantum_layer.py --epochs 1
python3 examples/grover/grover_example_sudoku.py
python3 examples/param_shift_onchip_training/param_shift.py
4 changes: 2 additions & 2 deletions examples/grover/grover_example_sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"""

import torchquantum as tq
from torchquantum.algorithms import Grover
from torchquantum.algorithm import Grover


# To simplify the process, we can compile this set of comparisons into a list of clauses for convenience.
Expand Down Expand Up @@ -90,4 +90,4 @@ def XOR(input0, input1, output):
print("b = ", key[1])
print("c = ", key[2])
print("d = ", key[3])
print("")
print("")
2 changes: 1 addition & 1 deletion examples/optimal_control/optimal_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
dtype=torch.complex64,
)

pulse = tq.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])
pulse = tq.pulse.QuantumPulseDirect(n_steps=4, hamil=[[0, 1], [1, 0]])

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

Expand Down
2 changes: 1 addition & 1 deletion examples/optimal_control/optimal_control_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
dtype=torch.complex64,
)

pulse = tq.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])
pulse = tq.pulse.QuantumPulseGaussian(hamil=[[0, 1], [1, 0]])

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

Expand Down
6 changes: 3 additions & 3 deletions examples/optimal_control/optimal_control_multi_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
dtype=torch.complex64,
)

pulse_q0 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q1 = tq.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q01 = tq.QuantumPulseDirect(
pulse_q0 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q1 = tq.pulse.QuantumPulseDirect(n_steps=10, hamil=[[0, 1], [1, 0]])
pulse_q01 = tq.pulse.QuantumPulseDirect(
n_steps=10,
hamil=[
[1, 0, 0, 0],
Expand Down
14 changes: 11 additions & 3 deletions examples/qubit_rotation/qubit_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import torchquantum as tq
import torch
from torchquantum.measurement import expval_joint_analytical
import argparse


class OptimizationModel(torch.nn.Module):
Expand Down Expand Up @@ -42,15 +43,14 @@ def train(model, device, optimizer):


# main function to run the optimization
def main():
def main(n_epochs):
seed = 0
torch.manual_seed(seed)

use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

model = OptimizationModel()
n_epochs = 200
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

for epoch in range(1, n_epochs + 1):
Expand All @@ -65,4 +65,12 @@ def main():


if __name__ == "__main__":
main()
parser = argparse.ArgumentParser(
prog="Qubit Rotation",
description="Specify Parameters for Qubit Rotation Optimization Example",
)
parser.add_argument(
"--epochs", type=int, default=200, help="number of training epochs"
)
args = parser.parse_args()
main(args.epochs)
5 changes: 2 additions & 3 deletions examples/regression/new_run_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,11 @@ def main():
model.set_qiskit_processor(processor_simulation)
valid_test(dataflow, q_device, "test", model, device, qiskit=True)

# final valid
valid_test(dataflow, q_device, "valid", model, device, True)
except:
pass

# final valid
valid_test(dataflow, q_device, "valid", model, device, True)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion examples/save_load_example/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def save_load3():
# print(model.q_layer.rx0._parameters)

traced_cell = torch.jit.trace(model, (x))
torch.jit.save(traced_cell, "model_trace.pth")
torch.jit.save(traced_cell, "model_trace.pt")

loaded_trace = torch.jit.load("model_trace.pt")
y2 = loaded_trace(x)
Expand Down
2 changes: 1 addition & 1 deletion examples/vqe/new_simple_vqe.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from torchquantum.plugin import qiskit2tq_op_history

if __name__ == "__main__":
hamil = Hamiltonian.from_file("./examples/simple_vqe/h2.txt")
hamil = Hamiltonian.from_file("./examples/vqe/h2.txt")

ops = [
{'name': 'u3', 'wires': 0, 'trainable': True},
Expand Down
1 change: 1 addition & 0 deletions torchquantum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from .noise_model import *
from .algorithm import *
from .dataset import *
from .pulse import *

# here we check whether the Qiskit parameterization bug is fixed, if not, a
# warning message will be printed
Expand Down
1 change: 1 addition & 0 deletions torchquantum/algorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
from .vqe import *
from .hamiltonian import *
from .qft import *
from .grover import *
2 changes: 2 additions & 0 deletions torchquantum/layer/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
"CXCXCXLayer",
"SWAPSWAPLayer",
"RXYZCXLayer0",
"U3CU3Layer0",
"QFTLayer",
"EntangleLinear",
"EntanglePairwise",
"EntangleFull",
"EntangleCircular",
"EntanglementLayer",
"SethLayer0",
]


Expand Down
1 change: 1 addition & 0 deletions torchquantum/pulse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
from .utils import *
from .sesolve import sesolve
from .mesolve import mesolve
from .pulses import *
# from .smesolve import smesolve

0 comments on commit 34b974c

Please sign in to comment.