From e0fb6970c0679a3629706aa4b5865dc044b8759d Mon Sep 17 00:00:00 2001 From: ale Date: Mon, 13 Oct 2025 00:41:42 +0200 Subject: [PATCH] fix HDH repo Signed-off-by: ale --- .gitmodules | 4 ++++ Dockerfile | 5 +---- HDH | 1 + README.md | 4 ++-- benchmark.py | 36 +++++++++++++++++++++++++++++++----- circuit_examples.py | 3 +-- 6 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 .gitmodules create mode 160000 HDH diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f67586e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "HDH"] + path = HDH + url = https://github.com/manalejandro/HDH + branch = database-branch diff --git a/Dockerfile b/Dockerfile index da65b35..f4d1b68 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,9 +52,6 @@ WORKDIR /app # Copy application files COPY . . -# Copy HDH library (assuming it's in the parent directory) -COPY ../HDH ./HDH - # Install HDH library RUN pip install -e ./HDH @@ -83,4 +80,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ VOLUME ["/app/hdh_results", "/app/benchmark_results", "/app/logs"] # Default command -CMD ["python", "main.py", "--demo-mode", "--output-dir", "/app/hdh_results"] \ No newline at end of file +CMD ["python", "main.py", "--demo-mode", "--output-dir", "/app/hdh_results"] diff --git a/HDH b/HDH new file mode 160000 index 0000000..e84b7d9 --- /dev/null +++ b/HDH @@ -0,0 +1 @@ +Subproject commit e84b7d9d8cc0438e260c23270a9a9860e96ed164 diff --git a/README.md b/README.md index 99c089a..993da16 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ source hdh-env/bin/activate # On Windows: hdh-env\Scripts\activate pip install -r requirements.txt # Install HDH library in development mode -pip install -e ../HDH +pip install -e ./HDH # Verify installation python main.py --help @@ -419,4 +419,4 @@ For questions and support: *Built with ❤️ for the quantum computing community* -*Thank you Maria for making quantum computation analysis more accessible through HDH! 🌟* \ No newline at end of file +*Thank you Maria for making quantum computation analysis more accessible through HDH! 🌟* diff --git a/benchmark.py b/benchmark.py index 839880a..74da33b 100644 --- a/benchmark.py +++ b/benchmark.py @@ -37,7 +37,8 @@ import numpy as np # Memory and performance monitoring import psutil import gc -from memory_profiler import profile +import tracemalloc +from memory_profiler import profile, memory_usage # Add HDH to path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'HDH'))) @@ -118,17 +119,34 @@ class HDHBenchmarkSuite: # Initial memory measurement gc.collect() # Force garbage collection + + # Start memory tracking with tracemalloc + tracemalloc.start() + initial_snapshot = tracemalloc.take_snapshot() initial_memory = self.get_memory_usage() try: - # Time the conversion + # Time the conversion and track memory start_time = time.perf_counter() hdh = from_qiskit(circuit) conversion_time = time.perf_counter() - start_time - # Memory peak measurement + # Memory peak measurement using tracemalloc + current_snapshot = tracemalloc.take_snapshot() + top_stats = current_snapshot.compare_to(initial_snapshot, 'lineno') + + # Calculate memory difference in MB + tracemalloc_memory = sum(stat.size_diff for stat in top_stats) / 1024 / 1024 + + # Also get psutil measurement as backup peak_memory = self.get_memory_usage() - memory_used = peak_memory - initial_memory + psutil_memory = peak_memory - initial_memory + + # Use the larger of the two measurements (tracemalloc is usually more accurate) + memory_used = max(abs(tracemalloc_memory), abs(psutil_memory)) + + # Stop tracemalloc + tracemalloc.stop() # HDH statistics hdh_nodes = len(hdh.S) @@ -142,8 +160,12 @@ class HDHBenchmarkSuite: if hdh_nodes > 1: try: num_parts = min(3, max(2, hdh_nodes // 2)) + # Calculate capacity: distribute qubits evenly across partitions with some buffer + num_qubits = circuit.num_qubits + capacity = max(1, (num_qubits + num_parts - 1) // num_parts + 1) # Ceiling division + buffer + start_partition = time.perf_counter() - partitions = compute_cut(hdh, num_parts) + partitions, _ = compute_cut(hdh, num_parts, capacity) partitioning_time = time.perf_counter() - start_partition partition_cost = cost(hdh, partitions) except Exception as e: @@ -177,6 +199,10 @@ class HDHBenchmarkSuite: ) except Exception as e: + # Make sure to stop tracemalloc even on error + if tracemalloc.is_tracing(): + tracemalloc.stop() + self.logger.error(f"Benchmark failed for {circuit_name}: {str(e)}") self.logger.debug(traceback.format_exc()) diff --git a/circuit_examples.py b/circuit_examples.py index 833a0f8..d9a51f3 100644 --- a/circuit_examples.py +++ b/circuit_examples.py @@ -28,8 +28,7 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister from qiskit.circuit.library import ( - QFT, GroverOperator, DeutschJozsaOracle, - TwoLocal, RealAmplitudes, EfficientSU2 + QFT, TwoLocal, RealAmplitudes, EfficientSU2 ) from qiskit.circuit import Parameter import qiskit.circuit.library as qlib