Python API: General
QuPRS provides a high-level Python entry point for comparing quantum circuits.
For symbolic circuit construction, gate methods, and reductions, see the
Advanced Path-sum API. These are separate import surfaces:
QuPRS and QuPRS.pathsum respectively.
from QuPRS import check_equivalence
result = check_equivalence("first.qasm", "second.qasm", method="hybrid")
print(result.equivalent)
The input filenames above are illustrative; provide existing OpenQASM files or
Qiskit QuantumCircuit objects. The generated signatures and descriptions
below are taken from the version-matched Python source, without importing the
package into the documentation builder.
Circuit equivalence
check_equivalence(circuit1, circuit2, method='hybrid', strategy='Difference', tool_name='gpmc', timeout=600, safe_mode=False, backend='python', wmc_phase_tolerance=None, approximate_distance_tolerance=1e-08, rr_tolerance=None, phase_tolerance=None, gpmc_precision_digits=30)
Check whether two quantum circuits are equivalent using different verification methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit1
|
str | QuantumCircuit
|
The first quantum circuit to compare. It can be a file path or a Qiskit circuit object. |
required |
circuit2
|
str | QuantumCircuit
|
The second quantum circuit to compare. It can be a file path or a Qiskit circuit object. |
required |
method
|
str
|
Verification method. Default is "hybrid". - 'hybrid': Uses both Reduction Rules (RR) and Weighted Model Counting (WMC). This is the recommended mode. - 'reduction_rules': Uses only Reduction Rules. If reduction is incomplete, returns 'unknown'. - 'wmc_only': Uses Weighted Model Counting (WMC), without reduction-rule matching. Python still normalizes phase coefficients while building the PathSum, but preserves small nonzero input phases rather than removing them using the local reduction tolerance. |
'hybrid'
|
strategy
|
str
|
Internal strategy for handling quantum gates. Default is "Difference". - 'Difference': Uses a difference-based strategy. - 'Proportional': Uses a proportional strategy. - 'Naive': Uses a naive strategy. - 'Straightforward': Uses a straightforward strategy. |
'Difference'
|
tool_name
|
str
|
External WMC solver selection: "gpmc" (default) or "ganak". This applies when a weighted count is required, including Hybrid fallback and WMC-only runs. QuPRS does not silently switch to another solver on failure. |
'gpmc'
|
backend
|
str
|
"python" (default) uses the Python symbolic implementation; "rust" invokes the native engine through the PyO3 extension. With safe_mode=True, the Rust backend is supervised in an isolated worker, not via the standalone Rust CLI. |
'python'
|
timeout
|
int
|
Requested deadline in seconds (default 600). The external WMC subprocess consumes only the remaining monotonic budget. In-process Rust and Python symbolic work on a non-main thread are NOT subject to a strict end-to-end hard timeout. Rust safe_mode=True supervises a separate worker with a monotonic deadline; parent-side Qiskit serialization remains non-preemptible. Solver/process/parse failures do not imply circuit inequivalence. |
600
|
safe_mode
|
bool
|
For the Python backend, use the existing strategy isolation; for the Rust backend, invoke PyO3 inside an isolated child worker with bounded IPC and a supervised deadline. Worker isolation alone does not guarantee a physical-memory cap or cleanup of solver processes that detach from its process group. Default is False. |
False
|
rr_tolerance
|
float | None
|
Per-call RR numerical coefficient matching. Defaults to 1e-10. Preserves small source phase terms. |
None
|
phase_tolerance
|
float | None
|
Phase zero classification in radians for RR and WMC; conflicts with a different wmc_phase_tolerance. |
None
|
wmc_phase_tolerance
|
float | None
|
WMC phase threshold separating "equivalent" and "equivalent*" for near-unit magnitudes. None uses config.WMC_PHASE_TOLERANCE. |
None
|
approximate_distance_tolerance
|
float | None
|
Projective Hilbert-Schmidt distance threshold in (0, 1), default 1e-8. None means the same default, not the removed magnitude classifier. Finite counts use one central printed-count distance verdict; raw printed uncertainty remains separately diagnostic. |
1e-08
|
gpmc_precision_digits
|
int
|
GPMC complex MPFR output/working digits: 30 by default or explicitly 90. Independent from distance tolerance. |
30
|
Returns:
| Name | Type | Description |
|---|---|---|
EquivalenceCheckResult |
EquivalenceCheckResult
|
A dataclass containing the verdict string in the equivalent field, timing/solver diagnostics, and the final symbolic PathSum. This is not a result dictionary. |
method selects hybrid, reduction_rules, or wmc_only. The
backend argument selects the Python or Rust implementation; the solver
tool_name is independently selected when WMC is reached. A timeout request
is not always an end-to-end hard deadline for in-process symbolic work: see
the safe_mode contract in the function's source-generated reference and
the Architecture guide.
Verification result
The function returns
QuPRS.interface.load_qiskit.EquivalenceCheckResult. It is a concrete
result dataclass, not currently an export of QuPRS.__all__.
EquivalenceCheckResult
dataclass
Dataclass for storing the result of quantum circuit equivalence checking.
Attributes:
| Name | Type | Description |
|---|---|---|
qubit_num |
int
|
Number of qubits in the circuit. |
gate_num |
int
|
Number of gates in the first circuit. |
gate_num2 |
int
|
Number of gates in the second circuit. |
method |
str
|
Verification method used. |
strategy |
str
|
Strategy used for gate handling. |
equivalent |
str
|
Result of the equivalence check. |
verification_time |
float
|
Total verification time in seconds. |
pathsum_time |
float
|
Time spent on PathSum computation. |
final_pathsum |
PathSum
|
Final PathSum circuit object. |
Statistics |
StatisticsManager
|
Statistics for the computation. |
progress |
str
|
Progress string (e.g., "N/M"). |
to_DIMACS_time |
Optional[float]
|
Time for CNF conversion. |
tool_name |
Optional[str]
|
Name of the WMC tool used (if applicable). |
tool_time |
Optional[float]
|
Time for weighted model counting. |
wmc_time |
Optional[float]
|
Total WMC time. |
CNF |
Optional[str]
|
CNF representation (if generated). |
CNF = None
class-attribute
instance-attribute
Statistics
instance-attribute
approximate_distance_tolerance = None
class-attribute
instance-attribute
equivalent
instance-attribute
expect = None
class-attribute
instance-attribute
final_pathsum
instance-attribute
gate_num
instance-attribute
gate_num2
instance-attribute
log_wmc = None
class-attribute
instance-attribute
method
instance-attribute
pathsum_time
instance-attribute
progress = '0/0'
class-attribute
instance-attribute
qubit_num
instance-attribute
rr_approximate = False
class-attribute
instance-attribute
strategy
instance-attribute
to_DIMACS_time = None
class-attribute
instance-attribute
tool_name = None
class-attribute
instance-attribute
tool_time = None
class-attribute
instance-attribute
verification_time
instance-attribute
wmc_numeric_model_bound = None
class-attribute
instance-attribute
wmc_numerical_verdict = None
class-attribute
instance-attribute
wmc_phase_tolerance = None
class-attribute
instance-attribute
wmc_precision_reason = None
class-attribute
instance-attribute
wmc_print_resolution_distance = None
class-attribute
instance-attribute
wmc_printed_verdict = None
class-attribute
instance-attribute
wmc_solver_invoked = None
class-attribute
instance-attribute
wmc_time = None
class-attribute
instance-attribute
__init__(qubit_num, gate_num, gate_num2, method, strategy, equivalent, verification_time, pathsum_time, final_pathsum, Statistics, progress='0/0', to_DIMACS_time=None, tool_name=None, tool_time=None, wmc_time=None, CNF=None, expect=None, log_wmc=None, wmc_numerical_verdict=None, wmc_precision_reason=None, wmc_printed_verdict=None, wmc_print_resolution_distance=None, wmc_numeric_model_bound=None, approximate_distance_tolerance=None, wmc_solver_invoked=None, rr_approximate=False, wmc_phase_tolerance=None)
__repr__()
__str__()
_fmt(val)
staticmethod
Unified formatting logic: 1. If the value is a number (float/int) -> format to .3f (without unit) 2. If the value is None -> return 'N/A' 3. If the value is something else (e.g., error string) -> return the original string
Important result fields include:
| Field | Meaning |
|---|---|
equivalent |
Verification verdict string; see the states below. |
final_pathsum |
Residual symbolic PathSum; consult the advanced reference. |
Statistics |
Reduction diagnostic object (StatisticsManager). |
rr_approximate |
An accepted RR-based result used tolerance-dependent reduction. |
wmc_solver_invoked |
Whether a real WMC solver process was called (when supplied). |
wmc_printed_verdict / wmc_precision_reason |
Solver-output interpretation and diagnostic provenance, not a mathematical error certificate. |
approximate_distance_tolerance / wmc_phase_tolerance |
Effective numerical thresholds recorded in the result. |
Verdicts and numerical meaning
equivalent can report equivalent, equivalent*,
not_equivalent, unknown, Timeout, or MemoryOut.
equivalent* denotes equivalence up to global phase under the
configured numerical policy. Neither unknown nor a resource-limited or
solver-error result proves inequivalence. Numerical WMC output granularity
is not an exact solver error bound.
The independent rr_tolerance, wmc_phase_tolerance (alias
phase_tolerance), and approximate_distance_tolerance parameters
must not be conflated. For their scientific interpretation, see
Verification and
Equivalence semantics.