Skip to content
Draft
32 changes: 16 additions & 16 deletions benchmarks/scripts/benchmark_clifford_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@

from tensorflow_quantum.core.ops import tfq_simulate_ops
from tensorflow_quantum.core.serialize.serializer import serialize_circuit
from models.random_clifford_circuit import random_clifford_circuit
import flags
import benchmark_util
from benchmarks.scripts.models.random_clifford_circuit import (
random_clifford_circuit)

from benchmarks.scripts import flags
from benchmarks.scripts import benchmark_util

SEED = 48510234
SRC = os.path.dirname(os.path.realpath(__file__))
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
TEST_PARAMS_1 = flags.TEST_FLAGS(n_qubits=3, n_moments=5, op_density=0.99)
TEST_PARAMS_2 = flags.TEST_FLAGS(n_qubits=4, n_moments=5, op_density=0.99)
TEST_PARAMS_1 = flags.test_flags(n_qubits=3, n_moments=5, op_density=0.99)
TEST_PARAMS_2 = flags.test_flags(n_qubits=4, n_moments=5, op_density=0.99)
ALL_PARAMS = [TEST_PARAMS_1, TEST_PARAMS_2]


Expand All @@ -42,22 +44,20 @@ class CliffordBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
("params_1", TEST_PARAMS_1),
("params_2", TEST_PARAMS_2),
)
def testBenchmarkCliffordCircuitEager(self, params):
def test_benchmark_clifford_circuit_eager(self, params):
"""Test that Op constructs and runs correctly."""
proto_file_path = os.path.join(
SRC, "reports/",
"CliffordBenchmarks.benchmark_clifford_circuit_{}_{}_{}".format(
params.n_qubits, params.n_moments, params.batch_size))
SRC, "reports/", f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")
self.addCleanup(os.remove, proto_file_path)

bench = CliffordBenchmarks(params=params)
bench.benchmark_clifford_circuit_eager()

res = benchmark_util.read_benchmark_entry(proto_file_path)
self.assertEqual(
res.name,
"CliffordBenchmarks.benchmark_clifford_circuit_{}_{}_{}".format(
params.n_qubits, params.n_moments, params.batch_size))
res.name, f"CliffordBenchmarks.benchmark_clifford_circuit_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}")
self.assertEqual(
res.extras.get("n_qubits").double_value, params.n_qubits)
self.assertEqual(
Expand All @@ -77,7 +77,7 @@ class CliffordBenchmarks(tf.test.Benchmark):

def __init__(self, params=None):
"""Pull in command line flags or use provided flags."""
super(CliffordBenchmarks, self).__init__()
super().__init__()
# Allow input params for testing purposes.
self.params = params if params else flags.FLAGS

Expand Down Expand Up @@ -113,11 +113,11 @@ def benchmark_clifford_circuit_eager(self):
'batch_size': self.params.batch_size,
"min_time": min(deltas),
}
name = "benchmark_clifford_circuit_{}_{}_{}".format(
self.params.n_qubits, self.params.n_moments, self.params.batch_size)
name = (f"benchmark_clifford_circuit_{self.params.n_qubits}_"
f"{self.params.n_moments}_{self.params.batch_size}")

full_path = os.path.join(os.environ['TEST_REPORT_FILE_PREFIX'],
"{}.{}".format(self.__class__.__name__, name))
f"{self.__class__.__name__}.{name}")
if os.path.exists(full_path):
os.remove(full_path)

Expand Down
32 changes: 17 additions & 15 deletions benchmarks/scripts/benchmark_op_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@
import numpy as np

from tensorflow_quantum.core.ops import tfq_simulate_ops
import benchmark_util
import flags

from tensorflow_quantum.python import util
from tensorflow_quantum.python.differentiators import (
linear_combination,
parameter_shift,
)

from benchmarks.scripts import benchmark_util
from benchmarks.scripts import flags

SRC = os.path.dirname(os.path.realpath(__file__))
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
TEST_PARAMS_1 = flags.TEST_FLAGS(n_symbols=4,
TEST_PARAMS_1 = flags.test_flags(n_symbols=4,
n_qubits=3,
n_moments=5,
op_density=0.9)
TEST_PARAMS_2 = flags.TEST_FLAGS(n_symbols=3,
TEST_PARAMS_2 = flags.test_flags(n_symbols=3,
n_qubits=4,
n_moments=5,
op_density=0.6)
Expand All @@ -58,13 +59,14 @@ class GradientBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
],
'params': [TEST_PARAMS_1, TEST_PARAMS_2]
})))
def testBenchmarkGradient(self, diff, params):
def test_benchmark_gradient(self, diff, params):
"""Test that op constructs and runs correctly."""

