3 Created on Fri Jun 26 14:42:56 2020 4 Copyright 2020 Peter Rakyta, Ph.D. 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 10 http://www.apache.org/licenses/LICENSE-2.0 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 18 @author: Peter Rakyta, Ph.D. 25 CNOTGateCount, SingleQubitGateCount, TotalRawGateCount, CircuitGateStats,
28 from squander
import utils
29 from squander
import Qiskit_IO
30 import time, requests, os, zipfile, tempfile, json, numpy
as np
31 from pathlib
import Path
32 from collections
import Counter
35 IBM_EAGLE_BASIS = [
"cx",
"rz",
"sx",
"x"]
39 """Transpile a circuit to IBM Eagle's native gate set and return consistent 40 gate stats via Squander's CircuitGateStats. 43 qasm_path_or_circ: Either a path to a .qasm file, or a Squander Circuit. 44 parameters: Required if passing a Squander Circuit. 47 dict from CircuitGateStats, using the same decomposition as the main 50 from qiskit
import QuantumCircuit, transpile
52 if isinstance(qasm_path_or_circ, str):
53 qc = QuantumCircuit.from_qasm_file(qasm_path_or_circ)
55 qc = Qiskit_IO.get_Qiskit_Circuit(
57 np.asarray(parameters
if parameters
is not None else [], dtype=np.float64),
60 transpiled = transpile(qc, basis_gates=IBM_EAGLE_BASIS, optimization_level=0)
61 eagle_circ, eagle_params = Qiskit_IO.convert_Qiskit_to_Squander(transpiled)
65 if __name__ ==
"__main__":
68 "strategy":
"TreeSearch",
69 "test_subcircuits":
False,
70 "test_final_circuit":
False,
71 "max_partition_size": 4,
74 "use_graph_search":
True,
75 "pre-opt-strategy":
"TreeSearch",
76 "routing-strategy":
"seqpam-ilp",
84 files = [os.path.join(Path(__file__).resolve().parent,
"bv_n14.qasm")]
87 RESULTS_FILE =
"results.json" 92 if os.path.exists(RESULTS_FILE):
93 with open(RESULTS_FILE)
as f:
94 existing_results = json.load(f)
95 print(f
"Loaded {len(existing_results)} existing results; will skip already-processed circuits.")
97 results = existing_results
98 for filename
in files:
99 fname = os.path.basename(filename)
101 print(f
"Skipping already processed: {fname}")
104 print(f
"executing optimization of circuit: {filename}")
108 circ, parameters, _ = utils.qasm_to_squander_circuit(filename)
109 config[
"topology"] = (
110 Wide_Circuit_Optimization.qgd_Wide_Circuit_Optimization.linear_topology(
120 wide_circuit_optimizer = (
121 Wide_Circuit_Optimization.qgd_Wide_Circuit_Optimization({**config})
123 start_time = time.time()
124 optcirc, optparameters = wide_circuit_optimizer.OptimizeWideCircuit(
127 elapsed = time.time() - start_time
132 opt_time = wide_circuit_optimizer.config.get(
"optimization_time",
None)
137 a2a_time = routing_time =
None 138 if wide_circuit_optimizer.config.get(
"routed_circuit",
None)
is not None:
139 a2acirc = wide_circuit_optimizer.config[
"all_to_all_circuit"]
140 routedcirc = wide_circuit_optimizer.config[
"routed_circuit"]
143 a2a_time = wide_circuit_optimizer.config.get(
"all_to_all_optimization_time",
None)
144 routing_time = wide_circuit_optimizer.config.get(
"routing_time",
None)
148 "strategy": config[
"strategy"],
149 "pre_opt_strategy": config[
"pre-opt-strategy"],
150 "routing_strategy": config[
"routing-strategy"],
152 "init_ibm_eagle": init_ibm_eagle,
154 "final_ibm_eagle": final_ibm_eagle,
156 "a2a": round(a2a_time, 2)
if a2a_time
else None,
157 "routing": round(routing_time, 2)
if routing_time
else None,
158 "optimization": round(opt_time, 2)
if opt_time
else None,
159 "total": round(elapsed, 2),
163 result_entry[
"all_to_all"] = a2a_stats
165 result_entry[
"routed"] = routed_stats
167 results[fname] = result_entry
170 with open(RESULTS_FILE,
"w")
as f:
171 json.dump(results, f, indent=2)
173 wide_circuit_optimizer.check_compare_circuits(
178 routing=wide_circuit_optimizer.config.get(
"routed_circuit",
None)
is not None,
180 label=
"example final original-to-output",
183 print(f
" init: {init_stats['cnot_equiv']} CNOT, {init_stats['single_qubit']} 1q, " 184 f
"{init_stats['total_raw']} total, {init_stats['qubits']}q")
185 print(f
" final: {opt_stats['cnot_equiv']} CNOT, {opt_stats['single_qubit']} 1q, " 186 f
"{opt_stats['total_raw']} total")
187 print(f
" time: {elapsed:.2f}s")
188 print(f
"--- {len(results)}/{len(files)} circuits processed ---")
190 print(
"--- %s seconds elapsed during optimization ---" % elapsed)
def transpile_to_ibm_eagle(qasm_path_or_circ, parameters=None)