Repository: cda-tum/mqt-bench (language: python)

Issue:
Remove Histogram Function
Since it is not used anymore, the respective functions to print and save a histogram of a circuit may be removed.

- [x] Remove histogram related functions


Discussion / hints:

Fix patch:
diff --git a/hist_output/.gitkeep b/hist_output/.gitkeep
deleted file mode 100644
index e69de29bb..000000000
diff --git a/mqt/bench/benchmark_generator.py b/mqt/bench/benchmark_generator.py
index 96e179736..7e3504002 100644
--- a/mqt/bench/benchmark_generator.py
+++ b/mqt/bench/benchmark_generator.py
@@ -57,10 +57,6 @@ def create_benchmarks_from_config(cfg_path: str):
         print("Read config successful")
 
     # global seetings
-    global save_png
-    save_png = cfg["save_png"]
-    global save_hist
-    save_hist = cfg["save_hist"]
     global max_depth
     max_depth = cfg["max_depth"]
     global timeout
@@ -311,21 +307,17 @@ def generate_benchmark(benchmark):
 
 def generate_circuits_on_all_levels(qc, num_qubits, file_precheck):
     characteristics = []
-    # filename_algo, depth = generate_algo_level_circuit(qc, n, save_png, save_hist)
+    # filename_algo, depth = generate_algo_level_circuit(qc, n)
     # characteristics.append([filename_algo, n, depth])
 
-    res_t_indep = generate_target_indep_level_circuit(
-        qc, num_qubits, save_png, save_hist, file_precheck
-    )
+    res_t_indep = generate_target_indep_level_circuit(qc, num_qubits, file_precheck)
 
     if res_t_indep:
         characteristics.extend(res_t_indep)
     else:
         return characteristics
 
-    res_t_dep = generate_target_dep_level_circuit(
-        qc, num_qubits, save_png, save_hist, file_precheck
-    )
+    res_t_dep = generate_target_dep_level_circuit(qc, num_qubits, file_precheck)
 
     if res_t_dep:
         characteristics.extend(res_t_dep)
@@ -336,12 +328,11 @@ def generate_circuits_on_all_levels(qc, num_qubits, file_precheck):
 
 
 def generate_algo_level_circuit(
-    qc: QuantumCircuit, num_qubits: int, save_png, save_hist
+    qc: QuantumCircuit,
+    num_qubits: int,
 ):
     characteristics = []
-    res = benchmark_generation_watcher(
-        utils.handle_algorithm_level, [qc, num_qubits, save_png, save_hist]
-    )
+    res = benchmark_generation_watcher(utils.handle_algorithm_level, [qc, num_qubits])
     characteristics.append(res)
 
     if res:
@@ -352,13 +343,13 @@ def generate_algo_level_circuit(
 
 
 def generate_target_indep_level_circuit(
-    qc: QuantumCircuit, num_qubits: int, save_png, save_hist, file_precheck
+    qc: QuantumCircuit, num_qubits: int, file_precheck
 ):
     characteristics = []
 
     res = benchmark_generation_watcher(
         utils.get_indep_level,
-        [qc, num_qubits, save_png, save_hist, file_precheck],
+        [qc, num_qubits, file_precheck],
     )
     if res:
         characteristics.append(res)
@@ -368,7 +359,7 @@ def generate_target_indep_level_circuit(
 
 
 def generate_target_dep_level_circuit(
-    qc: QuantumCircuit, num_qubits: int, save_png, save_hist, file_precheck
+    qc: QuantumCircuit, num_qubits: int, file_precheck
 ):
     characteristics = []
 
@@ -390,8 +381,6 @@ def generate_target_dep_level_circuit(
                         gate_set_name,
                         opt_level,
                         num_qubits,
-                        save_png,
-                        save_hist,
                         file_precheck,
                     ],
                 )
@@ -411,8 +400,6 @@ def generate_target_dep_level_circuit(
                         opt_level,
                         n_actual,
                         True,
-                        save_png,
-                        save_hist,
                         file_precheck,
                     ],
                 )
@@ -430,8 +417,6 @@ def generate_target_dep_level_circuit(
                         opt_level,
                         n_actual,
                         False,
-                        save_png,
-                        save_hist,
                         file_precheck,
                     ],
                 )
diff --git a/mqt/bench/utils/utils.py b/mqt/bench/utils/utils.py
index 2e6f54b63..37fd8e17f 100644
--- a/mqt/bench/utils/utils.py
+++ b/mqt/bench/utils/utils.py
@@ -1,7 +1,6 @@
-from qiskit import QuantumCircuit, Aer, __qiskit_version__
+from qiskit import QuantumCircuit, __qiskit_version__
 from qiskit.compiler import transpile
 from qiskit.transpiler import CouplingMap
-from qiskit.visualization import plot_histogram
 from qiskit.circuit import qpy_serialization
 from qiskit.algorithms import EstimationProblem
 