bench_name = "GradientBenchmarks.{}_{}_{}_{}_{}".format(
diff.__class__.__name__, params.n_qubits, params.n_moments,
params.batch_size, params.n_symbols)
proto_file_path = os.path.join(SRC, "reports/", "{}".format(bench_name))
bench_name = (
f"GradientBenchmarks.{diff.__class__.__name__}_"
f"{params.n_qubits}_{params.n_moments}_{params.batch_size}_"
f"{params.n_symbols}")
proto_file_path = os.path.join(SRC, "reports/", bench_name)
self.addCleanup(os.remove, proto_file_path)

bench = GradientBenchmarks(params=params)
Expand Down Expand Up @@ -93,7 +95,7 @@ class GradientBenchmarks(tf.test.Benchmark):

def __init__(self, params=None):
"""Pull in command line flags or use provided flags."""
super(GradientBenchmarks, self).__init__()
super().__init__()
self.params = params if params else flags.FLAGS
self.setup()

Expand All @@ -110,7 +112,8 @@ def setup(self):
replace=True)))
symbol_names = list(symbol_names)

circuit_batch, resolver_batch = util.random_symbol_circuit_resolver_batch(
circuit_batch, resolver_batch = \
util.random_symbol_circuit_resolver_batch(
qubits=qubits,
symbols=symbol_names,
batch_size=self.params.batch_size,
Expand Down Expand Up @@ -151,12 +154,11 @@ def _benchmark_tfq_differentiator(self, differentiator, params):
deltas[i] = time.perf_counter() - start

# Name benchmark logs by differentiator classname.
name = "{}_{}_{}_{}_{}".format(differentiator.__class__.__name__,
params.n_qubits, params.n_moments,
params.batch_size, params.n_symbols)
name = (f"{differentiator.__class__.__name__}_{params.n_qubits}_"
f"{params.n_moments}_{params.batch_size}_{params.n_symbols}")

full_path = os.path.join(os.environ['TEST_REPORT_FILE_PREFIX'],
"{}.{}".format(self.__class__.__name__, name))
f"{self.__class__.__name__}.{name}")
if os.path.exists(full_path):
os.remove(full_path)

Expand Down
32 changes: 16 additions & 16 deletions benchmarks/scripts/benchmark_random_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

from tensorflow_quantum.core.ops import tfq_simulate_ops
from tensorflow_quantum.core.serialize.serializer import serialize_circuit
import flags
import benchmark_util
from benchmarks.scripts import flags
from benchmarks.scripts import benchmark_util

SEED = 63536323
SRC = os.path.dirname(os.path.realpath(__file__))
os.environ['TEST_REPORT_FILE_PREFIX'] = os.path.join(SRC, 'reports/')
TEST_PARAMS_1 = flags.TEST_FLAGS(n_rows=3, n_cols=5, n_moments=5)
TEST_PARAMS_2 = flags.TEST_FLAGS(n_rows=4, n_cols=4, n_moments=20)
TEST_PARAMS_1 = flags.test_flags(n_rows=3, n_cols=5, n_moments=5)
TEST_PARAMS_2 = flags.test_flags(n_rows=4, n_cols=4, n_moments=20)


def make_random_circuit(n_rows, n_cols, depth):
Expand All @@ -49,22 +49,21 @@ class RandomCircuitBenchmarksTest(tf.test.TestCase, parameterized.TestCase):
("params_1", TEST_PARAMS_1),
("params_2", TEST_PARAMS_2),
)
def testBenchmarkRandomCircuit(self, params):
def test_benchmark_random_circuit(self, params):
"""Test that Op constructs and runs correctly."""
proto_file_path = os.path.join(
SRC, "reports/",
"RandomCircuitBenchmarks.benchmark_random_circuit_{}_{}_{}".format(
params.n_rows, params.n_cols, params.n_moments))
f"RandomCircuitBenchmarks.benchmark_random_circuit_"
f"{params.n_rows}_{params.n_cols}_{params.n_moments}")
self.addCleanup(os.remove, proto_file_path)

