Last active
May 5, 2022 08:57
-
-
Save refraction-ray/754bde72825a1d20ab1bc8dc22d5885e to your computer and use it in GitHub Desktop.
tensorflow quantum vs tensorcircuit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import tensorflow_quantum as tfq | |
import cirq | |
import sympy | |
import numpy as np | |
nwires, nlayers = 6, 3 | |
qubits = [cirq.GridQubit(0, i) for i in range(nwires)] | |
symbols = sympy.symbols("params_0:" + str(nlayers * nwires * 2)) | |
circuit = cirq.Circuit() | |
for i in range(nwires): | |
circuit.append(cirq.H(qubits[i])) | |
for j in range(nlayers): | |
for i in range(nwires - 1): | |
circuit.append( | |
cirq.ZZPowGate(exponent=symbols[j * nwires * 2 + i])( | |
qubits[i], qubits[(i + 1)] | |
) | |
) | |
for i in range(nwires): | |
circuit.append(cirq.rx(symbols[j * nwires * 2 + nwires + i])(qubits[i])) | |
circuit = tfq.convert_to_tensor([circuit]) | |
hamiltonian = tfq.convert_to_tensor( | |
[ | |
[ | |
sum( | |
[cirq.Z(qubits[i]) * cirq.Z(qubits[i + 1]) for i in range(nwires - 1)] | |
+ [-1.0 * cirq.X(qubits[i]) for i in range(nwires)] | |
) | |
] | |
] | |
) | |
ep = tfq.layers.Expectation() | |
@tf.function | |
def tf_vag(symbol_values): | |
with tf.GradientTape() as g: | |
g.watch(symbol_values) | |
expectations = ep( | |
circuit, | |
symbol_names=symbols, | |
symbol_values=symbol_values, | |
operators=hamiltonian, | |
) | |
grads = g.gradient(expectations, [symbol_values]) | |
return expectations, grads | |
symbol_values = [np.random.normal(size=[nlayers * nwires * 2]).astype(np.float32)] | |
symbol_values = tf.Variable(tf.convert_to_tensor(symbol_values)) | |
print(tf_vag(symbol_values)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorcircuit as tc | |
tc.set_backend("tensorflow") | |
nwires, nlayers = 6, 3 | |
def vqe_forward(param): | |
c = tc.Circuit(nwires) | |
for i in range(nwires): | |
c.H(i) | |
for j in range(nlayers): | |
for i in range(nwires - 1): | |
c.exp1(i, i + 1, theta=param[2 * j, i], unitary=tc.gates._zz_matrix) | |
for i in range(nwires): | |
c.rx(i, theta=param[2 * j + 1, i]) | |
e = sum( | |
[-1.0 * c.expectation_ps(x=[i]) for i in range(nwires)] | |
+ [1.0 * c.expectation_ps(z=[i, i + 1]) for i in range(nwires - 1)] | |
) | |
return e | |
tc_vag = tc.backend.jit(tc.backend.value_and_grad(vqe_forward)) | |
param = tc.backend.cast(tc.backend.randn([2 * nlayers, nwires]), "complex64") | |
print(tc_vag(param)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
imported packages: 5 vs 1
lines: 47 vs 20 (twice)