How to represent $U(t)$ (a unitary operator) in a code? Is there any package available for that in Python?


1 Answer 1


This depends a bit on what you want to use the operator for. If you want to build a quantum circuit from your unitary, Qiskit is a good Python package for designing and running quantum circuits -- there are lots of good introductory videos on YouTube.

To represent a general unitary operator and build a circuit which implements that unitary, you can write your unitary as a matrix (a list of lists) and use it to create a UnitaryGate object, which you can in turn feed into a QuantumCircuit:

from qiskit import QuantumCircuit
from qiskit.circuit.library import UnitaryGate

# Write unitary as a matrix:
matrix = [[0, 0, 0, 1],
          [0, 0, 1, 0],
          [1, 0, 0, 0],
          [0, 1, 0, 0]]

# Convert to UnitaryGate object
gate = UnitaryGate(matrix)

# Create quantum circuit implementing that gate
circuit = QuantumCircuit(2)
circuit.append(gate, [0, 1])

outputs a picture of the quantum circuit with your unitary attached:

q_0: ┤0         ├
     │  Unitary │
q_1: ┤1         ├

If you want to implement a time-dependent unitary of the form $U(t) = \text{exp}(-itH)$, you could also use the HamiltonianGate object.

  • $\begingroup$ I'd also highly recommend the qutip package for implementing unitaries, which is more tailored towards analog quantum computing than Qiskit. $\endgroup$
    – JoJo P
    Apr 24 at 9:28

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.