qrisp.QuantumCircuit.get_unitary#

QuantumCircuit.get_unitary(decimals=- 1)[source]#

Acquires the unitary matrix of the given QuantumCircuit as a Numpy array.

This method also works with abstract parameters. In this case a Numpy array with Sympy entries is returned.

Parameters
decimalsinteger, optional

The amount of decimals to be rounded to. By default, the full precision is returned.

Returns
numpy.ndarray

The unitary matrix as a numpy array.

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 phi is 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)