Skip to content

Phase folding has long compile time on certain large circuits #4889

Description

@taalexander

Required prerequisites

  • Consult the security policy. If reporting a security vulnerability, do not report the bug using this form. Use the process described in the policy to report the issue.
  • Make sure you've read the documentation. Your issue may be addressed there.
  • Search the issue tracker to verify that this hasn't already been reported. +1 or comment there if it has.
  • If possible, make a PR with a failing test to give us a starting point to work on!

Describe the bug

Running CUDA-Q resource estimation through a target that applies phase-folding-pipeline either hangs or at the very least exceeds 2 hours.

Steps to reproduce the bug

Reproducer.

LD_LIBRARY_PATH="$PWD/vendor/cuda-quantum/build/lib:${LD_LIBRARY_PATH:-}" \
PYTHONPATH="$PWD/vendor/cuda-quantum/build/python:${PYTHONPATH:-}" \
python /tmp/repro_cudaq_phase_folding_qasmbench.py \
  --target phase-folding-bench-mins \
  --timeout 7200
#!/usr/bin/env python3
import argparse
import multiprocessing as mp
import tempfile
import time
import urllib.request
from pathlib import Path


DEFAULT_URL = (
    "https://raw.githubusercontent.com/pnnl/QASMBench/master/"
    "large/multiplier_n400/"
    "multiplier_n400_transpiled.qasm"
)


def fetch_qasm(url: str, destination: Path) -> None:
    with urllib.request.urlopen(url, timeout=60) as response:
        destination.write_bytes(response.read())


def compile_qasm(qasm_path: str, target: str, queue: mp.Queue) -> None:
    import cudaq
    from cudaq.contrib import from_qasm

    cudaq.set_target(target)
    kernel = from_qasm(qasm_path)

    start = time.perf_counter()
    resources = cudaq.estimate_resources(kernel)
    elapsed_s = time.perf_counter() - start

    queue.put(
        {
            "elapsed_s": elapsed_s,
            "ops": resources.to_dict(),
        }
    )


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--url", default=DEFAULT_URL)
    parser.add_argument("--target", default="compiler-bench-ftqc-logical")
    parser.add_argument("--timeout", type=float, default=600.0)
    parser.add_argument("--keep", type=Path)
    args = parser.parse_args()

    with tempfile.TemporaryDirectory() as tmp:
        qasm_path = args.keep or Path(tmp) / "case.qasm"
        fetch_start = time.perf_counter()
        fetch_qasm(args.url, qasm_path)
        fetch_elapsed_s = time.perf_counter() - fetch_start
        print(
            f"FETCHED path={qasm_path} bytes={qasm_path.stat().st_size} "
            f"elapsed_s={fetch_elapsed_s:.1f}",
            flush=True,
        )

        queue: mp.Queue = mp.Queue()
        proc = mp.Process(
            target=compile_qasm,
            args=(str(qasm_path), args.target, queue),
        )
        compile_start = time.perf_counter()
        proc.start()
        proc.join(args.timeout)
        compile_elapsed_s = time.perf_counter() - compile_start

        if proc.is_alive():
            proc.terminate()
            proc.join()
            print(
                f"TIMEOUT target={args.target} timeout_s={args.timeout:.1f} "
                f"elapsed_s={compile_elapsed_s:.1f}",
                flush=True,
            )
            return 124

        if proc.exitcode != 0:
            print(f"FAILED target={args.target} exitcode={proc.exitcode}", flush=True)
            return proc.exitcode or 1

        result = queue.get()
        print(
            f"PASSED target={args.target} compile_elapsed_s={result['elapsed_s']:.1f}",
            flush=True,
        )
        print(f"OPS {result['ops']}", flush=True)
        return 0


if __name__ == "__main__":
    raise SystemExit(main())

Expected behavior

Should be relatively performant

Is this a regression? If it is, put the last known working version (or commit) here.

Not a regression

Environment

  • CUDA-Q version: main
  • Python version: 3.14
  • C++ compiler: gcc
  • Operating system: linux

Suggestions

No response

Metadata

Metadata

Assignees

Labels

stale-notifiedStale notification has already fired for this issue

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions