def run(d, fname): f = open(fname, "w") f.write("name,Orig. total,Orig. CNOT,Orig. T,PyZX total,PyZX CNOT,PyZX T,time\n") for fname in os.listdir(d): print("Processing %s..." % fname) circ = zx.Circuit.load(os.path.join(d, fname)).to_basic_gates() num_gates_before = len(circ.gates) cnot_count_before = circ.twoqubitcount() t_count_before = zx.tcount(circ) print("Original:\t Total %d, CNOT %d, T %d" % (num_gates_before, cnot_count_before, t_count_before)) start = time.perf_counter() # start timer g = circ.to_graph() zx.full_reduce(g,quiet=False) g.normalize() new_circ = zx.extract_circuit(g).to_basic_gates() stop = time.perf_counter() # stop timer num_gates_after = len(new_circ.gates) cnot_count_after = new_circ.twoqubitcount() t_count_after = zx.tcount(new_circ) print("Final:\t Total %d, CNOT %d, T %d\n" % (num_gates_after, cnot_count_after, t_count_after)) f.write("%s,%d,%d,%d,%d,%d,%d,%f\n" % (fname, num_gates_before, cnot_count_before, t_count_before, num_gates_after, cnot_count_after, t_count_after, stop - start))
def run_on_nam_benchmarks(fname): f = open(fname, "w") d = "nam-benchmarks" # This may need to be changed depending on where the script is run from! f.write( "name, num gates before, t-count before, num gates after, t-count after\n" ) for fname in os.listdir(d): print("Processing %s..." % fname) circ = zx.Circuit.load(os.path.join(d, fname)).to_basic_gates() num_gates_before = len(circ.gates) t_count_before = zx.tcount(circ) print("\nORIGINAL: %d gates, %d T-gates" % (num_gates_before, t_count_before)) g = circ.to_graph() zx.simplify.full_reduce(g, quiet=False) g.normalise() new_circ = zx.extract.streaming_extract(g).to_basic_gates() num_gates_after = len(new_circ.gates) t_count_after = zx.tcount(new_circ) print("OPTIMIZED: %d gates, %d T-gates\n" % (num_gates_after, t_count_after)) f.write("%s,%d,%d,%d,%d\n" % (fname, num_gates_before, t_count_before, num_gates_after, t_count_after))
def run(d, fname): f = open(fname, "w") f.write("name,T,2q,total,time\n") for fname in os.listdir(d): print("Processing %s..." % fname) circ = zx.Circuit.load(os.path.join(d, fname)).to_basic_gates() start = time.perf_counter() g = circ.to_graph() zx.full_reduce(g, quiet=False) g.normalize() new_circ = zx.extract_circuit(g).to_basic_gates() stop = time.perf_counter() total_count = len(new_circ.gates) two_count = new_circ.twoqubitcount() t_count = zx.tcount(new_circ) print("\t Total %d, T %d, CNOT %d\n" % (total_count, t_count, two_count)) f.write("%s,%d,%d,%d,%f\n" % (fname, t_count, two_count, total_count, stop - start)) f.close()
def run(d, fname): f = open(fname, "w") f.write("name,T,2q,total,time\n") for fname in os.listdir(d): print("Processing %s..." % fname) circ = zx.Circuit.load(os.path.join(d, fname)).to_basic_gates() start = time.perf_counter() g = circ.to_graph() zx.full_reduce(g) g.normalize() opt_circ = zx.extract_circuit(g).to_basic_gates() stop = time.perf_counter() elapsed = stop - start if elapsed < TIMEOUT: try: # try to run full_optimize, cancelling after 10 minutes - elapsed with time_limit(TIMEOUT - round(elapsed)): opt_circ = zx.full_optimize(opt_circ) stop = time.perf_counter() except TimeoutException: print("\tfull_optimize is slow; only running full_reduce") else: print("\tfull_optimize is slow; only running full_reduce") total_count = len(opt_circ.gates) two_count = opt_circ.twoqubitcount() t_count = zx.tcount(opt_circ) print("\tTotal %d, T %d, 2-qubit %d\n" % (total_count, t_count, two_count)) f.write("%s,%d,%d,%d,%f\n" % (fname, t_count, two_count, total_count, stop - start)) f.close()
def run(self): if self.has_run: return True #try: if self.fname_after: c = zx.Circuit.from_quipper_file(self.fname_after).to_basic_gates() self.t_opt = c.tcount() else: self.t_opt = '-' c = zx.Circuit.load(self.fname_before).to_basic_gates() self.qubits = c.qubits #except TypeError: return False if self.fname_tpar: c2 = zx.Circuit.load(self.fname_tpar) self.tpar = c2.tcount() else: self.tpar = "-" self.gatecount = len(c.gates) self.t_before = c.tcount() g = c.to_graph() t = time.time() while True: zx.simplify.full_reduce(g) break m = zx.rules.match_gadgets_phasepoly(g) if not m: break zx.rules.apply_gadget_phasepoly(g, m) self.t_after = zx.tcount(g) self.time_simpl = time.time() - t t = time.time() self.extracts = True try: c2 = zx.extract.streaming_extract(g, quiet=True) self.time_extr = time.time() - t except Exception: self.extracts = False self.time_extr = "-" self.has_run = True del c, g return True