forked from mit-han-lab/torchquantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
109 lines (84 loc) · 3.45 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
MIT License
Copyright (c) 2020-present TorchQuantum Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import torch
import argparse
import torchquantum as tq
import torchquantum.functional as tqf
import random
import numpy as np
class QLayer(tq.QuantumModule):
def __init__(self):
super().__init__()
self.n_wires = 4
self.random_layer = tq.RandomLayer(n_ops=50, wires=list(range(self.n_wires)))
# gates with trainable parameters
self.rx0 = tq.RX(has_params=True, trainable=True)
self.ry0 = tq.RY(has_params=True, trainable=True)
self.rz0 = tq.RZ(has_params=True, trainable=True)
self.crx0 = tq.CRX(has_params=True, trainable=True)
@tq.static_support
def forward(self, q_device: tq.QuantumDevice):
"""
1. To convert tq QuantumModule to qiskit or run in the static
model, need to:
(1) add @tq.static_support before the forward
(2) make sure to add
static=self.static_mode and
parent_graph=self.graph
to all the tqf functions, such as tqf.hadamard below
"""
self.q_device = q_device
self.random_layer(self.q_device)
# some trainable gates (instantiated ahead of time)
self.rx0(self.q_device, wires=0)
self.ry0(self.q_device, wires=1)
self.rz0(self.q_device, wires=3)
self.crx0(self.q_device, wires=[0, 2])
# add some more non-parameterized gates (add on-the-fly)
tqf.hadamard(
self.q_device, wires=3, static=self.static_mode, parent_graph=self.graph
)
tqf.sx(self.q_device, wires=2, static=self.static_mode, parent_graph=self.graph)
tqf.cnot(
self.q_device,
wires=[3, 0],
static=self.static_mode,
parent_graph=self.graph,
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--pdb", action="store_true", help="debug with pdb")
args = parser.parse_args()
if args.pdb:
import pdb
pdb.set_trace()
seed = 0
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
q_model = QLayer()
# convert the tq module to qiskit and draw
from torchquantum.plugin import tq2qiskit, qiskit2tq
circ = tq2qiskit(tq.QuantumDevice(n_wires=q_model.n_wires), q_model, draw=True)
# convert the QiskitCircuit to tq module
q_model_back = qiskit2tq(circ)
print(q_model_back)
if __name__ == "__main__":
main()