diff --git a/.github/workflows/functional_tests.yaml b/.github/workflows/functional_tests.yaml index af549120..7b9dfb50 100644 --- a/.github/workflows/functional_tests.yaml +++ b/.github/workflows/functional_tests.yaml @@ -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 diff --git a/examples/grover/grover_example_sudoku.py b/examples/grover/grover_example_sudoku.py index 25761594..4969eea2 100644 --- a/examples/grover/grover_example_sudoku.py +++ b/examples/grover/grover_example_sudoku.py @@ -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. @@ -90,4 +90,4 @@ def XOR(input0, input1, output): print("b = ", key[1]) print("c = ", key[2]) print("d = ", key[3]) - print("") \ No newline at end of file + print("") diff --git a/examples/optimal_control/optimal_control.py b/examples/optimal_control/optimal_control.py index 438e135b..89601153 100644 --- a/examples/optimal_control/optimal_control.py +++ b/examples/optimal_control/optimal_control.py @@ -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) diff --git a/examples/optimal_control/optimal_control_gaussian.py b/examples/optimal_control/optimal_control_gaussian.py index 861cc92a..559e6127 100644 --- a/examples/optimal_control/optimal_control_gaussian.py +++ b/examples/optimal_control/optimal_control_gaussian.py @@ -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) diff --git a/examples/optimal_control/optimal_control_multi_qubit.py b/examples/optimal_control/optimal_control_multi_qubit.py index 023d4f3c..148b526c 100644 --- a/examples/optimal_control/optimal_control_multi_qubit.py +++ b/examples/optimal_control/optimal_control_multi_qubit.py @@ -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], diff --git a/examples/qubit_rotation/qubit_rotation.py b/examples/qubit_rotation/qubit_rotation.py index be2dfc82..bae1e803 100644 --- a/examples/qubit_rotation/qubit_rotation.py +++ b/examples/qubit_rotation/qubit_rotation.py @@ -6,6 +6,7 @@ import torchquantum as tq import torch from torchquantum.measurement import expval_joint_analytical +import argparse class OptimizationModel(torch.nn.Module): @@ -42,7 +43,7 @@ def train(model, device, optimizer): # main function to run the optimization -def main(): +def main(n_epochs): seed = 0 torch.manual_seed(seed) @@ -50,7 +51,6 @@ def main(): 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): @@ -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) diff --git a/examples/regression/new_run_regression.py b/examples/regression/new_run_regression.py index fdeb5cd4..30fc3aa7 100644 --- a/examples/regression/new_run_regression.py +++ b/examples/regression/new_run_regression.py @@ -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() diff --git a/examples/save_load_example/save_load.py b/examples/save_load_example/save_load.py index 1022c3aa..c5a6c57a 100644 --- a/examples/save_load_example/save_load.py +++ b/examples/save_load_example/save_load.py @@ -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) diff --git a/examples/vqe/new_simple_vqe.py b/examples/vqe/new_simple_vqe.py index b5a83953..31cab355 100644 --- a/examples/vqe/new_simple_vqe.py +++ b/examples/vqe/new_simple_vqe.py @@ -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}, diff --git a/torchquantum/__init__.py b/torchquantum/__init__.py index c8aed9ed..b1529623 100644 --- a/torchquantum/__init__.py +++ b/torchquantum/__init__.py @@ -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 diff --git a/torchquantum/algorithm/__init__.py b/torchquantum/algorithm/__init__.py index 623d71b7..7dfb672a 100644 --- a/torchquantum/algorithm/__init__.py +++ b/torchquantum/algorithm/__init__.py @@ -25,3 +25,4 @@ from .vqe import * from .hamiltonian import * from .qft import * +from .grover import * diff --git a/torchquantum/layer/layers.py b/torchquantum/layer/layers.py index a28785cd..2f6aedec 100644 --- a/torchquantum/layer/layers.py +++ b/torchquantum/layer/layers.py @@ -51,12 +51,14 @@ "CXCXCXLayer", "SWAPSWAPLayer", "RXYZCXLayer0", + "U3CU3Layer0", "QFTLayer", "EntangleLinear", "EntanglePairwise", "EntangleFull", "EntangleCircular", "EntanglementLayer", + "SethLayer0", ] diff --git a/torchquantum/pulse/__init__.py b/torchquantum/pulse/__init__.py index 5a4539de..d281a73f 100644 --- a/torchquantum/pulse/__init__.py +++ b/torchquantum/pulse/__init__.py @@ -25,4 +25,5 @@ from .utils import * from .sesolve import sesolve from .mesolve import mesolve +from .pulses import * # from .smesolve import smesolve