It seems that it is fairly easy to construct a unitary gate given a real unitary matrix. However, I have tried several methods with a complex unitary matrix, but they all seem to be not working. How can I construct a unitary gate in Qiskit if the matrix given is complex?


1 Answer 1


Qiskit's class UnitaryGate accepts any unitary whether it complex or not. The following code snippet shows how to use it to construct a gate.

At first, let's generate a random unitary:

from qiskit.quantum_info import random_unitary
from qiskit.visualization import array_to_latex

n = 2
U = random_unitary(2 ** n)

The output will be something like

enter image description here

Now, construct the gate:

from qiskit import QuantumCircuit

# Create a QuantumCircuit that implements the unitary:
circ = QuantumCircuit(n)
circ.unitary(U, range(n))

# Convert it to gate:
gate = circ.to_gate()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.