Sequential Quantum Gate Decomposer  v1.9.6
Powerful decomposition of general unitarias into one- and two-qubit gates gates
benchmarks/partitioning/gatesupport.py
Go to the documentation of this file.
1 from qiskit import QuantumCircuit
2 from squander.partitioning.tools import qiskit_to_squander_name
3 import os
4 import tempfile
5 import requests
6 import zipfile
7 import shutil
8 from pathlib import Path
9 
10 from squander.gates import gates_Wrapper as gate
11 SUPPORTED_GATES = {x for n in dir(gate) for x in (getattr(gate, n),) if not n.startswith("_") and issubclass(x, gate.Gate) and n != "Gate"}
12 SUPPORTED_GATES_NAMES = {n for n in dir(gate) if not n.startswith("_") and issubclass(getattr(gate, n), gate.Gate) and n != "Gate"}
13 
15  """
16  Downloads GitHub repositories as zip archives, extracts them into a temporary
17  directory, and returns the list of all .qasm file paths found, along with
18  the temp folder path (for later cleanup).
19 
20  Args:
21  repo_urls (list[str]): List of GitHub repository URLs.
22 
23  Returns:
24  (list[str], str): List of QASM file paths and the temp directory path.
25  """
26  temp_dir = tempfile.mkdtemp(prefix="repos_")
27  qasm_files = []
28 
29  for url in repo_urls:
30  # Normalize repo URL -> owner/repo
31  parts = url.rstrip("/").replace(".git", "").split("/")
32  if len(parts) < 2:
33  raise ValueError(f"Invalid repo URL: {url}")
34  owner, repo = parts[-2], parts[-1]
35 
36  # GitHub zip URL (main branch assumed; you can change to 'master' if needed)
37  zip_url = f"https://github.com/{owner}/{repo}/archive/refs/heads/master.zip"
38 
39  zip_path = os.path.join(temp_dir, f"{repo}.zip")
40 
41  # Download zip
42  r = requests.get(zip_url, stream=True)
43  if r.status_code != 200:
44  raise RuntimeError(f"Failed to download {zip_url}: HTTP {r.status_code}")
45  with open(zip_path, "wb") as f:
46  for chunk in r.iter_content(chunk_size=8192):
47  f.write(chunk)
48 
49  # Extract zip
50  extract_path = os.path.join(temp_dir, repo)
51  with zipfile.ZipFile(zip_path, "r") as zf:
52  zf.extractall(extract_path)
53 
54  # Find QASM files
55  for path in Path(extract_path).rglob("*.qasm"):
56  qasm_files.append(str(path.resolve()))
57 
58  return qasm_files, temp_dir
59 
60 
61 def cleanup_repo(temp_dir):
62  """
63  Deletes the temporary repository folder created by download_and_collect_qasm.
64  """
65  shutil.rmtree(temp_dir, ignore_errors=True)
66 
68  #Veri-Q/Benchmark Missing gates: {'CCX': 172, 'CU1': 297, 'MEASURE': 232, 'BARRIER': 16, 'SWAP': 34, 'SDG': 19, 'MCX': 30, 'MCX_GRAY': 5, 'IF_ELSE': 203, 'SXDG': 19}
69  #pnnl/QASMBench Missing gates: {'MEASURE': 238, 'BARRIER': 118, 'CCX': 26, 'RESET': 19, 'SDG': 6, 'IF_ELSE': 18, 'CRZ': 2, 'CSWAP': 18, 'RZZ': 2, 'RYY': 6, 'ADD4': 1, 'SWAP': 3, 'P': 1, 'CP': 1, 'CU1': 4, 'UNMAJ': 1, 'MAJORITY': 1, 'ID': 1, 'CTU': 2, 'SYNDROME': 1, 'RYY_<value>': 637}
70  for repo in ["https://github.com/iic-jku/ibm_qx_mapping", "https://github.com/Veri-Q/Benchmark", "https://github.com/pnnl/QASMBench", "https://github.com/QML-Group/qbench"]:
71  qasm, temp = download_and_collect_qasm([repo])
72  #print(qasm, temp)
73  bad_files, missing_gates = [], {}
74  for filename in qasm:
75  try:
76  qc = QuantumCircuit.from_qasm_file(filename)
77  except Exception as e:
78  bad_files.append(filename)
79  #print(e)
80  continue
81  qc_gates_names = {qiskit_to_squander_name(inst.operation.name) for inst in qc.data}
82  if not qc_gates_names.issubset(SUPPORTED_GATES_NAMES):
83  for x in qc_gates_names-SUPPORTED_GATES_NAMES:
84  if not x in missing_gates: missing_gates[x] = 0
85  missing_gates[x] += 1
86  #print(f"Filename: {filename.replace(temp, '')} Unsupported gates: {qc_gates_names-SUPPORTED_GATES_NAMES}")
87  print(repo, "Missing gates:", missing_gates)
88  cleanup_repo(temp)
89 
90 if __name__ == "__main__":
Definition: split.py:1
def download_and_collect_qasm(repo_urls)
def qiskit_to_squander_name(qiskit_name)
Definition: tools.py:167