Skip to content
Merged
12 changes: 12 additions & 0 deletions benchmarks/scripts/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ licenses(["notice"])
# Export for the PIP package.
exports_files(["__init__.py"])

py_library(
name = "benchmark_flags",
srcs = ["flags.py"],
)

py_test(
name = "benchmark_clifford_circuit",
srcs = ["benchmark_clifford_circuit.py"],
python_version = "PY3",
deps = [
":benchmark_flags",
":benchmark_util",
"//benchmarks/scripts/models:random_clifford_circuit",
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
"//tensorflow_quantum/core/serialize:serializer",
"@local_config_tf//:test_log_pb2",
Expand All @@ -35,6 +43,8 @@ py_test(
srcs = ["benchmark_random_circuit.py"],
python_version = "PY3",
deps = [
":benchmark_flags",
":benchmark_util",
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
"//tensorflow_quantum/core/serialize:serializer",
"@local_config_tf//:test_log_pb2",
Expand All @@ -46,6 +56,8 @@ py_test(
srcs = ["benchmark_op_gradients.py"],
python_version = "PY3",
deps = [
":benchmark_flags",
":benchmark_util",
"//tensorflow_quantum/core/ops:batch_util",
"//tensorflow_quantum/core/ops:cirq_ops",
"//tensorflow_quantum/core/ops:tfq_simulate_ops_py",
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/scripts/benchmark_op_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def setup(self):
for resolver in resolver_batch],
dtype=np.float32)

self.symbol_names = symbol_names
self.symbol_names_tensor = tf.convert_to_tensor(symbol_names)
self.symbol_values_tensor = tf.convert_to_tensor(symbol_values_array)
self.programs = util.convert_to_tensor(circuit_batch)
self.psums = util.convert_to_tensor([psums])
Expand All @@ -140,15 +140,15 @@ def _benchmark_tfq_differentiator(self, differentiator, params):
analytic_op=tfq_simulate_ops.tfq_simulate_expectation)

for _ in range(params.n_burn):
op(self.programs, self.symbol_names, self.symbol_values_tensor,
self.psums)
op(self.programs, self.symbol_names_tensor,
self.symbol_values_tensor, self.psums)

deltas = [None] * params.n_runs
for i in range(params.n_runs):
start = time.perf_counter()
with tf.GradientTape() as g:
g.watch(self.symbol_values_tensor)
expectations = op(self.programs, self.symbol_names,
expectations = op(self.programs, self.symbol_names_tensor,
self.symbol_values_tensor, self.psums)
g.gradient(expectations, self.symbol_values_tensor)
deltas[i] = time.perf_counter() - start
Expand Down
43 changes: 37 additions & 6 deletions benchmarks/scripts/benchmark_random_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,43 @@


def make_random_circuit(n_rows, n_cols, depth):
"""Generate a random unparameterized circuit of fixed depth."""
return cirq.experiments.generate_boixo_2018_supremacy_circuits_v2_grid(
n_rows=n_rows,
n_cols=n_cols,
cz_depth=depth - 2, # Account for beginning/ending Hadamard layers
seed=SEED)
"""Generate a random unparameterized circuit of fixed depth.

Uses a simple supremacy-style pattern on a 2D grid that is robust across
Cirq versions.
"""
qubits = list(cirq.GridQubit.rect(n_rows, n_cols))
grid = {(q.row, q.col): q for q in qubits}
rng = np.random.RandomState(SEED)
single_qubit_gates = (cirq.X**0.5, cirq.Y**0.5)
moments = []

for moment_index in range(depth):
if moment_index % 2 == 0:
# Random single-qubit layer across the full grid.
moments.append(
cirq.Moment(rng.choice(single_qubit_gates)(q) for q in qubits))
continue

# Alternate horizontal and vertical nearest-neighbor CZ interactions.
parity = (moment_index // 2) % 2
use_horizontal = ((moment_index // 2) % 2) == 0
Comment thread
kenya-sk marked this conversation as resolved.
Outdated
two_qubit_ops = []
if use_horizontal:
for r in range(n_rows):
for c in range(n_cols - 1):
if (r + c) % 2 == parity:
two_qubit_ops.append(
cirq.CZ(grid[(r, c)], grid[(r, c + 1)]))
else:
for r in range(n_rows - 1):
for c in range(n_cols):
if (r + c) % 2 == parity:
two_qubit_ops.append(
cirq.CZ(grid[(r, c)], grid[(r + 1, c)]))
Comment thread
kenya-sk marked this conversation as resolved.
Outdated
moments.append(cirq.Moment(two_qubit_ops))

return cirq.Circuit(moments)


class RandomCircuitBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/scripts/models/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ licenses(["notice"])
# Export for the PIP package.
exports_files(["__init__.py"])

py_binary(
py_library(
name = "random_clifford_circuit",
srcs = ["random_clifford_circuit.py"],
python_version = "PY3",
)

py_test(
Expand Down
8 changes: 4 additions & 4 deletions scripts/test_benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -26,4 +26,4 @@ else
echo "Testing failed, please correct errors before proceeding."
echo "{$test_outputs}"
exit 64;
fi
fi
Loading