/
circuit_simulation.py
165 lines (152 loc) · 6.15 KB
/
circuit_simulation.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import numpy as np
import time
import math
from acqdp import circuit
from acqdp.tensor_network import ContractionScheme, TensorNetwork
from datetime import timedelta
import json
import argparse
import os
t = TensorNetwork(open_edges=[0, 1, 1, 0])
t.add_node(0, [0, 1], np.array([[1, 1j], [1j, np.exp(np.pi * 1j / 6)]]))
ISWAP_CZ = circuit.Unitary(2, t, "FSim", True)
def GRCS(f, in_state=0, simplify=False):
with open(f) as fin:
n = int(fin.readline())
c = circuit.Circuit()
if in_state is not None:
for qubit in range(n):
c.append(circuit.CompState[(in_state >> (n - qubit - 1)) & 1],
[qubit], -1)
gate_table = {
'h':
circuit.HGate,
'x_1_2':
circuit.Unitary(1,
np.array([[1 / np.sqrt(2), -1j / np.sqrt(2)],
[-1j / np.sqrt(2), 1 / np.sqrt(2)]]),
name='X_1_2'),
'y_1_2':
circuit.Unitary(1,
np.array([[1 / np.sqrt(2), -1 / np.sqrt(2)],
[1 / np.sqrt(2), 1 / np.sqrt(2)]]),
name='Y_1_2'),
'hz_1_2':
circuit.Unitary(
1,
np.array([[1 / np.sqrt(2), -np.sqrt(1j) / np.sqrt(2)],
[np.sqrt(-1j) / np.sqrt(2), 1 / np.sqrt(2)]]),
name='W_1_2'),
'cz':
circuit.CZGate,
't':
circuit.Diagonal(1, np.array([1, np.exp(1j * np.pi / 4)])),
'is':
circuit.Diagonal(2, np.array([1, 1j, 1j, 1])) | circuit.SWAPGate
}
size_table = {
'h': 1,
'cz': 2,
't': 1,
'x_1_2': 1,
'y_1_2': 1,
'hz_1_2': 1,
'is': 2,
'rz': 1,
'fs': 2
}
for line in fin:
words = line.split()
layer = int(words[0])
target = list(
int(x) for x in words[2:2 + size_table[words[1].lower()]])
params = words[2 + size_table[words[1].lower()]:]
if not params:
c.append(gate_table[words[1].lower()], target, layer)
elif len(params) == 1:
c.append(
circuit.Diagonal(
1,
np.array([1, np.exp(1j * float(params[0]))]),
name='R_Z({})'.format(params[0])), target, layer)
elif len(params) == 2:
c.append(
circuit.Unitary(
2,
np.array([[1, 0, 0, 0],
[
0,
math.cos(float(params[0])),
-math.sin(float(params[0])) * 1j, 0
],
[
0, -math.sin(float(params[0])) * 1j,
math.cos(float(params[0])), 0
],
[
0, 0, 0,
math.cos(-float(params[1])) + math.sin(-float(params[1])) * 1j
]]),
name='FSim'), target, layer)
if simplify:
for k in c.operations_by_name:
if c.operations_by_name[k][
'time_step'] == 2 or c.operations_by_name[k][
'time_step'] == c.max_time - 2:
c.operations_by_name[k]['operation'] = ISWAP_CZ
return c
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Simulate circuits with tensor network contraction.')
parser.add_argument('circuit_file', help='the ciruit file (in .qsim format) to be simulated')
parser.add_argument('-o', '--load-order', metavar='order_file', help='load a contraction order from a file')
parser.add_argument('-s', '--save-order', metavar='order_file', help='save the contraction order to a file')
parser.add_argument(
'-a',
'--num-amplitudes',
metavar='N_a',
default=1,
type=int,
help='number of amplitudes that would need to be sampled (used only to calculate the projected running time)')
args = parser.parse_args()
start_time_TZ = time.time()
c = GRCS(args.circuit_file, simplify=False)
n = len(c.all_qubits)
tn = c.tensor_pure
tn.cast(np.complex64)
tn.expand(recursive=True)
open_indices = [0, 1, 2, 3, 4, 5]
for i in range(n):
if i not in open_indices:
tn.fix_edge(tn.open_edges[i], 0)
tn.open_edges = [tn.open_edges[i] for i in open_indices]
this_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(this_dir, 'khp_params.json'), 'r') as f:
kwargs = json.load(f)
if args.load_order is not None:
print(f'Loading order file {args.load_order}\n')
with open(args.load_order, 'r') as f:
order = ContractionScheme.load(f)
else:
order = tn.find_order(**kwargs)
print(order.cost)
if args.save_order is not None:
print(f'Saving order file {args.save_order}\n')
with open(args.save_order, 'w') as f:
ContractionScheme.dump(order, f)
tsk = tn.compile(order, **kwargs)
print("Number of subtasks per batch --- %d ---" % (tsk.length))
pp_time_TZ = time.time()
compile_time = time.time()
print("TaiZhang Preprocessing Time --- %s seconds ---" % (pp_time_TZ - start_time_TZ))
start_time = time.time()
results = 0
num_samps = 5
tsk.cast('complex64')
for i in range(num_samps):
res = tsk[i].execute(**kwargs)
results += res
compute_time = time.time()
print(results)
tm = timedelta(seconds=args.num_amplitudes * (compute_time - start_time) * tsk.length / num_samps / 27648)
print("Compute Time --- %s seconds ---" % (compute_time - start_time))
print(f'Projected Running Time --- {tm} ---')