# Quantum Circuit Equivalence Checking

Consider a quantum circuit $G=g_0\dots g_{|G|-1}$ acting on $n$ qubits. Each
gate $g_i$ of $G$ is described by a corresponding unitary matrix $U_i$ that is
subsequently applied during the execution of the quantum circuit. Thus, the
functionality of the circuit $G$ can be obtained as a unitary *system matrix*
$U$ itself by computing $U=U_{|G|-1}\dots U_0$.

The *equivalence checking problem for quantum circuits* can be defined as
follows:

> Given two quantum circuits $G=g_0\dots g_{|G|-1}$ and
> $G'=g'_0\dots g'_{|G'|-1}$ acting on the same set of qubits, do they realize
> the same functionality? More specifically, does it hold that $U=e^{i\theta}U'$
> or, equivalently $UU' = e^{i\theta} \mathbb{I}$, where $\theta\in(-\pi,\pi]$
> denotes a physically unobservable global phase?

Conceptually, checking the equivalence of two quantum circuits is as simple as
constructing and comparing the respective system matrices. However, the size of
these matrices grows exponentially with the number of qubits, which quickly
renders straight-forward methods (such as arrays) infeasible. Equivalence
checking of quantum circuits has even been shown to be QMA-complete.

QCEC provides several complementary methods for efficiently tackling this
challenging problem—each with their respective use cases, capabilities, and
drawbacks.

If numerical error collapses a decision diagram to zero, its checker returns
`no_information`. A zero diagram cannot prove equivalence or non-equivalence of
unitary circuits. Other enabled checkers can still provide a result; if none
does, the overall result remains inconclusive.

The DD checkers use `trace_threshold` (default `1e-8`) to allow numerical
roundoff. Without ancillary inputs, full equivalence bounds the Frobenius
distance of the relative unitary from identity, allowing one global phase.
Partial equivalence bounds the Frobenius weight outside measured-output diagonal
blocks. Both distances are divided by the square root of the matrix dimension.
Adding idle qubits preserves the tolerance; errors on small input subspaces can
be diluted. A bounded traversal rejects when a conservative error sum already
exceeds the threshold.

Ancillary checks use structural complex-distance bounds on DD edge weights.
Their tolerance does not define a matrix norm. Use
[approximate equivalence checking](approximate_equivalence_checking.html.md) for a
separate projective Hilbert–Schmidt error threshold.

## Construction Equivalence Checker (using Decision Diagrams)