@@ -9,7 +8,7 @@
 
 import networkx as nx
 import numpy as np
-from os import path, mkdir
+from os import path
 
 from qiskit.test.mock import (
     FakeBogota,
@@ -142,37 +141,6 @@ def get_examplary_max_cut_qp(n_nodes: int, degree: int = 2):
     return maxcut.to_quadratic_program()
 
 
-def sim_and_print_hist(qc: QuantumCircuit, filename: str):
-    """Simulates a given quantum circuit and prints its resulting histogram.
-
-    Keyword arguments:
-    qc -- to be simulated quantum circuit
-    filename -- filename of the histogram
-    """
-
-    if not path.isdir("./hist_output"):
-        mkdir("hist_output")
-
-    simulator = Aer.get_backend("aer_simulator")
-    result = simulator.run(qc.decompose(), shots=1024).result()
-    counts = result.get_counts()
-    plot = plot_histogram(counts, figsize=(15, 5), title=filename)
-    plot.savefig("hist_output/" + filename + "_hist" + ".png", bbox_inches="tight")
-
-
-def save_circ(qc: QuantumCircuit, filename: str):
-    """Save schematic picture of a given quantum circuit.
-
-    Keyword arguments:
-    qc -- to be saved quantum circuit
-    filename -- filename of the schematic picture
-    """
-    if not path.isdir("./hist_output"):
-        mkdir("hist_output")
-
-    qc.draw(output="mpl", filename="hist_output/" + filename + ".png")
-
-
 class BernoulliA(QuantumCircuit):
     """A circuit representing the Bernoulli A operator."""
 
@@ -295,16 +263,13 @@ def get_google_c_map():
     return c_map_google
 
 
-def handle_algorithm_level(
-    qc: QuantumCircuit, num_qubits: int, save_png: bool, save_hist: bool
-):
+def handle_algorithm_level(qc: QuantumCircuit, num_qubits: int):
     """Handles the creation of the benchmark on the algorithm level.
 
     Keyword arguments:
     qc -- quantum circuit which shall be created
     num_qubits -- number of qubits
-    save_png -- flag indicated whether to save schematic picture of quantum circuit
-    save_hist -- flag indicated whether to simulate circuit and save its histogram
+
 
     Return values:
     filename_algo -- the filename of the created and saved benchmark
@@ -320,10 +285,6 @@ def handle_algorithm_level(
         depth = qc.depth()
     else:
         serialize_qc(qc, filename_algo)
-        if save_png:
-            save_circ(qc, filename_algo)
-        if save_hist:
-            sim_and_print_hist(qc, filename_algo)
         depth = qc.depth()
 
     return filename_algo, depth, num_qubits
@@ -332,8 +293,6 @@ def handle_algorithm_level(
 def get_indep_level(
     qc: QuantumCircuit,
     num_qubits: int,
-    save_png: bool,
-    save_hist: bool,
     file_precheck: bool,
 ):
     """Handles the creation of the benchmark on the target-independent level.
@@ -341,8 +300,6 @@ def get_indep_level(
     Keyword arguments:
     qc -- quantum circuit which the to be created benchmark circuit is based on
     num_qubits -- number of qubits
-    save_png -- flag indicated whether to save schematic picture of quantum circuit
-    save_hist -- flag indicated whether to simulate circuit and save its histogram
     file_precheck -- flag indicating whether to check whether the file already exists before creating it (again)
 
     Return values:
@@ -366,10 +323,6 @@ def get_indep_level(
             qc, basis_gates=openqasm_gates, optimization_level=1, seed_transpiler=10
         )
         save_as_qasm(target_independent, filename_indep)
-        if save_png:
-            save_circ(target_independent, filename_indep)
-        if save_hist:
-            sim_and_print_hist(target_independent, filename_indep)
 
         depth = target_independent.depth()
         return filename_indep, depth, qc.num_qubits
@@ -381,8 +334,6 @@ def get_native_gates_level(
     gate_set_name: str,
     opt_level: int,
     num_qubits: int,
-    save_png: bool,
-    save_hist: bool,
     file_precheck: bool,
 ):
     """Handles the creation of the benchmark on the target-dependent native gates level.
@@ -393,8 +344,6 @@ def get_native_gates_level(
     gate_set_name -- name of this gate set
     opt_level -- optimization level
     num_qubits -- number of qubits
-    save_png -- flag indicated whether to save schematic picture of quantum circuit
-    save_hist -- flag indicated whether to simulate circuit and save its histogram
     file_precheck -- flag indicating whether to check whether the file already exists before creating it (again)
 
     Return values:
@@ -442,10 +391,6 @@ def get_native_gates_level(
         save_as_qasm(
             compiled_without_architecture, filename_nativegates, gate_set, opt_level
         )
-        if save_png:
-            save_circ(compiled_without_architecture, filename_nativegates)
-        if save_hist:
-            sim_and_print_hist(compiled_without_architecture, filename_nativegates)
 
         depth = compiled_without_architecture.depth()
         return filename_nativegates, depth, n_actual
@@ -458,8 +403,6 @@ def get_mapped_level(
     opt_level: int,
     num_qubits: int,
     smallest_fitting_arch: bool,
-    save_png: bool,
-    save_hist: bool,
     file_precheck: bool,
 ):
     """Handles the creation of the benchmark on the target-dependent mapped level.
@@ -471,8 +414,6 @@ def get_mapped_level(
     opt_level -- optimization level
     num_qubits -- number of qubits
     smallest_fitting_arch -- flag indicating whether smallest fitting mapping scheme shall be used
-    save_png -- flag indicated whether to save schematic picture of quantum circuit
-    save_hist -- flag indicated whether to simulate circuit and save its histogram
     file_precheck -- flag indicating whether to check whether the file already exists before creating it (again)
 
     Return values:
@@ -519,11 +460,6 @@ def get_mapped_level(
                 gate_set_name_mapped + "-" + backend_name,
             )
 
-            if save_png:
-                save_circ(compiled_with_architecture, filename_mapped)
-            if save_hist:
-                sim_and_print_hist(compiled_with_architecture, filename_mapped)
-
             depth = compiled_with_architecture.depth()
         return filename_mapped, depth, num_qubits
     else:


Test patch:
diff --git a/mqt/bench/tests/test_all.py b/mqt/bench/tests/test_all.py
index 5d20652e7..db5985467 100644
--- a/mqt/bench/tests/test_all.py
+++ b/mqt/bench/tests/test_all.py
@@ -40,7 +40,6 @@ def test_configure_begin():
     if not os.path.exists(test_qasm_output_path):
         os.mkdir(test_qasm_output_path)
     utils.set_qasm_output_path(test_qasm_output_path)
-    print("test")
     assert utils.get_qasm_output_path() == test_qasm_output_path
 
 
@@ -71,7 +70,8 @@ def test_configure_begin():
 def test_quantumcircuit_algo_level(benchmark, num_qubits):
     qc = benchmark.create_circuit(num_qubits)
     filename_algo, depth, num_qubits = utils.handle_algorithm_level(
-        qc, num_qubits, save_png=False, save_hist=False
+        qc,
+        num_qubits,
     )
     assert depth > 0
 
@@ -113,11 +113,11 @@ def test_quantumcircuit_indep_level(benchmark, input_value, scalable):
     if scalable:
         assert qc.num_qubits == input_value
     filename_indep, depth, num_qubits = utils.get_indep_level(
-        qc, input_value, save_png=False, save_hist=False, file_precheck=False
+        qc, input_value, file_precheck=False
     )
     assert depth > 0
     filename_indep, depth, num_qubits = utils.get_indep_level(
-        qc, input_value, save_png=False, save_hist=False, file_precheck=True
+        qc, input_value, file_precheck=True
     )
     assert depth > 0
 
@@ -169,8 +169,6 @@ def test_quantumcircuit_native_and_mapped_levels(benchmark, input_value, scalabl
             gate_set_name,
             opt_level,
             input_value,
-            save_png=False,
-            save_hist=False,
             file_precheck=False,
         )
         assert depth_native > 0
@@ -180,8 +178,6 @@ def test_quantumcircuit_native_and_mapped_levels(benchmark, input_value, scalabl
             gate_set_name,
             opt_level,
             input_value,
-            save_png=False,
-            save_hist=False,
             file_precheck=True,
         )
         assert depth_native > 0
@@ -192,8 +188,6 @@ def test_quantumcircuit_native_and_mapped_levels(benchmark, input_value, scalabl
             opt_level,
             n_actual,
             False,
-            save_png=False,
-            save_hist=False,
             file_precheck=False,
         )
         assert depth_mapped > 0
@@ -204,8 +198,6 @@ def test_quantumcircuit_native_and_mapped_levels(benchmark, input_value, scalabl
             opt_level,
             n_actual,
             False,
-            save_png=False,
-            save_hist=False,
             file_precheck=True,
         )
         assert depth_mapped > 0
@@ -269,22 +261,6 @@ def test_get_google_c_map():
     assert len(utils.get_google_c_map()) == 176
 
 
-def test_save_circ():
-    qc = ghz.create_circuit(5)
-    utils.save_circ(qc, "pytest")
-    assert os.path.exists("hist_output/pytest.png")
-    if os.path.exists("hist_output/pytest.png"):
-        os.remove("hist_output/pytest.png")
-
-
-def test_sim_and_print_hist():
-    qc = ghz.create_circuit(5)
-    utils.sim_and_print_hist(qc, "pytest")
-    assert os.path.exists("hist_output/pytest_hist.png")
-    if os.path.exists("hist_output/pytest_hist.png"):
-        os.remove("hist_output/pytest_hist.png")
-
-
 def test_dj_constant_oracle():
     qc = dj.create_circuit(5, False)
     assert qc.depth() > 0