|
| 1 | +# Copyright 2020 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tool for benchmarking serialization of large circuits. |
| 16 | +
|
| 17 | +This tool was originally introduced to enable comparison of the two JSON |
| 18 | +serialization protocols (gzip and non-gzip): |
| 19 | +https://github.com/quantumlib/Cirq/pull/3662 |
| 20 | +
|
| 21 | +This is part of the "efficient serialization" effort: |
| 22 | +https://github.com/quantumlib/Cirq/issues/3438 |
| 23 | +
|
| 24 | +Run this benchmark with the following command (make sure to install cirq-dev): |
| 25 | +
|
| 26 | + python3 dev_tools/profiling/benchmark_serializers.py \ |
| 27 | + --num_gates=<int> --nesting_depth=<int> --num_repetitions=<int> |
| 28 | +
|
| 29 | +WARNING: runtime increases exponentially with nesting_depth. Values much |
| 30 | +higher than nesting_depth=10 are not recommended. |
| 31 | +""" |
| 32 | + |
| 33 | +import argparse |
| 34 | +import sys |
| 35 | +import timeit |
| 36 | + |
| 37 | +import numpy as np |
| 38 | + |
| 39 | +import cirq |
| 40 | + |
| 41 | +_JSON_GZIP = 'json_gzip' |
| 42 | +_JSON = 'json' |
| 43 | + |
| 44 | +NUM_QUBITS = 8 |
| 45 | + |
| 46 | +SUFFIXES = ['B', 'kB', 'MB', 'GB', 'TB'] |
| 47 | + |
| 48 | + |
| 49 | +def serialize(serializer: str, num_gates: int, nesting_depth: int) -> int: |
| 50 | + """"Runs a round-trip of the serializer.""" |
| 51 | + circuit = cirq.Circuit() |
| 52 | + for _ in range(num_gates): |
| 53 | + which = np.random.choice(['expz', 'expw', 'exp11']) |
| 54 | + if which == 'expw': |
| 55 | + q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS)) |
| 56 | + circuit.append( |
| 57 | + cirq.PhasedXPowGate( |
| 58 | + phase_exponent=np.random.random(), exponent=np.random.random() |
| 59 | + ).on(q1) |
| 60 | + ) |
| 61 | + elif which == 'expz': |
| 62 | + q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS)) |
| 63 | + circuit.append(cirq.Z(q1) ** np.random.random()) |
| 64 | + elif which == 'exp11': |
| 65 | + q1 = cirq.GridQubit(0, np.random.randint(NUM_QUBITS - 1)) |
| 66 | + q2 = cirq.GridQubit(0, q1.col + 1) |
| 67 | + circuit.append(cirq.CZ(q1, q2) ** np.random.random()) |
| 68 | + cs = [circuit] |
| 69 | + for _ in range(1, nesting_depth): |
| 70 | + fc = cs[-1].freeze() |
| 71 | + cs.append(cirq.Circuit(fc.to_op(), fc.to_op())) |
| 72 | + test_circuit = cs[-1] |
| 73 | + |
| 74 | + if serializer == _JSON: |
| 75 | + json_data = cirq.to_json(test_circuit) |
| 76 | + assert json_data is not None |
| 77 | + data_size = len(json_data) |
| 78 | + cirq.read_json(json_text=json_data) |
| 79 | + elif serializer == _JSON_GZIP: |
| 80 | + gzip_data = cirq.to_json_gzip(test_circuit) |
| 81 | + assert gzip_data is not None |
| 82 | + data_size = len(gzip_data) |
| 83 | + cirq.read_json_gzip(gzip_raw=gzip_data) |
| 84 | + return data_size |
| 85 | + |
| 86 | + |
| 87 | +def main( |
| 88 | + num_gates: int, |
| 89 | + nesting_depth: int, |
| 90 | + num_repetitions: int, |
| 91 | + setup: str = 'from __main__ import serialize', |
| 92 | +): |
| 93 | + for serializer in [_JSON_GZIP, _JSON]: |
| 94 | + print() |
| 95 | + print(f'Using serializer "{serializer}":') |
| 96 | + command = f'serialize(\'{serializer}\', {num_gates}, {nesting_depth})' |
| 97 | + time = timeit.timeit(command, setup, number=num_repetitions) |
| 98 | + print(f'Round-trip serializer time: {time / num_repetitions}s') |
| 99 | + data_size = float(serialize(serializer, num_gates, nesting_depth)) |
| 100 | + suffix_idx = 0 |
| 101 | + while data_size > 1000: |
| 102 | + data_size /= 1024 |
| 103 | + suffix_idx += 1 |
| 104 | + print(f'Serialized data size: {data_size} {SUFFIXES[suffix_idx]}.') |
| 105 | + |
| 106 | + |
| 107 | +def parse_arguments(args): |
| 108 | + parser = argparse.ArgumentParser('Benchmark a serializer.') |
| 109 | + parser.add_argument( |
| 110 | + '--num_gates', default=100, type=int, help='Number of gates at the bottom nesting layer.' |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + '--nesting_depth', |
| 114 | + default=1, |
| 115 | + type=int, |
| 116 | + help='Depth of nested subcircuits. Total gate count will be 2^nesting_depth * num_gates.', |
| 117 | + ) |
| 118 | + parser.add_argument( |
| 119 | + '--num_repetitions', default=10, type=int, help='Number of times to repeat serialization.' |
| 120 | + ) |
| 121 | + return vars(parser.parse_args(args)) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + main(**parse_arguments(sys.argv[1:])) |
0 commit comments