# Are gates in qiskit necessarily unitary?

We are programming with Qiskit and for our research project we need to insert non-unitary gates within a quantum circuit.

1. Is the unitarity of gates a mandatory property required to run the code without raising errors?
2. Are there specific parts of the Qiskit source code that can be modified in order to implement non-unitary gates without the need to change the whole Qiskit framework?
• Commented Jan 26, 2022 at 14:57
• Commented Jan 26, 2022 at 16:46

Non-unitary gate is a bit of an oxymoron for reasons explained here and here. Qiskit support non-unitary instructions. Gates are Instructions subclasses. A gate, like XGate is an instruction, but not every instruction, such as Reset, is a gate:

from qiskit.circuit import Gate, Instruction, Reset
issubclass(Reset, Instruction)  # True
issubclass(Reset, Gate)  # False

issubclass(XGate, Instruction)  # True
issubclass(XGate, Gate)  # True


So Qiskit already supports the notion of non-unitary instruction. You can create your own instruction with to_instruction. For example, this is a "reset to 1" instruction:

1. Create a circuit:
from qiskit import QuantumCircuit
reset_to_one = QuantumCircuit(1, name="reset to 1")
reset_to_one.reset(0)
reset_to_one.x(0)
print(reset_to_one)

        ┌───┐
q: ─|0>─┤ X ├
└───┘

1. Convert that circuit into a instruction:
reset_to_one_inst = reset_to_one.to_instruction()

circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.append(reset_to_one, [1])
circuit.measure_all()
print(circuit)

        ┌───┐                    ░ ┌─┐
q_0: ┤ H ├──■─────────────────░─┤M├───
└───┘┌─┴─┐┌────────────┐ ░ └╥┘┌─┐
q_1: ─────┤ X ├┤ reset to 1 ├─░──╫─┤M├
└───┘└────────────┘ ░  ║ └╥┘
meas: 2/════════════════════════════╩══╩═
0  1


In this case, $$q_1$$ will be reset to 1 before being measured into $$meas_1$$. reset to 1 is a non-unitary instruction.

• Thanks @luciano, your answer was really helpful! It seems that with your solution, one can define a new instruction as a composition of predefined Qiskit instructions. We are facing a slightly different problem: we know the entries of a non-unitary matrix and we want to define the instruction that corresponds to this matrix. Is this possible? Commented Jan 31, 2022 at 8:34
• Also interested to see if this is possible. Need to define non-unitary instructions from a matrix. Commented Aug 28, 2023 at 20:05
• Qiskit does not support non-unitary-matrix-defined operations (yet?). What's your use-case for them? Synthesis? Simulation? Feel free to make a case as a feature request github.com/Qiskit/qiskit/issues/… Commented Sep 5, 2023 at 13:20
• I'm looking at implementing the trajectories method for simulating a noisy circuit (quantumcomputing.stackexchange.com/a/23400/19585), and have Kraus operators that are not necessarily unitary. Commented Oct 9, 2023 at 23:46

One can define a non-unitary matrix as a density matrix, in the case it is hermitian positive-definite. It seems qiskit does not check the trace condition. However, it would be nice to know a way of building an instruction from a non-unitary matrix. All examples I found of non-unitary instructions where built from circuits.