|
| 1 | +import uuid |
| 2 | +import os |
| 3 | +import json |
| 4 | +import argparse |
| 5 | +import time |
| 6 | +import numpy as np |
| 7 | +import cpuinfo |
| 8 | +import sys |
| 9 | +import datetime |
| 10 | +import tensorcircuit as tc |
| 11 | + |
| 12 | +from benchmark_core import benchmark_mega_function |
| 13 | + |
| 14 | + |
| 15 | +def arg(): |
| 16 | + parser = argparse.ArgumentParser(description="TensorCircuit Benchmark Parameters.") |
| 17 | + parser.add_argument( |
| 18 | + "-n", dest="n", type=int, nargs=1, help="# of Qubits", default=[12] |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "-nlayer", dest="nlayer", type=int, nargs=1, help="# of layers", default=[3] |
| 22 | + ) |
| 23 | + parser.add_argument( |
| 24 | + "-nitrs", dest="nitrs", type=int, nargs=1, help="# of iterations", default=[10] |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "-t", dest="timeLimit", type=int, nargs=1, help="Time limit(s)", default=[600] |
| 28 | + ) |
| 29 | + parser.add_argument( |
| 30 | + "-gpu", dest="isgpu", type=int, nargs=1, help="GPU available", default=[0] |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "-lx", dest="lx", type=int, nargs=1, help="Lattice size x (for 2D)", default=[3] |
| 34 | + ) |
| 35 | + parser.add_argument( |
| 36 | + "-ly", dest="ly", type=int, nargs=1, help="Lattice size y (for 2D)", default=[4] |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "-path", |
| 40 | + dest="path", |
| 41 | + type=str, |
| 42 | + nargs=1, |
| 43 | + help="output json dir path ended with /", |
| 44 | + default=[None], |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + "-circuit_type", |
| 48 | + dest="circuit_type", |
| 49 | + type=str, |
| 50 | + nargs=1, |
| 51 | + help="Type of circuit (circuit, dmcircuit, mpscircuit)", |
| 52 | + default=["circuit"], |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "-layout_type", |
| 56 | + dest="layout_type", |
| 57 | + type=str, |
| 58 | + nargs=1, |
| 59 | + help="Circuit layout (1d, 2d)", |
| 60 | + default=["1d"], |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "-operation", |
| 64 | + dest="operation", |
| 65 | + type=str, |
| 66 | + nargs=1, |
| 67 | + help="Operation to perform (state, sample, exps)", |
| 68 | + default=["state"], |
| 69 | + ) |
| 70 | + parser.add_argument( |
| 71 | + "-noisy", |
| 72 | + dest="noisy", |
| 73 | + type=int, |
| 74 | + nargs=1, |
| 75 | + help="Whether to add noise (0 or 1)", |
| 76 | + default=[0], |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + "-noisy_type", |
| 80 | + dest="noisy_type", |
| 81 | + type=str, |
| 82 | + nargs=1, |
| 83 | + help="Type of noise channel (depolarizing, amplitudedamping)", |
| 84 | + default=["depolarizing"], |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + "-use_grad", |
| 88 | + dest="use_grad", |
| 89 | + type=int, |
| 90 | + nargs=1, |
| 91 | + help="Whether to compute gradient (0 or 1)", |
| 92 | + default=[0], |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + "-use_vmap", |
| 96 | + dest="use_vmap", |
| 97 | + type=int, |
| 98 | + nargs=1, |
| 99 | + help="Whether to use vectorized operations (0 or 1)", |
| 100 | + default=[0], |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "-batch_size", |
| 104 | + dest="batch_size", |
| 105 | + type=int, |
| 106 | + nargs=1, |
| 107 | + help="Batch size for vmap operations", |
| 108 | + default=[5], |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "-backend", |
| 112 | + dest="backend", |
| 113 | + type=str, |
| 114 | + nargs=1, |
| 115 | + help="Backend to use (tensorflow, jax, pytorch)", |
| 116 | + default=["tensorflow"], |
| 117 | + ) |
| 118 | + parser.add_argument( |
| 119 | + "-dtype", |
| 120 | + dest="dtype", |
| 121 | + type=str, |
| 122 | + nargs=1, |
| 123 | + help="Data type (complex64, complex128)", |
| 124 | + default=["complex64"], |
| 125 | + ) |
| 126 | + parser.add_argument( |
| 127 | + "-contractor", |
| 128 | + dest="contractor", |
| 129 | + type=str, |
| 130 | + nargs=1, |
| 131 | + help="Contractor setting (e.g., cotengra-16-128)", |
| 132 | + default=[None], |
| 133 | + ) |
| 134 | + args = parser.parse_args() |
| 135 | + return [ |
| 136 | + args.n[0], |
| 137 | + args.nlayer[0], |
| 138 | + args.nitrs[0], |
| 139 | + args.timeLimit[0], |
| 140 | + args.isgpu[0], |
| 141 | + args.lx[0], |
| 142 | + args.ly[0], |
| 143 | + args.path[0], |
| 144 | + args.circuit_type[0], |
| 145 | + args.layout_type[0], |
| 146 | + args.operation[0], |
| 147 | + args.noisy[0], |
| 148 | + args.noisy_type[0], |
| 149 | + args.use_grad[0], |
| 150 | + args.use_vmap[0], |
| 151 | + args.batch_size[0], |
| 152 | + args.backend[0], |
| 153 | + args.dtype[0], |
| 154 | + args.contractor[0], |
| 155 | + ] |
| 156 | + |
| 157 | + |
| 158 | +def timing(f, nitrs, timeLimit, params): |
| 159 | + t0 = time.time() |
| 160 | + a = f(params) |
| 161 | + if hasattr(a, "block_until_ready"): |
| 162 | + a.block_until_ready() |
| 163 | + t1 = time.time() |
| 164 | + Nitrs = 1e-8 |
| 165 | + for _ in range(nitrs): |
| 166 | + a = f(params) |
| 167 | + if hasattr(a, "block_until_ready"): |
| 168 | + a.block_until_ready() |
| 169 | + Nitrs += 1 |
| 170 | + if time.time() - t1 > timeLimit: |
| 171 | + break |
| 172 | + t2 = time.time() |
| 173 | + return t1 - t0, (t2 - t1) / Nitrs, int(Nitrs) |
| 174 | + |
| 175 | + |
| 176 | +def save(data, _uuid, path): |
| 177 | + if path is None: |
| 178 | + return |
| 179 | + with open(path + _uuid + ".json", "w") as f: |
| 180 | + json.dump( |
| 181 | + data, |
| 182 | + f, |
| 183 | + indent=4, |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def benchmark_cli( |
| 188 | + uuid, |
| 189 | + n, |
| 190 | + nlayer, |
| 191 | + nitrs, |
| 192 | + timeLimit, |
| 193 | + isgpu, |
| 194 | + lx, |
| 195 | + ly, |
| 196 | + circuit_type, |
| 197 | + layout_type, |
| 198 | + operation, |
| 199 | + noisy, |
| 200 | + noisy_type, |
| 201 | + use_grad, |
| 202 | + use_vmap, |
| 203 | + batch_size, |
| 204 | + backend, |
| 205 | + dtype, |
| 206 | + contractor, |
| 207 | + path, |
| 208 | +): |
| 209 | + meta = {} |
| 210 | + |
| 211 | + # Setup GPU |
| 212 | + if isgpu == 0: |
| 213 | + os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
| 214 | + meta["isgpu"] = "off" |
| 215 | + else: |
| 216 | + meta["isgpu"] = "on" |
| 217 | + |
| 218 | + # Setup backend and dtype |
| 219 | + tc.set_backend(backend) |
| 220 | + tc.set_dtype(dtype) |
| 221 | + |
| 222 | + meta["Software"] = "tensorcircuit-ng" |
| 223 | + meta["Cpuinfo"] = cpuinfo.get_cpu_info()["brand_raw"] |
| 224 | + meta["Version"] = { |
| 225 | + "sys": sys.version, |
| 226 | + "tensorcircuit": tc.__version__, |
| 227 | + "numpy": np.__version__, |
| 228 | + } |
| 229 | + meta["Benchmark test parameters"] = { |
| 230 | + "nQubits": n, |
| 231 | + "nlayer": nlayer, |
| 232 | + "nitrs": nitrs, |
| 233 | + "timeLimit": timeLimit, |
| 234 | + "lx": lx, |
| 235 | + "ly": ly, |
| 236 | + "circuit_type": circuit_type, |
| 237 | + "layout_type": layout_type, |
| 238 | + "operation": operation, |
| 239 | + "noisy": noisy, |
| 240 | + "noisy_type": noisy_type, |
| 241 | + "use_grad": use_grad, |
| 242 | + "use_vmap": use_vmap, |
| 243 | + "batch_size": batch_size, |
| 244 | + "backend": backend, |
| 245 | + "dtype": dtype, |
| 246 | + "contractor": contractor, |
| 247 | + } |
| 248 | + meta["UUID"] = uuid |
| 249 | + meta["Benchmark Time"] = ( |
| 250 | + datetime.datetime.now().astimezone().strftime("%Y-%m-%d %H:%M %Z") |
| 251 | + ) |
| 252 | + meta["Results"] = {} |
| 253 | + |
| 254 | + # Create benchmark function using the mega function |
| 255 | + benchmark_func = benchmark_mega_function( |
| 256 | + nqubits=n, |
| 257 | + nlayers=nlayer, |
| 258 | + lx=lx, |
| 259 | + ly=ly, |
| 260 | + circuit_type=circuit_type, |
| 261 | + layout_type=layout_type, |
| 262 | + operation=operation, |
| 263 | + noisy=bool(noisy), |
| 264 | + noisy_type=noisy_type, |
| 265 | + use_grad=bool(use_grad), |
| 266 | + use_vmap=bool(use_vmap), |
| 267 | + contractor=contractor, |
| 268 | + ) |
| 269 | + |
| 270 | + # Create parameters for testing |
| 271 | + params_shape = (nlayer, n) # Match the format in generate_1d_circuit |
| 272 | + if use_vmap: |
| 273 | + params_shape = (batch_size, nlayer, n) |
| 274 | + |
| 275 | + params = tc.backend.convert_to_tensor( |
| 276 | + np.random.uniform(0, 2 * np.pi, size=params_shape).astype( |
| 277 | + dtype.replace("complex", "float") |
| 278 | + ) |
| 279 | + ) |
| 280 | + |
| 281 | + # Run benchmark |
| 282 | + ct, it, Nitrs = timing(benchmark_func, nitrs, timeLimit, params) |
| 283 | + |
| 284 | + meta["Results"] = { |
| 285 | + "Construction time": ct, |
| 286 | + "Iteration time": it, |
| 287 | + "# of actual iterations": Nitrs, |
| 288 | + } |
| 289 | + |
| 290 | + print(meta) |
| 291 | + return meta |
| 292 | + |
| 293 | + |
| 294 | +if __name__ == "__main__": |
| 295 | + _uuid = str(uuid.uuid4()) |
| 296 | + ( |
| 297 | + n, |
| 298 | + nlayer, |
| 299 | + nitrs, |
| 300 | + timeLimit, |
| 301 | + isgpu, |
| 302 | + lx, |
| 303 | + ly, |
| 304 | + path, |
| 305 | + circuit_type, |
| 306 | + layout_type, |
| 307 | + operation, |
| 308 | + noisy, |
| 309 | + noisy_type, |
| 310 | + use_grad, |
| 311 | + use_vmap, |
| 312 | + batch_size, |
| 313 | + backend, |
| 314 | + dtype, |
| 315 | + contractor, |
| 316 | + ) = arg() |
| 317 | + |
| 318 | + results = benchmark_cli( |
| 319 | + _uuid, |
| 320 | + n, |
| 321 | + nlayer, |
| 322 | + nitrs, |
| 323 | + timeLimit, |
| 324 | + isgpu, |
| 325 | + lx, |
| 326 | + ly, |
| 327 | + circuit_type, |
| 328 | + layout_type, |
| 329 | + operation, |
| 330 | + noisy, |
| 331 | + noisy_type, |
| 332 | + use_grad, |
| 333 | + use_vmap, |
| 334 | + batch_size, |
| 335 | + backend, |
| 336 | + dtype, |
| 337 | + contractor, |
| 338 | + path, |
| 339 | + ) |
| 340 | + save(results, _uuid, path) |
0 commit comments