-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathbp_benchmark.py
144 lines (113 loc) · 4.2 KB
/
bp_benchmark.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
"""
benchmark on barren plateau using tfq and tc
"""
import os
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "true"
import time
import tensorflow as tf
import cirq
import sympy
import numpy as np
import tensorflow_quantum as tfq
import tensorcircuit as tc
def benchmark(f, *args, tries=3):
time0 = time.time()
f(*args)
time1 = time.time()
for _ in range(tries):
print(f(*args))
time2 = time.time()
print("staging time: ", time1 - time0, "running time: ", (time2 - time1) / tries)
def tfq_approach(n_qubits=10, depth=10, n_circuits=100):
"""adopted from https://www.tensorflow.org/quantum/tutorials/barren_plateaus"""
def generate_random_qnn(qubits, symbol, depth):
circuit = cirq.Circuit()
for qubit in qubits:
circuit += cirq.ry(np.pi / 4.0)(qubit)
j = 0
for _ in range(depth):
# Add a series of single qubit rotations.
for qubit in qubits:
random_n = np.random.uniform()
if random_n > 2.0 / 3.0:
# Add a Z.
circuit += cirq.rz(symbol[j])(qubit)
elif random_n > 1.0 / 3.0:
# Add a Y.
circuit += cirq.ry(symbol[j])(qubit)
else:
# Add a X.
circuit += cirq.rx(symbol[j])(qubit)
j += 1
# Add CZ ladder.
for src, dest in zip(qubits, qubits[1:]):
circuit += cirq.CZ(src, dest)
return circuit
def process_batch(circuits, symbol, op):
# Setup a simple layer to batch compute the expectation gradients.
expectation = tfq.layers.Expectation()
# Prep the inputs as tensors
circuit_tensor = tfq.convert_to_tensor(circuits)
values_tensor = tf.convert_to_tensor(
np.random.uniform(0, 2 * np.pi, (n_circuits, 100)).astype(np.float32)
)
# Use TensorFlow GradientTape to track gradients.
with tf.GradientTape() as g:
g.watch(values_tensor)
forward = expectation(
circuit_tensor,
operators=op,
symbol_names=symbol,
symbol_values=values_tensor,
)
# Return variance of gradients across all circuits.
grads = g.gradient(forward, values_tensor)
grad_var = tf.math.reduce_std(grads, axis=0)
return grad_var.numpy()[0]
qubits = cirq.GridQubit.rect(1, n_qubits)
symbol = sympy.symbols("theta_{0:100}")
circuits = [generate_random_qnn(qubits, symbol, depth) for _ in range(n_circuits)]
op = cirq.Z(qubits[0]) * cirq.Z(qubits[1])
theta_var = process_batch(circuits, symbol, op)
return theta_var
benchmark(tfq_approach)
K = tc.set_backend("tensorflow")
Rx = tc.gates.rx
Ry = tc.gates.ry
Rz = tc.gates.rz
def op_expectation(params, seed, n_qubits, depth):
paramsc = tc.backend.cast(params, dtype="float32") # parameters of gates
seedc = tc.backend.cast(seed, dtype="float32")
# parameters of circuit structure
c = tc.Circuit(n_qubits)
for i in range(n_qubits):
c.ry(i, theta=np.pi / 4)
for l in range(depth):
for i in range(n_qubits):
c.unitary_kraus(
[Rx(paramsc[i, l]), Ry(paramsc[i, l]), Rz(paramsc[i, l])],
i,
prob=[1 / 3, 1 / 3, 1 / 3],
status=seedc[i, l],
)
for i in range(n_qubits - 1):
c.cz(i, i + 1)
return K.real(c.expectation_ps(z=[0, 1]))
op_expectation_vmap_vvag = K.jit(
K.vvag(op_expectation, argnums=0, vectorized_argnums=(0, 1)),
static_argnums=(2, 3),
)
def tc_approach(n_qubits=10, depth=10, n_circuits=100):
seed = tc.array_to_tensor(
np.random.uniform(low=0.0, high=1.0, size=[n_circuits, n_qubits, depth]),
dtype="float32",
)
params = tc.array_to_tensor(
np.random.uniform(low=0.0, high=2 * np.pi, size=[n_circuits, n_qubits, depth]),
dtype="float32",
)
_, grad = op_expectation_vmap_vvag(params, seed, n_qubits, depth)
# the gradient variance of the first parameter
grad_var = tf.math.reduce_std(K.numpy(grad), axis=0)
return grad_var[0, 0]
benchmark(tc_approach)