qrisp.QuantumCircuit.compare_unitary#

QuantumCircuit.compare_unitary(other, precision=4, ignore_gphase=False)[source]#

Compares the unitaries of two QuantumCircuits. This can be used to check if a QuantumCircuit transformation is valid.

Parameters:
otherQuantumCircuit

The QuantumCircuit to compare to.

precisionint, optional

The precision of the comparison. This function will return True, if the norm of the difference of the unitaries is below the precision. The default is 4.

ignore_gphase: bool, optional

If set to True, this method returns True if the unitaries only differ in a global phase.

Returns:
Bool

The comparison outcome.

Examples

We create two QuantumCircuit with equivalent unitaries but differing by a non-trivial commutation:

>>> from qrisp import QuantumCircuit
>>> qc_0 = QuantumCircuit(2)
>>> qc_1 = QuantumCircuit(2)
>>> qc_0.z(0)
>>> qc_0.cx(0,1)
>>> print(qc_0)
      ┌───┐
qb_0: ┤ Z ├──■──
      └───┘┌─┴─┐
qb_1: ─────┤ X ├
           └───┘
>>> qc_1.cx(0,1)
>>> qc_1.z(0)
>>> print(qc_1)
           ┌───┐
qb_2: ──■──┤ Z ├
      ┌─┴─┐└───┘
qb_3: ┤ X ├─────
      └───┘
>>> qc_0.compare_unitary(qc_1)
True