qrisp.QuantumCircuit.get_unitary#
- QuantumCircuit.get_unitary(decimals: int | None = None) ndarray[source]#
Return the unitary matrix of this QuantumCircuit as a NumPy array.
Works with both numeric and abstract (SymPy) parameters. When the circuit contains symbolic parameters, the returned array has
dtype=objectwith SymPy expressions as entries.- Parameters:
- decimalsint, optional
Number of decimal places to round to. When not provided, full precision is returned. For symbolic arrays, floating-point coefficients inside each expression are rounded. Values within
10**(-decimals)of 1 are snapped to exactly 1 to suppress floating-point noise.
- Returns:
- numpy.ndarray
The unitary matrix.
dtypeiscomplex64for numeric circuits andobjectfor symbolic ones.
Examples
We synthesize a controlled phase gate and inspect the unitary:
>>> from qrisp import QuantumCircuit >>> import numpy as np >>> qc = QuantumCircuit(2) >>> phi = np.pi >>> qc.p(phi/2, 0) >>> qc.p(phi/2, 1) >>> qc.cx(0,1) >>> qc.p(-phi/2, 1) >>> qc.cx(0,1) >>> qc.get_unitary(decimals = 4) array([[ 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], [ 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], [ 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j], [ 0.+0.j, 0.+0.j, 0.+0.j, -1.+0.j]], dtype=complex64)
We now synthesize the exact same QuantumCircuit, but this time
phiis a SymPy symbol.>>> from sympy import Symbol >>> qc = QuantumCircuit(2) >>> phi = Symbol("phi") >>> qc.p(phi/2, 0) >>> qc.p(phi/2, 1) >>> qc.cx(0,1) >>> qc.p(-phi/2, 1) >>> qc.cx(0,1) >>> qc.get_unitary(decimals = 4) array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, exp(I*phi)]], dtype=object)