forked from tencent-quantum-lab/tensorcircuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonio.py
42 lines (37 loc) · 1.1 KB
/
jsonio.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
"""
example showcasing how circuit can be load from and dump to json:
useful for storage or restful api
"""
import numpy as np
import tensorcircuit as tc
tc.set_dtype("complex128")
def make_circuit():
c = tc.Circuit(3)
c.h(0)
c.H(2)
c.CNOT(1, 2)
c.rxx(0, 2, theta=0.3)
c.u(2, theta=0.2, lbd=-1.2, phi=0.5)
c.cu(1, 0, lbd=1.0)
c.crx(0, 1, theta=-0.8)
c.r(1, theta=tc.backend.ones([]), alpha=0.2)
c.toffoli(0, 2, 1)
c.ccnot(0, 1, 2)
c.any(0, 1, unitary=tc.gates._xx_matrix)
c.multicontrol(1, 2, 0, ctrl=[0, 1], unitary=tc.gates._x_matrix)
return c
if __name__ == "__main__":
c = make_circuit()
s = c.to_json(simplified=True)
print(s)
c.to_json(file="circuit.json")
# load from json string
c2 = tc.Circuit.from_json(s)
print("\n", c2.draw())
np.testing.assert_allclose(c.state(), c2.state(), atol=1e-5)
print("test correctness 1")
# load from json file
c3 = tc.Circuit.from_json_file("circuit.json")
print("\n", c3.draw())
np.testing.assert_allclose(c.state(), c3.state(), atol=1e-5)
print("test correctness 2")