bench = RandomCircuitBenchmarks(params=params)
bench.benchmark_random_circuit()

res = benchmark_util.read_benchmark_entry(proto_file_path)
self.assertEqual(
res.name,
"RandomCircuitBenchmarks.benchmark_random_circuit_{}_{}_{}".format(
params.n_rows, params.n_cols, params.n_moments))
res.name, f"RandomCircuitBenchmarks.benchmark_random_circuit_"
f"{params.n_rows}_{params.n_cols}_{params.n_moments}")
self.assertEqual(res.extras.get("n_rows").double_value, params.n_rows)
self.assertEqual(res.extras.get("n_cols").double_value, params.n_cols)
self.assertEqual(
Expand All @@ -77,7 +76,7 @@ def testBenchmarkRandomCircuit(self, params):
("params_1", TEST_PARAMS_1),
("params_2", TEST_PARAMS_2),
)
def testRandomCircuitParams(self, params):
def test_random_circuit_params(self, params):
"""Ensure that the random circuits are structured as advertised."""
circuit = make_random_circuit(params.n_rows, params.n_cols,
params.n_moments)
Expand All @@ -95,7 +94,7 @@ class RandomCircuitBenchmarks(tf.test.Benchmark):

def __init__(self, params=None):
"""Pull in command line flags or use provided flags."""
super(RandomCircuitBenchmarks, self).__init__()
super().__init__()
# Allow input params for testing purposes.
self.params = params if params else flags.FLAGS

Expand All @@ -106,7 +105,8 @@ def _simulate_circuit(self, circuit, params):
[[0]] * params.batch_size)

def benchmark_random_circuit(self):
"""Benchmark simulator performance on a classically intractable circuit."""
"""Benchmark simulator performance on
a classically intractable circuit."""

circuit = make_random_circuit(self.params.n_rows, self.params.n_cols,
self.params.n_moments)
Expand All @@ -128,10 +128,10 @@ def benchmark_random_circuit(self):
"min_time": min(deltas),
}

name = "benchmark_random_circuit_{}_{}_{}".format(
self.params.n_rows, self.params.n_cols, self.params.n_moments)
name = (f"benchmark_random_circuit_{self.params.n_rows}_"
f"{self.params.n_cols}_{self.params.n_moments}")
full_path = os.path.join(os.environ['TEST_REPORT_FILE_PREFIX'],
"{}.{}".format(self.__class__.__name__, name))
f"{self.__class__.__name__}.{name}")
if os.path.exists(full_path):
os.remove(full_path)

Expand Down
10 changes: 9 additions & 1 deletion benchmarks/scripts/benchmark_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@
# ==============================================================================
"""Utility functions for benchmark tools."""
import tensorflow as tf
import test_log_pb2
from tensorflow.core.util import test_log_pb2


def read_benchmark_entry(f):
"""Reads a benchmark entry from a file.

Args:
f: File path to read from.

Returns:
The first entry in the benchmark file.
"""
s = tf.io.gfile.GFile(f, "rb").read()
entries = test_log_pb2.BenchmarkEntries.FromString(s)
return entries.entry[0]
4 changes: 2 additions & 2 deletions benchmarks/scripts/benchmark_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import tensorflow as tf

import test_log_pb2
import benchmark_util
from tensorflow.core.util import test_log_pb2
from benchmarks.scripts import benchmark_util


def _make_dummy_benchmark_report():
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/scripts/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
lower_bound=0)


def TEST_FLAGS(**kwargs):
def test_flags(**kwargs):
"""Create a set of test flags by kwarg assignment.

This constructs a named tuple that mimics the interface of absl.flags.
Expand Down
1 change: 1 addition & 0 deletions benchmarks/scripts/models/random_clifford_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module for generating random Clifford circuits."""

from typing import Iterable

Expand Down
4 changes: 3 additions & 1 deletion benchmarks/scripts/models/random_clifford_circuit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for the random Clifford circuit generator."""

from absl.testing import parameterized
import cirq
import numpy as np
import tensorflow as tf

from random_clifford_circuit import random_clifford_circuit
from benchmarks.scripts.models.random_clifford_circuit import (
random_clifford_circuit)


class RandomCliffordCircuitTest(parameterized.TestCase, tf.test.TestCase):
Expand Down
Loading
Loading