While the underlying matrices are exponentially large with respect to the number
of qubits, they frequently are sparse or contain inherent redundancies. Decision
diagrams have been proposed as a means to exploit these redundancies. For a
detailed intro to this topic, see
[[1](references.html.md#id27)] and the references therein. In
many cases, using decision diagrams allows to obtain polynomially-sized
representations for the otherwise exponentially large system matrices—in the
best case even linear.

The functionality of both circuits is constructed by subsequently multiplying
the decision diagrams representing the individual gates of $G$ and $G'$—until,
eventually, decision diagram representations for the system matrices $U$ and
$U'$ are obtained. Since decision diagrams are canonical representations of the
underlying matrices, checking the equivalence of both circuits can be reduced to
comparing the root pointers of the resulting decision diagrams.

**Most effective for:** proving equivalence or non-equivalence of circuits whose
functionality is sparse and/or contains redundancies.

**Capable of showing:** equivalence and non-equivalence

**Drawback:** Decision diagrams might still be exponentially large in the worst
case.

## Alternating Equivalence Checker (using Decision Diagrams)

Due to the inherent reversibility of quantum computations, equivalence checking
of two circuits $G$ and $G'$ can also be reduced to proving that the composition
of one circuit with the inverse of the other implements the identity, i.e.,
$G^{\prime -1} G = \mathbb{I}$. While the identity is represented by a decision
diagram of linear size, sequentially constructing the representation for
$G^{\prime -1} G$ still constructs the (potentially exponentially large)
representation of one of the circuit’s system matrix along the way.

The main idea is to, instead, start with the identity $\mathbb{I}$ in between
both circuits, which is symbolized by $G \rightarrow \mathbb{I} \leftarrow G'$.
Then, gates from either circuit are applied in an alternating fashion according
to some *application scheme*, with the goal of staying as close as possible to
the identity. Given that a suitable order of execution can be determined, an
efficient representation can be maintained throughout the entire verification
process.

For further information on this method, see [[2](references.html.md#id15)].

**Most effective for:** proving equivalence of two circuits sharing a common
structure, e.g., one circuit being a compiled or optimized version of the other.

**Capable of showing:** equivalence and non-equivalence

**Drawback:** Performance depends on the availability of a suitable
*application scheme* or oracle for predicting when to apply gates from either
circuit.

## Simulation Equivalence Checker (using Decision Diagrams)

It has been shown in [[2](references.html.md#id15)], that even small
differences in quantum circuits frequently affect the entire functional
representation due to the reversibility of quantum computations. As a
consequence, simulating both circuits with a couple of arbitrary input states,
i.e., considering only a small part of the whole functionality, provides an
attractive alternative to the methods described above.

In [[3](references.html.md#id17)], several types of
states have been proposed that allow to trade off efficiency versus performance:

- Classical stimuli (i.e., random *computational basis states*) already offer
  extremely high error detection rates in general and are comparatively fast to
  simulate, which makes them the default.
- Local quantum stimuli (i.e., random *single-qubit basis states*) are a little
  bit more computationally intensive, but provide even better error detection
  rates.
- Global quantum stimuli (i.e., random *stabilizer states*) offer the highest
  available error detection rate, while at the same time incurring the highest
  computational effort.

**Most effective for:** quickly detecting non-equivalence, even in cases where
both circuits only differ slightly.

**Capable of showing:** non-equivalence

**Drawback:** Decision diagrams for state vectors might still be exponentially
large in the worst case.

## ZX-Calculus Equivalence Checker

In [[4](references.html.md#id21)], Kissinger and van de Wetering proposed
an algorithm for optimizing quantum circuits based on ZX-calculus rewriting. In
their initial article they also show that this rewriting approach can be used to
prove the equivalence of quantum circuits. The idea is to construct the
ZX-diagram of $G^{\prime -1} G$ and reduce this ZX-diagram using the rules of
the ZX-calculus until no further rewrites can be made. If the resulting diagram
consists only of wires (the identity ZX-diagram) then $G = G^{\prime}$. This
approach has been extended in [[5](references.html.md#id24)]
to also handle ancillary qubits, floating point inaccuracies in gate parameters,
as well as permutations of input and output layouts of quantum circuits.

In [[6](references.html.md#id23)], it has been shown that
equivalence checking with the ZX-calculus naturally complements equivalence
checking with decision diagrams. Since the size of the ZX-diagram during
rewriting is bounded by the size of the initial diagram, this checker can be
easily executed in parallel to the aforementioned approaches based on decision
diagrams. In cases where the size of the decision diagrams explodes, the
rewriting approach can often prove equivalence much more efficiently.

**Most effective for:** proving equivalence of two circuits involving many
rotation gates with angles of the form $\frac{\pi}{k}$ for $k\in\mathbb{N}$.

**Capable of showing:** equivalence

**Drawback:** Due to the incompleteness of the rewriting rules, this equivalence
checker cannot prove non-equivalence. Furthermore, multi-controlled gates have
to be decomposed prior to the equivalence check, which can quickly lead to large
ZX-diagrams and slow runtimes.

## Approximate Equivalence Checking

QCEC can also compare circuits using the projective Hilbert–Schmidt distance.
The construction and alternating checkers support this mode directly. For
shallow circuits with few gates crossing a horizontal circuit cut, the hybrid
Schrödinger–Feynman checker provides a standalone alternative that evaluates
the decomposed summands in parallel. See [Approximate Equivalence Checking](approximate_equivalence_checking.html.md)
for the metric, configuration, and current limitations.

## Resulting Equivalence Checking Flow

QCEC implements and expands upon the flow proposed in
[[2](references.html.md#id15)] as illustrated in the following figure that
orchestrates the construction, alternating, simulation, and ZX-calculus
equivalence checkers described above. The hybrid Schrödinger–Feynman checker
runs as a standalone alternative flow.

![Illustration of the QCEC verification flow](images/verification_flow.png)

In general, the following steps are performed:

- First, a couple of simulation runs with random computational basis states are
  started. Should any of these simulations show a difference in the resulting
  states, the check is finished.
- In parallel, the alternating equivalence checker is started. In case the check
  finishes, i.e., it does not run into a timeout, a definitive result is
  returned. Otherwise, if none of the simulations have shown a difference, this
  strongly indicates that both circuits are probably equivalent.
- In addition to the above, the ZX-calculus equivalence checker is started. In
  case it finishes with an affirmative answer, the check is finished. In case it
  finishes and was not able to reduce the ZX-diagram to the identity, this
  indicates that the circuits are probably not equivalent.

## Hard Timeouts for Circuit Inputs

For normal checks, use the cooperative `timeout` option of `verify`. It avoids
starting a worker process for each check. A long-running DD operation can delay
cancellation past the requested timeout.

Use `verify_with_hard_timeout`
**only when the worker must be terminated after a timeout**. It can stop a check
during a long-running DD operation, but starting the worker adds substantial
latency. In a five-run 133-gate self-check on a DGX Spark, the median was 4.3 ms
with `verify`, 114 ms with a file worker, and 253 ms with QPY input. The
function accepts file paths, OpenQASM text, or Qiskit circuits:

```python
from mqt import qcec

result = qcec.verify_with_hard_timeout("original.qasm", "optimized.qasm", deadline=5)
print(result["equivalence"])
```

The two inputs may use different forms. For example, a Qiskit circuit can be
checked against OpenQASM text:

```python
from mqt import qcec
from qiskit import QuantumCircuit

qiskit_circuit = QuantumCircuit(1)
qiskit_circuit.x(0)
qasm = "OPENQASM 3.0; qubit[1] q; x q[0];"
result = qcec.verify_with_hard_timeout(qiskit_circuit, qasm, deadline=5)
```

This starts a separate process and raises `TimeoutError` if the worker timeout
expires. The timeout covers worker startup and circuit loading after process
creation. Qiskit circuits use QPY; serialization in the caller occurs before the
deadline starts. Process creation itself may delay the timeout on some
platforms. `QuantumComputation` objects are not supported. Successful checks
return the JSON-style result of `verify`, including checker statistics but not
DD counterexamples.

## Selecting a Checker

`verify(..., method="auto")` is the default. It uses the configured
`run_*_checker` flags, automatic fallback between checkers, and the
parameterized verification flow.

Set `method` to `"alternating"`, `"construction"`, `"simulation"`, `"zx"`, or
`"hsf"` to run only that checker. This selection overrides the `run_*_checker`
flags without changing them. In a `Configuration`, set `execution.method`. The
same option is accepted by `verify_compilation` and `verify_with_hard_timeout`.

Explicit selection raises an error when the selected checker does not support
the preprocessed circuits or configuration. In particular:

- Unbound parameters require `"auto"` or `"zx"`. Explicit ZX does not
  instantiate parameters if the symbolic check is inconclusive.
- Alternating checking cannot handle an ancilla used by both circuits.
- ZX checking requires supported gates and ancillary outputs. It does not
  support partial or approximate equivalence checking.
- Simulation requires `max_sims > 0` and does not support approximate
  equivalence.
- HSF requires `check_approximate_equivalence=True` and its documented gate and
  circuit restrictions.

A supported check can still return `no_information`, for example after a timeout
or an inconclusive ZX reduction. Explicit selection does not fall back to
another checker in this case. Setting `method` back to `"auto"` restores
selection through the flags. `disable_all_checkers()` also resets the method to
`"auto"`.
